HADOOP-6899 RawLocalFileSystem#setWorkingDir() does not work for relative names
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1032730 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f1894a3c5b
commit
c979e30875
|
@ -309,6 +309,9 @@ Trunk (unreleased changes)
|
|||
HADOOP-6926. SocketInputStream incorrectly implements read().
|
||||
(Todd Lipcon via tomwhite)
|
||||
|
||||
HADOOP-6899 RawLocalFileSystem#setWorkingDir() does not work for relative names
|
||||
(Sanjay Radia)
|
||||
|
||||
Release 0.21.1 - Unreleased
|
||||
|
||||
IMPROVEMENTS
|
||||
|
|
|
@ -189,6 +189,15 @@ public class Path implements Comparable {
|
|||
return FileSystem.get(this.toUri(), conf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is an absolute path (ie a slash relative path part)
|
||||
* AND a scheme is null AND authority is null.
|
||||
*/
|
||||
public boolean isAbsoluteAndSchemeAuthorityNull() {
|
||||
return (isUriPathAbsolute() &&
|
||||
uri.getScheme() == null && uri.getAuthority() == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* True if the path component (i.e. directory) of this URI is absolute.
|
||||
*/
|
||||
|
|
|
@ -53,6 +53,14 @@ public class RawLocalFileSystem extends FileSystem {
|
|||
workingDir = getInitialWorkingDirectory();
|
||||
}
|
||||
|
||||
private Path makeAbsolute(Path f) {
|
||||
if (f.isAbsolute()) {
|
||||
return f;
|
||||
} else {
|
||||
return new Path(workingDir, f);
|
||||
}
|
||||
}
|
||||
|
||||
/** Convert a path to a File. */
|
||||
public File pathToFile(Path path) {
|
||||
checkPath(path);
|
||||
|
@ -368,7 +376,9 @@ public class RawLocalFileSystem extends FileSystem {
|
|||
*/
|
||||
@Override
|
||||
public void setWorkingDirectory(Path newDir) {
|
||||
workingDir = newDir;
|
||||
workingDir = makeAbsolute(newDir);
|
||||
checkPath(workingDir);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -34,7 +34,7 @@ public class UnsupportedFileSystemException extends IOException {
|
|||
* Constructs exception with the specified detail message.
|
||||
* @param message exception message.
|
||||
*/
|
||||
UnsupportedFileSystemException(final String message) {
|
||||
public UnsupportedFileSystemException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,197 @@
|
|||
/**
|
||||
* 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.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
|
||||
import org.apache.hadoop.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* Helper class for unit tests.
|
||||
*/
|
||||
public final class FileSystemTestHelper {
|
||||
// The test root is relative to the <wd>/build/test/data by default
|
||||
public static final String TEST_ROOT_DIR =
|
||||
System.getProperty("test.build.data", "build/test/data") + "/test";
|
||||
private static final int DEFAULT_BLOCK_SIZE = 1024;
|
||||
private static final int DEFAULT_NUM_BLOCKS = 2;
|
||||
private static String absTestRootDir = null;
|
||||
|
||||
/** Hidden constructor */
|
||||
private FileSystemTestHelper() {}
|
||||
|
||||
public static int getDefaultBlockSize() {
|
||||
return DEFAULT_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
public static byte[] getFileData(int numOfBlocks, long blockSize) {
|
||||
byte[] data = new byte[(int) (numOfBlocks * blockSize)];
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
data[i] = (byte) (i % 10);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public static Path getTestRootPath(FileSystem fSys) {
|
||||
return fSys.makeQualified(new Path(TEST_ROOT_DIR));
|
||||
}
|
||||
|
||||
public static Path getTestRootPath(FileSystem fSys, String pathString) {
|
||||
return fSys.makeQualified(new Path(TEST_ROOT_DIR, pathString));
|
||||
}
|
||||
|
||||
|
||||
// the getAbsolutexxx method is needed because the root test dir
|
||||
// can be messed up by changing the working dir.
|
||||
|
||||
public static String getAbsoluteTestRootDir(FileSystem fSys)
|
||||
throws IOException {
|
||||
if (absTestRootDir == null) {
|
||||
if (TEST_ROOT_DIR.startsWith("/")) {
|
||||
absTestRootDir = TEST_ROOT_DIR;
|
||||
} else {
|
||||
absTestRootDir = fSys.getWorkingDirectory().toString() + "/"
|
||||
+ TEST_ROOT_DIR;
|
||||
}
|
||||
}
|
||||
return absTestRootDir;
|
||||
}
|
||||
|
||||
public static Path getAbsoluteTestRootPath(FileSystem fSys) throws IOException {
|
||||
return fSys.makeQualified(new Path(getAbsoluteTestRootDir(fSys)));
|
||||
}
|
||||
|
||||
public static Path getDefaultWorkingDirectory(FileSystem fSys)
|
||||
throws IOException {
|
||||
return getTestRootPath(fSys, "/user/" + System.getProperty("user.name"))
|
||||
.makeQualified(fSys.getUri(),
|
||||
fSys.getWorkingDirectory());
|
||||
}
|
||||
|
||||
/*
|
||||
* Create files with numBlocks blocks each with block size blockSize.
|
||||
*/
|
||||
public static void createFile(FileSystem fSys, Path path, int numBlocks,
|
||||
int blockSize, boolean createParent) throws IOException {
|
||||
FSDataOutputStream out =
|
||||
fSys.create(path, false, 4096, fSys.getDefaultReplication(), blockSize );
|
||||
|
||||
byte[] data = getFileData(numBlocks, blockSize);
|
||||
out.write(data, 0, data.length);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static void createFile(FileSystem fSys, Path path, int numBlocks,
|
||||
int blockSize) throws IOException {
|
||||
createFile(fSys, path, numBlocks, blockSize, true);
|
||||
}
|
||||
|
||||
public static void createFile(FileSystem fSys, Path path) throws IOException {
|
||||
createFile(fSys, path, DEFAULT_NUM_BLOCKS, DEFAULT_BLOCK_SIZE, true);
|
||||
}
|
||||
|
||||
public static Path createFile(FileSystem fSys, String name) throws IOException {
|
||||
Path path = getTestRootPath(fSys, name);
|
||||
createFile(fSys, path);
|
||||
return path;
|
||||
}
|
||||
|
||||
public static boolean exists(FileSystem fSys, Path p) throws IOException {
|
||||
return fSys.exists(p);
|
||||
}
|
||||
|
||||
public static boolean isFile(FileSystem fSys, Path p) throws IOException {
|
||||
try {
|
||||
return fSys.getFileStatus(p).isFile();
|
||||
} catch (FileNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isDir(FileSystem fSys, Path p) throws IOException {
|
||||
try {
|
||||
return fSys.getFileStatus(p).isDirectory();
|
||||
} catch (FileNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void writeFile(FileSystem fSys, Path path,byte b[])
|
||||
throws Exception {
|
||||
FSDataOutputStream out =
|
||||
fSys.create(path);
|
||||
out.write(b);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static byte[] readFile(FileSystem fSys, Path path, int len )
|
||||
throws Exception {
|
||||
DataInputStream dis = fSys.open(path);
|
||||
byte[] buffer = new byte[len];
|
||||
IOUtils.readFully(dis, buffer, 0, len);
|
||||
dis.close();
|
||||
return buffer;
|
||||
}
|
||||
public static FileStatus containsPath(FileSystem fSys, Path path,
|
||||
FileStatus[] dirList)
|
||||
throws IOException {
|
||||
for(int i = 0; i < dirList.length; i ++) {
|
||||
if (getTestRootPath(fSys, path.toString()).equals(
|
||||
dirList[i].getPath()))
|
||||
return dirList[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static FileStatus containsPath(Path path,
|
||||
FileStatus[] dirList)
|
||||
throws IOException {
|
||||
for(int i = 0; i < dirList.length; i ++) {
|
||||
if (path.equals(dirList[i].getPath()))
|
||||
return dirList[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static FileStatus containsPath(FileSystem fSys, String path, FileStatus[] dirList)
|
||||
throws IOException {
|
||||
return containsPath(fSys, new Path(path), dirList);
|
||||
}
|
||||
|
||||
public static enum fileType {isDir, isFile, isSymlink};
|
||||
public static void checkFileStatus(FileSystem aFs, String path,
|
||||
fileType expectedType) throws IOException {
|
||||
FileStatus s = aFs.getFileStatus(new Path(path));
|
||||
Assert.assertNotNull(s);
|
||||
if (expectedType == fileType.isDir) {
|
||||
Assert.assertTrue(s.isDirectory());
|
||||
} else if (expectedType == fileType.isFile) {
|
||||
Assert.assertTrue(s.isFile());
|
||||
} else if (expectedType == fileType.isSymlink) {
|
||||
Assert.assertTrue(s.isSymlink());
|
||||
}
|
||||
Assert.assertEquals(aFs.makeQualified(new Path(path)), s.getPath());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/**
|
||||
* 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;
|
||||
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestFSMainOperationsLocalFileSystem extends FSMainOperationsBaseTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
fSys = FileSystem.getLocal(new Configuration());
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
static Path wd = null;
|
||||
protected Path getDefaultWorkingDirectory() throws IOException {
|
||||
if (wd == null)
|
||||
wd = FileSystem.getLocal(new Configuration()).getWorkingDirectory();
|
||||
return wd;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testWDAbsolute() throws IOException {
|
||||
Path absoluteDir = FileSystemTestHelper.getTestRootPath(fSys,
|
||||
"test/existingDir");
|
||||
fSys.mkdirs(absoluteDir);
|
||||
fSys.setWorkingDirectory(absoluteDir);
|
||||
Assert.assertEquals(absoluteDir, fSys.getWorkingDirectory());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue