HADOOP-15633. fs.TrashPolicyDefault: Can't create trash directory. Contributed by Fei Hui.

This commit is contained in:
John Zhuge 2018-08-27 09:22:59 -07:00
parent 6eecd251d8
commit f9c6fd9471
No known key found for this signature in database
GPG Key ID: 30289F8D94F5F0DE
2 changed files with 68 additions and 0 deletions

View File

@ -148,6 +148,20 @@ public boolean moveToTrash(Path path) throws IOException {
LOG.warn("Can't create(mkdir) trash directory: " + baseTrashPath);
return false;
}
} catch (FileAlreadyExistsException e) {
// find the path which is not a directory, and modify baseTrashPath
// & trashPath, then mkdirs
Path existsFilePath = baseTrashPath;
while (!fs.exists(existsFilePath)) {
existsFilePath = existsFilePath.getParent();
}
baseTrashPath = new Path(baseTrashPath.toString().replace(
existsFilePath.toString(), existsFilePath.toString() + Time.now())
);
trashPath = new Path(baseTrashPath, trashPath.getName());
// retry, ignore current failure
--i;
continue;
} catch (IOException e) {
LOG.warn("Can't create trash directory: " + baseTrashPath, e);
cause = e;

View File

@ -517,6 +517,60 @@ public void testTrash() throws IOException {
trashShell(FileSystem.getLocal(conf), TEST_DIR);
}
@Test
public void testExistingFileTrash() throws IOException {
Configuration conf = new Configuration();
conf.setClass("fs.file.impl", TestLFS.class, FileSystem.class);
FileSystem fs = FileSystem.getLocal(conf);
conf.set("fs.defaultFS", fs.getUri().toString());
conf.setLong(FS_TRASH_INTERVAL_KEY, 0); // disabled
assertFalse(new Trash(conf).isEnabled());
conf.setLong(FS_TRASH_INTERVAL_KEY, 10); // 10 minute
assertTrue(new Trash(conf).isEnabled());
FsShell shell = new FsShell();
shell.setConf(conf);
// First create a new directory with mkdirs
Path myPath = new Path(TEST_DIR, "test/mkdirs");
mkdir(fs, myPath);
// Second, create a file in that directory.
Path myFile = new Path(TEST_DIR, "test/mkdirs/myExistingFile");
writeFile(fs, myFile, 10);
// First rm a file
mkdir(fs, myPath);
writeFile(fs, myFile, 10);
String[] args1 = new String[2];
args1[0] = "-rm";
args1[1] = myFile.toString();
int val1 = -1;
try {
val1 = shell.run(args1);
} catch (Exception e) {
System.err.println("Exception raised from Trash.run " +
e.getLocalizedMessage());
}
assertTrue(val1 == 0);
// Second rm a file which parent path is the same as above
mkdir(fs, myFile);
writeFile(fs, new Path(myFile, "mySubFile"), 10);
String[] args2 = new String[2];
args2[0] = "-rm";
args2[1] = new Path(myFile, "mySubFile").toString();
int val2 = -1;
try {
val2 = shell.run(args2);
} catch (Exception e) {
System.err.println("Exception raised from Trash.run " +
e.getLocalizedMessage());
}
assertTrue(val2 == 0);
}
@Test
public void testNonDefaultFS() throws IOException {
Configuration conf = new Configuration();