Package com.atlassian.bamboo.util
Class BambooFileUtils
java.lang.Object
com.atlassian.bamboo.util.BambooFileUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe value ofSystemUtils.JAVA_IO_TMPDIR. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcalculateRelativePath(@NotNull File directory, @NotNull File file) Calculates a relative path to a file from the given directory.static voidcleanDirectory(@NotNull File directory) Deprecated.static booleanThis method checks whether given pattern occurs in a file.static booleancontainsRegex(@NotNull File file, @NotNull String regexToMatch) Convenience method.static voidcreateDirectoryIfNotExists(File directory) Create directory if not present.createDirectorySupplier(File directory) createDirectorySupplier(Supplier<File> delegate) Supplies directory.static voidcreateFileWithSize(@NotNull File file, long size) Creates a file of exact size (in bytes).static FilecreateSafeTempFile(@NotNull String name, @Nullable File dir) Deprecated.since 5.4 useSecureTemporaryFilesclassstatic FilecreateSafeTempFile(@NotNull String prefix, @Nullable String suffix) Deprecated.since 5.4 useSecureTemporaryFilesclassstatic FilecreateSafeTempFile(@NotNull String prefix, @Nullable String suffix, @Nullable File dir) Deprecated.since 5.4 useSecureTemporaryFilesclassstatic FilecreateTempDirectory(@Nullable File parentDir, @NotNull String prefix) Deprecated.static FilecreateTempDirectory(@NotNull Object client) static FilecreateTempDirectory(@NotNull String prefix) static voiddeleteDirectory(@NotNull File directory) Deprecated.since 5.10 useBambooPathUtils.deleteDirectory(Path)static booleandeleteQuietly(@Nullable File fileOrDir) Deprecated.since 5.10 useBambooPathUtils.deleteQuietly(Path)insteadstatic voidforceDelete(@NotNull File file) Deprecated.since 5.10 useinsteadgetFilesFromFileSet(@NotNull org.apache.tools.ant.types.FileSet fileSet) static FilegetSharedTemporaryFile(SharedTemporaryFiles.SharedTemporaryFileSpec temporaryFileSpec) Deprecated.static FilegetSharedTemporaryFile(@NotNull String content, @NotNull String prefix, @Nullable String suffix, boolean executable, @Nullable File directory) Deprecated.static @NotNull FilegetSingleFileFromDirectory(@NotNull File directory) Obtain a single file from a directory.static booleanisContainsDirectoryTraversalString(@Nullable String name) Deprecated.since 5.10 useBambooFilenameUtils.containsDirectoryTraversalString(String)static booleanisDirectoryImportant(@Nullable File directory) Checks a path against a defined list of directories to see if it is important (and should not be deleted)static booleanChecks whether aFileis a directory or a link to a directory.static voidmoveDirectory(File srcDir, File destDir) Moves directory but unlikeFileUtils.moveDirectory(File, File)doesn't throw if srcDir fails to be deleted.static voidmoveDirectoryContentToDirectory(@NotNull File src, @NotNull File destDir, boolean createDestDir) Moves directory content to destination directory.static FilenewFileUnderParent(@NotNull File parent, @NotNull String child) Deprecated, for removal: This API element is subject to removal in a future version.since 5.12 useBambooFiles.resolvePathUnderParent(Path, String)insteadpersistentFileSupplier(Supplier<File> fileSupplier) Supplies Files.static org.apache.commons.io.filefilter.IOFileFilterregexPathFilter(@NotNull String pathPattern) Returns anIOFileFilterthat performs a full path match against the supplied pattern.static StringrelativizePath(@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 voidRenames a file or directory.static @NotNull FilesafeWriteStringToFile(@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 booleansetExecutableByOwnerOnly(File file) Sets the file permissions so that only the owner can execute a filesplitPathToComponents(@NotNull String path) Deprecated, for removal: This API element is subject to removal in a future version.since 12.0 without replacement
-
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 aFileis 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 useSecureTemporaryFilesclass- Throws:
IOException
-
createSafeTempFile
@Deprecated public static File createSafeTempFile(@NotNull @NotNull String prefix, @Nullable @Nullable String suffix) throws IOException Deprecated.since 5.4 useSecureTemporaryFilesclassseeFile.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 useSecureTemporaryFilesclassseeFile.createTempFile(java.lang.String, java.lang.String, java.io.File)andcreateSafeTempFile(String, java.io.File)- Throws:
IOException
-
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
@Deprecated(since="12.0", forRemoval=true) @NotNull public static @NotNull List<String> splitPathToComponents(@NotNull @NotNull String path) Deprecated, for removal: This API element is subject to removal in a future version.since 12.0 without replacement -
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 anIOFileFilterthat performs a full path match against the supplied pattern. -
newFileUnderParent
@Deprecated(since="5.12", forRemoval=true) public static File newFileUnderParent(@NotNull @NotNull File parent, @NotNull @NotNull String child) throws IOException Deprecated, for removal: This API element is subject to removal in a future version.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
IterableofFiles converted from Apache'sFileSetofFileResources.The resulting iterable relies on the
iteratorof 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:
-
cleanDirectory
Deprecated.since 5.10 try usingBambooPathUtils.cleanDirectory(Path)) instead- Throws:
IOException- See Also:
-
deleteQuietly
Deprecated.since 5.10 useBambooPathUtils.deleteQuietly(Path)instead- See Also:
-
forceDelete
Deprecated.since 5.10 useinstead- Throws:
IOException- See Also:
-
BambooPathUtils.cleanDirectory(Path)) instead