1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jgit.util;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.net.InetAddress;
19 import java.net.UnknownHostException;
20 import java.nio.file.Files;
21 import java.nio.file.InvalidPathException;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.security.AccessController;
25 import java.security.PrivilegedAction;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Locale;
29 import java.util.TimeZone;
30 import java.util.concurrent.atomic.AtomicReference;
31
32 import org.eclipse.jgit.errors.ConfigInvalidException;
33 import org.eclipse.jgit.errors.CorruptObjectException;
34 import org.eclipse.jgit.internal.JGitText;
35 import org.eclipse.jgit.lib.Config;
36 import org.eclipse.jgit.lib.Constants;
37 import org.eclipse.jgit.lib.ObjectChecker;
38 import org.eclipse.jgit.lib.StoredConfig;
39 import org.eclipse.jgit.storage.file.FileBasedConfig;
40 import org.eclipse.jgit.util.time.MonotonicClock;
41 import org.eclipse.jgit.util.time.MonotonicSystemClock;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47
48
49
50
51
52
53 public abstract class SystemReader {
54
55 private static final Logger LOG = LoggerFactory
56 .getLogger(SystemReader.class);
57
58 private static final SystemReader DEFAULT;
59
60 private static Boolean isMacOS;
61
62 private static Boolean isWindows;
63
64 static {
65 SystemReader r = new Default();
66 r.init();
67 DEFAULT = r;
68 }
69
70 private static class Default extends SystemReader {
71 private volatile String hostname;
72
73 @Override
74 public String getenv(String variable) {
75 return System.getenv(variable);
76 }
77
78 @Override
79 public String getProperty(String key) {
80 return System.getProperty(key);
81 }
82
83 @Override
84 public FileBasedConfig openSystemConfig(Config parent, FS fs) {
85 if (StringUtils
86 .isEmptyOrNull(getenv(Constants.GIT_CONFIG_NOSYSTEM_KEY))) {
87 File configFile = fs.getGitSystemConfig();
88 if (configFile != null) {
89 return new FileBasedConfig(parent, configFile, fs);
90 }
91 }
92 return new FileBasedConfig(parent, null, fs) {
93 @Override
94 public void load() {
95
96 }
97
98 @Override
99 public boolean isOutdated() {
100
101 return false;
102 }
103 };
104 }
105
106 @Override
107 public FileBasedConfig openUserConfig(Config parent, FS fs) {
108 return new FileBasedConfig(parent, new File(fs.userHome(), ".gitconfig"),
109 fs);
110 }
111
112 private Path getXDGConfigHome(FS fs) {
113 String configHomePath = getenv(Constants.XDG_CONFIG_HOME);
114 if (StringUtils.isEmptyOrNull(configHomePath)) {
115 configHomePath = new File(fs.userHome(), ".config")
116 .getAbsolutePath();
117 }
118 try {
119 Path xdgHomePath = Paths.get(configHomePath);
120 Files.createDirectories(xdgHomePath);
121 return xdgHomePath;
122 } catch (IOException | InvalidPathException e) {
123 LOG.error(JGitText.get().createXDGConfigHomeFailed,
124 configHomePath, e);
125 }
126 return null;
127 }
128
129 @Override
130 public FileBasedConfig openJGitConfig(Config parent, FS fs) {
131 Path xdgPath = getXDGConfigHome(fs);
132 if (xdgPath != null) {
133 Path configPath = null;
134 try {
135 configPath = xdgPath.resolve("jgit");
136 Files.createDirectories(configPath);
137 configPath = configPath.resolve(Constants.CONFIG);
138 return new FileBasedConfig(parent, configPath.toFile(), fs);
139 } catch (IOException e) {
140 LOG.error(JGitText.get().createJGitConfigFailed, configPath,
141 e);
142 }
143 }
144 return new FileBasedConfig(parent,
145 new File(fs.userHome(), ".jgitconfig"), fs);
146 }
147
148 @Override
149 public String getHostname() {
150 if (hostname == null) {
151 try {
152 InetAddress localMachine = InetAddress.getLocalHost();
153 hostname = localMachine.getCanonicalHostName();
154 } catch (UnknownHostException e) {
155
156 hostname = "localhost";
157 }
158 assert hostname != null;
159 }
160 return hostname;
161 }
162
163 @Override
164 public long getCurrentTime() {
165 return System.currentTimeMillis();
166 }
167
168 @Override
169 public int getTimezone(long when) {
170 return getTimeZone().getOffset(when) / (60 * 1000);
171 }
172 }
173
174 private static volatile SystemReader INSTANCE = DEFAULT;
175
176
177
178
179
180
181 public static SystemReader getInstance() {
182 return INSTANCE;
183 }
184
185
186
187
188
189
190
191
192 public static void setInstance(SystemReader newReader) {
193 isMacOS = null;
194 isWindows = null;
195 if (newReader == null)
196 INSTANCE = DEFAULT;
197 else {
198 newReader.init();
199 INSTANCE = newReader;
200 }
201 }
202
203 private ObjectChecker platformChecker;
204
205 private AtomicReference<FileBasedConfig> systemConfig = new AtomicReference<>();
206
207 private AtomicReference<FileBasedConfig> userConfig = new AtomicReference<>();
208
209 private AtomicReference<FileBasedConfig> jgitConfig = new AtomicReference<>();
210
211 private void init() {
212
213
214 if (platformChecker == null)
215 setPlatformChecker();
216 }
217
218
219
220
221
222
223 protected final void setPlatformChecker() {
224 platformChecker = new ObjectChecker()
225 .setSafeForWindows(isWindows())
226 .setSafeForMacOS(isMacOS());
227 }
228
229
230
231
232
233
234
235 public abstract String getHostname();
236
237
238
239
240
241
242
243
244 public abstract String getenv(String variable);
245
246
247
248
249
250
251
252
253 public abstract String getProperty(String key);
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 public abstract FileBasedConfig openJGitConfig(Config parent, FS fs);
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 public StoredConfig getUserConfig()
317 throws ConfigInvalidException, IOException {
318 FileBasedConfig c = userConfig.get();
319 if (c == null) {
320 userConfig.compareAndSet(null,
321 openUserConfig(getSystemConfig(), FS.DETECTED));
322 c = userConfig.get();
323 }
324
325
326 updateAll(c);
327 return c;
328 }
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 public StoredConfig getJGitConfig()
344 throws ConfigInvalidException, IOException {
345 FileBasedConfig c = jgitConfig.get();
346 if (c == null) {
347 jgitConfig.compareAndSet(null,
348 openJGitConfig(null, FS.DETECTED));
349 c = jgitConfig.get();
350 }
351 updateAll(c);
352 return c;
353 }
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368 public StoredConfig getSystemConfig()
369 throws ConfigInvalidException, IOException {
370 FileBasedConfig c = systemConfig.get();
371 if (c == null) {
372 systemConfig.compareAndSet(null,
373 openSystemConfig(getJGitConfig(), FS.DETECTED));
374 c = systemConfig.get();
375 }
376 updateAll(c);
377 return c;
378 }
379
380
381
382
383
384
385
386
387
388
389
390 private void updateAll(Config config)
391 throws ConfigInvalidException, IOException {
392 if (config == null) {
393 return;
394 }
395 updateAll(config.getBaseConfig());
396 if (config instanceof FileBasedConfig) {
397 FileBasedConfig cfg = (FileBasedConfig) config;
398 if (cfg.isOutdated()) {
399 LOG.debug("loading config {}", cfg);
400 cfg.load();
401 }
402 }
403 }
404
405
406
407
408
409
410 public abstract long getCurrentTime();
411
412
413
414
415
416
417
418 public MonotonicClock getClock() {
419 return new MonotonicSystemClock();
420 }
421
422
423
424
425
426
427
428
429 public abstract int getTimezone(long when);
430
431
432
433
434
435
436
437 public TimeZone getTimeZone() {
438 return TimeZone.getDefault();
439 }
440
441
442
443
444
445
446
447 public Locale getLocale() {
448 return Locale.getDefault();
449 }
450
451
452
453
454
455
456
457
458
459
460 public SimpleDateFormat getSimpleDateFormat(String pattern) {
461 return new SimpleDateFormat(pattern);
462 }
463
464
465
466
467
468
469
470
471
472
473
474
475 public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
476 return new SimpleDateFormat(pattern, locale);
477 }
478
479
480
481
482
483
484
485
486
487
488
489
490
491 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
492 return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
493 }
494
495
496
497
498
499
500 public boolean isWindows() {
501 if (isWindows == null) {
502 String osDotName = getOsName();
503 isWindows = Boolean.valueOf(osDotName.startsWith("Windows"));
504 }
505 return isWindows.booleanValue();
506 }
507
508
509
510
511
512
513 public boolean isMacOS() {
514 if (isMacOS == null) {
515 String osDotName = getOsName();
516 isMacOS = Boolean.valueOf(
517 "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName));
518 }
519 return isMacOS.booleanValue();
520 }
521
522 private String getOsName() {
523 return AccessController.doPrivileged(
524 (PrivilegedAction<String>) () -> getProperty("os.name")
525 );
526 }
527
528
529
530
531
532
533
534
535
536
537 public void checkPath(String path) throws CorruptObjectException {
538 platformChecker.checkPath(path);
539 }
540
541
542
543
544
545
546
547
548
549
550
551
552 public void checkPath(byte[] path) throws CorruptObjectException {
553 platformChecker.checkPath(path, 0, path.length);
554 }
555 }