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