/** Tests {@link UnderFileSystem#rename(String, String)} works file to new location. */ @Test public void renameFile() throws IOException { String testFileSrc = PathUtils.concatPath(mUnderfsAddress, "testFileSrc"); String testFileDst = PathUtils.concatPath(mUnderfsAddress, "testFileDst"); createEmptyFile(testFileSrc); mUfs.rename(testFileSrc, testFileDst); Assert.assertFalse(mUfs.exists(testFileSrc)); Assert.assertTrue(mUfs.exists(testFileDst)); }
/** Tests {@link UnderFileSystem#rename(String, String)} works file to a folder if supported. */ @Test public void renameFileToFolder() throws IOException { String testFileSrc = PathUtils.concatPath(mUnderfsAddress, "testFileSrc"); String testFileDst = PathUtils.concatPath(mUnderfsAddress, "testDirDst"); String testFileFinalDst = PathUtils.concatPath(testFileDst, "testFileSrc"); createEmptyFile(testFileSrc); mUfs.mkdirs(testFileDst, false); if (mUfs.rename(testFileSrc, testFileDst)) { Assert.assertFalse(mUfs.exists(testFileSrc)); Assert.assertTrue(mUfs.exists(testFileFinalDst)); } }
@Override public void close() throws IOException { if (mClosed) { return; } if (mCurrentBlockOutStream != null) { mPreviousBlockOutStreams.add(mCurrentBlockOutStream); } CompleteFileOptions options = CompleteFileOptions.defaults(); if (mUnderStorageType.isSyncPersist()) { String tmpPath = PathUtils.temporaryFileName(mNonce, mUfsPath); UnderFileSystem ufs = UnderFileSystem.get(tmpPath, ClientContext.getConf()); if (mCanceled) { // TODO(yupeng): Handle this special case in under storage integrations. mUnderStorageOutputStream.close(); if (!ufs.exists(tmpPath)) { // Location of the temporary file has changed, recompute it. updateUfsPath(); tmpPath = PathUtils.temporaryFileName(mNonce, mUfsPath); } ufs.delete(tmpPath, false); } else { mUnderStorageOutputStream.flush(); mUnderStorageOutputStream.close(); if (!ufs.exists(tmpPath)) { // Location of the temporary file has changed, recompute it. updateUfsPath(); tmpPath = PathUtils.temporaryFileName(mNonce, mUfsPath); } if (!ufs.rename(tmpPath, mUfsPath)) { throw new IOException("Failed to rename " + tmpPath + " to " + mUfsPath); } options.setUfsLength(ufs.getFileSize(mUfsPath)); } } if (mAlluxioStorageType.isStore()) { try { if (mCanceled) { for (BufferedBlockOutStream bos : mPreviousBlockOutStreams) { bos.cancel(); } } else { for (BufferedBlockOutStream bos : mPreviousBlockOutStreams) { bos.close(); } } } catch (IOException e) { handleCacheWriteException(e); } } // Complete the file if it's ready to be completed. if (!mCanceled && (mUnderStorageType.isSyncPersist() || mAlluxioStorageType.isStore())) { FileSystemMasterClient masterClient = mContext.acquireMasterClient(); try { masterClient.completeFile(mUri, options); } catch (AlluxioException e) { throw new IOException(e); } finally { mContext.releaseMasterClient(masterClient); } } if (mUnderStorageType.isAsyncPersist()) { scheduleAsyncPersist(); } mClosed = true; }