Top Description Fields Constructors Methods
org.eclipse.jgit.internal.storage.file

public Class LockFile

extends Object
Class Inheritance
Static Imports
org.eclipse.jgit.lib.Constants.LOCK_SUFFIX

Git style file locking and replacement.

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

Modifier and TypeField and Description
private FileSnapshot
pack-priv static final FilenameFilter
FILTER

Filter to skip over active lock files when listing a directory.

private boolean
private boolean
private boolean
private final File
private static final Logger
private boolean
private FileOutputStream
private final File
private ShutdownHook.Listener
private boolean
private FS.LockToken
private boolean

Constructor Summary

AccessConstructor and Description
public
LockFile(File
the file that will be locked.
f
)

Create a new lock for any file.

Method Summary

Modifier and TypeMethod and Description
private void
public boolean

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.
commit
()

Commit this change and release the lock.

public void
copyCurrentContent()

Copy the current file content into the temporary file.

public void
createCommitSnapshot()

Update the commit snapshot getCommitSnapshot() before commit.

private FS.LockToken
private FS.LockToken
public Instant

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.

public FileSnapshot

Returns:

get the FileSnapshot just before commit.
getCommitSnapshot
()

Get the FileSnapshot just before commit.

pack-priv static File

Returns:

lock file
getLockFile
(File
given file
file
)

Get the lock file corresponding to the given file.

public OutputStream

Returns:

a stream to write to the new file. The stream is unbuffered.
getOutputStream
()

Obtain the direct output stream for this lock.

private FileOutputStream
pack-priv boolean
public boolean

Returns:

true if the lock is now held by the caller; false if it is held by someone else.
lock
()

Try to establish the lock.

public boolean

Returns:

true if the lock is now held by the caller; false if it is held by someone else.
lockForAppend
()

Try to establish the lock for appending.

pack-priv void
private void
public void
setFSync(boolean
true if dirty data should be forced to the drive.
on
)

Request that commit() force dirty data to the drive.

public void
setNeedSnapshot(boolean
true if the commit method must remember the FileSnapshot.
on
)

Request that commit() remember the org.eclipse.jgit.internal.storage.file.FileSnapshot.

public void
setNeedSnapshotNoConfig(boolean
true if the commit method must remember the FileSnapshot.
on
)

Request that commit() remember the org.eclipse.jgit.internal.storage.file.FileSnapshot without using config file to get filesystem timestamp resolution.

public void
setNeedStatInformation(boolean
true if the commit method must remember the modification time.
on
)

Request that commit() remember modification time.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public static boolean

Returns:

true if unlocked, false if unlocking failed
unlock
(File
a java.io.File object.
file
)

Unlock the given file.

public void
unlock()

Unlock this file and abort this change.

public void
waitForStatChange()

Wait until the lock file information differs from the old file.

public void
write(ObjectId
the id to store in the file. The id will be written in hex, followed by a sole LF.
id
)

Write an ObjectId and LF to the temporary file.

public void
write(byte[]
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.
content
)

Write arbitrary data to the temporary file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

commitSnapshotback to summary
private FileSnapshot commitSnapshot
FILTERback to summary
pack-priv static final FilenameFilter FILTER

Filter to skip over active lock files when listing a directory.

fsyncback to summary
private boolean fsync
haveLckback to summary
private boolean haveLck
isAppendback to summary
private boolean isAppend
lckback to summary
private final File lck
LOGback to summary
private static final Logger LOG
needSnapshotback to summary
private boolean needSnapshot
osback to summary
private FileOutputStream os
refback to summary
private final File ref
shutdownListenerback to summary
private ShutdownHook.Listener shutdownListener
snapshotNoConfigback to summary
private boolean snapshotNoConfig
tokenback to summary
private FS.LockToken token
writtenback to summary
private boolean written

Constructor Detail

LockFileback to summary
public LockFile(File f)

Create a new lock for any file.

Parameters
f:File

the file that will be locked.

Method Detail

closeTokenback to summary
private void closeToken()
commitback to summary
public boolean commit()

Commit this change and release the lock.

If this method fails (returns false) the lock is still released.

Returns:boolean

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.

Exceptions
IllegalStateException:
the lock is not held.
copyCurrentContentback to summary
public void copyCurrentContent() throws IOException

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.

Exceptions
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.
createCommitSnapshotback to summary
public void createCommitSnapshot()

Update the commit snapshot getCommitSnapshot() before commit.

This may be necessary if you need time stamp before commit occurs, e.g while writing the index.

createLockFileback to summary
private FS.LockToken createLockFile() throws IOException
createLockFileWithRetryback to summary
private FS.LockToken createLockFileWithRetry() throws IOException
getCommitLastModifiedInstantback to summary
public Instant getCommitLastModifiedInstant()

Get the modification time of the output file when it was committed.

Returns:Instant

modification time of the lock file right before we committed it.

getCommitSnapshotback to summary
public FileSnapshot getCommitSnapshot()

Get the FileSnapshot just before commit.

Returns:FileSnapshot

get the FileSnapshot just before commit.

getLockFileback to summary
pack-priv static File getLockFile(File file)

Get the lock file corresponding to the given file.

Parameters
file:File

given file

Returns:File

lock file

getOutputStreamback to summary
public OutputStream 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 calling commit() to commit the change.

Returns:OutputStream

a stream to write to the new file. The stream is unbuffered.

getStreamback to summary
private FileOutputStream getStream() throws IOException
isLockedback to summary
pack-priv boolean isLocked()
lockback to summary
public boolean lock() throws IOException

Try to establish the lock.

Returns:boolean

true if the lock is now held by the caller; false if it is held by someone else.

Exceptions
IOException:
the temporary output file could not be created. The caller does not hold the lock.
lockForAppendback to summary
public boolean lockForAppend() throws IOException

Try to establish the lock for appending.

Returns:boolean

true if the lock is now held by the caller; false if it is held by someone else.

Exceptions
IOException:
the temporary output file could not be created. The caller does not hold the lock.
requireLockback to summary
pack-priv void requireLock()
saveStatInformationback to summary
private void saveStatInformation()
setFSyncback to summary
public void setFSync(boolean on)

Request that commit() force dirty data to the drive.

Parameters
on:boolean

true if dirty data should be forced to the drive.

setNeedSnapshotback to summary
public void setNeedSnapshot(boolean on)

Request that commit() remember the org.eclipse.jgit.internal.storage.file.FileSnapshot.

Parameters
on:boolean

true if the commit method must remember the FileSnapshot.

setNeedSnapshotNoConfigback to summary
public void setNeedSnapshotNoConfig(boolean on)

Request that commit() remember the org.eclipse.jgit.internal.storage.file.FileSnapshot without using config file to get filesystem timestamp resolution. This method should be invoked before the file is accessed. It is used by FileBasedConfig to avoid endless recursion.

Parameters
on:boolean

true if the commit method must remember the FileSnapshot.

setNeedStatInformationback to summary
public void setNeedStatInformation(boolean on)

Request that commit() remember modification time.

This is an alias for setNeedSnapshot(true).

Parameters
on:boolean

true if the commit method must remember the modification time.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@SuppressWarnings:nls
@Override
unlockback to summary
public static boolean unlock(File file)

Unlock the given file.

This method can be used for recovering from a thrown org.eclipse.jgit.errors.LockFailedException . This method does not validate that the lock is or is not currently held before attempting to unlock it.

Parameters
file:File

a java.io.File object.

Returns:boolean

true if unlocked, false if unlocking failed

unlockback to summary
public void unlock()

Unlock this file and abort this change.

The temporary file (if created) is deleted before returning.

waitForStatChangeback to summary
public void waitForStatChange() throws InterruptedException

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.

Exceptions
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.
writeback to summary
public void write(ObjectId id) throws IOException

Write an ObjectId and LF to the temporary file.

Parameters
id:ObjectId

the id to store in the file. The id will be written in hex, followed by a sole LF.

Exceptions
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.
writeback to summary
public void write(byte[] content) throws IOException

Write arbitrary data to the temporary file.

Parameters
content:byte[]

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.

Exceptions
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.