1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.eclipse.jgit.util;
48
49 import java.io.File;
50 import java.net.InetAddress;
51 import java.net.UnknownHostException;
52 import java.security.AccessController;
53 import java.security.PrivilegedAction;
54 import java.text.DateFormat;
55 import java.text.SimpleDateFormat;
56 import java.util.Locale;
57 import java.util.TimeZone;
58
59 import org.eclipse.jgit.errors.CorruptObjectException;
60 import org.eclipse.jgit.lib.Config;
61 import org.eclipse.jgit.lib.ObjectChecker;
62 import org.eclipse.jgit.storage.file.FileBasedConfig;
63 import org.eclipse.jgit.util.time.MonotonicClock;
64 import org.eclipse.jgit.util.time.MonotonicSystemClock;
65
66
67
68
69
70
71
72
73
74 public abstract class SystemReader {
75 private static final SystemReader DEFAULT;
76
77 private static Boolean isMacOS;
78
79 private static Boolean isWindows;
80
81 static {
82 SystemReader r = new Default();
83 r.init();
84 DEFAULT = r;
85 }
86
87 private static class Default extends SystemReader {
88 private volatile String hostname;
89
90 @Override
91 public String getenv(String variable) {
92 return System.getenv(variable);
93 }
94
95 @Override
96 public String getProperty(String key) {
97 return System.getProperty(key);
98 }
99
100 @Override
101 public FileBasedConfig openSystemConfig(Config parent, FS fs) {
102 File configFile = fs.getGitSystemConfig();
103 if (configFile == null) {
104 return new FileBasedConfig(null, fs) {
105 @Override
106 public void load() {
107
108 }
109
110 @Override
111 public boolean isOutdated() {
112
113 return false;
114 }
115 };
116 }
117 return new FileBasedConfig(parent, configFile, fs);
118 }
119
120 @Override
121 public FileBasedConfig openUserConfig(Config parent, FS fs) {
122 final File home = fs.userHome();
123 return new FileBasedConfig(parent, new File(home, ".gitconfig"), fs);
124 }
125
126 @Override
127 public String getHostname() {
128 if (hostname == null) {
129 try {
130 InetAddress localMachine = InetAddress.getLocalHost();
131 hostname = localMachine.getCanonicalHostName();
132 } catch (UnknownHostException e) {
133
134 hostname = "localhost";
135 }
136 assert hostname != null;
137 }
138 return hostname;
139 }
140
141 @Override
142 public long getCurrentTime() {
143 return System.currentTimeMillis();
144 }
145
146 @Override
147 public int getTimezone(long when) {
148 return getTimeZone().getOffset(when) / (60 * 1000);
149 }
150 }
151
152 private static SystemReader INSTANCE = DEFAULT;
153
154
155 public static SystemReader getInstance() {
156 return INSTANCE;
157 }
158
159
160
161
162
163
164 public static void setInstance(SystemReader newReader) {
165 isMacOS = null;
166 isWindows = null;
167 if (newReader == null)
168 INSTANCE = DEFAULT;
169 else {
170 newReader.init();
171 INSTANCE = newReader;
172 }
173 }
174
175 private ObjectChecker platformChecker;
176
177 private void init() {
178
179
180 if (platformChecker == null)
181 setPlatformChecker();
182 }
183
184
185
186
187
188
189 protected final void setPlatformChecker() {
190 platformChecker = new ObjectChecker()
191 .setSafeForWindows(isWindows())
192 .setSafeForMacOS(isMacOS());
193 }
194
195
196
197
198
199
200
201 public abstract String getHostname();
202
203
204
205
206
207 public abstract String getenv(String variable);
208
209
210
211
212
213 public abstract String getProperty(String key);
214
215
216
217
218
219
220
221
222
223 public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
224
225
226
227
228
229
230
231
232
233
234
235 public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
236
237
238
239
240 public abstract long getCurrentTime();
241
242
243
244
245
246 public MonotonicClock getClock() {
247 return new MonotonicSystemClock();
248 }
249
250
251
252
253
254 public abstract int getTimezone(long when);
255
256
257
258
259
260 public TimeZone getTimeZone() {
261 return TimeZone.getDefault();
262 }
263
264
265
266
267
268 public Locale getLocale() {
269 return Locale.getDefault();
270 }
271
272
273
274
275
276
277
278
279
280
281 public SimpleDateFormat getSimpleDateFormat(String pattern) {
282 return new SimpleDateFormat(pattern);
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296 public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
297 return new SimpleDateFormat(pattern, locale);
298 }
299
300
301
302
303
304
305
306
307
308
309
310
311
312 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
313 return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
314 }
315
316
317
318
319 public boolean isWindows() {
320 if (isWindows == null) {
321 String osDotName = getOsName();
322 isWindows = Boolean.valueOf(osDotName.startsWith("Windows"));
323 }
324 return isWindows.booleanValue();
325 }
326
327
328
329
330 public boolean isMacOS() {
331 if (isMacOS == null) {
332 String osDotName = getOsName();
333 isMacOS = Boolean.valueOf(
334 "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName));
335 }
336 return isMacOS.booleanValue();
337 }
338
339 private String getOsName() {
340 return AccessController.doPrivileged(new PrivilegedAction<String>() {
341 @Override
342 public String run() {
343 return getProperty("os.name");
344 }
345 });
346 }
347
348
349
350
351
352
353
354
355
356
357 public void checkPath(String path) throws CorruptObjectException {
358 platformChecker.checkPath(path);
359 }
360
361
362
363
364
365
366
367
368
369
370
371
372 public void checkPath(byte[] path) throws CorruptObjectException {
373 platformChecker.checkPath(path, 0, path.length);
374 }
375 }