001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.fs;
019    
020    import java.io.IOException;
021    
022    import org.apache.hadoop.classification.InterfaceAudience;
023    import org.apache.hadoop.classification.InterfaceStability;
024    import org.apache.hadoop.conf.Configuration;
025    import org.apache.hadoop.conf.Configured;
026    
027    /** 
028     * Provides a trash facility which supports pluggable Trash policies. 
029     *
030     * See the implementation of the configured TrashPolicy for more
031     * details.
032     */
033    @InterfaceAudience.Public
034    @InterfaceStability.Stable
035    public class Trash extends Configured {
036      private TrashPolicy trashPolicy; // configured trash policy instance
037    
038      /** 
039       * Construct a trash can accessor.
040       * @param conf a Configuration
041       */
042      public Trash(Configuration conf) throws IOException {
043        this(FileSystem.get(conf), conf);
044      }
045    
046      /**
047       * Construct a trash can accessor for the FileSystem provided.
048       * @param fs the FileSystem
049       * @param conf a Configuration
050       */
051      public Trash(FileSystem fs, Configuration conf) throws IOException {
052        super(conf);
053        trashPolicy = TrashPolicy.getInstance(conf, fs, fs.getHomeDirectory());
054      }
055    
056      /**
057       * In case of the symlinks or mount points, one has to move the appropriate
058       * trashbin in the actual volume of the path p being deleted.
059       *
060       * Hence we get the file system of the fully-qualified resolved-path and
061       * then move the path p to the trashbin in that volume,
062       * @param fs - the filesystem of path p
063       * @param p - the  path being deleted - to be moved to trasg
064       * @param conf - configuration
065       * @return false if the item is already in the trash or trash is disabled
066       * @throws IOException on error
067       */
068      public static boolean moveToAppropriateTrash(FileSystem fs, Path p,
069          Configuration conf) throws IOException {
070        Path fullyResolvedPath = fs.resolvePath(p);
071        Trash trash = new Trash(FileSystem.get(fullyResolvedPath.toUri(), conf), conf);
072        boolean success =  trash.moveToTrash(fullyResolvedPath);
073        if (success) {
074          System.out.println("Moved: '" + p + "' to trash at: " +
075              trash.getCurrentTrashDir() );
076        }
077        return success;
078      }
079      
080      /**
081       * Returns whether the trash is enabled for this filesystem
082       */
083      public boolean isEnabled() {
084        return trashPolicy.isEnabled();
085      }
086    
087      /** Move a file or directory to the current trash directory.
088       * @return false if the item is already in the trash or trash is disabled
089       */ 
090      public boolean moveToTrash(Path path) throws IOException {
091        return trashPolicy.moveToTrash(path);
092      }
093    
094      /** Create a trash checkpoint. */
095      public void checkpoint() throws IOException {
096        trashPolicy.createCheckpoint();
097      }
098    
099      /** Delete old checkpoint(s). */
100      public void expunge() throws IOException {
101        trashPolicy.deleteCheckpoint();
102      }
103    
104      /** get the current working directory */
105      Path getCurrentTrashDir() {
106        return trashPolicy.getCurrentTrashDir();
107      }
108    
109      /** get the configured trash policy */
110      TrashPolicy getTrashPolicy() {
111        return trashPolicy;
112      }
113    
114      /** Return a {@link Runnable} that periodically empties the trash of all
115       * users, intended to be run by the superuser.
116       */
117      public Runnable getEmptier() throws IOException {
118        return trashPolicy.getEmptier();
119      }
120    
121      /** Run an emptier.*/
122      public static void main(String[] args) throws Exception {
123        new Trash(new Configuration()).getEmptier().run();
124      }
125    }