/** * Calls deletePath() on each directory in the given list of directories to delete. * If moveToTrash is set, it will be moved to trash according to the file system trash policy. */ public static void deleteDirectories(FileSystem fs, List<String> directoriesToDelete, boolean recursive, boolean moveToTrash) throws IOException { for (String directory : directoriesToDelete) { if (moveToTrash) { moveToTrash(fs, new Path(directory)); } else { deletePath(fs, new Path(directory), recursive); } } }
/** * A wrapper around {@link FileSystem#rename(Path, Path)} which throws {@link IOException} if * {@link FileSystem#rename(Path, Path)} returns False. */ public static void renamePath(FileSystem fs, Path oldName, Path newName, boolean overwrite) throws IOException { if (!fs.exists(oldName)) { throw new FileNotFoundException(String.format("Failed to rename %s to %s: src not found", oldName, newName)); } if (fs.exists(newName)) { if (overwrite) { HadoopUtils.moveToTrash(fs, newName); } else { throw new FileAlreadyExistsException( String.format("Failed to rename %s to %s: dst already exists", oldName, newName)); } } if (!fs.rename(oldName, newName)) { throw new IOException(String.format("Failed to rename %s to %s", oldName, newName)); } }
/** * Calls deletePath() on each directory in the given list of directories to delete. * If moveToTrash is set, it will be moved to trash according to the file system trash policy. */ public static void deleteDirectories(FileSystem fs, List<String> directoriesToDelete, boolean recursive, boolean moveToTrash) throws IOException { for (String directory : directoriesToDelete) { if (moveToTrash) { moveToTrash(fs, new Path(directory)); } else { deletePath(fs, new Path(directory), recursive); } } }
/** * A wrapper around {@link FileSystem#rename(Path, Path)} which throws {@link IOException} if * {@link FileSystem#rename(Path, Path)} returns False. */ public static void renamePath(FileSystem fs, Path oldName, Path newName, boolean overwrite) throws IOException { if (!fs.exists(oldName)) { throw new FileNotFoundException(String.format("Failed to rename %s to %s: src not found", oldName, newName)); } if (fs.exists(newName)) { if (overwrite) { HadoopUtils.moveToTrash(fs, newName); } else { throw new FileAlreadyExistsException( String.format("Failed to rename %s to %s: dst already exists", oldName, newName)); } } if (!fs.rename(oldName, newName)) { throw new IOException(String.format("Failed to rename %s to %s", oldName, newName)); } }