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.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 org.eclipse.jgit.errors.LockFailedException} . This method does
84  	 * not validate that the lock is or is not currently held before attempting
85  	 * to unlock it.
86  	 *
87  	 * @param file
88  	 *            a {@link java.io.File} object.
89  	 * @return true if unlocked, false if unlocking failed
90  	 */
91  	public static boolean unlock(final File file) {
92  		final File lockFile = getLockFile(file);
93  		final int flags = FileUtils.RETRY | FileUtils.SKIP_MISSING;
94  		try {
95  			FileUtils.delete(lockFile, flags);
96  		} catch (IOException ignored) {
97  			// Ignore and return whether lock file still exists
98  		}
99  		return !lockFile.exists();
100 	}
101 
102 	/**
103 	 * Get the lock file corresponding to the given file.
104 	 *
105 	 * @param file
106 	 * @return lock file
107 	 */
108 	static File getLockFile(File file) {
109 		return new File(file.getParentFile(), file.getName() + SUFFIX);
110 	}
111 
112 	/** Filter to skip over active lock files when listing a directory. */
113 	static final FilenameFilter FILTER = new FilenameFilter() {
114 		@Override
115 		public boolean accept(File dir, String name) {
116 			return !name.endsWith(SUFFIX);
117 		}
118 	};
119 
120 	private final File ref;
121 
122 	private final File lck;
123 
124 	private boolean haveLck;
125 
126 	FileOutputStream os;
127 
128 	private boolean needSnapshot;
129 
130 	boolean fsync;
131 
132 	private FileSnapshot commitSnapshot;
133 
134 	/**
135 	 * Create a new lock for any file.
136 	 *
137 	 * @param f
138 	 *            the file that will be locked.
139 	 * @param fs
140 	 *            the file system abstraction which will be necessary to perform
141 	 *            certain file system operations.
142 	 * @deprecated use
143 	 *             {@link org.eclipse.jgit.internal.storage.file.LockFile#LockFile(File)}
144 	 *             instead
145 	 */
146 	@Deprecated
147 	public LockFile(final File f, final FS fs) {
148 		ref = f;
149 		lck = getLockFile(ref);
150 	}
151 
152 	/**
153 	 * Create a new lock for any file.
154 	 *
155 	 * @param f
156 	 *            the file that will be locked.
157 	 */
158 	public LockFile(final File f) {
159 		ref = f;
160 		lck = getLockFile(ref);
161 	}
162 
163 	/**
164 	 * Try to establish the lock.
165 	 *
166 	 * @return true if the lock is now held by the caller; false if it is held
167 	 *         by someone else.
168 	 * @throws java.io.IOException
169 	 *             the temporary output file could not be created. The caller
170 	 *             does not hold the lock.
171 	 */
172 	public boolean lock() throws IOException {
173 		FileUtils.mkdirs(lck.getParentFile(), true);
174 		if (FS.DETECTED.createNewFile(lck)) {
175 			haveLck = true;
176 			try {
177 				os = new FileOutputStream(lck);
178 			} catch (IOException ioe) {
179 				unlock();
180 				throw ioe;
181 			}
182 		}
183 		return haveLck;
184 	}
185 
186 	/**
187 	 * Try to establish the lock for appending.
188 	 *
189 	 * @return true if the lock is now held by the caller; false if it is held
190 	 *         by someone else.
191 	 * @throws java.io.IOException
192 	 *             the temporary output file could not be created. The caller
193 	 *             does not hold the lock.
194 	 */
195 	public boolean lockForAppend() throws IOException {
196 		if (!lock())
197 			return false;
198 		copyCurrentContent();
199 		return true;
200 	}
201 
202 	/**
203 	 * Copy the current file content into the temporary file.
204 	 * <p>
205 	 * This method saves the current file content by inserting it into the
206 	 * temporary file, so that the caller can safely append rather than replace
207 	 * the primary file.
208 	 * <p>
209 	 * This method does nothing if the current file does not exist, or exists
210 	 * but is empty.
211 	 *
212 	 * @throws java.io.IOException
213 	 *             the temporary file could not be written, or a read error
214 	 *             occurred while reading from the current file. The lock is
215 	 *             released before throwing the underlying IO exception to the
216 	 *             caller.
217 	 * @throws java.lang.RuntimeException
218 	 *             the temporary file could not be written. The lock is released
219 	 *             before throwing the underlying exception to the caller.
220 	 */
221 	public void copyCurrentContent() throws IOException {
222 		requireLock();
223 		try {
224 			try (FileInputStream fis = new FileInputStream(ref)) {
225 				if (fsync) {
226 					FileChannel in = fis.getChannel();
227 					long pos = 0;
228 					long cnt = in.size();
229 					while (0 < cnt) {
230 						long r = os.getChannel().transferFrom(in, pos, cnt);
231 						pos += r;
232 						cnt -= r;
233 					}
234 				} else {
235 					final byte[] buf = new byte[2048];
236 					int r;
237 					while ((r = fis.read(buf)) >= 0)
238 						os.write(buf, 0, r);
239 				}
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 java.io.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 java.lang.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 java.io.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 java.lang.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 	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
398 	 * {@link org.eclipse.jgit.internal.storage.file.FileSnapshot}.
399 	 *
400 	 * @param on
401 	 *            true if the commit method must remember the FileSnapshot.
402 	 */
403 	public void setNeedSnapshot(final boolean on) {
404 		needSnapshot = on;
405 	}
406 
407 	/**
408 	 * Request that {@link #commit()} force dirty data to the drive.
409 	 *
410 	 * @param on
411 	 *            true if dirty data should be forced to the drive.
412 	 */
413 	public void setFSync(final boolean on) {
414 		fsync = on;
415 	}
416 
417 	/**
418 	 * Wait until the lock file information differs from the old file.
419 	 * <p>
420 	 * This method tests the last modification date. If both are the same, this
421 	 * method sleeps until it can force the new lock file's modification date to
422 	 * be later than the target file.
423 	 *
424 	 * @throws java.lang.InterruptedException
425 	 *             the thread was interrupted before the last modified date of
426 	 *             the lock file was different from the last modified date of
427 	 *             the target file.
428 	 */
429 	public void waitForStatChange() throws InterruptedException {
430 		FileSnapshot o = FileSnapshot.save(ref);
431 		FileSnapshot n = FileSnapshot.save(lck);
432 		while (o.equals(n)) {
433 			Thread.sleep(25 /* milliseconds */);
434 			lck.setLastModified(System.currentTimeMillis());
435 			n = FileSnapshot.save(lck);
436 		}
437 	}
438 
439 	/**
440 	 * Commit this change and release the lock.
441 	 * <p>
442 	 * If this method fails (returns false) the lock is still released.
443 	 *
444 	 * @return true if the commit was successful and the file contains the new
445 	 *         data; false if the commit failed and the file remains with the
446 	 *         old data.
447 	 * @throws java.lang.IllegalStateException
448 	 *             the lock is not held.
449 	 */
450 	public boolean commit() {
451 		if (os != null) {
452 			unlock();
453 			throw new IllegalStateException(MessageFormat.format(JGitText.get().lockOnNotClosed, ref));
454 		}
455 
456 		saveStatInformation();
457 		try {
458 			FileUtils.rename(lck, ref, StandardCopyOption.ATOMIC_MOVE);
459 			haveLck = false;
460 			return true;
461 		} catch (IOException e) {
462 			unlock();
463 			return false;
464 		}
465 	}
466 
467 	private void saveStatInformation() {
468 		if (needSnapshot)
469 			commitSnapshot = FileSnapshot.save(lck);
470 	}
471 
472 	/**
473 	 * Get the modification time of the output file when it was committed.
474 	 *
475 	 * @return modification time of the lock file right before we committed it.
476 	 */
477 	public long getCommitLastModified() {
478 		return commitSnapshot.lastModified();
479 	}
480 
481 	/**
482 	 * Get the {@link FileSnapshot} just before commit.
483 	 *
484 	 * @return get the {@link FileSnapshot} just before commit.
485 	 */
486 	public FileSnapshot getCommitSnapshot() {
487 		return commitSnapshot;
488 	}
489 
490 	/**
491 	 * Update the commit snapshot {@link #getCommitSnapshot()} before commit.
492 	 * <p>
493 	 * This may be necessary if you need time stamp before commit occurs, e.g
494 	 * while writing the index.
495 	 */
496 	public void createCommitSnapshot() {
497 		saveStatInformation();
498 	}
499 
500 	/**
501 	 * Unlock this file and abort this change.
502 	 * <p>
503 	 * The temporary file (if created) is deleted before returning.
504 	 */
505 	public void unlock() {
506 		if (os != null) {
507 			try {
508 				os.close();
509 			} catch (IOException ioe) {
510 				// Ignore this
511 			}
512 			os = null;
513 		}
514 
515 		if (haveLck) {
516 			haveLck = false;
517 			try {
518 				FileUtils.delete(lck, FileUtils.RETRY);
519 			} catch (IOException e) {
520 				// couldn't delete the file even after retry.
521 			}
522 		}
523 	}
524 
525 	/** {@inheritDoc} */
526 	@SuppressWarnings("nls")
527 	@Override
528 	public String toString() {
529 		return "LockFile[" + lck + ", haveLck=" + haveLck + "]";
530 	}
531 }