/** * 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); } /**
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; } }
/** * Returns the current trash location for the path specified * @param path to be deleted * @return path to the trash * @throws IOException */ public Path getCurrentTrashDir(Path path) throws IOException { return getTrash().getCurrentTrashDir(path); }
@Override protected void processArguments(LinkedList<PathData> args) throws IOException { FileSystem[] childFileSystems = FileSystem.get(getConf()).getChildFileSystems(); if (null != childFileSystems) { for (FileSystem fs : childFileSystems) { Trash trash = new Trash(fs, getConf()); trash.expunge(); trash.checkpoint(); } } else { Trash trash = new Trash(getConf()); trash.expunge(); trash.checkpoint(); } } }
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); } } } }
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(); }
protected Trash getTrash() throws IOException { if (this.trash == null) { this.trash = new Trash(getConf()); } return this.trash; }
/** * 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; } }
Trash trash = new Trash(fs, conf); Path trashRoot = trash.getCurrentTrashDir(); while(true) { trashRoot = trashRoot.getParent(); Trash userTrash = new Trash(fs, conf); assertTrue(userTrash.isEnabled()); userTrash.moveToTrash(user1Dir); fail("User2 should not be allowed to move" + "user1's dir to trash");
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)); }
@Override public boolean undelete(Path f, String userName) throws IOException { Trash trash = new Trash(this, getConf(), userName); return trash.moveFromTrash(f); } }
private boolean moveToTrash(Path path) throws IOException { if (trash == null) return false; try { return trash.moveToTrash(path); } catch (FileNotFoundException ex) { return false; } }
private void expunge() throws IOException { trash.expunge(); trash.checkpoint(); }
throw new IOException("Failed to get server trash configuration", e); Trash trash = new Trash(fullyResolvedFs, conf); boolean success = trash.moveToTrash(fullyResolvedPath); if (success) { System.out.println("Moved: '" + p + "' to trash at: " + trash.getCurrentTrashDir() );
/** Run an emptier.*/ public static void main(String[] args) throws Exception { new Trash(new Configuration()).getEmptier().run(); } }
@Override protected void processArguments(LinkedList<PathData> args) throws IOException { Trash trash = new Trash(getConf()); trash.expunge(); trash.checkpoint(); } }
GarbageCollectWriteAheadLogs(Instance instance, FileSystem fs, boolean noTrash) throws IOException { this.instance = instance; this.fs = fs; if (!noTrash) this.trash = new Trash(fs, fs.getConf()); }
/** * 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; } }