com.atlassian.bamboo.util
Class BambooFileUtils

java.lang.Object
  extended by com.atlassian.bamboo.util.BambooFileUtils

public class BambooFileUtils
extends java.lang.Object


Method Summary
static boolean contains(java.io.File file, java.util.regex.Pattern pattern)
          This method checks whether given pattern occurs in a file.
static boolean containsRegex(java.io.File file, java.lang.String regexToMatch)
          Convenience method.
static java.io.File createSafeTempFile(java.lang.String name, java.io.File dir)
          Creates a file with a given name in specified directory.
static java.io.File createTempDirectory(java.io.File parentDir, java.lang.String prefix)
          Create a temporary directory in specified location.
static java.io.File createTempDirectory(java.lang.Object client)
           
static java.io.File createTempDirectory(java.lang.String prefix)
           
static java.io.File getSharedTemporaryFile(java.lang.String content, java.lang.String prefix, java.lang.String suffix, boolean executable, java.io.File directory)
          Returns a created temporary file.
static boolean isDirectoryImportant(java.io.File planSourceDirectory)
          Checks a path against a defined list of directories to see if it is important (and should not be deleted)
static void moveDirectoryContentToDirectory(java.io.File src, java.io.File destDir, boolean createDestDir)
          Moves directory content to destination directory.
protected static java.io.File openSafeTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory)
          Creates a temporary file readable and writable only by the owner (if supported by the platform).
static java.lang.String pathFromComponents(java.lang.String... pathComponents)
           
static java.lang.String relativizePath(java.io.File rootDirectory, java.io.File workingDirectory, java.lang.String path)
          Given parent and child directories, and a path relative to parent directory, return path relative to the child directory.
static void renameTo(java.io.File from, java.io.File to)
          Renames a file or directory.
static java.io.File safeWriteStringToFile(java.lang.String string, java.lang.String prefix, java.lang.String suffix, java.io.File directory)
          Writes a string to a newly created temporary file in as safe manner as possible.
static boolean setReadableByOwnerOnly(java.io.File file)
          Sets the file permissions so that only the owner can read from a file
static boolean setWritableByOwnerOnly(java.io.File file)
          Sets the file permissions so that only the owner can write to a file
static java.util.List<java.lang.String> splitPathToComponents(java.lang.String path)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isDirectoryImportant

public static boolean isDirectoryImportant(java.io.File planSourceDirectory)
Checks a path against a defined list of directories to see if it is important (and should not be deleted)

Parameters:
planSourceDirectory -
Returns:
important

createTempDirectory

public static java.io.File createTempDirectory(@NotNull
                                               java.lang.Object client)
                                        throws java.io.IOException
Throws:
java.io.IOException

createTempDirectory

public static java.io.File createTempDirectory(@Nullable
                                               java.io.File parentDir,
                                               @NotNull
                                               java.lang.String prefix)
                                        throws java.io.IOException
Create a temporary directory in specified location.

Parameters:
parentDir - null indicates default tmp directory
prefix - filename prefix
Returns:
Throws:
java.io.IOException

createTempDirectory

public static java.io.File createTempDirectory(@NotNull
                                               java.lang.String prefix)
                                        throws java.io.IOException
Throws:
java.io.IOException

openSafeTempFile

@NotNull
protected static java.io.File openSafeTempFile(@NotNull
                                                       java.lang.String prefix,
                                                       @Nullable
                                                       java.lang.String suffix,
                                                       @Nullable
                                                       java.io.File directory)
                                        throws java.io.IOException
Creates a temporary file readable and writable only by the owner (if supported by the platform). Note that depending on FS permissions set on directory and general OS limitations, the file may be substituted with another file for malicious reasons after its creation.

Throws:
java.io.IOException
See Also:
File.createTempFile()

createSafeTempFile

public static java.io.File createSafeTempFile(@NotNull
                                              java.lang.String name,
                                              @Nullable
                                              java.io.File dir)
                                       throws java.io.IOException
Creates a file with a given name in specified directory. If the directory is not specified, temporary directory is used instead. If a file with the same name already exists, it is first deleted. If deletion is unsuccessful, an exception is thrown. If the file cannot be created, an exception is thrown. The file is created as readable and writable only by the owner with deleteOnExit flag enabled.

Parameters:
name - the name of file to create
dir - the directory in which to create the file
Returns:
created file
Throws:
java.io.IOException - if the file could not be created

setWritableByOwnerOnly

public static boolean setWritableByOwnerOnly(java.io.File file)
Sets the file permissions so that only the owner can write to a file

Parameters:
file -
Returns:
if permission change was successful

setReadableByOwnerOnly

public static boolean setReadableByOwnerOnly(java.io.File file)
Sets the file permissions so that only the owner can read from a file

Parameters:
file -
Returns:
if permission change was successful, always false on Windows

safeWriteStringToFile

@NotNull
public static java.io.File safeWriteStringToFile(@NotNull
                                                         java.lang.String string,
                                                         @NotNull
                                                         java.lang.String prefix,
                                                         @Nullable
                                                         java.lang.String suffix,
                                                         @Nullable
                                                         java.io.File directory)
                                          throws java.io.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:
java.io.IOException
See Also:
File.createTempFile()

splitPathToComponents

@NotNull
public static java.util.List<java.lang.String> splitPathToComponents(@NotNull
                                                                             java.lang.String path)

pathFromComponents

@NotNull
public static java.lang.String pathFromComponents(@NotNull
                                                          java.lang.String... pathComponents)

contains

public static boolean contains(java.io.File file,
                               java.util.regex.Pattern pattern)
                        throws java.io.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 check
pattern - the pattern to search for
Returns:
true if pattern is found
Throws:
java.io.IOException - in case of file access problems

containsRegex

public static boolean containsRegex(@NotNull
                                    java.io.File file,
                                    @NotNull
                                    java.lang.String regexToMatch)
                             throws java.io.IOException
Convenience method.

Throws:
java.io.IOException
See Also:
contains(File, Pattern)

relativizePath

public static java.lang.String relativizePath(@NotNull
                                              java.io.File rootDirectory,
                                              @NotNull
                                              java.io.File workingDirectory,
                                              @Nullable
                                              java.lang.String path)
Given parent and child directories, and a path relative to parent directory, return path relative to the child directory.

Parameters:
rootDirectory - root directory
workingDirectory - working directory (must be inside root directory)
path - path relative to root directory
Returns:
path relative to working directory

renameTo

public static void renameTo(java.io.File from,
                            java.io.File to)
                     throws java.io.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/directory
to - the new location of source file
Throws:
java.io.IOException - if the destination file/directory already exists or move operation fails for other reason

moveDirectoryContentToDirectory

public static void moveDirectoryContentToDirectory(@NotNull
                                                   java.io.File src,
                                                   @NotNull
                                                   java.io.File destDir,
                                                   boolean createDestDir)
                                            throws java.io.IOException
Moves directory content to destination directory.

Parameters:
src - - the directory which content should be moved
destDir - - the destination directory
createDestDir - - If true create the destination directory, otherwise if false throw an IOException
Throws:
java.lang.NullPointerException - - if source or destination is null
FileExistsException - - if the directory or file exists in the destination directory
java.io.IOException - - if source or destination is invalid
java.io.IOException - - if an IO error occurs moving the file

getSharedTemporaryFile

public static java.io.File getSharedTemporaryFile(@NotNull
                                                  java.lang.String content,
                                                  @NotNull
                                                  java.lang.String prefix,
                                                  @Nullable
                                                  java.lang.String suffix,
                                                  boolean executable,
                                                  @Nullable
                                                  java.io.File directory)
                                           throws java.io.IOException
Returns a created temporary file.

Parameters:
content - file content
prefix - file name prefix
suffix - file name suffix
executable - whether the file should have executable bit set
directory - directory in which the file should be created
Returns:
Throws:
java.io.IOException


Copyright © 2012 Atlassian. All Rights Reserved.