From 575c39e8336bbc2803599414935f1ecf413c4633 Mon Sep 17 00:00:00 2001 From: Martin Ritchie Date: Fri, 3 Oct 2008 09:20:45 +0000 Subject: 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@701329 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/qpid/util/FileUtils.java | 100 ++++++++++++++++++--- 1 file changed, 88 insertions(+), 12 deletions(-) (limited to 'qpid/java/common/src/main') diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java index 814d6e73c9..883373ba35 100644 --- a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java +++ b/qpid/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 true if and only if the file or directory is + * successfully deleted; false 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())); + } + } + + + } } -- cgit v1.2.1