HADOOP-13483. File create should throw error rather than overwrite directories. Contributed by Genmao Yu.

This commit is contained in:
Mingfei 2016-08-23 16:18:34 +08:00
parent 4d84c814fc
commit bd2d97adee
2 changed files with 22 additions and 9 deletions

View File

@ -63,6 +63,8 @@
import com.aliyun.oss.model.PartETag; import com.aliyun.oss.model.PartETag;
import com.aliyun.oss.model.UploadPartCopyRequest; import com.aliyun.oss.model.UploadPartCopyRequest;
import com.aliyun.oss.model.UploadPartCopyResult; import com.aliyun.oss.model.UploadPartCopyResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Implementation of {@link FileSystem} for <a href="https://oss.aliyun.com"> * Implementation of {@link FileSystem} for <a href="https://oss.aliyun.com">
@ -70,6 +72,8 @@
*/ */
public class AliyunOSSFileSystem extends FileSystem { public class AliyunOSSFileSystem extends FileSystem {
private static final Logger LOG =
LoggerFactory.getLogger(AliyunOSSFileSystem.class);
private URI uri; private URI uri;
private Path workingDir; private Path workingDir;
private OSSClient ossClient; private OSSClient ossClient;
@ -101,9 +105,24 @@ public FSDataOutputStream create(Path path, FsPermission permission,
boolean overwrite, int bufferSize, short replication, long blockSize, boolean overwrite, int bufferSize, short replication, long blockSize,
Progressable progress) throws IOException { Progressable progress) throws IOException {
String key = pathToKey(path); String key = pathToKey(path);
FileStatus status = null;
if (!overwrite && exists(path)) { try {
throw new FileAlreadyExistsException(path + " already exists"); // get the status or throw a FNFE
status = getFileStatus(path);
// if the thread reaches here, there is something at the path
if (status.isDirectory()) {
// path references a directory
throw new FileAlreadyExistsException(path + " is a directory");
}
if (!overwrite) {
// path references a file and overwrite is disabled
throw new FileAlreadyExistsException(path + " already exists");
}
LOG.debug("Overwriting file {}", path);
} catch (FileNotFoundException e) {
// this means the file is not found
} }
return new FSDataOutputStream(new AliyunOSSOutputStream(getConf(), return new FSDataOutputStream(new AliyunOSSOutputStream(getConf(),
@ -540,7 +559,7 @@ public FileStatus[] listStatus(Path path) throws IOException {
/** /**
* Used to create an empty file that represents an empty directory. * Used to create an empty file that represents an empty directory.
* *
* @param bucketName the bucket this directory belongs to * @param bucket the bucket this directory belongs to
* @param objectName directory path * @param objectName directory path
* @return true if directory successfully created * @return true if directory successfully created
* @throws IOException * @throws IOException

View File

@ -21,7 +21,6 @@
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.contract.AbstractContractCreateTest; import org.apache.hadoop.fs.contract.AbstractContractCreateTest;
import org.apache.hadoop.fs.contract.AbstractFSContract; import org.apache.hadoop.fs.contract.AbstractFSContract;
import org.apache.hadoop.fs.contract.ContractTestUtils;
/** /**
* OSS contract creating tests. * OSS contract creating tests.
@ -33,9 +32,4 @@ protected AbstractFSContract createContract(Configuration conf) {
return new OSSContract(conf); return new OSSContract(conf);
} }
@Override
public void testOverwriteEmptyDirectory() throws Throwable {
ContractTestUtils.skip(
"blobstores can't distinguish empty directories from files");
}
} }