diff options
| author | Martin Ritchie <ritchiem@apache.org> | 2008-10-03 09:20:45 +0000 |
|---|---|---|
| committer | Martin Ritchie <ritchiem@apache.org> | 2008-10-03 09:20:45 +0000 |
| commit | b7454a239ac156d93ee5dbb909f8a2e2a0f0347a (patch) | |
| tree | 9d98a67f22e926502da4ce61dd0944ff0fff3bd0 /java/common/src/main | |
| parent | 5e98bae5a6207aa32ed2602f5fe5a2c3f3b5266f (diff) | |
| download | qpid-python-b7454a239ac156d93ee5dbb909f8a2e2a0f0347a.tar.gz | |
QPID-1268 : Added additional deleteDirectory method that behaves like rmdir, modified delete to behave like rm. Added further tests for copy and delete. Encorporated review feedback from ASkinner.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@701329 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common/src/main')
| -rw-r--r-- | java/common/src/main/java/org/apache/qpid/util/FileUtils.java | 100 |
1 files changed, 88 insertions, 12 deletions
diff --git a/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/java/common/src/main/java/org/apache/qpid/util/FileUtils.java index 814d6e73c9..883373ba35 100644 --- a/java/common/src/main/java/org/apache/qpid/util/FileUtils.java +++ b/java/common/src/main/java/org/apache/qpid/util/FileUtils.java @@ -196,31 +196,107 @@ public class FileUtils /* * Deletes a given file */ - public static void deleteFile(String filePath) throws IOException + public static boolean deleteFile(String filePath) { - delete(new File(filePath), false); + return delete(new File(filePath), false); + } + + /* + * Deletes a given empty directory + */ + public static boolean deleteDirectory(String directoryPath) + { + File directory = new File(directoryPath); + + if (directory.isDirectory()) + { + if (directory.listFiles().length == 0) + { + return delete(directory, true); + } + } + + return false; } /** - * Delete a given file, if a directory is specified and recursive set then delete the whole tree - * @param filePath the File object to start at + * Delete a given file/directory, + * A directory will always require the recursive flag to be set. + * if a directory is specified and recursive set then delete the whole tree + * @param file the File object to start at * @param recursive boolean to recurse if a directory is specified. - * @throws IOException + * @return <code>true</code> if and only if the file or directory is + * successfully deleted; <code>false</code> otherwise */ - public static void delete(File filePath, boolean recursive) throws IOException + public static boolean delete(File file, boolean recursive) { - if (filePath.isDirectory()) + boolean success=true; + + if (file.isDirectory()) { if (recursive) { - for (File subFile : filePath.listFiles()) - { - delete(subFile, true); + for (File subFile : file.listFiles()) + { + success = delete(subFile, true) & success ; } + + return file.delete(); } + + return false; } - filePath.delete(); + return success && file.delete(); } - + + + public static class UnableToCopyException extends Exception + { + UnableToCopyException(String msg) + { + super(msg); + } + } + + public static void copyRecursive(File source, File dst) throws FileNotFoundException, UnableToCopyException + { + + if (!source.exists()) + { + throw new FileNotFoundException("Unable to copy '" + source.toString() + "' as it does not exist."); + } + + if (dst.exists() && !dst.isDirectory()) + { + throw new IllegalArgumentException("Unable to copy '" + source.toString() + "' to '" + dst + "' a file with same name exists."); + } + + + if (source.isFile()) + { + copy(source, dst); + } + + //else we have a source directory + if (!dst.isDirectory() && !dst.mkdir()) + { + throw new UnableToCopyException("Unable to create destination directory"); + } + + + for (File file : source.listFiles()) + { + if (file.isFile()) + { + copy(file, new File(dst.toString() + File.separator + file.getName())); + } + else + { + copyRecursive(file, new File(dst + File.separator + file.getName())); + } + } + + + } } |
