HADOOP-6201. Change FileSystem::listStatus contract to throw

FileNotFoundException if the directory does not exist, rather than letting
this be implementation-specific. Contributed by Jakob Homan.


git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@806745 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christopher Douglas 2009-08-21 22:50:04 +00:00
parent 0a8e65c23b
commit 76a77aea78
11 changed files with 85 additions and 63 deletions

View File

@ -74,6 +74,10 @@ Trunk (unreleased changes)
MAPREDUCE-711. Removed Distributed Cache from Common, to move it MAPREDUCE-711. Removed Distributed Cache from Common, to move it
under Map/Reduce. (Vinod Kumar Vavilapalli via yhemanth) under Map/Reduce. (Vinod Kumar Vavilapalli via yhemanth)
HADOOP-6201. Change FileSystem::listStatus contract to throw
FileNotFoundException if the directory does not exist, rather than letting
this be implementation-specific. (Jakob Homan via cdouglas)
NEW FEATURES NEW FEATURES
HADOOP-4268. Change fsck to use ClientProtocol methods so that the HADOOP-4268. Change fsck to use ClientProtocol methods so that the

View File

@ -731,28 +731,28 @@ public boolean accept(Path file) {
* List the statuses of the files/directories in the given path if the path is * List the statuses of the files/directories in the given path if the path is
* a directory. * a directory.
* *
* @param f * @param f given path
* given path
* @return the statuses of the files/directories in the given patch * @return the statuses of the files/directories in the given patch
* @throws IOException * @throws FileNotFoundException when the path does not exist;
* IOException see specific implementation
*/ */
public abstract FileStatus[] listStatus(Path f) throws IOException; public abstract FileStatus[] listStatus(Path f) throws FileNotFoundException,
IOException;
/* /*
* Filter files/directories in the given path using the user-supplied path * Filter files/directories in the given path using the user-supplied path
* filter. Results are added to the given array <code>results</code>. * filter. Results are added to the given array <code>results</code>.
*/ */
private void listStatus(ArrayList<FileStatus> results, Path f, private void listStatus(ArrayList<FileStatus> results, Path f,
PathFilter filter) throws IOException { PathFilter filter) throws FileNotFoundException, IOException {
FileStatus listing[] = listStatus(f); FileStatus listing[] = listStatus(f);
if (listing != null) {
for (int i = 0; i < listing.length; i++) { for (int i = 0; i < listing.length; i++) {
if (filter.accept(listing[i].getPath())) { if (filter.accept(listing[i].getPath())) {
results.add(listing[i]); results.add(listing[i]);
} }
} }
} }
}
/** /**
* Filter files/directories in the given path using the user-supplied path * Filter files/directories in the given path using the user-supplied path
@ -764,10 +764,11 @@ private void listStatus(ArrayList<FileStatus> results, Path f,
* the user-supplied path filter * the user-supplied path filter
* @return an array of FileStatus objects for the files under the given path * @return an array of FileStatus objects for the files under the given path
* after applying the filter * after applying the filter
* @throws IOException * @throws FileNotFoundException when the path does not exist;
* if encounter any problem while fetching the status * IOException see specific implementation
*/ */
public FileStatus[] listStatus(Path f, PathFilter filter) throws IOException { public FileStatus[] listStatus(Path f, PathFilter filter)
throws FileNotFoundException, IOException {
ArrayList<FileStatus> results = new ArrayList<FileStatus>(); ArrayList<FileStatus> results = new ArrayList<FileStatus>();
listStatus(results, f, filter); listStatus(results, f, filter);
return results.toArray(new FileStatus[results.size()]); return results.toArray(new FileStatus[results.size()]);
@ -781,10 +782,11 @@ public FileStatus[] listStatus(Path f, PathFilter filter) throws IOException {
* a list of paths * a list of paths
* @return a list of statuses for the files under the given paths after * @return a list of statuses for the files under the given paths after
* applying the filter default Path filter * applying the filter default Path filter
* @exception IOException * @throws FileNotFoundException when the path does not exist;
* IOException see specific implementation
*/ */
public FileStatus[] listStatus(Path[] files) public FileStatus[] listStatus(Path[] files)
throws IOException { throws FileNotFoundException, IOException {
return listStatus(files, DEFAULT_FILTER); return listStatus(files, DEFAULT_FILTER);
} }
@ -798,10 +800,11 @@ public FileStatus[] listStatus(Path[] files)
* the user-supplied path filter * the user-supplied path filter
* @return a list of statuses for the files under the given paths after * @return a list of statuses for the files under the given paths after
* applying the filter * applying the filter
* @exception IOException * @throws FileNotFoundException when the path does not exist;
* IOException see specific implementation
*/ */
public FileStatus[] listStatus(Path[] files, PathFilter filter) public FileStatus[] listStatus(Path[] files, PathFilter filter)
throws IOException { throws FileNotFoundException, IOException {
ArrayList<FileStatus> results = new ArrayList<FileStatus>(); ArrayList<FileStatus> results = new ArrayList<FileStatus>();
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
listStatus(results, files[i], filter); listStatus(results, files[i], filter);

View File

@ -532,9 +532,11 @@ private void setReplication(short newRep, FileSystem srcFs,
return; return;
} }
FileStatus items[] = srcFs.listStatus(src); FileStatus items[] = srcFs.listStatus(src);
if (items == null) { try {
items = srcFs.listStatus(src);
} catch (FileNotFoundException fnfe) {
throw new IOException("Could not get listing for " + src); throw new IOException("Could not get listing for " + src);
} else { }
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
if (!items[i].isDir()) { if (!items[i].isDir()) {
@ -545,7 +547,6 @@ private void setReplication(short newRep, FileSystem srcFs,
} }
} }
} }
}
/** /**
* Actually set the replication for this file * Actually set the replication for this file
@ -706,7 +707,11 @@ void du(String[] cmd, int pos) throws IOException {
statusToPrint = globStatus; statusToPrint = globStatus;
} else { } else {
Path statPaths[] = FileUtil.stat2Paths(globStatus, srcPath); Path statPaths[] = FileUtil.stat2Paths(globStatus, srcPath);
try {
statusToPrint = srcFs.listStatus(statPaths); statusToPrint = srcFs.listStatus(statPaths);
} catch(FileNotFoundException fnfe) {
statusToPrint = null;
}
} }
if ((statusToPrint == null) || ((statusToPrint.length == 0) && if ((statusToPrint == null) || ((statusToPrint.length == 0) &&
(!srcFs.exists(srcPath)))){ (!srcFs.exists(srcPath)))){
@ -1234,11 +1239,10 @@ private static FileStatus[] shellListStatus(String cmd,
Path path = src.getPath(); Path path = src.getPath();
try { try {
FileStatus[] files = srcFs.listStatus(path); FileStatus[] files = srcFs.listStatus(path);
if ( files == null ) {
System.err.println(cmd +
": could not get listing for '" + path + "'");
}
return files; return files;
} catch(FileNotFoundException fnfe) {
System.err.println(cmd + ": could not get listing for '" + path + "'");
} catch (IOException e) { } catch (IOException e) {
System.err.println(cmd + System.err.println(cmd +
": could not get get listing for '" + path + "' : " + ": could not get get listing for '" + path + "' : " +

View File

@ -285,7 +285,7 @@ public FileStatus[] listStatus(Path f) throws IOException {
FileStatus[] results; FileStatus[] results;
if (!localf.exists()) { if (!localf.exists()) {
return null; throw new FileNotFoundException("File " + f + " does not exist.");
} }
if (localf.isFile()) { if (localf.isFile()) {
return new FileStatus[] { return new FileStatus[] {

View File

@ -170,10 +170,14 @@ public void checkpoint() throws IOException {
/** Delete old checkpoints. */ /** Delete old checkpoints. */
public void expunge() throws IOException { public void expunge() throws IOException {
FileStatus[] dirs = fs.listStatus(trash); // scan trash sub-directories FileStatus[] dirs = null;
if( dirs == null){
try {
dirs = fs.listStatus(trash); // scan trash sub-directories
} catch (FileNotFoundException fnfe) {
return; return;
} }
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
for (int i = 0; i < dirs.length; i++) { for (int i = 0; i < dirs.length; i++) {
Path path = dirs[i].getPath(); Path path = dirs[i].getPath();
@ -253,9 +257,6 @@ public void run() {
continue; continue;
} }
if (homes == null)
continue;
for (FileStatus home : homes) { // dump each trash for (FileStatus home : homes) { // dump each trash
if (!home.isDir()) if (!home.isDir())
continue; continue;

View File

@ -142,6 +142,9 @@ public FileStatus[] listStatus(Path path) throws IOException {
Path absolute = makeAbsolute(path); Path absolute = makeAbsolute(path);
String srep = absolute.toUri().getPath(); String srep = absolute.toUri().getPath();
if(!kfsImpl.exists(srep))
throw new FileNotFoundException("File " + path + " does not exist.");
if (kfsImpl.isFile(srep)) if (kfsImpl.isFile(srep))
return new FileStatus[] { getFileStatus(path) } ; return new FileStatus[] { getFileStatus(path) } ;
@ -249,16 +252,14 @@ public boolean delete(Path path, boolean recursive) throws IOException {
return kfsImpl.remove(srep) == 0; return kfsImpl.remove(srep) == 0;
FileStatus[] dirEntries = listStatus(absolute); FileStatus[] dirEntries = listStatus(absolute);
if ((!recursive) && (dirEntries != null) && if (!recursive && (dirEntries.length != 0)) {
(dirEntries.length != 0)) {
throw new IOException("Directory " + path.toString() + throw new IOException("Directory " + path.toString() +
" is not empty."); " is not empty.");
} }
if (dirEntries != null) {
for (int i = 0; i < dirEntries.length; i++) { for (int i = 0; i < dirEntries.length; i++) {
delete(new Path(absolute, dirEntries[i].getPath()), recursive); delete(new Path(absolute, dirEntries[i].getPath()), recursive);
} }
}
return kfsImpl.rmdir(srep) == 0; return kfsImpl.rmdir(srep) == 0;
} }

View File

@ -178,7 +178,7 @@ public FileStatus[] listStatus(Path f) throws IOException {
Path absolutePath = makeAbsolute(f); Path absolutePath = makeAbsolute(f);
INode inode = store.retrieveINode(absolutePath); INode inode = store.retrieveINode(absolutePath);
if (inode == null) { if (inode == null) {
return null; throw new FileNotFoundException("File " + f + " does not exist.");
} }
if (inode.isFile()) { if (inode.isFile()) {
return new FileStatus[] { return new FileStatus[] {
@ -303,10 +303,13 @@ public boolean delete(Path path, boolean recursive) throws IOException {
store.deleteBlock(block); store.deleteBlock(block);
} }
} else { } else {
FileStatus[] contents = listStatus(absolutePath); FileStatus[] contents = null;
if (contents == null) { try {
contents = listStatus(absolutePath);
} catch(FileNotFoundException fnfe) {
return false; return false;
} }
if ((contents.length !=0) && (!recursive)) { if ((contents.length !=0) && (!recursive)) {
throw new IOException("Directory " + path.toString() throw new IOException("Directory " + path.toString()
+ " is not empty."); + " is not empty.");

View File

@ -456,7 +456,7 @@ else if (relativePath.endsWith(FOLDER_SUFFIX)) {
if (status.isEmpty() && if (status.isEmpty() &&
store.retrieveMetadata(key + FOLDER_SUFFIX) == null) { store.retrieveMetadata(key + FOLDER_SUFFIX) == null) {
return null; throw new FileNotFoundException("File " + f + " does not exist.");
} }
return status.toArray(new FileStatus[status.size()]); return status.toArray(new FileStatus[status.size()]);

View File

@ -162,8 +162,13 @@ public void testGetFileStatusThrowsExceptionForNonExistentFile()
} }
} }
public void testListStatusReturnsNullForNonExistentFile() throws Exception { public void testListStatusThrowsExceptionForNonExistentFile() throws Exception {
assertNull(fs.listStatus(path("/test/hadoop/file"))); try {
fs.listStatus(path("/test/hadoop/file"));
fail("Should throw FileNotFoundException");
} catch (FileNotFoundException fnfe) {
// expected
}
} }
public void testListStatus() throws Exception { public void testListStatus() throws Exception {

View File

@ -20,6 +20,7 @@
package org.apache.hadoop.fs.kfs; package org.apache.hadoop.fs.kfs;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
@ -50,11 +51,12 @@ public boolean isFile(String path) throws IOException {
public String[] readdir(String path) throws IOException { public String[] readdir(String path) throws IOException {
FileStatus[] p = localFS.listStatus(new Path(path)); FileStatus[] p = localFS.listStatus(new Path(path));
String[] entries = null; try {
p = localFS.listStatus(new Path(path));
if (p == null) { } catch ( FileNotFoundException fnfe ) {
return null; return null;
} }
String[] entries = null;
entries = new String[p.length]; entries = new String[p.length];
for (int i = 0; i < p.length; i++) for (int i = 0; i < p.length; i++)

View File

@ -545,7 +545,7 @@ private int initFileDirTables() {
*/ */
private void initFileDirTables(Path path) throws IOException { private void initFileDirTables(Path path) throws IOException {
FileStatus[] stats = fs.listStatus(path); FileStatus[] stats = fs.listStatus(path);
if (stats != null) {
for (FileStatus stat : stats) { for (FileStatus stat : stats) {
if (stat.isDir()) { if (stat.isDir()) {
dirs.add(stat.getPath().toString()); dirs.add(stat.getPath().toString());
@ -558,7 +558,6 @@ private void initFileDirTables(Path path) throws IOException {
} }
} }
} }
}
/** Returns when the current number of seconds from the epoch equals /** Returns when the current number of seconds from the epoch equals
* the command line argument given by <code>-startTime</code>. * the command line argument given by <code>-startTime</code>.