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