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