1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
4 * and other copyright owners as documented in the project's IP log.
5 *
6 * This program and the accompanying materials are made available
7 * under the terms of the Eclipse Distribution License v1.0 which
8 * accompanies this distribution, is reproduced below, and is
9 * available at http://www.eclipse.org/org/documents/edl-v10.php
10 *
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
24 *
25 * - Neither the name of the Eclipse Foundation, Inc. nor the
26 * names of its contributors may be used to endorse or promote
27 * products derived from this software without specific prior
28 * written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * Git style file locking and replacement.
73 * <p>
74 * To modify a ref file Git tries to use an atomic update approach: we write the
75 * new data into a brand new file, then rename it in place over the old name.
76 * This way we can just delete the temporary file if anything goes wrong, and
77 * nothing has been damaged. To coordinate access from multiple processes at
78 * once Git tries to atomically create the new temporary file under a well-known
79 * name.
80 */
81 public class LockFile {
82 private final static Logger LOG = LoggerFactory.getLogger(LockFile.class);
83
84 /**
85 * Unlock the given file.
86 * <p>
87 * This method can be used for recovering from a thrown
88 * {@link org.eclipse.jgit.errors.LockFailedException} . This method does
89 * not validate that the lock is or is not currently held before attempting
90 * to unlock it.
91 *
92 * @param file
93 * a {@link java.io.File} object.
94 * @return true if unlocked, false if unlocking failed
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 // Ignore and return whether lock file still exists
103 }
104 return !lockFile.exists();
105 }
106
107 /**
108 * Get the lock file corresponding to the given file.
109 *
110 * @param file
111 * @return lock file
112 */
113 static File getLockFile(File file) {
114 return new File(file.getParentFile(),
115 file.getName() + LOCK_SUFFIX);
116 }
117
118 /** Filter to skip over active lock files when listing a directory. */
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 * Create a new lock for any file.
144 *
145 * @param f
146 * the file that will be locked.
147 */
148 public LockFile(File f) {
149 ref = f;
150 lck = getLockFile(ref);
151 }
152
153 /**
154 * Try to establish the lock.
155 *
156 * @return true if the lock is now held by the caller; false if it is held
157 * by someone else.
158 * @throws java.io.IOException
159 * the temporary output file could not be created. The caller
160 * does not hold the lock.
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 * Try to establish the lock for appending.
181 *
182 * @return true if the lock is now held by the caller; false if it is held
183 * by someone else.
184 * @throws java.io.IOException
185 * the temporary output file could not be created. The caller
186 * does not hold the lock.
187 */
188 public boolean lockForAppend() throws IOException {
189 if (!lock())
190 return false;
191 copyCurrentContent();
192 return true;
193 }
194
195 /**
196 * Copy the current file content into the temporary file.
197 * <p>
198 * This method saves the current file content by inserting it into the
199 * temporary file, so that the caller can safely append rather than replace
200 * the primary file.
201 * <p>
202 * This method does nothing if the current file does not exist, or exists
203 * but is empty.
204 *
205 * @throws java.io.IOException
206 * the temporary file could not be written, or a read error
207 * occurred while reading from the current file. The lock is
208 * released before throwing the underlying IO exception to the
209 * caller.
210 * @throws java.lang.RuntimeException
211 * the temporary file could not be written. The lock is released
212 * before throwing the underlying exception to the caller.
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 // Don't worry about a file that doesn't exist yet, it
240 // conceptually has no current content to copy.
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 * Write an ObjectId and LF to the temporary file.
256 *
257 * @param id
258 * the id to store in the file. The id will be written in hex,
259 * followed by a sole LF.
260 * @throws java.io.IOException
261 * the temporary file could not be written. The lock is released
262 * before throwing the underlying IO exception to the caller.
263 * @throws java.lang.RuntimeException
264 * the temporary file could not be written. The lock is released
265 * before throwing the underlying exception to the caller.
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 * Write arbitrary data to the temporary file.
276 *
277 * @param content
278 * the bytes to store in the temporary file. No additional bytes
279 * are added, so if the file must end with an LF it must appear
280 * at the end of the byte array.
281 * @throws java.io.IOException
282 * the temporary file could not be written. The lock is released
283 * before throwing the underlying IO exception to the caller.
284 * @throws java.lang.RuntimeException
285 * the temporary file could not be written. The lock is released
286 * before throwing the underlying exception to the caller.
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 * Obtain the direct output stream for this lock.
316 * <p>
317 * The stream may only be accessed once, and only after {@link #lock()} has
318 * been successfully invoked and returned true. Callers must close the
319 * stream prior to calling {@link #commit()} to commit the change.
320 *
321 * @return a stream to write to the new file. The stream is unbuffered.
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 * Request that {@link #commit()} remember modification time.
379 * <p>
380 * This is an alias for {@code setNeedSnapshot(true)}.
381 *
382 * @param on
383 * true if the commit method must remember the modification time.
384 */
385 public void setNeedStatInformation(boolean on) {
386 setNeedSnapshot(on);
387 }
388
389 /**
390 * Request that {@link #commit()} remember the
391 * {@link org.eclipse.jgit.internal.storage.file.FileSnapshot}.
392 *
393 * @param on
394 * true if the commit method must remember the FileSnapshot.
395 */
396 public void setNeedSnapshot(boolean on) {
397 needSnapshot = on;
398 }
399
400 /**
401 * Request that {@link #commit()} force dirty data to the drive.
402 *
403 * @param on
404 * true if dirty data should be forced to the drive.
405 */
406 public void setFSync(boolean on) {
407 fsync = on;
408 }
409
410 /**
411 * Wait until the lock file information differs from the old file.
412 * <p>
413 * This method tests the last modification date. If both are the same, this
414 * method sleeps until it can force the new lock file's modification date to
415 * be later than the target file.
416 *
417 * @throws java.lang.InterruptedException
418 * the thread was interrupted before the last modified date of
419 * the lock file was different from the last modified date of
420 * the target file.
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 /* milliseconds */);
427 lck.setLastModified(System.currentTimeMillis());
428 n = FileSnapshot.save(lck);
429 }
430 }
431
432 /**
433 * Commit this change and release the lock.
434 * <p>
435 * If this method fails (returns false) the lock is still released.
436 *
437 * @return true if the commit was successful and the file contains the new
438 * data; false if the commit failed and the file remains with the
439 * old data.
440 * @throws java.lang.IllegalStateException
441 * the lock is not held.
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 * Get the modification time of the output file when it was committed.
475 *
476 * @return modification time of the lock file right before we committed it.
477 */
478 public long getCommitLastModified() {
479 return commitSnapshot.lastModified();
480 }
481
482 /**
483 * Get the {@link FileSnapshot} just before commit.
484 *
485 * @return get the {@link FileSnapshot} just before commit.
486 */
487 public FileSnapshot getCommitSnapshot() {
488 return commitSnapshot;
489 }
490
491 /**
492 * Update the commit snapshot {@link #getCommitSnapshot()} before commit.
493 * <p>
494 * This may be necessary if you need time stamp before commit occurs, e.g
495 * while writing the index.
496 */
497 public void createCommitSnapshot() {
498 saveStatInformation();
499 }
500
501 /**
502 * Unlock this file and abort this change.
503 * <p>
504 * The temporary file (if created) is deleted before returning.
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 /** {@inheritDoc} */
531 @SuppressWarnings("nls")
532 @Override
533 public String toString() {
534 return "LockFile[" + lck + ", haveLck=" + haveLck + "]";
535 }
536 }