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.
042   * @param conf the configuration to be used
043   * @param fs the filesystem to be used
044   * @param home the home directory
045   */
046  public abstract void initialize(Configuration conf, FileSystem fs, Path home);
047
048  /**
049   * Used to setup the trash policy. Must be implemented by all TrashPolicy
050   * implementations. Different from initialize(conf, fs, home), this one does
051   * not assume trash always under /user/$USER due to HDFS encryption zone.
052   * @param conf the configuration to be used
053   * @param fs the filesystem to be used
054   * @throws IOException
055   */
056  public void initialize(Configuration conf, FileSystem fs) throws IOException{
057    throw new UnsupportedOperationException();
058  }
059
060  /**
061   * Returns whether the Trash Policy is enabled for this filesystem.
062   */
063  public abstract boolean isEnabled();
064
065  /** 
066   * Move a file or directory to the current trash directory.
067   * @return false if the item is already in the trash or trash is disabled
068   */ 
069  public abstract boolean moveToTrash(Path path) throws IOException;
070
071  /** 
072   * Create a trash checkpoint. 
073   */
074  public abstract void createCheckpoint() throws IOException;
075
076  /** 
077   * Delete old trash checkpoint(s).
078   */
079  public abstract void deleteCheckpoint() throws IOException;
080
081  /**
082   * Get the current working directory of the Trash Policy
083   * This API does not work with files deleted from encryption zone when HDFS
084   * data encryption at rest feature is enabled as rename file between
085   * encryption zones or encryption zone and non-encryption zone is not allowed.
086   *
087   * The caller is recommend to use the new API
088   * TrashPolicy#getCurrentTrashDir(Path path).
089   * It returns the trash location correctly for the path specified no matter
090   * the path is in encryption zone or not.
091   */
092  public abstract Path getCurrentTrashDir();
093
094  /**
095   * Get the current trash directory for path specified based on the Trash
096   * Policy
097   * @param path path to be deleted
098   * @return current trash directory for the path to be deleted
099   * @throws IOException
100   */
101  public Path getCurrentTrashDir(Path path) throws IOException {
102    throw new UnsupportedOperationException();
103  }
104
105  /** 
106   * Return a {@link Runnable} that periodically empties the trash of all
107   * users, intended to be run by the superuser.
108   */
109  public abstract Runnable getEmptier() throws IOException;
110
111  /**
112   * Get an instance of the configured TrashPolicy based on the value
113   * of the configuration parameter fs.trash.classname.
114   *
115   * @param conf the configuration to be used
116   * @param fs the file system to be used
117   * @param home the home directory
118   * @return an instance of TrashPolicy
119   */
120  public static TrashPolicy getInstance(Configuration conf, FileSystem fs, Path home) {
121    Class<? extends TrashPolicy> trashClass = conf.getClass(
122        "fs.trash.classname", TrashPolicyDefault.class, TrashPolicy.class);
123    TrashPolicy trash = ReflectionUtils.newInstance(trashClass, conf);
124    trash.initialize(conf, fs, home); // initialize TrashPolicy
125    return trash;
126  }
127
128  /**
129   * Get an instance of the configured TrashPolicy based on the value
130   * of the configuration parameter fs.trash.classname.
131   *
132   * @param conf the configuration to be used
133   * @param fs the file system to be used
134   * @return an instance of TrashPolicy
135   */
136  public static TrashPolicy getInstance(Configuration conf, FileSystem fs)
137      throws IOException {
138    Class<? extends TrashPolicy> trashClass = conf.getClass(
139        "fs.trash.classname", TrashPolicyDefault.class, TrashPolicy.class);
140    TrashPolicy trash = ReflectionUtils.newInstance(trashClass, conf);
141    trash.initialize(conf, fs); // initialize TrashPolicy
142    return trash;
143  }
144}