public class LockFile
extends java.lang.Object
To modify a ref file Git tries to use an atomic update approach: we write the new data into a brand new file, then rename it in place over the old name. This way we can just delete the temporary file if anything goes wrong, and nothing has been damaged. To coordinate access from multiple processes at once Git tries to atomically create the new temporary file under a well-known name.
Modifier and Type | Field and Description |
---|---|
private FileSnapshot |
commitSnapshot |
(package private) static java.io.FilenameFilter |
FILTER
Filter to skip over active lock files when listing a directory.
|
(package private) boolean |
fsync |
private boolean |
haveLck |
private java.io.File |
lck |
private static org.slf4j.Logger |
LOG |
private boolean |
needSnapshot |
(package private) java.io.FileOutputStream |
os |
private java.io.File |
ref |
private FS.LockToken |
token |
Constructor and Description |
---|
LockFile(java.io.File f)
Create a new lock for any file.
|
Modifier and Type | Method and Description |
---|---|
private void |
closeToken() |
boolean |
commit()
Commit this change and release the lock.
|
void |
copyCurrentContent()
Copy the current file content into the temporary file.
|
void |
createCommitSnapshot()
Update the commit snapshot
getCommitSnapshot() before commit. |
long |
getCommitLastModified()
Get the modification time of the output file when it was committed.
|
FileSnapshot |
getCommitSnapshot()
Get the
FileSnapshot just before commit. |
(package private) static java.io.File |
getLockFile(java.io.File file)
Get the lock file corresponding to the given file.
|
java.io.OutputStream |
getOutputStream()
Obtain the direct output stream for this lock.
|
boolean |
lock()
Try to establish the lock.
|
boolean |
lockForAppend()
Try to establish the lock for appending.
|
(package private) void |
requireLock() |
private void |
saveStatInformation() |
void |
setFSync(boolean on)
Request that
commit() force dirty data to the drive. |
void |
setNeedSnapshot(boolean on)
Request that
commit() remember the
FileSnapshot . |
void |
setNeedStatInformation(boolean on)
Request that
commit() remember modification time. |
java.lang.String |
toString() |
void |
unlock()
Unlock this file and abort this change.
|
static boolean |
unlock(java.io.File file)
Unlock the given file.
|
void |
waitForStatChange()
Wait until the lock file information differs from the old file.
|
void |
write(byte[] content)
Write arbitrary data to the temporary file.
|
void |
write(ObjectId id)
Write an ObjectId and LF to the temporary file.
|
private static final org.slf4j.Logger LOG
static final java.io.FilenameFilter FILTER
private final java.io.File ref
private final java.io.File lck
private boolean haveLck
java.io.FileOutputStream os
private boolean needSnapshot
boolean fsync
private FileSnapshot commitSnapshot
private FS.LockToken token
public LockFile(java.io.File f)
f
- the file that will be locked.public static boolean unlock(java.io.File file)
This method can be used for recovering from a thrown
LockFailedException
. This method does
not validate that the lock is or is not currently held before attempting
to unlock it.
file
- a File
object.static java.io.File getLockFile(java.io.File file)
file
- public boolean lock() throws java.io.IOException
java.io.IOException
- the temporary output file could not be created. The caller
does not hold the lock.public boolean lockForAppend() throws java.io.IOException
java.io.IOException
- the temporary output file could not be created. The caller
does not hold the lock.public void copyCurrentContent() throws java.io.IOException
This method saves the current file content by inserting it into the temporary file, so that the caller can safely append rather than replace the primary file.
This method does nothing if the current file does not exist, or exists but is empty.
java.io.IOException
- the temporary file could not be written, or a read error
occurred while reading from the current file. The lock is
released before throwing the underlying IO exception to the
caller.java.lang.RuntimeException
- the temporary file could not be written. The lock is released
before throwing the underlying exception to the caller.public void write(ObjectId id) throws java.io.IOException
id
- the id to store in the file. The id will be written in hex,
followed by a sole LF.java.io.IOException
- the temporary file could not be written. The lock is released
before throwing the underlying IO exception to the caller.java.lang.RuntimeException
- the temporary file could not be written. The lock is released
before throwing the underlying exception to the caller.public void write(byte[] content) throws java.io.IOException
content
- the bytes to store in the temporary file. No additional bytes
are added, so if the file must end with an LF it must appear
at the end of the byte array.java.io.IOException
- the temporary file could not be written. The lock is released
before throwing the underlying IO exception to the caller.java.lang.RuntimeException
- the temporary file could not be written. The lock is released
before throwing the underlying exception to the caller.public java.io.OutputStream getOutputStream()
The stream may only be accessed once, and only after lock()
has
been successfully invoked and returned true. Callers must close the
stream prior to calling commit()
to commit the change.
void requireLock()
public void setNeedStatInformation(boolean on)
commit()
remember modification time.
This is an alias for setNeedSnapshot(true)
.
on
- true if the commit method must remember the modification time.public void setNeedSnapshot(boolean on)
commit()
remember the
FileSnapshot
.on
- true if the commit method must remember the FileSnapshot.public void setFSync(boolean on)
commit()
force dirty data to the drive.on
- true if dirty data should be forced to the drive.public void waitForStatChange() throws java.lang.InterruptedException
This method tests the last modification date. If both are the same, this method sleeps until it can force the new lock file's modification date to be later than the target file.
java.lang.InterruptedException
- the thread was interrupted before the last modified date of
the lock file was different from the last modified date of
the target file.public boolean commit()
If this method fails (returns false) the lock is still released.
java.lang.IllegalStateException
- the lock is not held.private void closeToken()
private void saveStatInformation()
public long getCommitLastModified()
public FileSnapshot getCommitSnapshot()
FileSnapshot
just before commit.FileSnapshot
just before commit.public void createCommitSnapshot()
getCommitSnapshot()
before commit.
This may be necessary if you need time stamp before commit occurs, e.g while writing the index.
public void unlock()
The temporary file (if created) is deleted before returning.
public java.lang.String toString()
toString
in class java.lang.Object