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