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.nio.file.StandardCopyOption;
58  import java.text.MessageFormat;
59  
60  import org.eclipse.jgit.errors.LockFailedException;
61  import org.eclipse.jgit.internal.JGitText;
62  import org.eclipse.jgit.lib.Constants;
63  import org.eclipse.jgit.lib.ObjectId;
64  import org.eclipse.jgit.util.FS;
65  import org.eclipse.jgit.util.FileUtils;
66  
67  /**
68   * Git style file locking and replacement.
69   * <p>
70   * To modify a ref file Git tries to use an atomic update approach: we write the
71   * new data into a brand new file, then rename it in place over the old name.
72   * This way we can just delete the temporary file if anything goes wrong, and
73   * nothing has been damaged. To coordinate access from multiple processes at
74   * once Git tries to atomically create the new temporary file under a well-known
75   * name.
76   */
77  public class LockFile {
78  	static final String SUFFIX = ".lock"; //$NON-NLS-1$
79  
80  	/**
81  	 * Unlock the given file.
82  	 * <p>
83  	 * This method can be used for recovering from a thrown
84  	 * {@link LockFailedException} . This method does not validate that the lock
85  	 * is or is not currently held before attempting to unlock it.
86  	 *
87  	 * @param file
88  	 * @return true if unlocked, false if unlocking failed
89  	 */
90  	public static boolean unlock(final File file) {
91  		final File lockFile = getLockFile(file);
92  		final int flags = FileUtils.RETRY | FileUtils.SKIP_MISSING;
93  		try {
94  			FileUtils.delete(lockFile, flags);
95  		} catch (IOException ignored) {
96  			// Ignore and return whether lock file still exists
97  		}
98  		return !lockFile.exists();
99  	}
100 
101 	/**
102 	 * Get the lock file corresponding to the given file.
103 	 *
104 	 * @param file
105 	 * @return lock file
106 	 */
107 	static File getLockFile(File file) {
108 		return new File(file.getParentFile(), file.getName() + SUFFIX);
109 	}
110 
111 	/** Filter to skip over active lock files when listing a directory. */
112 	static final FilenameFilter FILTER = new FilenameFilter() {
113 		@Override
114 		public boolean accept(File dir, String name) {
115 			return !name.endsWith(SUFFIX);
116 		}
117 	};
118 
119 	private final File ref;
120 
121 	private final File lck;
122 
123 	private boolean haveLck;
124 
125 	FileOutputStream os;
126 
127 	private boolean needSnapshot;
128 
129 	boolean fsync;
130 
131 	private FileSnapshot commitSnapshot;
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 	 * @deprecated use {@link LockFile#LockFile(File)} instead
142 	 */
143 	@Deprecated
144 	public LockFile(final File f, final FS fs) {
145 		ref = f;
146 		lck = getLockFile(ref);
147 	}
148 
149 	/**
150 	 * Create a new lock for any file.
151 	 *
152 	 * @param f
153 	 *            the file that will be locked.
154 	 */
155 	public LockFile(final File f) {
156 		ref = f;
157 		lck = getLockFile(ref);
158 	}
159 
160 	/**
161 	 * Try to establish the lock.
162 	 *
163 	 * @return true if the lock is now held by the caller; false if it is held
164 	 *         by someone else.
165 	 * @throws IOException
166 	 *             the temporary output file could not be created. The caller
167 	 *             does not hold the lock.
168 	 */
169 	public boolean lock() throws IOException {
170 		FileUtils.mkdirs(lck.getParentFile(), true);
171 		if (lck.createNewFile()) {
172 			haveLck = true;
173 			try {
174 				os = new FileOutputStream(lck);
175 			} catch (IOException ioe) {
176 				unlock();
177 				throw ioe;
178 			}
179 		}
180 		return haveLck;
181 	}
182 
183 	/**
184 	 * Try to establish the lock for appending.
185 	 *
186 	 * @return true if the lock is now held by the caller; false if it is held
187 	 *         by someone else.
188 	 * @throws IOException
189 	 *             the temporary output file could not be created. The caller
190 	 *             does not hold the lock.
191 	 */
192 	public boolean lockForAppend() throws IOException {
193 		if (!lock())
194 			return false;
195 		copyCurrentContent();
196 		return true;
197 	}
198 
199 	/**
200 	 * Copy the current file content into the temporary file.
201 	 * <p>
202 	 * This method saves the current file content by inserting it into the
203 	 * temporary file, so that the caller can safely append rather than replace
204 	 * the primary file.
205 	 * <p>
206 	 * This method does nothing if the current file does not exist, or exists
207 	 * but is empty.
208 	 *
209 	 * @throws IOException
210 	 *             the temporary file could not be written, or a read error
211 	 *             occurred while reading from the current file. The lock is
212 	 *             released before throwing the underlying IO exception to the
213 	 *             caller.
214 	 * @throws RuntimeException
215 	 *             the temporary file could not be written. The lock is released
216 	 *             before throwing the underlying exception to the caller.
217 	 */
218 	public void copyCurrentContent() throws IOException {
219 		requireLock();
220 		try {
221 			final FileInputStream fis = new FileInputStream(ref);
222 			try {
223 				if (fsync) {
224 					FileChannel in = fis.getChannel();
225 					long pos = 0;
226 					long cnt = in.size();
227 					while (0 < cnt) {
228 						long r = os.getChannel().transferFrom(in, pos, cnt);
229 						pos += r;
230 						cnt -= r;
231 					}
232 				} else {
233 					final byte[] buf = new byte[2048];
234 					int r;
235 					while ((r = fis.read(buf)) >= 0)
236 						os.write(buf, 0, r);
237 				}
238 			} finally {
239 				fis.close();
240 			}
241 		} catch (FileNotFoundException fnfe) {
242 			if (ref.exists()) {
243 				unlock();
244 				throw fnfe;
245 			}
246 			// Don't worry about a file that doesn't exist yet, it
247 			// conceptually has no current content to copy.
248 			//
249 		} catch (IOException ioe) {
250 			unlock();
251 			throw ioe;
252 		} catch (RuntimeException ioe) {
253 			unlock();
254 			throw ioe;
255 		} catch (Error ioe) {
256 			unlock();
257 			throw ioe;
258 		}
259 	}
260 
261 	/**
262 	 * Write an ObjectId and LF to the temporary file.
263 	 *
264 	 * @param id
265 	 *            the id to store in the file. The id will be written in hex,
266 	 *            followed by a sole LF.
267 	 * @throws IOException
268 	 *             the temporary file could not be written. The lock is released
269 	 *             before throwing the underlying IO exception to the caller.
270 	 * @throws RuntimeException
271 	 *             the temporary file could not be written. The lock is released
272 	 *             before throwing the underlying exception to the caller.
273 	 */
274 	public void write(final ObjectId id) throws IOException {
275 		byte[] buf = new byte[Constants.OBJECT_ID_STRING_LENGTH + 1];
276 		id.copyTo(buf, 0);
277 		buf[Constants.OBJECT_ID_STRING_LENGTH] = '\n';
278 		write(buf);
279 	}
280 
281 	/**
282 	 * Write arbitrary data to the temporary file.
283 	 *
284 	 * @param content
285 	 *            the bytes to store in the temporary file. No additional bytes
286 	 *            are added, so if the file must end with an LF it must appear
287 	 *            at the end of the byte array.
288 	 * @throws IOException
289 	 *             the temporary file could not be written. The lock is released
290 	 *             before throwing the underlying IO exception to the caller.
291 	 * @throws RuntimeException
292 	 *             the temporary file could not be written. The lock is released
293 	 *             before throwing the underlying exception to the caller.
294 	 */
295 	public void write(final byte[] content) throws IOException {
296 		requireLock();
297 		try {
298 			if (fsync) {
299 				FileChannel fc = os.getChannel();
300 				ByteBuffer buf = ByteBuffer.wrap(content);
301 				while (0 < buf.remaining())
302 					fc.write(buf);
303 				fc.force(true);
304 			} else {
305 				os.write(content);
306 			}
307 			os.close();
308 			os = null;
309 		} catch (IOException ioe) {
310 			unlock();
311 			throw ioe;
312 		} catch (RuntimeException ioe) {
313 			unlock();
314 			throw ioe;
315 		} catch (Error ioe) {
316 			unlock();
317 			throw ioe;
318 		}
319 	}
320 
321 	/**
322 	 * Obtain the direct output stream for this lock.
323 	 * <p>
324 	 * The stream may only be accessed once, and only after {@link #lock()} has
325 	 * been successfully invoked and returned true. Callers must close the
326 	 * stream prior to calling {@link #commit()} to commit the change.
327 	 *
328 	 * @return a stream to write to the new file. The stream is unbuffered.
329 	 */
330 	public OutputStream getOutputStream() {
331 		requireLock();
332 
333 		final OutputStream out;
334 		if (fsync)
335 			out = Channels.newOutputStream(os.getChannel());
336 		else
337 			out = os;
338 
339 		return new OutputStream() {
340 			@Override
341 			public void write(final byte[] b, final int o, final int n)
342 					throws IOException {
343 				out.write(b, o, n);
344 			}
345 
346 			@Override
347 			public void write(final byte[] b) throws IOException {
348 				out.write(b);
349 			}
350 
351 			@Override
352 			public void write(final int b) throws IOException {
353 				out.write(b);
354 			}
355 
356 			@Override
357 			public void close() throws IOException {
358 				try {
359 					if (fsync)
360 						os.getChannel().force(true);
361 					out.close();
362 					os = null;
363 				} catch (IOException ioe) {
364 					unlock();
365 					throw ioe;
366 				} catch (RuntimeException ioe) {
367 					unlock();
368 					throw ioe;
369 				} catch (Error ioe) {
370 					unlock();
371 					throw ioe;
372 				}
373 			}
374 		};
375 	}
376 
377 	private void requireLock() {
378 		if (os == null) {
379 			unlock();
380 			throw new IllegalStateException(MessageFormat.format(JGitText.get().lockOnNotHeld, ref));
381 		}
382 	}
383 
384 	/**
385 	 * Request that {@link #commit()} remember modification time.
386 	 * <p>
387 	 * This is an alias for {@code setNeedSnapshot(true)}.
388 	 *
389 	 * @param on
390 	 *            true if the commit method must remember the modification time.
391 	 */
392 	public void setNeedStatInformation(final boolean on) {
393 		setNeedSnapshot(on);
394 	}
395 
396 	/**
397 	 * Request that {@link #commit()} remember the {@link FileSnapshot}.
398 	 *
399 	 * @param on
400 	 *            true if the commit method must remember the FileSnapshot.
401 	 */
402 	public void setNeedSnapshot(final boolean on) {
403 		needSnapshot = on;
404 	}
405 
406 	/**
407 	 * Request that {@link #commit()} force dirty data to the drive.
408 	 *
409 	 * @param on
410 	 *            true if dirty data should be forced to the drive.
411 	 */
412 	public void setFSync(final boolean on) {
413 		fsync = on;
414 	}
415 
416 	/**
417 	 * Wait until the lock file information differs from the old file.
418 	 * <p>
419 	 * This method tests the last modification date. If both are the same, this
420 	 * method sleeps until it can force the new lock file's modification date to
421 	 * be later than the target file.
422 	 *
423 	 * @throws InterruptedException
424 	 *             the thread was interrupted before the last modified date of
425 	 *             the lock file was different from the last modified date of
426 	 *             the target file.
427 	 */
428 	public void waitForStatChange() throws InterruptedException {
429 		FileSnapshot o = FileSnapshot.save(ref);
430 		FileSnapshot n = FileSnapshot.save(lck);
431 		while (o.equals(n)) {
432 			Thread.sleep(25 /* milliseconds */);
433 			lck.setLastModified(System.currentTimeMillis());
434 			n = FileSnapshot.save(lck);
435 		}
436 	}
437 
438 	/**
439 	 * Commit this change and release the lock.
440 	 * <p>
441 	 * If this method fails (returns false) the lock is still released.
442 	 *
443 	 * @return true if the commit was successful and the file contains the new
444 	 *         data; false if the commit failed and the file remains with the
445 	 *         old data.
446 	 * @throws IllegalStateException
447 	 *             the lock is not held.
448 	 */
449 	public boolean commit() {
450 		if (os != null) {
451 			unlock();
452 			throw new IllegalStateException(MessageFormat.format(JGitText.get().lockOnNotClosed, ref));
453 		}
454 
455 		saveStatInformation();
456 		try {
457 			FileUtils.rename(lck, ref, StandardCopyOption.ATOMIC_MOVE);
458 			haveLck = false;
459 			return true;
460 		} catch (IOException e) {
461 			unlock();
462 			return false;
463 		}
464 	}
465 
466 	private void saveStatInformation() {
467 		if (needSnapshot)
468 			commitSnapshot = FileSnapshot.save(lck);
469 	}
470 
471 	/**
472 	 * Get the modification time of the output file when it was committed.
473 	 *
474 	 * @return modification time of the lock file right before we committed it.
475 	 */
476 	public long getCommitLastModified() {
477 		return commitSnapshot.lastModified();
478 	}
479 
480 	/** @return get the {@link FileSnapshot} just before commit. */
481 	public FileSnapshot getCommitSnapshot() {
482 		return commitSnapshot;
483 	}
484 
485 	/**
486 	 * Update the commit snapshot {@link #getCommitSnapshot()} before commit.
487 	 * <p>
488 	 * This may be necessary if you need time stamp before commit occurs, e.g
489 	 * while writing the index.
490 	 */
491 	public void createCommitSnapshot() {
492 		saveStatInformation();
493 	}
494 
495 	/**
496 	 * Unlock this file and abort this change.
497 	 * <p>
498 	 * The temporary file (if created) is deleted before returning.
499 	 */
500 	public void unlock() {
501 		if (os != null) {
502 			try {
503 				os.close();
504 			} catch (IOException ioe) {
505 				// Ignore this
506 			}
507 			os = null;
508 		}
509 
510 		if (haveLck) {
511 			haveLck = false;
512 			try {
513 				FileUtils.delete(lck, FileUtils.RETRY);
514 			} catch (IOException e) {
515 				// couldn't delete the file even after retry.
516 			}
517 		}
518 	}
519 
520 	@SuppressWarnings("nls")
521 	@Override
522 	public String toString() {
523 		return "LockFile[" + lck + ", haveLck=" + haveLck + "]";
524 	}
525 }