- java.lang.Object
-
- org.eclipse.jgit.util.FS
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFS.AttributesFile attributes we typically care for.static classFS.ExecutionResultResult of an executed process.static classFS.FileStoreAttributesAttributes of FileStores on this systemstatic classFS.FSFactoryThis class creates FS instances.static classFS.LockTokenA token representing a file created bycreateNewFileAtomic(File).
-
Field Summary
Fields Modifier and Type Field Description static FSDETECTEDThe auto-detected implementation selected for this operating system and JRE.protected static WorkingTreeIterator.Entry[]NO_ENTRIESAn empty array of entries, suitable as a return value forlist(File, FileModeStrategy).
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description abstract booleancanExecute(File f)Determine if the file is executable (or not).booleancreateNewFile(File path)Deprecated.usecreateNewFileAtomic(File)insteadFS.LockTokencreateNewFileAtomic(File path)Create a new file.voidcreateSymLink(File path, String target)Create a symbolic linkvoiddelete(File f)Delete a file.static FSdetect()Auto-detect the appropriate file system abstraction.static FSdetect(Boolean cygwinUsed)Auto-detect the appropriate file system abstraction, taking into account the presence of a Cygwin installation on the system.protected abstract FilediscoverGitExe()Discover the path to the Git executable.protected FilediscoverGitSystemConfig()Discover the path to the system-wide Git configuration fileFS.ExecutionResultexecute(ProcessBuilder pb, InputStream in)Execute a command defined by aProcessBuilder.booleanexists(File path)Tests if the path exists, in case of a symbolic link, true even if the target does not existBasicFileAttributesfileAttributes(File file)Return all the attributes of a file, without following symbolic links.FilefindHook(Repository repository, String hookName)Tries to find a hook matching the given one in the given repository.FS.AttributesgetAttributes(File path)Get the file attributes we care for.static FS.FileStoreAttributesgetFileStoreAttributes(Path dir)Get cached FileStore attributes, if not yet available measure them using a probe file under the given directory.FilegetGitSystemConfig()Get the currently used path to the system-wide Git configuration file.protected ProcessResultinternalRunHookIfPresent(Repository repository, String hookName, String[] args, OutputStream outRedirect, OutputStream errRedirect, String stdinArgs)abstract booleanisCaseSensitive()Is this file system case sensitivebooleanisDirectory(File path)Check if path is a directory.booleanisFile(File path)Examine if path represents a regular file.booleanisHidden(File path)Whether path is hidden, either starts with .booleanisSymLink(File path)Whether the path is a symbolic link (and we support these).longlastModified(File f)Deprecated.uselastModifiedInstant(Path)insteadInstantlastModifiedInstant(File f)Get the last modified time of a file system object.InstantlastModifiedInstant(Path p)Get the last modified time of a file system object.longlength(File path)Get the length of a file or link, If the OS/JRE supports symbolic links it's the length of the link, else the length of the target.WorkingTreeIterator.Entry[]list(File directory, FileTreeIterator.FileModeStrategy fileModeStrategy)Enumerates children of a directory.abstract FSnewInstance()Create a new instance of the same type of FS.Filenormalize(File file)Normalize the unicode path to composed form.Stringnormalize(String name)Normalize the unicode path to composed form.protected static StringreadPipe(File dir, String[] command, String encoding)Execute a command and return a single line of output as a Stringprotected static StringreadPipe(File dir, String[] command, String encoding, Map<String,String> env)Execute a command and return a single line of output as a StringStringreadSymLink(File path)Check if a file is a symbolic link and read itStringrelativize(String base, String other)Fileresolve(File dir, String name)Resolve this file to its actual path name that the JRE can use.protected static FileresolveGrandparentFile(File grandchild)Get the parent directory of this file's parent directoryabstract booleanretryFailedLockFileCommit()Does this file system have problems with atomic renames?ProcessResultrunHookIfPresent(Repository repository, String hookName, String[] args)Checks whether the given hook is defined for the given repository, then runs it with the given arguments.ProcessResultrunHookIfPresent(Repository repository, String hookName, String[] args, OutputStream outRedirect, OutputStream errRedirect, String stdinArgs)Checks whether the given hook is defined for the given repository, then runs it with the given arguments.abstract ProcessBuilderrunInShell(String cmd, String[] args)Initialize a ProcessBuilder to run a command using the system shell.intrunProcess(ProcessBuilder processBuilder, OutputStream outRedirect, OutputStream errRedirect, InputStream inRedirect)Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.intrunProcess(ProcessBuilder processBuilder, OutputStream outRedirect, OutputStream errRedirect, String stdinArgs)Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.protected static FilesearchPath(String path, String... lookFor)Searches the given path to see if it contains one of the given files.static voidsetAsyncFileStoreAttributes(boolean asynch)Deprecated.UseFS.FileStoreAttributes.setBackground(boolean)insteadabstract booleansetExecute(File f, boolean canExec)Set a file to be executable by the user.FSsetGitSystemConfig(File configFile)Set the path to the system-wide Git configuration file to use.voidsetHidden(File path, boolean hidden)Set the hidden attribute for file whose name starts with a period.voidsetLastModified(File f, long time)Deprecated.usesetLastModified(Path, Instant)insteadvoidsetLastModified(Path p, Instant time)Set the last modified time of a file system object.FSsetUserHome(File path)Set the user's home directory location.booleansupportsAtomicCreateNewFile()Does this file system support atomic file creation via java.io.File#createNewFile()? In certain environments (e.g.abstract booleansupportsExecute()Does this operating system and JRE support the execute flag on files?booleansupportsSymlinks()Does this operating system and JRE supports symbolic links.FileuserHome()Determine the user's home directory (location where preferences are).protected FileuserHomeImpl()Determine the user's home directory (location where preferences are).
-
-
-
Field Detail
-
NO_ENTRIES
protected static final WorkingTreeIterator.Entry[] NO_ENTRIES
An empty array of entries, suitable as a return value forlist(File, FileModeStrategy).- Since:
- 5.0
-
DETECTED
public static final FS DETECTED
The auto-detected implementation selected for this operating system and JRE.
-
-
Constructor Detail
-
FS
protected FS()
Constructs a file system abstraction.
-
FS
protected FS(FS src)
Initialize this FS using another's current settings.- Parameters:
src- the source FS to copy from.
-
-
Method Detail
-
detect
public static FS detect()
Auto-detect the appropriate file system abstraction.- Returns:
- detected file system abstraction
-
setAsyncFileStoreAttributes
@Deprecated public static void setAsyncFileStoreAttributes(boolean asynch)
Deprecated.UseFS.FileStoreAttributes.setBackground(boolean)insteadWhether FileStore attributes should be determined asynchronously- Parameters:
asynch- whether FileStore attributes should be determined asynchronously. If false access to cached attributes may block for some seconds for the first call per FileStore- Since:
- 5.1.9
-
detect
public static FS detect(Boolean cygwinUsed)
Auto-detect the appropriate file system abstraction, taking into account the presence of a Cygwin installation on the system. Using jgit in combination with Cygwin requires a more elaborate (and possibly slower) resolution of file system paths.- Parameters:
cygwinUsed-Boolean.TRUEto assume that Cygwin is used in combination with jgitBoolean.FALSEto assume that Cygwin is not used with jgitnullto auto-detect whether a Cygwin installation is present on the system and in this case assume that Cygwin is used
- Returns:
- detected file system abstraction
-
getFileStoreAttributes
public static FS.FileStoreAttributes getFileStoreAttributes(@NonNull Path dir)
Get cached FileStore attributes, if not yet available measure them using a probe file under the given directory.- Parameters:
dir- the directory under which the probe file will be created to measure the timer resolution.- Returns:
- measured filesystem timestamp resolution
- Since:
- 5.1.9
-
newInstance
public abstract FS newInstance()
Create a new instance of the same type of FS.- Returns:
- a new instance of the same type of FS.
-
supportsExecute
public abstract boolean supportsExecute()
Does this operating system and JRE support the execute flag on files?- Returns:
- true if this implementation can provide reasonably accurate executable bit information; false otherwise.
-
supportsAtomicCreateNewFile
public boolean supportsAtomicCreateNewFile()
Does this file system support atomic file creation via java.io.File#createNewFile()? In certain environments (e.g. on NFS) it is not guaranteed that when two file system clients run createNewFile() in parallel only one will succeed. In such cases both clients may think they created a new file.- Returns:
- true if this implementation support atomic creation of new Files
by
File.createNewFile() - Since:
- 4.5
-
supportsSymlinks
public boolean supportsSymlinks()
Does this operating system and JRE supports symbolic links. The capability to handle symbolic links is detected at runtime.- Returns:
- true if symbolic links may be used
- Since:
- 3.0
-
isCaseSensitive
public abstract boolean isCaseSensitive()
Is this file system case sensitive- Returns:
- true if this implementation is case sensitive
-
canExecute
public abstract boolean canExecute(File f)
Determine if the file is executable (or not).Not all platforms and JREs support executable flags on files. If the feature is unsupported this method will always return false.
If the platform supports symbolic links and
fis a symbolic link this method returns false, rather than the state of the executable flags on the target file.- Parameters:
f- abstract path to test.- Returns:
- true if the file is believed to be executable by the user.
-
setExecute
public abstract boolean setExecute(File f, boolean canExec)
Set a file to be executable by the user.Not all platforms and JREs support executable flags on files. If the feature is unsupported this method will always return false and no changes will be made to the file specified.
- Parameters:
f- path to modify the executable status of.canExec- true to enable execution; false to disable it.- Returns:
- true if the change succeeded; false otherwise.
-
lastModified
@Deprecated public long lastModified(File f) throws IOException
Deprecated.uselastModifiedInstant(Path)insteadGet the last modified time of a file system object. If the OS/JRE support symbolic links, the modification time of the link is returned, rather than that of the link target.- Parameters:
f- aFileobject.- Returns:
- last modified time of f
- Throws:
IOException- Since:
- 3.0
-
lastModifiedInstant
public Instant lastModifiedInstant(Path p)
Get the last modified time of a file system object. If the OS/JRE support symbolic links, the modification time of the link is returned, rather than that of the link target.- Parameters:
p- aPathobject.- Returns:
- last modified time of p
- Since:
- 5.1.9
-
lastModifiedInstant
public Instant lastModifiedInstant(File f)
Get the last modified time of a file system object. If the OS/JRE support symbolic links, the modification time of the link is returned, rather than that of the link target.- Parameters:
f- aFileobject.- Returns:
- last modified time of p
- Since:
- 5.1.9
-
setLastModified
@Deprecated public void setLastModified(File f, long time) throws IOException
Deprecated.usesetLastModified(Path, Instant)insteadSet the last modified time of a file system object.For symlinks it sets the modified time of the link target.
- Parameters:
f- aFileobject.time- last modified time- Throws:
IOException- Since:
- 3.0
-
setLastModified
public void setLastModified(Path p, Instant time) throws IOException
Set the last modified time of a file system object.For symlinks it sets the modified time of the link target.
- Parameters:
p- aPathobject.time- last modified time- Throws:
IOException- Since:
- 5.1.9
-
length
public long length(File path) throws IOException
Get the length of a file or link, If the OS/JRE supports symbolic links it's the length of the link, else the length of the target.- Parameters:
path- aFileobject.- Returns:
- length of a file
- Throws:
IOException- Since:
- 3.0
-
delete
public void delete(File f) throws IOException
Delete a file. Throws an exception if delete fails.- Parameters:
f- aFileobject.- Throws:
IOException- this may be a Java7 subclass with detailed information- Since:
- 3.3
-
resolve
public File resolve(File dir, String name)
Resolve this file to its actual path name that the JRE can use.This method can be relatively expensive. Computing a translation may require forking an external process per path name translated. Callers should try to minimize the number of translations necessary by caching the results.
Not all platforms and JREs require path name translation. Currently only Cygwin on Win32 require translation for Cygwin based paths.
- Parameters:
dir- directory relative to which the path name is.name- path name to translate.- Returns:
- the translated path.
new File(dir,name)if this platform does not require path name translation.
-
userHome
public File userHome()
Determine the user's home directory (location where preferences are).This method can be expensive on the first invocation if path name translation is required. Subsequent invocations return a cached result.
Not all platforms and JREs require path name translation. Currently only Cygwin on Win32 requires translation of the Cygwin HOME directory.
- Returns:
- the user's home directory; null if the user does not have one.
-
setUserHome
public FS setUserHome(File path)
Set the user's home directory location.- Parameters:
path- the location of the user's preferences; null if there is no home directory for the current user.- Returns:
this.
-
retryFailedLockFileCommit
public abstract boolean retryFailedLockFileCommit()
Does this file system have problems with atomic renames?- Returns:
- true if the caller should retry a failed rename of a lock file.
-
fileAttributes
public BasicFileAttributes fileAttributes(File file) throws IOException
Return all the attributes of a file, without following symbolic links.- Parameters:
file-- Returns:
BasicFileAttributesof the file- Throws:
IOException- in case of any I/O errors accessing the file- Since:
- 4.5.6
-
userHomeImpl
protected File userHomeImpl()
Determine the user's home directory (location where preferences are).- Returns:
- the user's home directory; null if the user does not have one.
-
searchPath
protected static File searchPath(String path, String... lookFor)
Searches the given path to see if it contains one of the given files. Returns the first it finds which is executable. Returns null if not found or if path is null.- Parameters:
path- List of paths to search separated by File.pathSeparatorlookFor- Files to search for in the given path- Returns:
- the first match found, or null
- Since:
- 3.0
-
readPipe
@Nullable protected static String readPipe(File dir, String[] command, String encoding) throws CommandFailedException
Execute a command and return a single line of output as a String- Parameters:
dir- Working directory for the commandcommand- as component arrayencoding- to be used to parse the command's output- Returns:
- the one-line output of the command or
nullif there is none - Throws:
CommandFailedException- thrown when the command failed (return code was non-zero)
-
readPipe
@Nullable protected static String readPipe(File dir, String[] command, String encoding, Map<String,String> env) throws CommandFailedException
Execute a command and return a single line of output as a String- Parameters:
dir- Working directory for the commandcommand- as component arrayencoding- to be used to parse the command's outputenv- Map of environment variables to be merged with those of the current process- Returns:
- the one-line output of the command or
nullif there is none - Throws:
CommandFailedException- thrown when the command failed (return code was non-zero)- Since:
- 4.0
-
discoverGitExe
protected abstract File discoverGitExe()
Discover the path to the Git executable.- Returns:
- the path to the Git executable or
nullif it cannot be determined. - Since:
- 4.0
-
discoverGitSystemConfig
protected File discoverGitSystemConfig()
Discover the path to the system-wide Git configuration file- Returns:
- the path to the system-wide Git configuration file or
nullif it cannot be determined. - Since:
- 4.0
-
getGitSystemConfig
public File getGitSystemConfig()
Get the currently used path to the system-wide Git configuration file.- Returns:
- the currently used path to the system-wide Git configuration file
or
nullif none has been set. - Since:
- 4.0
-
setGitSystemConfig
public FS setGitSystemConfig(File configFile)
Set the path to the system-wide Git configuration file to use.- Parameters:
configFile- the path to the config file.- Returns:
this- Since:
- 4.0
-
resolveGrandparentFile
protected static File resolveGrandparentFile(File grandchild)
Get the parent directory of this file's parent directory- Parameters:
grandchild- aFileobject.- Returns:
- the parent directory of this file's parent directory or
nullin case there's no grandparent directory - Since:
- 4.0
-
readSymLink
public String readSymLink(File path) throws IOException
Check if a file is a symbolic link and read it- Parameters:
path- aFileobject.- Returns:
- target of link or null
- Throws:
IOException- Since:
- 3.0
-
isSymLink
public boolean isSymLink(File path) throws IOException
Whether the path is a symbolic link (and we support these).- Parameters:
path- aFileobject.- Returns:
- true if the path is a symbolic link (and we support these)
- Throws:
IOException- Since:
- 3.0
-
exists
public boolean exists(File path)
Tests if the path exists, in case of a symbolic link, true even if the target does not exist- Parameters:
path- aFileobject.- Returns:
- true if path exists
- Since:
- 3.0
-
isDirectory
public boolean isDirectory(File path)
Check if path is a directory. If the OS/JRE supports symbolic links and path is a symbolic link to a directory, this method returns false.- Parameters:
path- aFileobject.- Returns:
- true if file is a directory,
- Since:
- 3.0
-
isFile
public boolean isFile(File path)
Examine if path represents a regular file. If the OS/JRE supports symbolic links the test returns false if path represents a symbolic link.- Parameters:
path- aFileobject.- Returns:
- true if path represents a regular file
- Since:
- 3.0
-
isHidden
public boolean isHidden(File path) throws IOException
Whether path is hidden, either starts with . on unix or has the hidden attribute in windows- Parameters:
path- aFileobject.- Returns:
- true if path is hidden, either starts with . on unix or has the hidden attribute in windows
- Throws:
IOException- Since:
- 3.0
-
setHidden
public void setHidden(File path, boolean hidden) throws IOException
Set the hidden attribute for file whose name starts with a period.- Parameters:
path- aFileobject.hidden- whether to set the file hidden- Throws:
IOException- Since:
- 3.0
-
createSymLink
public void createSymLink(File path, String target) throws IOException
Create a symbolic link- Parameters:
path- aFileobject.target- target path of the symlink- Throws:
IOException- Since:
- 3.0
-
createNewFile
@Deprecated public boolean createNewFile(File path) throws IOException
Deprecated.usecreateNewFileAtomic(File)insteadCreate a new file. SeeFile.createNewFile(). Subclasses of this class may take care to provide a safe implementation for this even ifsupportsAtomicCreateNewFile()isfalse- Parameters:
path- the file to be created- Returns:
trueif the file was created,falseif the file already existed- Throws:
IOException- Since:
- 4.5
-
createNewFileAtomic
public FS.LockToken createNewFileAtomic(File path) throws IOException
Create a new file. SeeFile.createNewFile(). Subclasses of this class may take care to provide a safe implementation for this even ifsupportsAtomicCreateNewFile()isfalse- Parameters:
path- the file to be created- Returns:
- LockToken this token must be closed after the created file was deleted
- Throws:
IOException- Since:
- 4.7
-
relativize
public String relativize(String base, String other)
- Parameters:
base- The path against whichothershould be relativized.other- The path that will be made relative tobase.- Returns:
- A relative path that, when resolved against
base, will yield the originalother. - Since:
- 3.7
- See Also:
FileUtils.relativizePath(String, String, String, boolean)
-
list
public WorkingTreeIterator.Entry[] list(File directory, FileTreeIterator.FileModeStrategy fileModeStrategy)
Enumerates children of a directory.- Parameters:
directory- to get the children offileModeStrategy- to use to calculate the git mode of a child- Returns:
- an array of entries for the children
- Since:
- 5.0
-
runHookIfPresent
public ProcessResult runHookIfPresent(Repository repository, String hookName, String[] args) throws JGitInternalException
Checks whether the given hook is defined for the given repository, then runs it with the given arguments.The hook's standard output and error streams will be redirected to
System.outandSystem.errrespectively. The hook will have no stdin.- Parameters:
repository- The repository for which a hook should be run.hookName- The name of the hook to be executed.args- Arguments to pass to this hook. Cannot benull, but can be an empty array.- Returns:
- The ProcessResult describing this hook's execution.
- Throws:
JGitInternalException- if we fail to run the hook somehow. Causes may include an interrupted process or I/O errors.- Since:
- 4.0
-
runHookIfPresent
public ProcessResult runHookIfPresent(Repository repository, String hookName, String[] args, OutputStream outRedirect, OutputStream errRedirect, String stdinArgs) throws JGitInternalException
Checks whether the given hook is defined for the given repository, then runs it with the given arguments.- Parameters:
repository- The repository for which a hook should be run.hookName- The name of the hook to be executed.args- Arguments to pass to this hook. Cannot benull, but can be an empty array.outRedirect- A print stream on which to redirect the hook's stdout. Can benull, in which case the hook's standard output will be lost.errRedirect- A print stream on which to redirect the hook's stderr. Can benull, in which case the hook's standard error will be lost.stdinArgs- A string to pass on to the standard input of the hook. May benull.- Returns:
- The ProcessResult describing this hook's execution.
- Throws:
JGitInternalException- if we fail to run the hook somehow. Causes may include an interrupted process or I/O errors.- Since:
- 5.11
-
internalRunHookIfPresent
protected ProcessResult internalRunHookIfPresent(Repository repository, String hookName, String[] args, OutputStream outRedirect, OutputStream errRedirect, String stdinArgs) throws JGitInternalException
SeerunHookIfPresent(Repository, String, String[], OutputStream, OutputStream, String). Should only be called by FS supporting shell scripts execution.- Parameters:
repository- The repository for which a hook should be run.hookName- The name of the hook to be executed.args- Arguments to pass to this hook. Cannot benull, but can be an empty array.outRedirect- A print stream on which to redirect the hook's stdout. Can benull, in which case the hook's standard output will be lost.errRedirect- A print stream on which to redirect the hook's stderr. Can benull, in which case the hook's standard error will be lost.stdinArgs- A string to pass on to the standard input of the hook. May benull.- Returns:
- The ProcessResult describing this hook's execution.
- Throws:
JGitInternalException- if we fail to run the hook somehow. Causes may include an interrupted process or I/O errors.- Since:
- 5.11
-
findHook
public File findHook(Repository repository, String hookName)
Tries to find a hook matching the given one in the given repository.- Parameters:
repository- The repository within which to find a hook.hookName- The name of the hook we're trying to find.- Returns:
- The
Filecontaining this particular hook if it exists in the given repository,nullotherwise. - Since:
- 4.0
-
runProcess
public int runProcess(ProcessBuilder processBuilder, OutputStream outRedirect, OutputStream errRedirect, String stdinArgs) throws IOException, InterruptedException
Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.- Parameters:
processBuilder- The process builder configured for this process.outRedirect- A OutputStream on which to redirect the processes stdout. Can benull, in which case the processes standard output will be lost.errRedirect- A OutputStream on which to redirect the processes stderr. Can benull, in which case the processes standard error will be lost.stdinArgs- A string to pass on to the standard input of the hook. Can benull.- Returns:
- the exit value of this process.
- Throws:
IOException- if an I/O error occurs while executing this process.InterruptedException- if the current thread is interrupted while waiting for the process to end.- Since:
- 4.2
-
runProcess
public int runProcess(ProcessBuilder processBuilder, OutputStream outRedirect, OutputStream errRedirect, InputStream inRedirect) throws IOException, InterruptedException
Runs the given process until termination, clearing its stdout and stderr streams on-the-fly.- Parameters:
processBuilder- The process builder configured for this process.outRedirect- An OutputStream on which to redirect the processes stdout. Can benull, in which case the processes standard output will be lost.errRedirect- An OutputStream on which to redirect the processes stderr. Can benull, in which case the processes standard error will be lost.inRedirect- An InputStream from which to redirect the processes stdin. Can benull, in which case the process doesn't get any data over stdin. It is assumed that the whole InputStream will be consumed by the process. The method will close the inputstream after all bytes are read.- Returns:
- the return code of this process.
- Throws:
IOException- if an I/O error occurs while executing this process.InterruptedException- if the current thread is interrupted while waiting for the process to end.- Since:
- 4.2
-
runInShell
public abstract ProcessBuilder runInShell(String cmd, String[] args)
Initialize a ProcessBuilder to run a command using the system shell.- Parameters:
cmd- command to execute. This string should originate from the end-user, and thus is platform specific.args- arguments to pass to command. These should be protected from shell evaluation.- Returns:
- a partially completed process builder. Caller should finish populating directory, environment, and then start the process.
-
execute
public FS.ExecutionResult execute(ProcessBuilder pb, InputStream in) throws IOException, InterruptedException
Execute a command defined by aProcessBuilder.- Parameters:
pb- The command to be executedin- The standard input stream passed to the process- Returns:
- The result of the executed command
- Throws:
InterruptedExceptionIOException- Since:
- 4.2
-
getAttributes
public FS.Attributes getAttributes(File path)
Get the file attributes we care for.- Parameters:
path- aFileobject.- Returns:
- the file attributes we care for.
- Since:
- 3.3
-
normalize
public File normalize(File file)
Normalize the unicode path to composed form.- Parameters:
file- aFileobject.- Returns:
- NFC-format File
- Since:
- 3.3
-
-