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 package org.eclipse.jgit.internal.storage.file;
46
47 import static org.eclipse.jgit.lib.Constants.LOCK_SUFFIX;
48
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.FileNotFoundException;
52 import java.io.FileOutputStream;
53 import java.io.FilenameFilter;
54 import java.io.IOException;
55 import java.io.OutputStream;
56 import java.nio.ByteBuffer;
57 import java.nio.channels.Channels;
58 import java.nio.channels.FileChannel;
59 import java.nio.file.Files;
60 import java.nio.file.StandardCopyOption;
61 import java.nio.file.attribute.FileTime;
62 import java.text.MessageFormat;
63 import java.time.Instant;
64 import java.util.concurrent.TimeUnit;
65
66 import org.eclipse.jgit.internal.JGitText;
67 import org.eclipse.jgit.lib.Constants;
68 import org.eclipse.jgit.lib.ObjectId;
69 import org.eclipse.jgit.util.FS;
70 import org.eclipse.jgit.util.FS.LockToken;
71 import org.eclipse.jgit.util.FileUtils;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74
75
76
77
78
79
80
81
82
83
84
85 public class LockFile {
86 private final static Logger LOG = LoggerFactory.getLogger(LockFile.class);
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public static boolean unlock(File file) {
101 final File lockFile = getLockFile(file);
102 final int flags = FileUtils.RETRY | FileUtils.SKIP_MISSING;
103 try {
104 FileUtils.delete(lockFile, flags);
105 } catch (IOException ignored) {
106
107 }
108 return !lockFile.exists();
109 }
110
111
112
113
114
115
116
117 static File getLockFile(File file) {
118 return new File(file.getParentFile(),
119 file.getName() + LOCK_SUFFIX);
120 }
121
122
123 static final FilenameFilter FILTER = new FilenameFilter() {
124 @Override
125 public boolean accept(File dir, String name) {
126 return !name.endsWith(LOCK_SUFFIX);
127 }
128 };
129
130 private final File ref;
131
132 private final File lck;
133
134 private boolean haveLck;
135
136 FileOutputStream os;
137
138 private boolean needSnapshot;
139
140 boolean fsync;
141
142 private FileSnapshot commitSnapshot;
143
144 private LockToken token;
145
146
147
148
149
150
151
152 public LockFile(File f) {
153 ref = f;
154 lck = getLockFile(ref);
155 }
156
157
158
159
160
161
162
163
164
165
166 public boolean lock() throws IOException {
167 FileUtils.mkdirs(lck.getParentFile(), true);
168 try {
169 token = FS.DETECTED.createNewFileAtomic(lck);
170 } catch (IOException e) {
171 LOG.error(JGitText.get().failedCreateLockFile, lck, e);
172 throw e;
173 }
174 if (token.isCreated()) {
175 haveLck = true;
176 try {
177 os = new FileOutputStream(lck);
178 } catch (IOException ioe) {
179 unlock();
180 throw ioe;
181 }
182 } else {
183 closeToken();
184 }
185 return haveLck;
186 }
187
188
189
190
191
192
193
194
195
196
197 public boolean lockForAppend() throws IOException {
198 if (!lock())
199 return false;
200 copyCurrentContent();
201 return true;
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public void copyCurrentContent() throws IOException {
224 requireLock();
225 try {
226 try (FileInputStream fis = new FileInputStream(ref)) {
227 if (fsync) {
228 FileChannel in = fis.getChannel();
229 long pos = 0;
230 long cnt = in.size();
231 while (0 < cnt) {
232 long r = os.getChannel().transferFrom(in, pos, cnt);
233 pos += r;
234 cnt -= r;
235 }
236 } else {
237 final byte[] buf = new byte[2048];
238 int r;
239 while ((r = fis.read(buf)) >= 0)
240 os.write(buf, 0, r);
241 }
242 }
243 } catch (FileNotFoundException fnfe) {
244 if (ref.exists()) {
245 unlock();
246 throw fnfe;
247 }
248
249
250
251 } catch (IOException ioe) {
252 unlock();
253 throw ioe;
254 } catch (RuntimeException ioe) {
255 unlock();
256 throw ioe;
257 } catch (Error ioe) {
258 unlock();
259 throw ioe;
260 }
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 public void write(ObjectId id) throws IOException {
277 byte[] buf = new byte[Constants.OBJECT_ID_STRING_LENGTH + 1];
278 id.copyTo(buf, 0);
279 buf[Constants.OBJECT_ID_STRING_LENGTH] = '\n';
280 write(buf);
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297 public void write(byte[] content) throws IOException {
298 requireLock();
299 try {
300 if (fsync) {
301 FileChannel fc = os.getChannel();
302 ByteBuffer buf = ByteBuffer.wrap(content);
303 while (0 < buf.remaining())
304 fc.write(buf);
305 fc.force(true);
306 } else {
307 os.write(content);
308 }
309 os.close();
310 os = null;
311 } catch (IOException ioe) {
312 unlock();
313 throw ioe;
314 } catch (RuntimeException ioe) {
315 unlock();
316 throw ioe;
317 } catch (Error ioe) {
318 unlock();
319 throw ioe;
320 }
321 }
322
323
324
325
326
327
328
329
330
331
332 public OutputStream getOutputStream() {
333 requireLock();
334
335 final OutputStream out;
336 if (fsync)
337 out = Channels.newOutputStream(os.getChannel());
338 else
339 out = os;
340
341 return new OutputStream() {
342 @Override
343 public void write(byte[] b, int o, int n)
344 throws IOException {
345 out.write(b, o, n);
346 }
347
348 @Override
349 public void write(byte[] b) throws IOException {
350 out.write(b);
351 }
352
353 @Override
354 public void write(int b) throws IOException {
355 out.write(b);
356 }
357
358 @Override
359 public void close() throws IOException {
360 try {
361 if (fsync)
362 os.getChannel().force(true);
363 out.close();
364 os = null;
365 } catch (IOException ioe) {
366 unlock();
367 throw ioe;
368 } catch (RuntimeException ioe) {
369 unlock();
370 throw ioe;
371 } catch (Error ioe) {
372 unlock();
373 throw ioe;
374 }
375 }
376 };
377 }
378
379 void requireLock() {
380 if (os == null) {
381 unlock();
382 throw new IllegalStateException(MessageFormat.format(JGitText.get().lockOnNotHeld, ref));
383 }
384 }
385
386
387
388
389
390
391
392
393
394 public void setNeedStatInformation(boolean on) {
395 setNeedSnapshot(on);
396 }
397
398
399
400
401
402
403
404
405 public void setNeedSnapshot(boolean on) {
406 needSnapshot = on;
407 }
408
409
410
411
412
413
414
415 public void setFSync(boolean on) {
416 fsync = on;
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430
431 public void waitForStatChange() throws InterruptedException {
432 FileSnapshot o = FileSnapshot.save(ref);
433 FileSnapshot n = FileSnapshot.save(lck);
434 long fsTimeResolution = FS.getFileStoreAttributes(lck.toPath())
435 .getFsTimestampResolution().toNanos();
436 while (o.equals(n)) {
437 TimeUnit.NANOSECONDS.sleep(fsTimeResolution);
438 try {
439 Files.setLastModifiedTime(lck.toPath(),
440 FileTime.from(Instant.now()));
441 } catch (IOException e) {
442 n.waitUntilNotRacy();
443 }
444 n = FileSnapshot.save(lck);
445 }
446 }
447
448
449
450
451
452
453
454
455
456
457
458
459 public boolean commit() {
460 if (os != null) {
461 unlock();
462 throw new IllegalStateException(MessageFormat.format(JGitText.get().lockOnNotClosed, ref));
463 }
464
465 saveStatInformation();
466 try {
467 FileUtils.rename(lck, ref, StandardCopyOption.ATOMIC_MOVE);
468 haveLck = false;
469 closeToken();
470 return true;
471 } catch (IOException e) {
472 unlock();
473 return false;
474 }
475 }
476
477 private void closeToken() {
478 if (token != null) {
479 token.close();
480 token = null;
481 }
482 }
483
484 private void saveStatInformation() {
485 if (needSnapshot)
486 commitSnapshot = FileSnapshot.save(lck);
487 }
488
489
490
491
492
493
494
495 @Deprecated
496 public long getCommitLastModified() {
497 return commitSnapshot.lastModified();
498 }
499
500
501
502
503
504
505 public Instant getCommitLastModifiedInstant() {
506 return commitSnapshot.lastModifiedInstant();
507 }
508
509
510
511
512
513
514 public FileSnapshot getCommitSnapshot() {
515 return commitSnapshot;
516 }
517
518
519
520
521
522
523
524 public void createCommitSnapshot() {
525 saveStatInformation();
526 }
527
528
529
530
531
532
533 public void unlock() {
534 if (os != null) {
535 try {
536 os.close();
537 } catch (IOException e) {
538 LOG.error(MessageFormat
539 .format(JGitText.get().unlockLockFileFailed, lck), e);
540 }
541 os = null;
542 }
543
544 if (haveLck) {
545 haveLck = false;
546 try {
547 FileUtils.delete(lck, FileUtils.RETRY);
548 } catch (IOException e) {
549 LOG.error(MessageFormat
550 .format(JGitText.get().unlockLockFileFailed, lck), e);
551 } finally {
552 closeToken();
553 }
554 }
555 }
556
557
558 @SuppressWarnings("nls")
559 @Override
560 public String toString() {
561 return "LockFile[" + lck + ", haveLck=" + haveLck + "]";
562 }
563 }