private boolean moveToTrash(PathData item) throws IOException { boolean success = false; if (!skipTrash) { try { success = Trash.moveToAppropriateTrash(item.fs, item.path, getConf()); } catch(FileNotFoundException fnfe) { throw fnfe; } catch (IOException ioe) { String msg = ioe.getMessage(); if (ioe.getCause() != null) { msg += ": " + ioe.getCause().getMessage(); } throw new IOException(msg + ". Consider using -skipTrash option", ioe); } } return success; }
private void startTrashEmptier(final Configuration conf) throws IOException { long trashInterval = conf.getLong(FS_TRASH_INTERVAL_KEY, FS_TRASH_INTERVAL_DEFAULT); if (trashInterval == 0) { return; } else if (trashInterval < 0) { throw new IOException("Cannot start trash emptier with negative interval." + " Set " + FS_TRASH_INTERVAL_KEY + " to a positive value."); } // This may be called from the transitionToActive code path, in which // case the current user is the administrator, not the NN. The trash // emptier needs to run as the NN. See HDFS-3972. FileSystem fs = SecurityUtil.doAsLoginUser( new PrivilegedExceptionAction<FileSystem>() { @Override public FileSystem run() throws IOException { return FileSystem.get(conf); } }); this.emptier = new Thread(new Trash(fs, conf).getEmptier(), "Trash Emptier"); this.emptier.setDaemon(true); this.emptier.start(); }
/** * Delete the specified directory, using the trash as available. * * @param conf configuration object * @param path path to delete * * @throws IOException if there's an error deleting the directory. */ public static void deleteDirectory(Configuration conf, Path path) throws IOException { Trash trash = new Trash(path.getFileSystem(conf), conf); try { if (!trash.isEnabled()) { LOG.debug("Trash is not enabled for " + path + " so deleting instead"); FileSystem fs = path.getFileSystem(conf); fs.delete(path, true); } else { boolean removed = trash.moveToTrash(path); if (removed) { LOG.debug("Moved to trash: " + path); } else { LOG.error("Item already in trash: " + path); } } } catch (FileNotFoundException e) { LOG.debug("Attempting to delete non-existent directory " + path); return; } }
public static void trashNonDefaultFS(Configuration conf) throws IOException { conf.set("fs.trash.interval", "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); } } } }
private void startTrashEmptier(final Configuration conf) throws IOException { long trashInterval = conf.getLong(FS_TRASH_INTERVAL_KEY, FS_TRASH_INTERVAL_DEFAULT); if (trashInterval == 0) { return; } else if (trashInterval < 0) { throw new IOException( "Cannot start tresh emptier with negative interval." + " Set " + FS_TRASH_INTERVAL_KEY + " to a positive value."); } // This may be called from the transitionToActive code path, in which // case the current user is the administrator, not the NN. The trash // emptier needs to run as the NN. See HDFS-3972. FileSystem fs = SecurityUtil.doAsLoginUser(new PrivilegedExceptionAction<FileSystem>() { @Override public FileSystem run() throws IOException { return FileSystem.get(conf); } }); this.emptier = new Thread(new Trash(fs, conf).getEmptier(), "Trash Emptier"); this.emptier.setDaemon(true); this.emptier.start(); }
@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 protected void processArguments(LinkedList<PathData> args) throws IOException { Trash trash = new Trash(getConf()); trash.expunge(); trash.checkpoint(); }
public static void moveToTrash(Configuration conf, Path path) throws IOException { Trash t = new Trash(conf); boolean isMoved = t.moveToTrash(path); t.expunge(); if (!isMoved) { logger.error("Trash is not enabled or file is already in the trash."); } }
@Test public void testPluggableTrash() throws IOException { Configuration conf = new Configuration(); // Test plugged TrashPolicy conf.setClass("fs.trash.classname", TestTrashPolicy.class, TrashPolicy.class); Trash trash = new Trash(conf); assertTrue(trash.getTrashPolicy().getClass().equals(TestTrashPolicy.class)); }
private void startTrashEmptier(Configuration conf) throws IOException { if (conf.getInt("fs.trash.interval", 0) == 0) { return; } FileSystem fs = NameNode.getTrashFileSystem(conf); this.trash = new Trash(fs, conf); this.emptier = new Thread(trash.getEmptier(), "Trash Emptier"); this.emptier.setDaemon(true); this.emptier.start(); }
private boolean moveToTrash(PathData item) throws IOException { boolean success = false; if (!skipTrash) { try { success = Trash.moveToAppropriateTrash(item.fs, item.path, getConf()); } catch(FileNotFoundException fnfe) { throw fnfe; } catch (IOException ioe) { throw new IOException(ioe.getMessage() + ". Consider using -skipTrash option", ioe); } } return success; }
public boolean trash(final Path path) { return run(new Retryable<Boolean>() { @Override public Boolean call() throws Exception { return Trash.moveToAppropriateTrash(fs, path, conf); } }); }