/** * Convenience internal method to remove files or non-empty directories. * * @param path The path to remove * @return 0 on success, a negative value on error */ private int rmInternal(String path) { final AlluxioURI turi = mPathResolverCache.getUnchecked(path); try { mFileSystem.delete(turi); } catch (Throwable t) { LOG.error("Failed to remove {}", path, t); return AlluxioFuseUtils.getErrorCode(t); } return 0; }
/** * Writes a file. * * @param count the count to determine the filename * @throws Exception if it fails to write */ private static void writeFile(CyclicBarrier barrier, AtomicLong runtime, int count, AlluxioConfiguration alluxioConf) throws Exception { FileSystem fileSystem = FileSystem.Factory.get(alluxioConf); byte[] buffer = new byte[(int) Math.min(sFileSize, 4 * Constants.MB)]; Arrays.fill(buffer, (byte) 'a'); AlluxioURI path = filename(count); if (fileSystem.exists(path)) { fileSystem.delete(path); } barrier.await(); long startTime = System.nanoTime(); long bytesWritten = 0; try (FileOutStream outStream = fileSystem.createFile(path)) { while (bytesWritten < sFileSize) { outStream.write(buffer, 0, (int) Math.min(buffer.length, sFileSize - bytesWritten)); bytesWritten += buffer.length; } } runtime.addAndGet(System.nanoTime() - startTime); }
os.cancel(); if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath);
/** * Tests that the worker will delete the source directory if the directory contains nothing. */ @Test public void deleteEmptySourceDir() throws Exception { when(mMockFileSystem.listStatus(new AlluxioURI(TEST_DIR))) .thenReturn(Lists.<URIStatus>newArrayList()); runTask(TEST_DIR, TEST_SOURCE, TEST_DESTINATION, WriteType.THROUGH); verify(mMockFileSystem).delete(eq(new AlluxioURI(TEST_DIR)), any(DeletePOptions.class)); }
/** * Attempts to delete the file or directory with the specified path. * * @param path path to delete * @param recursive if true, will attempt to delete all children of the path * @return true if one or more files/directories were deleted; false otherwise */ @Override public boolean delete(Path path, boolean recursive) throws IOException { LOG.debug("delete({}, {})", path, recursive); if (mStatistics != null) { mStatistics.incrementWriteOps(1); } AlluxioURI uri = new AlluxioURI(HadoopUtils.getPathWithoutScheme(path)); DeletePOptions options = DeletePOptions.newBuilder().setRecursive(recursive).build(); try { mFileSystem.delete(uri, options); return true; } catch (InvalidPathException | FileDoesNotExistException e) { LOG.warn("delete failed: {}", e.getMessage()); return false; } catch (AlluxioException e) { throw new IOException(e); } }
private void deleteObject(String bucket, String object) throws S3Exception { String bucketPath = parseBucketPath(AlluxioURI.SEPARATOR + bucket); // Delete the object. String objectPath = bucketPath + AlluxioURI.SEPARATOR + object; DeletePOptions options = DeletePOptions.newBuilder().setAlluxioOnly(ServerConfiguration .get(PropertyKey.PROXY_S3_DELETE_TYPE).equals(Constants.S3_DELETE_IN_ALLUXIO_ONLY)).build(); try { mFileSystem.delete(new AlluxioURI(objectPath), options); } catch (Exception e) { throw toObjectS3Exception(e, objectPath); } }
@Override protected void runPlainPath(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException { // TODO(calvin): Remove explicit state checking. boolean recursive = cl.hasOption("R"); if (!mFileSystem.exists(path)) { throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path)); } if (!recursive && mFileSystem.getStatus(path).isFolder()) { throw new IOException( path.getPath() + " is a directory, to remove it, please use \"rm -R <path>\""); } boolean isAlluxioOnly = cl.hasOption(REMOVE_ALLUXIO_ONLY.getLongOpt()); DeletePOptions options = DeletePOptions.newBuilder().setRecursive(recursive).setAlluxioOnly(isAlluxioOnly) .setUnchecked(cl.hasOption(REMOVE_UNCHECKED_OPTION_CHAR)).build(); mFileSystem.delete(path, options); if (!isAlluxioOnly) { System.out.println(path + " has been removed"); } else { System.out.println(path + " has been removed from Alluxio space"); } }
private void applyOperation() throws IOException, AlluxioException { AlluxioURI uri = new AlluxioURI(String.format("%s/%s", mDir, UUID.randomUUID())); switch (mOperation) { case CreateEmptyFile: mFileSystem.createFile(uri).close(); break; case CreateAndDeleteEmptyFile: mFileSystem.createFile(uri).close(); mFileSystem.delete(uri); break; case CreateFile: try (FileOutStream file = mFileSystem.createFile(uri)) { file.write(mFiledata); } break; case ListStatus: mFileSystem.listStatus(new AlluxioURI(mDir)); break; default: throw new IllegalStateException("Unknown operation: " + mOperation); } } }
/** * Tests that the worker will not delete the source directory if the directory still contains * files. */ @Test public void dontDeleteNonEmptySourceTest() throws Exception { when(mMockFileSystem.listStatus(new AlluxioURI(TEST_DIR))) .thenReturn(Lists.newArrayList(new URIStatus(new FileInfo()))); runTask(TEST_DIR, TEST_SOURCE, TEST_DESTINATION, WriteType.THROUGH); verify(mMockFileSystem, times(0)).delete(eq(new AlluxioURI(TEST_DIR)), any(DeletePOptions.class)); }
/** * Tests that the worker will delete the source directory if the directory contains only * directories. */ @Test public void deleteDirsOnlySourceDir() throws Exception { String inner = TEST_DIR + "/innerDir"; when(mMockFileSystem.listStatus(new AlluxioURI(TEST_DIR))).thenReturn( Lists.newArrayList(new URIStatus(new FileInfo().setPath(inner).setFolder(true)))); when(mMockFileSystem.listStatus(new AlluxioURI(inner))) .thenReturn(Lists.<URIStatus>newArrayList()); runTask(TEST_DIR, TEST_SOURCE, TEST_DESTINATION, WriteType.THROUGH); verify(mMockFileSystem).delete(eq(new AlluxioURI(TEST_DIR)), any(DeletePOptions.class)); }
/** * Tests that the bytes of the file to move are written to the destination stream. */ @Test public void basicMoveTest() throws Exception { runTask(TEST_SOURCE, TEST_SOURCE, TEST_DESTINATION, WriteType.THROUGH); Assert.assertArrayEquals(TEST_SOURCE_CONTENTS, mMockOutStream.toByteArray()); verify(mMockFileSystem).delete(new AlluxioURI(TEST_SOURCE)); }
/** * @param command the move command to execute * @param writeType the write type to use for the moved file * @param fileSystem the Alluxio file system */ private static void move(MoveCommand command, WritePType writeType, FileSystem fileSystem) throws Exception { String source = command.getSource(); String destination = command.getDestination(); LOG.debug("Moving {} to {}", source, destination); CreateFilePOptions createOptions = CreateFilePOptions.newBuilder().setWriteType(writeType).build(); try (FileOutStream out = fileSystem.createFile(new AlluxioURI(destination), createOptions)) { try (FileInStream in = fileSystem.openFile(new AlluxioURI(source))) { IOUtils.copy(in, out); } catch (Throwable t) { try { out.cancel(); } catch (Throwable t2) { t.addSuppressed(t2); } throw t; } } fileSystem.delete(new AlluxioURI(source)); }
private FileOutStream createFile(FileSystem fileSystem, AlluxioURI filePath, boolean deleteIfExists) throws IOException, AlluxioException { CreateFilePOptions options = CreateFilePOptions.newBuilder().setWriteType(mWriteType.toProto()) .setRecursive(true).build(); if (!fileSystem.exists(filePath)) { // file doesn't exist yet, so create it return fileSystem.createFile(filePath, options); } else if (deleteIfExists) { // file exists, so delete it and recreate fileSystem.delete(filePath); return fileSystem.createFile(filePath, options); } // file exists and deleteIfExists is false throw new FileAlreadyExistsException("File exists and deleteIfExists is false"); }
private void abortMultipartUpload(String bucket, String object, long uploadId) throws S3Exception { String bucketPath = parseBucketPath(AlluxioURI.SEPARATOR + bucket); checkBucketIsAlluxioDirectory(bucketPath); String objectPath = bucketPath + AlluxioURI.SEPARATOR + object; AlluxioURI multipartTemporaryDir = new AlluxioURI(S3RestUtils.getMultipartTemporaryDirForObject(bucketPath, object)); checkUploadId(multipartTemporaryDir, uploadId); try { mFileSystem.delete(multipartTemporaryDir, DeletePOptions.newBuilder().setRecursive(true).build()); } catch (Exception e) { throw toObjectS3Exception(e, objectPath); } }
/** * Tests for the {@link BaseFileSystem#delete(AlluxioURI, DeletePOptions)} method. */ @Test public void delete() throws Exception { AlluxioURI file = new AlluxioURI("/file"); DeletePOptions deleteOptions = DeletePOptions.newBuilder().setRecursive(true).build(); mFileSystem.delete(file, deleteOptions); verify(mFileSystemMasterClient).delete(file, deleteOptions); verifyFilesystemContextAcquiredAndReleased(); }
/** * Ensures that an exception is propagated correctly when deleting a file. */ @Test public void deleteException() throws Exception { AlluxioURI file = new AlluxioURI("/file"); DeletePOptions deleteOptions = DeletePOptions.newBuilder().setRecursive(true).build(); doThrow(EXCEPTION).when(mFileSystemMasterClient).delete(file, deleteOptions); try { mFileSystem.delete(file, deleteOptions); fail(SHOULD_HAVE_PROPAGATED_MESSAGE); } catch (Exception e) { assertSame(EXCEPTION, e); } verifyFilesystemContextAcquiredAndReleased(); }
FileSystem.Factory.get(fsContext); if (fs.exists(testDir)) { fs.delete(testDir, DeletePOptions.newBuilder().setRecursive(true).setUnchecked(true).build());
/** * {@inheritDoc} * * Moves the file specified in the config to the configured path. If the destination path is a * directory, the file is moved inside that directory. */ @Override public SerializableVoid runTask(MoveConfig config, ArrayList<MoveCommand> commands, JobWorkerContext jobWorkerContext) throws Exception { WriteType writeType = config.getWriteType() == null ? ServerConfiguration.getEnum(PropertyKey.USER_FILE_WRITE_TYPE_DEFAULT, WriteType.class) : WriteType.valueOf(config.getWriteType()); for (MoveCommand command : commands) { move(command, writeType.toProto(), mFileSystem); } // Try to delete the source directory if it is empty. if (!hasFiles(new AlluxioURI(config.getSource()), mFileSystem)) { try { LOG.debug("Deleting {}", config.getSource()); mFileSystem.delete(new AlluxioURI(config.getSource()), DeletePOptions.newBuilder().setRecursive(true).build()); } catch (FileDoesNotExistException e) { // It's already deleted, possibly by another worker. } } return null; }
sFileSystem.delete(testURI); } else if (ClientOpType.CREATE_RENAME_FILE == mOpType) { try {