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 org.apache.hadoop.classification.InterfaceAudience;
021 import org.apache.hadoop.classification.InterfaceStability;
022 import org.apache.hadoop.conf.Configuration;
023 import org.apache.hadoop.conf.Configured;
024 import org.apache.hadoop.util.ReflectionUtils;
025
026 import 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
034 public 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 * Returns whether the Trash Policy is enabled for this filesystem
050 */
051 public abstract boolean isEnabled();
052
053 /**
054 * Move a file or directory to the current trash directory.
055 * @return false if the item is already in the trash or trash is disabled
056 */
057 public abstract boolean moveToTrash(Path path) throws IOException;
058
059 /**
060 * Create a trash checkpoint.
061 */
062 public abstract void createCheckpoint() throws IOException;
063
064 /**
065 * Delete old trash checkpoint(s).
066 */
067 public abstract void deleteCheckpoint() throws IOException;
068
069 /**
070 * Get the current working directory of the Trash Policy
071 */
072 public abstract Path getCurrentTrashDir();
073
074 /**
075 * Return a {@link Runnable} that periodically empties the trash of all
076 * users, intended to be run by the superuser.
077 */
078 public abstract Runnable getEmptier() throws IOException;
079
080 /**
081 * Get an instance of the configured TrashPolicy based on the value
082 * of the configuration parameter fs.trash.classname.
083 *
084 * @param conf the configuration to be used
085 * @param fs the file system to be used
086 * @param home the home directory
087 * @return an instance of TrashPolicy
088 */
089 public static TrashPolicy getInstance(Configuration conf, FileSystem fs, Path home) {
090 Class<? extends TrashPolicy> trashClass = conf.getClass(
091 "fs.trash.classname", TrashPolicyDefault.class, TrashPolicy.class);
092 TrashPolicy trash = ReflectionUtils.newInstance(trashClass, conf);
093 trash.initialize(conf, fs, home); // initialize TrashPolicy
094 return trash;
095 }
096 }