@Override protected void processArguments(LinkedList<PathData> args) throws IOException { // if more than one arg, the destination must be a directory // if one arg, the dst must not exist or must be a directory if (args.size() > 1) { if (!dst.exists) { throw new PathNotFoundException(dst.toString()); } if (!dst.stat.isDirectory()) { throw new PathIsNotDirectoryException(dst.toString()); } } else if (dst.exists) { if (!dst.stat.isDirectory() && !overwrite) { throw new PathExistsException(dst.toString()); } } else if (!dst.parentExists()) { throw new PathNotFoundException(dst.toString()); } super.processArguments(args); }
/** * If direct write is disabled ,copies the stream contents to a temporary * file "<target>._COPYING_". If the copy is * successful, the temporary file will be renamed to the real path, * else the temporary file will be deleted. * if direct write is enabled , then creation temporary file is skipped. * @param in the input stream for the copy * @param target where to store the contents of the stream * @throws IOException if copy fails */ protected void copyStreamToTarget(InputStream in, PathData target) throws IOException { if (target.exists && (target.stat.isDirectory() || !overwrite)) { throw new PathExistsException(target.toString()); } TargetFileSystem targetFs = new TargetFileSystem(target.fs); try { PathData tempTarget = direct ? target : target.suffix("._COPYING_"); targetFs.setWriteChecksum(writeChecksum); targetFs.writeStreamToFile(in, tempTarget, lazyPersist, direct); if (!direct) { targetFs.rename(tempTarget, target); } } finally { targetFs.close(); // last ditch effort to ensure temp file is removed } }
/** * Copies the stream contents to a temporary file. If the copy is * successful, the temporary file will be renamed to the real path, * else the temporary file will be deleted. * @param in the input stream for the copy * @param target where to store the contents of the stream * @throws IOException if copy fails */ protected void copyStreamToTarget(InputStream in, PathData target) throws IOException { if (target.exists && (target.stat.isDirectory() || !overwrite)) { throw new PathExistsException(target.toString()); } TargetFileSystem targetFs = new TargetFileSystem(target.fs); try { PathData tempTarget = target.suffix("._COPYING_"); targetFs.setWriteChecksum(writeChecksum); targetFs.writeStreamToFile(in, tempTarget, lazyPersist); targetFs.rename(tempTarget, target); } finally { targetFs.close(); // last ditch effort to ensure temp file is removed } }
/** * Copies the stream contents to a temporary file. If the copy is * successful, the temporary file will be renamed to the real path, * else the temporary file will be deleted. * @param in the input stream for the copy * @param target where to store the contents of the stream * @throws IOException if copy fails */ protected void copyStreamToTarget(InputStream in, PathData target) throws IOException { if (target.exists && (target.stat.isDirectory() || !overwrite)) { throw new PathExistsException(target.toString()); } TargetFileSystem targetFs = new TargetFileSystem(target.fs); try { PathData tempTarget = target.suffix("._COPYING_"); targetFs.setWriteChecksum(writeChecksum); targetFs.writeStreamToFile(in, tempTarget); targetFs.rename(tempTarget, target); } finally { targetFs.close(); // last ditch effort to ensure temp file is removed } }
@Override protected void processArguments(LinkedList<PathData> args) throws IOException { // if more than one arg, the destination must be a directory // if one arg, the dst must not exist or must be a directory if (args.size() > 1) { if (!dst.exists) { throw new PathNotFoundException(dst.toString()); } if (!dst.stat.isDirectory()) { throw new PathIsNotDirectoryException(dst.toString()); } } else if (dst.exists) { if (!dst.stat.isDirectory() && !overwrite) { throw new PathExistsException(dst.toString()); } } else if (!dst.parentExists()) { throw new PathNotFoundException(dst.toString()) .withFullyQualifiedPath(dst.path.toUri().toString()); } super.processArguments(args); }
@Override protected void processPath(PathData src, PathData target) throws IOException { String srcUri = src.fs.getUri().getScheme() + "://" + src.fs.getUri().getHost(); String dstUri = target.fs.getUri().getScheme() + "://" + target.fs.getUri().getHost(); if (!srcUri.equals(dstUri)) { throw new PathIOException(src.toString(), "Does not match target filesystem"); } if (target.exists) { throw new PathExistsException(target.toString()); } if (!target.fs.rename(src.path, target.path)) { // we have no way to know the actual error... throw new PathIOException(src.toString()); } }
@Override protected void processPath(PathData src, PathData target) throws IOException { // unlike copy, don't merge existing dirs during move if (target.exists && target.stat.isDirectory()) { throw new PathExistsException(target.toString()); } super.processPath(src, target); }
@Override protected void processPath(PathData src, PathData target) throws IOException { if (!src.fs.getUri().equals(target.fs.getUri())) { throw new PathIOException(src.toString(), "Does not match target filesystem"); } if (target.exists) { throw new PathExistsException(target.toString()); } if (!target.fs.rename(src.path, target.path)) { // we have no way to know the actual error... throw new PathIOException(src.toString()); } }
@Override protected void processPath(PathData item) throws IOException { if (item.stat.isDirectory()) { if (!createParents) { throw new PathExistsException(item.toString()); } } else { throw new PathIsNotDirectoryException(item.toString()); } }
/** * Cast IO exception to IGFS exception. * * @param msg Error message. * @param e IO exception. * @return IGFS exception. */ public static IgfsException cast(String msg, IOException e) { if (e instanceof FileNotFoundException) return new IgfsPathNotFoundException(e); else if (e instanceof ParentNotDirectoryException) return new IgfsParentNotDirectoryException(msg, e); else if (e instanceof PathIsNotEmptyDirectoryException) return new IgfsDirectoryNotEmptyException(e); else if (e instanceof PathExistsException) return new IgfsPathAlreadyExistsException(msg, e); else return new IgfsException(msg, e); }
/** @throws Exception If failed. */ public void testCreateCheckOverwrite() throws Exception { Path fsHome = new Path(primaryFsUri); Path dir = new Path(fsHome, "/someDir1/someDir2/someDir3"); final Path file = new Path(dir, "someFile"); FSDataOutputStream out = fs.create(file, EnumSet.noneOf(CreateFlag.class), Options.CreateOpts.perms(FsPermission.getDefault())); out.close(); // Check intermediate directory permissions. assertEquals(FsPermission.getDefault(), fs.getFileStatus(dir).getPermission()); assertEquals(FsPermission.getDefault(), fs.getFileStatus(dir.getParent()).getPermission()); assertEquals(FsPermission.getDefault(), fs.getFileStatus(dir.getParent().getParent()).getPermission()); GridTestUtils.assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { return fs.create(file, EnumSet.noneOf(CreateFlag.class), Options.CreateOpts.perms(FsPermission.getDefault())); } }, PathExistsException.class, null); // Overwrite should be successful. FSDataOutputStream out1 = fs.create(file, EnumSet.of(CreateFlag.OVERWRITE), Options.CreateOpts.perms(FsPermission.getDefault())); out1.close(); }