Package com.atlassian.bamboo.util
Class BambooFileUtils
- java.lang.Object
-
- com.atlassian.bamboo.util.BambooFileUtils
-
public class BambooFileUtils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static Supplier<File>
JAVA_IO_SHORT_TMPDIR
The value ofSystemUtils.JAVA_IO_TMPDIR
.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static 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.since 5.10 try usingBambooPathUtils.cleanDirectory(Path)
) insteadstatic boolean
contains(File file, Pattern pattern)
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.static Supplier<File>
createDirectorySupplier(File directory)
static Supplier<File>
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
static @NotNull Iterable<File>
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
isDirectoryOrLinkToDirectory(File file)
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)
static Supplier<File>
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
renameTo(File from, File to)
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)
static @NotNull List<String>
splitPathToComponents(@NotNull String path)
-
-
-
Method Detail
-
isDirectoryImportant
public static boolean isDirectoryImportant(@Nullable @Nullable File directory)
Checks a path against a defined list of directories to see if it is important (and should not be deleted)- Returns:
- important
-
isDirectoryOrLinkToDirectory
public static boolean isDirectoryOrLinkToDirectory(File file)
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
public static File createTempDirectory(@NotNull @NotNull Object client) throws IOException
- 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
public static File createTempDirectory(@NotNull @NotNull String prefix) throws IOException
- 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 public static boolean setWritableByOwnerOnly(File file)
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 public static boolean setReadableByOwnerOnly(File file)
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
public static boolean setExecutableByOwnerOnly(File file)
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
@NotNull public static @NotNull List<String> splitPathToComponents(@NotNull @NotNull String path)
-
pathFromComponents
@NotNull public static @NotNull String pathFromComponents(@NotNull @NotNull String... pathComponents)
-
contains
public static boolean contains(File file, Pattern pattern) throws IOException
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:
contains(File, Pattern)
-
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
public static void renameTo(File from, File to) throws IOException
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
public static void moveDirectory(File srcDir, File destDir) throws IOException
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
-
getSharedTemporaryFile
@Deprecated public static File getSharedTemporaryFile(@NotNull @NotNull String content, @NotNull @NotNull String prefix, @Nullable @Nullable String suffix, boolean executable, @Nullable @Nullable File directory) throws IOException
Deprecated.Returns a file with given prefix, suffix and content in the specified directory. If the file already exists and was not created using this method, it's first deleted, otherwise an existing handle will be returned and no I/O will take place. Do not cache the returned reference or file name for a long time: the file will exist for 5 minutes after the call to this method is made. Repeated calls to this method with the same parameters reset the remaining file lifetime to 5 minutes.- Parameters:
content
- the content that the file should haveprefix
- the file name prefixsuffix
- the file name suffixexecutable
- whether or not make the file executabledirectory
- the directory in which to create file (temp directory unless specified otherwise)- Throws:
IOException
-
getSharedTemporaryFile
@Deprecated public static File getSharedTemporaryFile(@NotNull SharedTemporaryFiles.SharedTemporaryFileSpec temporaryFileSpec) throws IOException
Deprecated.Returns a file with given prefix, suffix and content in the specified directory. If the file already exists and was not created using this method, it's first deleted, otherwise an existing handle will be returned and no I/O will take place. Do not cache the returned reference or file name for a long time: the file will exist for 5 minutes after the call to this method is made. Repeated calls to this method with the same parameters reset the remaining file lifetime to 5 minutes.- Parameters:
temporaryFileSpec
- the directory in which to create file (temp directory unless specified otherwise)- Returns:
- a file with given prefix, suffix and content in the specified directory
- Throws:
IOException
-
persistentFileSupplier
public static Supplier<File> persistentFileSupplier(Supplier<File> fileSupplier)
Supplies Files. If the file supplied in the previous call exists, it's returned. If it no longer exists, it's recreated.
-
createDirectorySupplier
public static Supplier<File> createDirectorySupplier(Supplier<File> delegate)
Supplies directory. Makes sure that directory exists.
-
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
public static void createFileWithSize(@NotNull @NotNull File file, long size) throws IOException
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
public static void createDirectoryIfNotExists(File directory) throws IllegalStateException
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 public static void deleteDirectory(@NotNull @NotNull File directory) throws IOException
Deprecated.since 5.10 useBambooPathUtils.deleteDirectory(Path)
- Throws:
IOException
- See Also:
FileUtils.deleteDirectory(File)
-
cleanDirectory
@Deprecated public static void cleanDirectory(@NotNull @NotNull File directory) throws IOException
Deprecated.since 5.10 try usingBambooPathUtils.cleanDirectory(Path)
) instead- Throws:
IOException
- See Also:
FileUtils.cleanDirectory(File)
-
deleteQuietly
@Deprecated public static boolean deleteQuietly(@Nullable @Nullable File fileOrDir)
Deprecated.since 5.10 useBambooPathUtils.deleteQuietly(Path)
instead- See Also:
FileUtils.deleteQuietly(File)
-
forceDelete
@Deprecated public static void forceDelete(@NotNull @NotNull File file) throws IOException
Deprecated.since 5.10 useinstead
- Throws:
IOException
- See Also:
FileUtils.forceDelete(File)
-
-