fix for deleting an invalid file

If the file for delete is invalid (typically a wrong filename) an IOException will be thrown.
This commit is contained in:
Tomas Tulka 2017-05-08 09:32:56 +02:00 committed by Andrew Gaul
parent 15d27da739
commit 6452960c72
2 changed files with 22 additions and 3 deletions

View File

@ -27,6 +27,7 @@ import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.InvalidPathException;
import java.nio.file.attribute.AclEntry; import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission; import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclEntryType; import java.nio.file.attribute.AclEntryType;
@ -77,10 +78,17 @@ public class Utils {
} }
public static void delete(File file) throws IOException { public static void delete(File file) throws IOException {
Path path;
try {
path = file.toPath();
} catch (InvalidPathException ipe) {
throw new IOException("Invalid file: " + file, ipe);
}
for (int n = 0; n < 10; n++) { for (int n = 0; n < 10; n++) {
try { try {
Files.delete(file.toPath()); Files.delete(path);
if (Files.exists(file.toPath())) { if (Files.exists(path)) {
Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS); Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
continue; continue;
} }
@ -98,7 +106,7 @@ public class Utils {
} }
} }
// File could not be deleted multiple times. It is very likely locked in another process // File could not be deleted multiple times. It is very likely locked in another process
throw new IOException("Could not delete: " + file.toPath()); throw new IOException("Could not delete: " + path);
} }
/** /**

View File

@ -29,6 +29,7 @@ import static org.testng.Assert.fail;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.InvalidPathException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -685,6 +686,16 @@ public class FilesystemStorageStrategyImplTest {
} }
} }
@Test
public void testDeletingInvalidPathFileEndsNormally() {
String invalidPathBlobKey = "A<!:!@#$%^&*?]8 /\0";
try {
storageStrategy.removeBlob(CONTAINER_NAME, invalidPathBlobKey);
} catch (InvalidPathException ipe) {
fail("Deleting an invalid path ended with an InvalidPathException.", ipe);
}
}
// ---------------------------------------------------------- Private methods // ---------------------------------------------------------- Private methods
/** /**