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