Package com.atlassian.bamboo.util
Class BambooFileUtils
java.lang.Object
com.atlassian.bamboo.util.BambooFileUtils
-
Field Summary
Modifier and TypeFieldDescriptionThe value ofSystemUtils.JAVA_IO_TMPDIR
. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
calculateRelativePath
(@NotNull File directory, @NotNull File file) Calculates a relative path to a file from the given directory.static void
cleanDirectory
(@NotNull File directory) Deprecated.static boolean
This method checks whether given pattern occurs in a file.static boolean
containsRegex
(@NotNull File file, @NotNull String regexToMatch) Convenience method.static void
createDirectoryIfNotExists
(File directory) Create directory if not present.createDirectorySupplier
(File directory) createDirectorySupplier
(Supplier<File> delegate) Supplies directory.static void
createFileWithSize
(@NotNull File file, long size) Creates a file of exact size (in bytes).static File
createSafeTempFile
(@NotNull String name, @Nullable File dir) Deprecated.since 5.4 useSecureTemporaryFiles
classstatic File
createSafeTempFile
(@NotNull String prefix, @Nullable String suffix) Deprecated.since 5.4 useSecureTemporaryFiles
classstatic File
createSafeTempFile
(@NotNull String prefix, @Nullable String suffix, @Nullable File dir) Deprecated.since 5.4 useSecureTemporaryFiles
classstatic File
createTempDirectory
(@NotNull Object client) static File
createTempDirectory
(@NotNull String prefix) static File
createTempDirectory
(@Nullable File parentDir, @NotNull String prefix) Deprecated.static void
deleteDirectory
(@NotNull File directory) Deprecated.since 5.10 useBambooPathUtils.deleteDirectory(Path)
static boolean
deleteQuietly
(@Nullable File fileOrDir) Deprecated.since 5.10 useBambooPathUtils.deleteQuietly(Path)
insteadstatic void
forceDelete
(@NotNull File file) Deprecated.since 5.10 useinstead
getFilesFromFileSet
(@NotNull org.apache.tools.ant.types.FileSet fileSet) static File
getSharedTemporaryFile
(@NotNull String content, @NotNull String prefix, @Nullable String suffix, boolean executable, @Nullable File directory) Deprecated.static File
getSharedTemporaryFile
(SharedTemporaryFiles.SharedTemporaryFileSpec temporaryFileSpec) Deprecated.static @NotNull File
getSingleFileFromDirectory
(@NotNull File directory) Obtain a single file from a directory.static boolean
isContainsDirectoryTraversalString
(@Nullable String name) Deprecated.since 5.10 useBambooFilenameUtils.containsDirectoryTraversalString(String)
static boolean
isDirectoryImportant
(@Nullable File directory) Checks a path against a defined list of directories to see if it is important (and should not be deleted)static boolean
Checks whether aFile
is a directory or a link to a directory.static void
moveDirectory
(File srcDir, File destDir) Moves directory but unlikeFileUtils.moveDirectory(File, File)
doesn't throw if srcDir fails to be deleted.static void
moveDirectoryContentToDirectory
(@NotNull File src, @NotNull File destDir, boolean createDestDir) Moves directory content to destination directory.static File
newFileUnderParent
(@NotNull File parent, @NotNull String child) Deprecated.since 5.12 useBambooFiles.resolvePathUnderParent(Path, String)
insteadstatic @NotNull String
pathFromComponents
(@NotNull String... pathComponents) persistentFileSupplier
(Supplier<File> fileSupplier) Supplies Files.static org.apache.commons.io.filefilter.IOFileFilter
regexPathFilter
(@NotNull String pathPattern) Returns anIOFileFilter
that performs a full path match against the supplied pattern.static String
relativizePath
(@NotNull File rootDirectory, @NotNull File workingDirectory, @Nullable String path) Given parent and child directories, and a path relative to parent directory, return path relative to the child directory.static void
Renames a file or directory.static @NotNull File
safeWriteStringToFile
(@NotNull String string, @NotNull String prefix, @Nullable String suffix, @Nullable File directory) Writes a string to a newly created temporary file in as safe manner as possible.static boolean
setExecutableByOwnerOnly
(File file) Sets the file permissions so that only the owner can execute a filestatic boolean
setReadableByOwnerOnly
(File file) Deprecated.since 6.4 useBambooFiles.setAccessibleOnlyByOwner(Path)
static boolean
setWritableByOwnerOnly
(File file) Deprecated.since 6.4 useBambooFiles.setAccessibleOnlyByOwner(Path)
splitPathToComponents
(@NotNull String path)
-
Field Details
-
JAVA_IO_SHORT_TMPDIR
The value ofSystemUtils.JAVA_IO_TMPDIR
. On Windows, it will be shortened to 8.3 format.
-
-
Method Details
-
isDirectoryImportant
Checks a path against a defined list of directories to see if it is important (and should not be deleted)- Returns:
- important
-
isDirectoryOrLinkToDirectory
Checks whether aFile
is a directory or a link to a directory.- Parameters:
file
- File expected to be a directory.- Returns:
- true if the file is a directory or a link to a directory, false otherwise.
-
createTempDirectory
- Throws:
IOException
-
createTempDirectory
@Deprecated public static File createTempDirectory(@Nullable @Nullable File parentDir, @NotNull @NotNull String prefix) throws IOException Deprecated.Create a temporary directory in specified location.- Parameters:
parentDir
- null indicates default tmp directoryprefix
- filename prefix- Returns:
- temporary directory in specified location
- Throws:
IOException
-
createTempDirectory
- Throws:
IOException
-
createSafeTempFile
@Deprecated public static File createSafeTempFile(@NotNull @NotNull String name, @Nullable @Nullable File dir) throws IOException Deprecated.since 5.4 useSecureTemporaryFiles
class- Throws:
IOException
-
createSafeTempFile
@Deprecated public static File createSafeTempFile(@NotNull @NotNull String prefix, @Nullable @Nullable String suffix) throws IOException Deprecated.since 5.4 useSecureTemporaryFiles
classseeFile.createTempFile(java.lang.String, java.lang.String, java.io.File)
andcreateSafeTempFile(String, File)
- Throws:
IOException
-
createSafeTempFile
@Deprecated public static File createSafeTempFile(@NotNull @NotNull String prefix, @Nullable @Nullable String suffix, @Nullable @Nullable File dir) throws IOException Deprecated.since 5.4 useSecureTemporaryFiles
classseeFile.createTempFile(java.lang.String, java.lang.String, java.io.File)
andcreateSafeTempFile(String, java.io.File)
- Throws:
IOException
-
setWritableByOwnerOnly
Deprecated.since 6.4 useBambooFiles.setAccessibleOnlyByOwner(Path)
Sets the file permissions so that only the owner can write to a file- Returns:
- if permission change was successful
-
setReadableByOwnerOnly
Deprecated.since 6.4 useBambooFiles.setAccessibleOnlyByOwner(Path)
Sets the file permissions so that only the owner can read from a file- Returns:
- if permission change was successful, always false on Windows
-
setExecutableByOwnerOnly
Sets the file permissions so that only the owner can execute a file- Returns:
- if permission change was successful
-
safeWriteStringToFile
@NotNull public static @NotNull File safeWriteStringToFile(@NotNull @NotNull String string, @NotNull @NotNull String prefix, @Nullable @Nullable String suffix, @Nullable @Nullable File directory) throws IOException Writes a string to a newly created temporary file in as safe manner as possible. On Windows, the file content can still be hijacked by a member of the same group as the user creating the file. The fact that this method exists does not mean that you should use it, unless absolutely necessary.- Parameters:
string
- the string to write- Returns:
- the created file, should be deleted after use
- Throws:
IOException
-
splitPathToComponents
-
pathFromComponents
-
contains
This method checks whether given pattern occurs in a file. Empty pattern is always found. File is assumed to be UTF8. Malformed, non-utf character sequences will be ignored.- Parameters:
file
- the file to checkpattern
- the pattern to search for- Returns:
- true if pattern is found
- Throws:
IOException
- in case of file access problems
-
containsRegex
public static boolean containsRegex(@NotNull @NotNull File file, @NotNull @NotNull String regexToMatch) throws IOException Convenience method.- Throws:
IOException
- See Also:
-
relativizePath
public static String relativizePath(@NotNull @NotNull File rootDirectory, @NotNull @NotNull File workingDirectory, @Nullable @Nullable String path) Given parent and child directories, and a path relative to parent directory, return path relative to the child directory.- Parameters:
rootDirectory
- root directoryworkingDirectory
- working directory (must be inside root directory)path
- path relative to root directory- Returns:
- path relative to working directory
-
calculateRelativePath
public static String calculateRelativePath(@NotNull @NotNull File directory, @NotNull @NotNull File file) Calculates a relative path to a file from the given directory. The file must exist inside the directory, otherwise the absolute path to the file will be returned.Example:
directory: /home/user/ file: /home/user/documents/credits.txt ... relative path: documents/credits.txt
- Parameters:
directory
- the directory from which the path should be calculatedfile
- the file to which the path should be calculated; can be a directory too- Returns:
- relative path to the file
-
renameTo
Renames a file or directory. Supports cases when a rename would cause the source file to be moved between volumes regardless of support in the underlying platform.- Parameters:
from
- the source file/directoryto
- the new location of source file- Throws:
IOException
- if the destination file/directory already exists or move operation fails for other reason
-
moveDirectory
Moves directory but unlikeFileUtils.moveDirectory(File, File)
doesn't throw if srcDir fails to be deleted.- Throws:
IOException
-
moveDirectoryContentToDirectory
public static void moveDirectoryContentToDirectory(@NotNull @NotNull File src, @NotNull @NotNull File destDir, boolean createDestDir) throws IOException Moves directory content to destination directory.- Parameters:
src
- - the directory which content should be moveddestDir
- - the destination directorycreateDestDir
- - If true create the destination directory, otherwise if false throw an IOException- Throws:
NullPointerException
- - if source or destination is nullorg.apache.commons.io.FileExistsException
- - if the directory or file exists in the destination directoryIOException
- - if source or destination is invalidIOException
- - if an IO error occurs moving the file
-
persistentFileSupplier
Supplies Files. If the file supplied in the previous call exists, it's returned. If it no longer exists, it's recreated. -
createDirectorySupplier
Supplies directory. Makes sure that directory exists. -
createDirectorySupplier
-
regexPathFilter
public static org.apache.commons.io.filefilter.IOFileFilter regexPathFilter(@NotNull @NotNull String pathPattern) Returns anIOFileFilter
that performs a full path match against the supplied pattern. -
newFileUnderParent
@Deprecated public static File newFileUnderParent(@NotNull @NotNull File parent, @NotNull @NotNull String child) throws IOException Deprecated.since 5.12 useBambooFiles.resolvePathUnderParent(Path, String)
insteadFulfills the general contract ofFile(java.io.File, String)
. The returned file is always in canonical format and always rooted under the parent argument. Attempts to traverse out of the parent directory will result in IllegalArgumentException being thrown.- Parameters:
parent
- parent directorychild
- child under parent directory. Relative paths are allowed as long as the result is under the parent directory- Returns:
- a canonical child File rooted under parent directory
- Throws:
IOException
- if a problem occurred during directory tree access
-
isContainsDirectoryTraversalString
@Deprecated public static boolean isContainsDirectoryTraversalString(@Nullable @Nullable String name) Deprecated.since 5.10 useBambooFilenameUtils.containsDirectoryTraversalString(String)
Basic validation of a String to see if it contains directory traversal chars. Prefer usingBambooFiles.resolvePathUnderParent(Path, String)
but can use when that is not practical -
createFileWithSize
Creates a file of exact size (in bytes).The file may exist prior to calling this method - in such case it's content will be overwritten.
The size has to be a non-negative value. The parent directory of the file must exist.
- Parameters:
file
- file to createsize
- size of the file (in bytes)- Throws:
IOException
-
createDirectoryIfNotExists
Create directory if not present.- Parameters:
directory
- to be created- Throws:
IllegalStateException
- if directory can't be created
-
getFilesFromFileSet
@NotNull public static @NotNull Iterable<File> getFilesFromFileSet(@NotNull @NotNull org.apache.tools.ant.types.FileSet fileSet) Returns an
Iterable
ofFile
s converted from Apache'sFileSet
ofFileResource
s.The resulting iterable relies on the
iterator
of the FileSet, which means that it's lazy loaded.- Parameters:
fileSet
- a file set- Returns:
- a lazy loaded iterable of files from the given file set
-
getSingleFileFromDirectory
@NotNull public static @NotNull File getSingleFileFromDirectory(@NotNull @NotNull File directory) throws IOException Obtain a single file from a directory. The directory has to exist and must contain exactly one file.- Parameters:
directory
- a directory- Returns:
- a single file within the directory, which can be a directory as well
- Throws:
IOException
- if any filesystem operation failsIllegalArgumentException
- if the argument is invalid, e.g. the directory doesn't exist, the file is not a directory, the directory contains more than one file or no files
-
deleteDirectory
Deprecated.since 5.10 useBambooPathUtils.deleteDirectory(Path)
- Throws:
IOException
- See Also:
-
FileUtils.deleteDirectory(File)
-
cleanDirectory
Deprecated.since 5.10 try usingBambooPathUtils.cleanDirectory(Path)
) instead- Throws:
IOException
- See Also:
-
FileUtils.cleanDirectory(File)
-
deleteQuietly
Deprecated.since 5.10 useBambooPathUtils.deleteQuietly(Path)
instead- See Also:
-
FileUtils.deleteQuietly(File)
-
forceDelete
Deprecated.since 5.10 useinstead
- Throws:
IOException
- See Also:
-
FileUtils.forceDelete(File)
-
BambooPathUtils.cleanDirectory(Path)
) instead