HADOOP-6229. Attempt to make a directory under an existing file on LocalFileSystem should throw an Exception. Contributed by Boris Shkolnik.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@812127 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas White 2009-09-07 13:25:48 +00:00
parent e24071be0a
commit 9b0b37e410
4 changed files with 75 additions and 1 deletions

View File

@ -972,6 +972,9 @@ Trunk (unreleased changes)
HADOOP-6199. Move io.map.skip.index property to core-default from mapred. HADOOP-6199. Move io.map.skip.index property to core-default from mapred.
(Amareshwari Sriramadasu via cdouglas) (Amareshwari Sriramadasu via cdouglas)
HADOOP-6229. Attempt to make a directory under an existing file on
LocalFileSystem should throw an Exception. (Boris Shkolnik via tomwhite)
Release 0.20.1 - Unreleased Release 0.20.1 - Unreleased
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -0,0 +1,38 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs;
import java.io.IOException;
/**
* Used when target file already exists for any operation and
* is not configured to be overwritten.
*/
public class FileAlreadyExistsException
extends IOException {
public FileAlreadyExistsException() {
super();
}
public FileAlreadyExistsException(String msg) {
super(msg);
}
}

View File

@ -308,8 +308,18 @@ public FileStatus[] listStatus(Path f) throws IOException {
* treat existence as an error. * treat existence as an error.
*/ */
public boolean mkdirs(Path f) throws IOException { public boolean mkdirs(Path f) throws IOException {
if(f == null) {
throw new IllegalArgumentException("mkdirs path arg is null");
}
Path parent = f.getParent(); Path parent = f.getParent();
File p2f = pathToFile(f); File p2f = pathToFile(f);
if(parent != null) {
File parent2f = pathToFile(parent);
if(parent2f != null && parent2f.exists() && !parent2f.isDirectory()) {
throw new FileAlreadyExistsException("Parent path is not a directory: "
+ parent);
}
}
return (parent == null || mkdirs(parent)) && return (parent == null || mkdirs(parent)) &&
(p2f.mkdir() || p2f.isDirectory()); (p2f.mkdir() || p2f.isDirectory());
} }
@ -318,6 +328,7 @@ public boolean mkdirs(Path f) throws IOException {
@Override @Override
public boolean mkdirs(Path f, FsPermission permission) throws IOException { public boolean mkdirs(Path f, FsPermission permission) throws IOException {
boolean b = mkdirs(f); boolean b = mkdirs(f);
if(b)
setPermission(f, permission); setPermission(f, permission);
return b; return b;
} }

View File

@ -154,4 +154,26 @@ public void testPathEscapes() throws IOException {
assertEquals(path.makeQualified(fs), status.getPath()); assertEquals(path.makeQualified(fs), status.getPath());
cleanupFile(fs, path); cleanupFile(fs, path);
} }
public void testMkdirs() throws IOException {
Configuration conf = new Configuration();
LocalFileSystem fs = FileSystem.getLocal(conf);
Path test_dir = new Path(TEST_ROOT_DIR, "test_dir");
Path test_file = new Path(TEST_ROOT_DIR, "file1");
assertTrue(fs.mkdirs(test_dir));
writeFile(fs, test_file);
// creating dir over a file
Path bad_dir = new Path(test_file, "another_dir");
try {
fs.mkdirs(bad_dir);
fail("Failed to detect existing file in path");
} catch (FileAlreadyExistsException e) { }
try {
fs.mkdirs(null);
fail("Failed to detect null in mkdir arg");
} catch (IllegalArgumentException e) { }
}
} }