View Javadoc
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 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   * Git style file locking and replacement.
68   * <p>
69   * To modify a ref file Git tries to use an atomic update approach: we write the
70   * new data into a brand new file, then rename it in place over the old name.
71   * This way we can just delete the temporary file if anything goes wrong, and
72   * nothing has been damaged. To coordinate access from multiple processes at
73   * once Git tries to atomically create the new temporary file under a well-known
74   * name.
75   */
76  public class LockFile {
77  	static final String SUFFIX = ".lock"; //$NON-NLS-1$
78  
79  	/**
80  	 * Unlock the given file.
81  	 * <p>
82  	 * This method can be used for recovering from a thrown
83  	 * {@link LockFailedException} . This method does not validate that the lock
84  	 * is or is not currently held before attempting to unlock it.
85  	 *
86  	 * @param file
87  	 * @return true if unlocked, false if unlocking failed
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  			// Ignore and return whether lock file still exists
96  		}
97  		return !lockFile.exists();
98  	}
99  
100 	/**
101 	 * Get the lock file corresponding to the given file.
102 	 *
103 	 * @param file
104 	 * @return lock file
105 	 */
106 	static File getLockFile(File file) {
107 		return new File(file.getParentFile(), file.getName() + SUFFIX);
108 	}
109 
110 	/** Filter to skip over active lock files when listing a directory. */
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 	 * Create a new lock for any file.
135 	 *
136 	 * @param f
137 	 *            the file that will be locked.
138 	 * @param fs
139 	 *            the file system abstraction which will be necessary to perform
140 	 *            certain file system operations.
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 	 * Try to establish the lock.
150 	 *
151 	 * @return true if the lock is now held by the caller; false if it is held
152 	 *         by someone else.
153 	 * @throws IOException
154 	 *             the temporary output file could not be created. The caller
155 	 *             does not hold the lock.
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 	 * Try to establish the lock for appending.
173 	 *
174 	 * @return true if the lock is now held by the caller; false if it is held
175 	 *         by someone else.
176 	 * @throws IOException
177 	 *             the temporary output file could not be created. The caller
178 	 *             does not hold the lock.
179 	 */
180 	public boolean lockForAppend() throws IOException {
181 		if (!lock())
182 			return false;
183 		copyCurrentContent();
184 		return true;
185 	}
186 
187 	/**
188 	 * Copy the current file content into the temporary file.
189 	 * <p>
190 	 * This method saves the current file content by inserting it into the
191 	 * temporary file, so that the caller can safely append rather than replace
192 	 * the primary file.
193 	 * <p>
194 	 * This method does nothing if the current file does not exist, or exists
195 	 * but is empty.
196 	 *
197 	 * @throws IOException
198 	 *             the temporary file could not be written, or a read error
199 	 *             occurred while reading from the current file. The lock is
200 	 *             released before throwing the underlying IO exception to the
201 	 *             caller.
202 	 * @throws RuntimeException
203 	 *             the temporary file could not be written. The lock is released
204 	 *             before throwing the underlying exception to the caller.
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 			// Don't worry about a file that doesn't exist yet, it
231 			// conceptually has no current content to copy.
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 	 * Write an ObjectId and LF to the temporary file.
247 	 *
248 	 * @param id
249 	 *            the id to store in the file. The id will be written in hex,
250 	 *            followed by a sole LF.
251 	 * @throws IOException
252 	 *             the temporary file could not be written. The lock is released
253 	 *             before throwing the underlying IO exception to the caller.
254 	 * @throws RuntimeException
255 	 *             the temporary file could not be written. The lock is released
256 	 *             before throwing the underlying exception to the caller.
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 	 * Write arbitrary data to the temporary file.
267 	 *
268 	 * @param content
269 	 *            the bytes to store in the temporary file. No additional bytes
270 	 *            are added, so if the file must end with an LF it must appear
271 	 *            at the end of the byte array.
272 	 * @throws IOException
273 	 *             the temporary file could not be written. The lock is released
274 	 *             before throwing the underlying IO exception to the caller.
275 	 * @throws RuntimeException
276 	 *             the temporary file could not be written. The lock is released
277 	 *             before throwing the underlying exception to the caller.
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 	 * Obtain the direct output stream for this lock.
307 	 * <p>
308 	 * The stream may only be accessed once, and only after {@link #lock()} has
309 	 * been successfully invoked and returned true. Callers must close the
310 	 * stream prior to calling {@link #commit()} to commit the change.
311 	 *
312 	 * @return a stream to write to the new file. The stream is unbuffered.
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 	 * Request that {@link #commit()} remember modification time.
370 	 * <p>
371 	 * This is an alias for {@code setNeedSnapshot(true)}.
372 	 *
373 	 * @param on
374 	 *            true if the commit method must remember the modification time.
375 	 */
376 	public void setNeedStatInformation(final boolean on) {
377 		setNeedSnapshot(on);
378 	}
379 
380 	/**
381 	 * Request that {@link #commit()} remember the {@link FileSnapshot}.
382 	 *
383 	 * @param on
384 	 *            true if the commit method must remember the FileSnapshot.
385 	 */
386 	public void setNeedSnapshot(final boolean on) {
387 		needSnapshot = on;
388 	}
389 
390 	/**
391 	 * Request that {@link #commit()} force dirty data to the drive.
392 	 *
393 	 * @param on
394 	 *            true if dirty data should be forced to the drive.
395 	 */
396 	public void setFSync(final boolean on) {
397 		fsync = on;
398 	}
399 
400 	/**
401 	 * Wait until the lock file information differs from the old file.
402 	 * <p>
403 	 * This method tests the last modification date. If both are the same, this
404 	 * method sleeps until it can force the new lock file's modification date to
405 	 * be later than the target file.
406 	 *
407 	 * @throws InterruptedException
408 	 *             the thread was interrupted before the last modified date of
409 	 *             the lock file was different from the last modified date of
410 	 *             the target file.
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 /* milliseconds */);
417 			lck.setLastModified(System.currentTimeMillis());
418 			n = FileSnapshot.save(lck);
419 		}
420 	}
421 
422 	/**
423 	 * Commit this change and release the lock.
424 	 * <p>
425 	 * If this method fails (returns false) the lock is still released.
426 	 *
427 	 * @return true if the commit was successful and the file contains the new
428 	 *         data; false if the commit failed and the file remains with the
429 	 *         old data.
430 	 * @throws IllegalStateException
431 	 *             the lock is not held.
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 		// File deletion fails on windows if another thread is
459 		// concurrently reading the same file. So try a few times.
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 		// File renaming fails on windows if another thread is
478 		// concurrently reading the same file. So try a few times.
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 	 * Get the modification time of the output file when it was committed.
499 	 *
500 	 * @return modification time of the lock file right before we committed it.
501 	 */
502 	public long getCommitLastModified() {
503 		return commitSnapshot.lastModified();
504 	}
505 
506 	/** @return get the {@link FileSnapshot} just before commit. */
507 	public FileSnapshot getCommitSnapshot() {
508 		return commitSnapshot;
509 	}
510 
511 	/**
512 	 * Update the commit snapshot {@link #getCommitSnapshot()} before commit.
513 	 * <p>
514 	 * This may be necessary if you need time stamp before commit occurs, e.g
515 	 * while writing the index.
516 	 */
517 	public void createCommitSnapshot() {
518 		saveStatInformation();
519 	}
520 
521 	/**
522 	 * Unlock this file and abort this change.
523 	 * <p>
524 	 * The temporary file (if created) is deleted before returning.
525 	 */
526 	public void unlock() {
527 		if (os != null) {
528 			try {
529 				os.close();
530 			} catch (IOException ioe) {
531 				// Ignore this
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 				// couldn't delete the file even after retry.
542 			}
543 		}
544 	}
545 
546 	@SuppressWarnings("nls")
547 	@Override
548 	public String toString() {
549 		return "LockFile[" + lck + ", haveLck=" + haveLck + "]";
550 	}
551 }