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