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