/** * Copies a file in the Alluxio filesystem. * * @param srcPath the source {@link AlluxioURI} (has to be a file) * @param dstPath the destination path in the Alluxio filesystem */ private void copyFile(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException { try (Closer closer = Closer.create()) { OpenFilePOptions openFileOptions = OpenFilePOptions.getDefaultInstance(); FileInStream is = closer.register(mFileSystem.openFile(srcPath, openFileOptions)); FileOutStream os = closer.register(mFileSystem.createFile(dstPath)); try { IOUtils.copy(is, os); } catch (Exception e) { os.cancel(); throw e; } System.out.println(String.format(COPY_SUCCEED_MESSAGE, srcPath, dstPath)); } }
os.cancel(); if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath);
/** * Tests that {@link FileOutStream#cancel()} will cancel and close the underlying out streams, and * delete from the under file system when the delegation flag is set. Also makes sure that * cancel() doesn't persist or complete the file. */ @Test public void cancelWithDelegation() throws Exception { mTestStream.write(BufferUtils.getIncreasingByteArray((int) (BLOCK_LENGTH * 1.5))); mTestStream.cancel(); for (long streamIndex = 0; streamIndex < 2; streamIndex++) { Assert.assertTrue(mAlluxioOutStreamMap.get(streamIndex).isClosed()); Assert.assertTrue(mAlluxioOutStreamMap.get(streamIndex).isCanceled()); } // Don't complete the file if the stream was canceled verify(mFileSystemMasterClient, times(0)).completeFile(any(AlluxioURI.class), any(CompleteFilePOptions.class)); }
/** * @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)); }