public class BambooFileUtils extends Object
Modifier and Type | Field and Description |
---|---|
static Supplier<File> |
JAVA_IO_SHORT_TMPDIR
The value of
SystemUtils.JAVA_IO_TMPDIR . |
Modifier and Type | Method and Description |
---|---|
static String |
calculateRelativePath(File directory,
File file)
Calculates a relative path to a file from the given directory.
|
static void |
cleanDirectory(File directory)
Deprecated.
since 5.10 try using
BambooPathUtils.cleanDirectory(Path) ) instead |
static boolean |
contains(File file,
Pattern pattern)
This method checks whether given pattern occurs in a file.
|
static boolean |
containsRegex(File file,
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(File file,
long size)
Creates a file of exact size (in bytes).
|
static File |
createSafeTempFile(String name,
File dir)
Deprecated.
since 5.4 use
SecureTemporaryFiles class |
static File |
createSafeTempFile(String prefix,
String suffix)
Deprecated.
since 5.4 use
SecureTemporaryFiles class |
static File |
createSafeTempFile(String prefix,
String suffix,
File dir)
Deprecated.
since 5.4 use
SecureTemporaryFiles class |
static File |
createTempDirectory(File parentDir,
String prefix)
Deprecated.
since 6.4 use
java.nio.file.Files.createTempDirectory() instead |
static File |
createTempDirectory(Object client) |
static File |
createTempDirectory(String prefix) |
static void |
deleteDirectory(File directory)
Deprecated.
since 5.10 use
BambooPathUtils.deleteDirectory(Path) |
static boolean |
deleteQuietly(File fileOrDir)
Deprecated.
since 5.10 use
BambooPathUtils.deleteQuietly(Path) instead |
static void |
forceDelete(File file)
Deprecated.
since 5.10 use
instead |
static Iterable<File> |
getFilesFromFileSet(org.apache.tools.ant.types.FileSet fileSet)
|
static File |
getSharedTemporaryFile(SharedTemporaryFiles.SharedTemporaryFileSpec temporaryFileSpec)
Deprecated.
|
static File |
getSharedTemporaryFile(String content,
String prefix,
String suffix,
boolean executable,
File directory)
Deprecated.
|
static File |
getSingleFileFromDirectory(File directory)
Obtain a single file from a directory.
|
static boolean |
isContainsDirectoryTraversalString(String name)
Deprecated.
since 5.10 use
BambooFilenameUtils.containsDirectoryTraversalString(String) |
static boolean |
isDirectoryImportant(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 a
File is a directory or a link to a directory. |
static void |
moveDirectoryContentToDirectory(File src,
File destDir,
boolean createDestDir)
Moves directory content to destination directory.
|
static File |
newFileUnderParent(File parent,
String child)
Deprecated.
since 5.12 use
BambooFiles.resolvePathUnderParent(Path, String) instead |
static String |
pathFromComponents(String... pathComponents) |
static Supplier<File> |
persistentFileSupplier(Supplier<File> fileSupplier)
Supplies Files.
|
static org.apache.commons.io.filefilter.IOFileFilter |
regexPathFilter(String pathPattern)
Returns an
IOFileFilter that performs a full path match against the supplied pattern. |
static String |
relativizePath(File rootDirectory,
File workingDirectory,
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 File |
safeWriteStringToFile(String string,
String prefix,
String suffix,
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 file
|
static boolean |
setReadableByOwnerOnly(File file)
Deprecated.
since 6.4 use
BambooFiles.setAccessibleOnlyByOwner(Path) |
static boolean |
setWritableByOwnerOnly(File file)
Deprecated.
since 6.4 use
BambooFiles.setAccessibleOnlyByOwner(Path) |
static List<String> |
splitPathToComponents(String path) |
public static boolean isDirectoryImportant(@Nullable File directory)
directory
- public static boolean isDirectoryOrLinkToDirectory(File file)
File
is a directory or a link to a directory.file
- File expected to be a directory.public static File createTempDirectory(@NotNull Object client) throws IOException
IOException
@Deprecated public static File createTempDirectory(@Nullable File parentDir, @NotNull String prefix) throws IOException
java.nio.file.Files.createTempDirectory()
insteadparentDir
- null indicates default tmp directoryprefix
- filename prefixIOException
public static File createTempDirectory(@NotNull String prefix) throws IOException
IOException
@Deprecated public static File createSafeTempFile(@NotNull String name, @Nullable File dir) throws IOException
SecureTemporaryFiles
classIOException
@Deprecated public static File createSafeTempFile(@NotNull String prefix, @Nullable String suffix) throws IOException
SecureTemporaryFiles
classFile.createTempFile(java.lang.String, java.lang.String, java.io.File)
and createSafeTempFile(String, File)
prefix
- suffix
- IOException
@Deprecated public static File createSafeTempFile(@NotNull String prefix, @Nullable String suffix, @Nullable File dir) throws IOException
SecureTemporaryFiles
classFile.createTempFile(java.lang.String, java.lang.String, java.io.File)
and createSafeTempFile(String, java.io.File)
prefix
- suffix
- IOException
@Deprecated public static boolean setWritableByOwnerOnly(File file)
BambooFiles.setAccessibleOnlyByOwner(Path)
file
- @Deprecated public static boolean setReadableByOwnerOnly(File file)
BambooFiles.setAccessibleOnlyByOwner(Path)
file
- public static boolean setExecutableByOwnerOnly(File file)
file
- @NotNull public static File safeWriteStringToFile(@NotNull String string, @NotNull String prefix, @Nullable String suffix, @Nullable File directory) throws IOException
string
- the string to writeIOException
File#createTempFile(String, String, File))
@NotNull public static List<String> splitPathToComponents(@NotNull String path)
@NotNull public static String pathFromComponents(@NotNull String... pathComponents)
public static boolean contains(File file, Pattern pattern) throws IOException
file
- the file to checkpattern
- the pattern to search forIOException
- in case of file access problemspublic static boolean containsRegex(@NotNull File file, @NotNull String regexToMatch) throws IOException
IOException
contains(File, Pattern)
public static String relativizePath(@NotNull File rootDirectory, @NotNull File workingDirectory, @Nullable String path)
rootDirectory
- root directoryworkingDirectory
- working directory (must be inside root directory)path
- path relative to root directorypublic static String calculateRelativePath(@NotNull File directory, @NotNull File file)
Example:
directory: /home/user/ file: /home/user/documents/credits.txt ... relative path: documents/credits.txt
directory
- the directory from which the path should be calculatedfile
- the file to which the path should be calculated; can be a directory toopublic static void renameTo(File from, File to) throws IOException
from
- the source file/directoryto
- the new location of source fileIOException
- if the destination file/directory already exists or move operation fails for other reasonpublic static void moveDirectoryContentToDirectory(@NotNull File src, @NotNull File destDir, boolean createDestDir) throws IOException
src
- - the directory which content should be moveddestDir
- - the destination directorycreateDestDir
- - If true create the destination directory, otherwise if false throw an IOExceptionNullPointerException
- - 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@Deprecated public static File getSharedTemporaryFile(@NotNull String content, @NotNull String prefix, @Nullable String suffix, boolean executable, @Nullable File directory) throws IOException
SharedTemporaryFiles.create(com.atlassian.bamboo.util.SharedTemporaryFiles.SharedTemporaryFileSpec)
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)IOException
@Deprecated public static File getSharedTemporaryFile(@NotNull SharedTemporaryFiles.SharedTemporaryFileSpec temporaryFileSpec) throws IOException
SharedTemporaryFiles.create(com.atlassian.bamboo.util.SharedTemporaryFiles.SharedTemporaryFileSpec)
temporaryFileSpec
- the directory in which to create file (temp directory unless specified otherwise)IOException
public static Supplier<File> persistentFileSupplier(Supplier<File> fileSupplier)
public static Supplier<File> createDirectorySupplier(Supplier<File> delegate)
public static org.apache.commons.io.filefilter.IOFileFilter regexPathFilter(@NotNull String pathPattern)
IOFileFilter
that performs a full path match against the supplied pattern.@Deprecated public static File newFileUnderParent(@NotNull File parent, @NotNull String child) throws IOException
BambooFiles.resolvePathUnderParent(Path, String)
insteadFile.File(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.parent
- parent directorychild
- child under parent directory. Relative paths are allowed as long as the result is under the parent directoryIOException
- if a problem occurred during directory tree access@Deprecated public static boolean isContainsDirectoryTraversalString(@Nullable String name)
BambooFilenameUtils.containsDirectoryTraversalString(String)
BambooPathUtils#resolvePathUnderParent(Path, String)
but can use when that is not practicalpublic static void createFileWithSize(@NotNull File file, long size) throws IOException
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.
file
- file to createsize
- size of the file (in bytes)IOException
public static void createDirectoryIfNotExists(File directory) throws IllegalStateException
directory
- to be createdIllegalStateException
- if directory can't be created@NotNull public static Iterable<File> getFilesFromFileSet(@NotNull org.apache.tools.ant.types.FileSet fileSet)
Returns an Iterable
of File
s converted from Apache's FileSet
of FileResource
s.
The resulting iterable relies on the iterator
of the FileSet, which means that it's
lazy loaded. Creating a new Collection from this Iterable might cause problems due to potential high memory
consumption.
fileSet
- a file set@NotNull public static File getSingleFileFromDirectory(@NotNull File directory) throws IOException
directory
- a directoryIOException
- 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@Deprecated public static void deleteDirectory(@NotNull File directory) throws IOException
BambooPathUtils.deleteDirectory(Path)
IOException
FileUtils.deleteDirectory(File)
@Deprecated public static void cleanDirectory(@NotNull File directory) throws IOException
BambooPathUtils.cleanDirectory(Path)
) insteadIOException
FileUtils.cleanDirectory(File)
@Deprecated public static boolean deleteQuietly(@Nullable File fileOrDir)
BambooPathUtils.deleteQuietly(Path)
insteadFileUtils.deleteQuietly(File)
@Deprecated public static void forceDelete(@NotNull File file) throws IOException
instead
IOException
FileUtils.forceDelete(File)
Copyright © 2019 Atlassian Software Systems Pty Ltd. All rights reserved.