Class LockFile
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate FileSnapshot
(package private) static final FilenameFilter
Filter to skip over active lock files when listing a directory.(package private) boolean
private boolean
private final File
private static final org.slf4j.Logger
private boolean
(package private) FileOutputStream
private final File
private FS.LockToken
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
boolean
commit()
Commit this change and release the lock.void
Copy the current file content into the temporary file.void
Update the commit snapshotgetCommitSnapshot()
before commit.long
Deprecated.Get the modification time of the output file when it was committed.Get theFileSnapshot
just before commit.(package private) static File
getLockFile
(File file) Get the lock file corresponding to the given file.Obtain the direct output stream for this lock.boolean
lock()
Try to establish the lock.boolean
Try to establish the lock for appending.(package private) void
private void
void
setFSync
(boolean on) Request thatcommit()
force dirty data to the drive.void
setNeedSnapshot
(boolean on) Request thatcommit()
remember theFileSnapshot
.void
setNeedStatInformation
(boolean on) Request thatcommit()
remember modification time.toString()
void
unlock()
Unlock this file and abort this change.static boolean
Unlock the given file.void
Wait until the lock file information differs from the old file.void
write
(byte[] content) Write arbitrary data to the temporary file.void
Write an ObjectId and LF to the temporary file.
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
FILTER
Filter to skip over active lock files when listing a directory. -
ref
-
lck
-
haveLck
private boolean haveLck -
os
-
needSnapshot
private boolean needSnapshot -
fsync
boolean fsync -
commitSnapshot
-
token
-
-
Constructor Details
-
LockFile
Create a new lock for any file.- Parameters:
f
- the file that will be locked.
-
-
Method Details
-
unlock
Unlock the given 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.- Parameters:
file
- aFile
object.- Returns:
- true if unlocked, false if unlocking failed
-
getLockFile
Get the lock file corresponding to the given file.- Parameters:
file
-- Returns:
- lock file
-
lock
Try to establish the lock.- Returns:
- true if the lock is now held by the caller; false if it is held by someone else.
- Throws:
IOException
- the temporary output file could not be created. The caller does not hold the lock.
-
lockForAppend
Try to establish the lock for appending.- Returns:
- true if the lock is now held by the caller; false if it is held by someone else.
- Throws:
IOException
- the temporary output file could not be created. The caller does not hold the lock.
-
copyCurrentContent
Copy the current file content into the temporary file.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.
- Throws:
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.RuntimeException
- the temporary file could not be written. The lock is released before throwing the underlying exception to the caller.
-
write
Write an ObjectId and LF to the temporary file.- Parameters:
id
- the id to store in the file. The id will be written in hex, followed by a sole LF.- Throws:
IOException
- the temporary file could not be written. The lock is released before throwing the underlying IO exception to the caller.RuntimeException
- the temporary file could not be written. The lock is released before throwing the underlying exception to the caller.
-
write
Write arbitrary data to the temporary file.- Parameters:
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.- Throws:
IOException
- the temporary file could not be written. The lock is released before throwing the underlying IO exception to the caller.RuntimeException
- the temporary file could not be written. The lock is released before throwing the underlying exception to the caller.
-
getOutputStream
Obtain the direct output stream for this lock.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 callingcommit()
to commit the change.- Returns:
- a stream to write to the new file. The stream is unbuffered.
-
requireLock
void requireLock() -
setNeedStatInformation
public void setNeedStatInformation(boolean on) Request thatcommit()
remember modification time.This is an alias for
setNeedSnapshot(true)
.- Parameters:
on
- true if the commit method must remember the modification time.
-
setNeedSnapshot
public void setNeedSnapshot(boolean on) Request thatcommit()
remember theFileSnapshot
.- Parameters:
on
- true if the commit method must remember the FileSnapshot.
-
setFSync
public void setFSync(boolean on) Request thatcommit()
force dirty data to the drive.- Parameters:
on
- true if dirty data should be forced to the drive.
-
waitForStatChange
Wait until the lock file information differs from the old file.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.
- Throws:
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.
-
commit
public boolean commit()Commit this change and release the lock.If this method fails (returns false) the lock is still released.
- Returns:
- true if the commit was successful and the file contains the new data; false if the commit failed and the file remains with the old data.
- Throws:
IllegalStateException
- the lock is not held.
-
closeToken
private void closeToken() -
saveStatInformation
private void saveStatInformation() -
getCommitLastModified
Deprecated.usegetCommitLastModifiedInstant()
insteadGet the modification time of the output file when it was committed.- Returns:
- modification time of the lock file right before we committed it.
-
getCommitLastModifiedInstant
Get the modification time of the output file when it was committed.- Returns:
- modification time of the lock file right before we committed it.
-
getCommitSnapshot
Get theFileSnapshot
just before commit.- Returns:
- get the
FileSnapshot
just before commit.
-
createCommitSnapshot
public void createCommitSnapshot()Update the commit snapshotgetCommitSnapshot()
before commit.This may be necessary if you need time stamp before commit occurs, e.g while writing the index.
-
unlock
public void unlock()Unlock this file and abort this change.The temporary file (if created) is deleted before returning.
-
toString
-
getCommitLastModifiedInstant()
instead