/** * Tests an empty directory can be deleted. Tests a non empty directory will not be deleted if * recursive is not specified. Tests a non empty directory will be deleted if recursive is * specified. */ @Test public void deleteDir() throws IOException { String testDirEmpty = PathUtils.concatPath(mUnderfsAddress, "testDirEmpty"); String testDirNonEmpty = PathUtils.concatPath(mUnderfsAddress, "testDirNonEmpty1"); String testDirNonEmptyChildDir = PathUtils.concatPath(testDirNonEmpty, "testDirNonEmpty2"); String testDirNonEmptyChildFile = PathUtils.concatPath(testDirNonEmpty, "testDirNonEmptyF"); String testDirNonEmptyChildDirFile = PathUtils.concatPath(testDirNonEmptyChildDir, "testDirNonEmptyChildDirF"); mUfs.mkdirs(testDirEmpty, false); mUfs.mkdirs(testDirNonEmpty, false); mUfs.mkdirs(testDirNonEmptyChildDir, false); createEmptyFile(testDirNonEmptyChildFile); createEmptyFile(testDirNonEmptyChildDirFile); mUfs.delete(testDirEmpty, false); Assert.assertFalse(mUfs.exists(testDirEmpty)); try { mUfs.delete(testDirNonEmpty, false); } catch (IOException e) { // Some File systems may throw IOException } Assert.assertTrue(mUfs.exists(testDirNonEmpty)); mUfs.delete(testDirNonEmpty, true); Assert.assertFalse(mUfs.exists(testDirNonEmpty)); Assert.assertFalse(mUfs.exists(testDirNonEmptyChildDir)); Assert.assertFalse(mUfs.exists(testDirNonEmptyChildFile)); Assert.assertFalse(mUfs.exists(testDirNonEmptyChildDirFile)); }
/** Tests a file can be deleted. */ @Test public void deleteFile() throws IOException { String testFile = PathUtils.concatPath(mUnderfsAddress, "testFile"); createEmptyFile(testFile); mUfs.delete(testFile, false); Assert.assertFalse(mUfs.exists(testFile)); }
/** Tests if delete deletes all files or folders for a large directory. */ @Test public void deleteLargeDirectory() throws IOException { LargeDirectoryConfig config = prepareLargeDirectoryTest(); mUfs.delete(config.getTopLevelDirectory(), true); String[] children = config.getChildren(); for (String child : children) { // Retry for some time to allow list operation eventual consistency for S3 and GCS. // See http://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html and // https://cloud.google.com/storage/docs/consistency for more details. // Note: not using CommonUtils.waitFor here because we intend to sleep with a longer interval. boolean childDeleted = false; for (int i = 0; i < 20; i++) { childDeleted = !mUfs.exists(child); if (childDeleted) { break; } CommonUtils.sleepMs(500); } Assert.assertTrue(childDeleted); } }
@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; }