Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c2f7911aa97fde479cd319c6e6fdd4e7d97cd61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright (C) 2009, Google Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.internal.storage.file;

import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;

/**
 * Keeps track of a {@link org.eclipse.jgit.internal.storage.file.PackFile}'s
 * associated <code>.keep</code> file.
 */
public class PackLock {
	private final File keepFile;

	/**
	 * Create a new lock for a pack file.
	 *
	 * @param packFile
	 *            location of the <code>pack-*.pack</code> file.
	 * @param fs
	 *            the filesystem abstraction used by the repository.
	 */
	public PackLock(File packFile, FS fs) {
		final File p = packFile.getParentFile();
		final String n = packFile.getName();
		keepFile = new File(p, n.substring(0, n.length() - 5) + ".keep"); //$NON-NLS-1$
	}

	/**
	 * Create the <code>pack-*.keep</code> file, with the given message.
	 *
	 * @param msg
	 *            message to store in the file.
	 * @return true if the keep file was successfully written; false otherwise.
	 * @throws java.io.IOException
	 *             the keep file could not be written.
	 */
	public boolean lock(String msg) throws IOException {
		if (msg == null)
			return false;
		if (!msg.endsWith("\n")) //$NON-NLS-1$
			msg += "\n"; //$NON-NLS-1$
		final LockFile lf = new LockFile(keepFile);
		if (!lf.lock())
			return false;
		lf.write(Constants.encode(msg));
		return lf.commit();
	}

	/**
	 * Remove the <code>.keep</code> file that holds this pack in place.
	 *
	 * @throws java.io.IOException
	 *             if deletion of .keep file failed
	 */
	public void unlock() throws IOException {
		FileUtils.delete(keepFile);
	}
}

Back to the top