@Override public void setWorkingDirectory(Path newDir) { Path absolutePath = toAbsolutePath(newDir); checkPath(absolutePath); this.workingDirectory = absolutePath; }
@Override public FSDataInputStream open(Path f, int bufferSize) throws IOException { Path absolutePath = toAbsolutePath(f); checkPath(absolutePath); // Handle root if (absolutePath.isRoot()) { throw new AccessControlException("Cannot open " + f); } try { RemotePath remotePath = getRemotePath(absolutePath); FileSystem delegate = getDelegateFileSystem(remotePath.address); return delegate.open(remotePath.path, bufferSize); } catch (IllegalArgumentException e) { throw (FileNotFoundException) (new FileNotFoundException("No file " + absolutePath).initCause(e)); } }
@Override public boolean mkdirs(Path f, FsPermission permission) throws IOException { Path absolutePath = toAbsolutePath(f); checkPath(absolutePath); // Handle root if (absolutePath.isRoot()) { // Path always exists return true; } if (isRemoteFile(absolutePath)) { // Attempting to create a subdirectory for a file throw new IOException("Cannot create a directory under file " + f); } return new MkdirsTask(absolutePath, permission).get(); }
/** * Create a new file. Three possibilities: * - This is a data node and you're trying to create a unqualified file => write locally. * - This is a client node and you're trying to create unqualified file => pick a random data node and write there. * - The path you provide is qualified => write to that node. */ @Override public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException { final Path absolutePath = toAbsolutePath(f); checkPath(absolutePath); // Handle root if (absolutePath.isRoot()) { throw new AccessControlException("Cannot create " + f); } if(!isRemoteFile(f)){ if (isDirectory(absolutePath)) { throw new FileAlreadyExistsException("Directory already exists: " + f); } // Only canonicalized path/remote files are allowed throw new IOException("Cannot create non-canonical path " + f); } try { RemotePath remotePath = getRemotePath(absolutePath); return getDelegateFileSystem(remotePath.address).create(remotePath.path, permission, overwrite, bufferSize, replication, blockSize, progress); } catch (IllegalArgumentException e) { throw (IOException) (new IOException("Cannot create file " + absolutePath).initCause(e)); } }
@Override public boolean rename(Path src, Path dst) throws IOException { Path absoluteSrc = toAbsolutePath(src); Path absoluteDst = toAbsolutePath(dst); checkPath(absoluteSrc); checkPath(absoluteDst);
@Override public RemoteIterator<FileStatus> listStatusIterator(Path f) throws FileNotFoundException, IOException { final Path absolutePath = toAbsolutePath(f); checkPath(absolutePath); if (isRemoteFile(absolutePath)) { return new RemoteIterator<FileStatus>() { private boolean hasNext = true; @Override public boolean hasNext() throws IOException { return hasNext; } @Override public FileStatus next() throws IOException { if (!hasNext) { throw new NoSuchElementException(); } hasNext = false; return getFileStatus(absolutePath); } }; } return new ListStatusIteratorTask(absolutePath).get(); }
@Override public FileStatus getFileStatus(Path f) throws IOException { Path absolutePath = toAbsolutePath(f); checkPath(absolutePath); // if the path is not a remote file path if (!isRemoteFile(absolutePath)) { return new GetFileStatusTask(absolutePath).get(); } // Parse top level directory try { RemotePath remotePath = getRemotePath(absolutePath); FileSystem delegate = getDelegateFileSystem(remotePath.address); FileStatus status = delegate.getFileStatus(remotePath.path); return fixFileStatus(remotePath.address, status); } catch (IllegalArgumentException e) { throw (FileNotFoundException) (new FileNotFoundException("No file " + absolutePath).initCause(e)); } }
@Override public boolean delete(Path f, boolean recursive) throws IOException { Path absolutePath = toAbsolutePath(f); checkPath(absolutePath); // Handle root if (absolutePath.isRoot()) { throw new AccessControlException("Cannot delete " + f); } if (!isRemoteFile(f)) { // In our remote view, there might be a directory, so delete task should handle this case return new DeleteTask(absolutePath, recursive).get(); } try { RemotePath remotePath = getRemotePath(absolutePath); FileSystem delegate = getDelegateFileSystem(remotePath.address); return delegate.delete(remotePath.path, recursive); } catch (IllegalArgumentException e) { throw (FileNotFoundException) (new FileNotFoundException("No file " + absolutePath).initCause(e)); } }
/** * Create a new file. Three possibilities: * - This is a data node and you're trying to append a unqualified file => write locally. * - The path you provide is qualified => write to that node. * * If this is a client node and you try to write to a unqualified file, we'll throw */ @Override public FSDataOutputStream append(Path f, int bufferSize, Progressable progress) throws IOException { Path absolutePath = toAbsolutePath(f); checkPath(absolutePath); // Handle root if (absolutePath.isRoot()) { throw new AccessControlException("Cannot open " + f); } if(!isRemoteFile(f)){ if (isDirectory(absolutePath)) { throw new FileAlreadyExistsException("Directory already exists: " + f); } // Only fully canonicalized/remote files are allowed throw new IOException("Cannot create non-canonical path " + f); } try { RemotePath remotePath = getRemotePath(absolutePath); FileSystem delegate = getDelegateFileSystem(remotePath.address); return delegate.append(remotePath.path, bufferSize, progress); } catch (IllegalArgumentException e) { throw (FileNotFoundException) (new FileNotFoundException("No file " + absolutePath).initCause(e)); } }
@Override public Path canonicalizePath(Path p) throws IOException { Path absolutePath = toAbsolutePath(p); checkPath(absolutePath); if (isRemoteFile(absolutePath)) { return absolutePath; } if (isDirectory(absolutePath)) { return absolutePath; } if (localAccessAllowed) { return createRemotePath(localAddress, absolutePath); } // We aren't local or remote. That means we are a client node that wants to create a file. // We'll pick a random external location. final NodeEndpoint randomDelegate = getRandomDelegate(); return createRemotePath(randomDelegate.getAddress(), absolutePath); }