/** * Moves the object to the filesystem trash according to the file system policy. * @param fs FileSystem object * @param path Path to the object to be moved to trash. * @throws IOException */ public static void moveToTrash(FileSystem fs, Path path) throws IOException { Trash trash = new Trash(fs, new Configuration()); trash.moveToTrash(path); } /**
return trash.moveToTrash(fullyResolvedPath);
@Test public void testMoveToTrash() throws IOException { Path hadoopUtilsTestDir = new Path(Files.createTempDir().getAbsolutePath(), "HadoopUtilsTestDir"); Configuration conf = new Configuration(); // Set the time to keep it in trash to 10 minutes. // 0 means object will be deleted instantly. conf.set("fs.trash.interval", "10"); FileSystem fs = FileSystem.getLocal(conf); Trash trash = new Trash(fs, conf); TrashPolicy trashPolicy = TrashPolicy.getInstance(conf, fs, fs.getHomeDirectory()); Path trashPath = trashPolicy.getCurrentTrashDir(); fs.mkdirs(hadoopUtilsTestDir); Assert.assertTrue(fs.exists(hadoopUtilsTestDir)); trash.moveToTrash(hadoopUtilsTestDir.getParent()); Assert.assertFalse(fs.exists(hadoopUtilsTestDir)); Assert.assertTrue(fs.exists(trashPath)); } }
@Override public boolean moveToTrash(Path path) throws IOException { FileSystem fs = getVolumeByPath(path).getFileSystem(); Trash trash = new Trash(fs, fs.getConf()); return trash.moveToTrash(path); }
private boolean moveToTrash(Path path) throws IOException { if (trash == null) return false; try { return trash.moveToTrash(path); } catch (FileNotFoundException ex) { return false; } }
/** * Moves the object to the filesystem trash according to the file system policy. * @param fs FileSystem object * @param path Path to the object to be moved to trash. * @throws IOException */ public static void moveToTrash(FileSystem fs, Path path) throws IOException { Trash trash = new Trash(fs, new Configuration()); trash.moveToTrash(path); } /**
public void trash() throws IOException { Trash trash = new Trash(fs, fs.getConf()); trash.moveToTrash(this); }
@Override public boolean moveToAppropriateTrash(FileSystem fs, Path path, Configuration conf) throws IOException { // older versions of Hadoop don't have a Trash constructor based on the // Path or FileSystem. So need to achieve this by creating a dummy conf. // this needs to be filtered out based on version Configuration dupConf = new Configuration(conf); FileSystem.setDefaultUri(dupConf, fs.getUri()); Trash trash = new Trash(dupConf); return trash.moveToTrash(path); } @Override
@Override public boolean moveToAppropriateTrash(FileSystem fs, Path path, Configuration conf) throws IOException { // older versions of Hadoop don't have a Trash constructor based on the // Path or FileSystem. So need to achieve this by creating a dummy conf. // this needs to be filtered out based on version Configuration dupConf = new Configuration(conf); FileSystem.setDefaultUri(dupConf, fs.getUri()); Trash trash = new Trash(dupConf); return trash.moveToTrash(path); } @Override
@Override public boolean moveToAppropriateTrash(FileSystem fs, Path path, Configuration conf) throws IOException { // older versions of Hadoop don't have a Trash constructor based on the // Path or FileSystem. So need to achieve this by creating a dummy conf. // this needs to be filtered out based on version Configuration dupConf = new Configuration(conf); FileSystem.setDefaultUri(dupConf, fs.getUri()); Trash trash = new Trash(dupConf); return trash.moveToTrash(path); }
@Override public boolean moveToAppropriateTrash(FileSystem fs, Path path, Configuration conf) throws IOException { // older versions of Hadoop don't have a Trash constructor based on the // Path or FileSystem. So need to achieve this by creating a dummy conf. // this needs to be filtered out based on version Configuration dupConf = new Configuration(conf); FileSystem.setDefaultUri(dupConf, fs.getUri()); Trash trash = new Trash(dupConf); return trash.moveToTrash(path); }
@Override public boolean moveToAppropriateTrash(FileSystem fs, Path path, Configuration conf) throws IOException { // older versions of Hadoop don't have a Trash constructor based on the // Path or FileSystem. So need to achieve this by creating a dummy conf. // this needs to be filtered out based on version Configuration dupConf = new Configuration(conf); FileSystem.setDefaultUri(dupConf, fs.getUri()); Trash trash = new Trash(dupConf); return trash.moveToTrash(path); }
@Override public FileSystem run() throws Exception { Trash trash = new Trash(fs.getConf()); if (!trash.moveToTrash(p)) { throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS005", "Could not move path [{0}] to trash on delete", p); } return null; } });
@Override public FileSystem run() throws Exception { Trash trash = new Trash(fs.getConf()); if (!trash.moveToTrash(p)) { throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FS005", "Could not move path [{0}] to trash on delete", p); } return null; } });
private void delete(Path src, FileSystem srcFs, boolean recursive) throws IOException { if (srcFs.isDirectory(src) && !recursive) { throw new IOException("Cannot remove directory \"" + src + "\", use -rmr instead"); } Trash trashTmp = new Trash(srcFs, getConf()); if (trashTmp.moveToTrash(src)) { System.out.println("Moved to trash: " + src); return; } if (srcFs.delete(src, true)) { System.out.println("Deleted " + src); } else { if (!srcFs.exists(src)) { throw new FileNotFoundException("cannot remove " + src + ": No such file or directory."); } throw new IOException("Delete failed " + src); } }
@Override public boolean moveToTrash(Path path) throws IOException { FileSystem fs = getVolumeByPath(path).getFileSystem(); Trash trash = new Trash(fs, fs.getConf()); return trash.moveToTrash(path); }
public static void trashNonDefaultFS(Configuration conf) throws IOException { conf.set(FS_TRASH_INTERVAL_KEY, "10"); // 10 minute // attempt non-default FileSystem trash { final FileSystem lfs = FileSystem.getLocal(conf); Path p = TEST_DIR; Path f = new Path(p, "foo/bar"); if (lfs.exists(p)) { lfs.delete(p, true); } try { f = writeFile(lfs, f); FileSystem.closeAll(); FileSystem localFs = FileSystem.get(URI.create("file:///"), conf); Trash lTrash = new Trash(localFs, conf); lTrash.moveToTrash(f.getParent()); checkTrash(localFs, lTrash.getCurrentTrashDir(), f); } finally { if (lfs.exists(p)) { lfs.delete(p, true); } } } }
public void rm(boolean recursive, boolean skipTrash, String... uris) { for (String uri : uris) { try { Path src = new Path(uri); FileSystem srcFs = getFS(src); for (Path p : FileUtil.stat2Paths(srcFs.globStatus(src), src)) { FileStatus status = srcFs.getFileStatus(p); if (status.isDir() && !recursive) { throw new IllegalStateException("Cannot remove directory \"" + src + "\", if recursive deletion was not specified"); } if (!skipTrash) { try { Trash trashTmp = new Trash(srcFs, configuration); trashTmp.moveToTrash(p); } catch (IOException ex) { throw new HadoopException("Cannot move to Trash resource " + p, ex); } } srcFs.delete(p, recursive); } } catch (IOException ex) { throw new HadoopException("Cannot delete (all) resources " + ex.getMessage(), ex); } } }
public static void trashNonDefaultFS(Configuration conf) throws IOException { conf.setLong(FS_TRASH_INTERVAL_KEY, 10); // 10 minute // attempt non-default FileSystem trash { final FileSystem lfs = FileSystem.getLocal(conf); Path p = TEST_DIR; Path f = new Path(p, "foo/bar"); if (lfs.exists(p)) { lfs.delete(p, true); } try { writeFile(lfs, f, 10); FileSystem.closeAll(); FileSystem localFs = FileSystem.get(URI.create("file:///"), conf); Trash lTrash = new Trash(localFs, conf); lTrash.moveToTrash(f.getParent()); checkTrash(localFs, lTrash.getCurrentTrashDir(), f); } finally { if (lfs.exists(p)) { lfs.delete(p, true); } } } }
public static void trashNonDefaultFS(Configuration conf) throws IOException { conf.setLong(FS_TRASH_INTERVAL_KEY, 10); // 10 minute // attempt non-default FileSystem trash { final FileSystem lfs = FileSystem.getLocal(conf); Path p = TEST_DIR; Path f = new Path(p, "foo/bar"); if (lfs.exists(p)) { lfs.delete(p, true); } try { writeFile(lfs, f, 10); FileSystem.closeAll(); FileSystem localFs = FileSystem.get(URI.create("file:///"), conf); Trash lTrash = new Trash(localFs, conf); lTrash.moveToTrash(f.getParent()); checkTrash(localFs, lTrash.getCurrentTrashDir(), f); } finally { if (lfs.exists(p)) { lfs.delete(p, true); } } } }