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 */
018package org.apache.hadoop.fs;
019
020import org.apache.hadoop.classification.InterfaceAudience;
021import org.apache.hadoop.classification.InterfaceStability;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.conf.Configured;
024import org.apache.hadoop.util.ReflectionUtils;
025
026import java.io.IOException;
027
028/** 
029 * This interface is used for implementing different Trash policies.
030 * Provides factory method to create instances of the configured Trash policy.
031 */
032@InterfaceAudience.Public
033@InterfaceStability.Evolving
034public abstract class TrashPolicy extends Configured {
035  protected FileSystem fs; // the FileSystem
036  protected Path trash; // path to trash directory
037  protected long deletionInterval; // deletion interval for Emptier
038
039  /**
040   * Used to setup the trash policy. Must be implemented by all TrashPolicy
041   * implementations. Different from initialize(conf, fs, home), this one does
042   * not assume trash always under /user/$USER due to HDFS encryption zone.
043   * @param conf the configuration to be used
044   * @param fs the filesystem to be used
045   * @throws IOException
046   */
047  public void initialize(Configuration conf, FileSystem fs) throws IOException{
048    throw new UnsupportedOperationException();
049  }
050
051  /**
052   * Returns whether the Trash Policy is enabled for this filesystem.
053   */
054  public abstract boolean isEnabled();
055
056  /** 
057   * Move a file or directory to the current trash directory.
058   * @return false if the item is already in the trash or trash is disabled
059   */ 
060  public abstract boolean moveToTrash(Path path) throws IOException;
061
062  /** 
063   * Create a trash checkpoint. 
064   */
065  public abstract void createCheckpoint() throws IOException;
066
067  /** 
068   * Delete old trash checkpoint(s).
069   */
070  public abstract void deleteCheckpoint() throws IOException;
071
072  /**
073   * Get the current working directory of the Trash Policy
074   * This API does not work with files deleted from encryption zone when HDFS
075   * data encryption at rest feature is enabled as rename file between
076   * encryption zones or encryption zone and non-encryption zone is not allowed.
077   *
078   * The caller is recommend to use the new API
079   * TrashPolicy#getCurrentTrashDir(Path path).
080   * It returns the trash location correctly for the path specified no matter
081   * the path is in encryption zone or not.
082   */
083  public abstract Path getCurrentTrashDir();
084
085  /**
086   * Get the current trash directory for path specified based on the Trash
087   * Policy
088   * @param path path to be deleted
089   * @return current trash directory for the path to be deleted
090   * @throws IOException
091   */
092  public Path getCurrentTrashDir(Path path) throws IOException {
093    throw new UnsupportedOperationException();
094  }
095
096  /** 
097   * Return a {@link Runnable} that periodically empties the trash of all
098   * users, intended to be run by the superuser.
099   */
100  public abstract Runnable getEmptier() throws IOException;
101
102  /**
103   * Get an instance of the configured TrashPolicy based on the value
104   * of the configuration parameter fs.trash.classname.
105   *
106   * @param conf the configuration to be used
107   * @param fs the file system to be used
108   * @return an instance of TrashPolicy
109   */
110  public static TrashPolicy getInstance(Configuration conf, FileSystem fs)
111      throws IOException {
112    Class<? extends TrashPolicy> trashClass = conf.getClass(
113        "fs.trash.classname", TrashPolicyDefault.class, TrashPolicy.class);
114    TrashPolicy trash = ReflectionUtils.newInstance(trashClass, conf);
115    trash.initialize(conf, fs); // initialize TrashPolicy
116    return trash;
117  }
118}