/** * Equivalent to {@link #rm(File) rm(this)}. * * @return {@code this} * @throws IOException if any I/O error occurs. * @see <a href="#bulkIOMethods">Bulk I/O Methods</a> */ public TFile rm() throws IOException { rm(this); return this; }
@Test public final void testList() throws IOException { final File dir = createTempFile(); final TFile tdir = new TFile(dir); assertNull(dir.listFiles()); assertNull(tdir.listFiles()); assertNull(tdir.toNonArchiveFile().listFiles()); TFile.rm(dir); // Create regular directory for testing. assertTrue(dir.mkdir()); for (int i = MEMBERS.length; --i >= 0; ) assertTrue(new File(dir, MEMBERS[i]).createNewFile()); final File[] files = dir.listFiles(); Arrays.sort(files); assertList(files, tdir); TFile.rm_r(tdir); // Repeat test with regular archive file. assertTrue(tdir.mkdir()); for (int i = MEMBERS.length; --i >= 0; ) assertTrue(new TFile(tdir, MEMBERS[i]).createNewFile()); assertList(files, tdir); TFile.rm_r(tdir); }
/** * {@inheritDoc} * * @deprecated This method just returns a boolean value to indicate failure, * which is hard to analyze. * @see #rm() */ @Deprecated @Override public boolean delete() { try { rm(this); return true; } catch (IOException ex) { return false; } }
@Test public final void testIllegalDirectoryOperations() throws IOException { try { final String[] names = { "inner" + getSuffix(), "dir", }; TFile file = archive; for (int i = 0; i <= names.length; i++) { final TFile file2 = file.toNonArchiveFile(); assertTrue(file2.mkdir()); assertIllegalDirectoryOperations(file2); file2.rm(); assertTrue(file.mkdir()); assertIllegalDirectoryOperations(file); if (i < names.length) file = new TFile(file, names[i]); } } finally { archive.rm_r(); } }
/** * Deletes the file or empty directory {@code node} * and updates the tree accordingly. * Note that the current selection may get lost. * * @throws IOException On any I/O failure. */ public void rm(TFile node) throws IOException { TFile.rm(node); nodeRemoved(node); }
private void assertRenameArchiveToTemp(final TFile archive) throws IOException { assert archive.isArchive(); // regular archive or false positive assert !archive.isEntry(); // not contained in another archive file // Create a temporary file. TFile tmp = new TFile(TFile.createTempFile(TEMP_FILE_PREFIX, null)); tmp.rm(); assertFalse(tmp.exists()); assertFalse(tmp.toNonArchiveFile().exists()); // Now rename the archive to the temporary path. // Depending on the true state of the object "archive", this will // either create a directory (iff archive is a regular archive) or a // plain file (iff archive is a false positive). archive.mv(tmp); assertFalse(archive.exists()); assertFalse(archive.toNonArchiveFile().exists()); // Now delete resulting temporary file or directory. tmp.rm_r(); assertFalse(tmp.exists()); assertFalse(tmp.toNonArchiveFile().exists()); }
/** * Recursively deletes the given file or directory tree. * * @param node the file or directory tree to delete recursively. * @throws IOException if an elementary operation fails for any reason. */ static void rm_r(final File node, final TArchiveDetector detector) throws IOException { if (node.isDirectory()) { final String[] members = node.list(); if (null == members) throw new IOException(node + " (cannot list directory)"); for (final String member : members) rm_r(new TFile(node, member, detector), detector); } TFile.rm(node); }
private TFile newTempArchive() throws IOException { // TODO: Removing .getCanonicalFile() causes archive.rm_r() to // fail in testCopyContainingOrSameFiles() - explain why! final File temp = File.createTempFile(TEMP_FILE_PREFIX, getSuffix()) .getCanonicalFile(); TFile.rm(temp); return new TFile(temp); }
@edu.umd.cs.findbugs.annotations.SuppressWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") private void assertCreateNewPlainFile() throws IOException { final File archive = createTempFile(); TFile.rm(archive); final File file1 = new File(archive, "test.txt"); final File file2 = new File(file1, "test.txt"); try { file1.createNewFile(); fail("Creating a file in a non-existent directory should throw an IOException!"); } catch (IOException expected) { } assertCreateNewFile(archive, file1, file2); }
private void assertInputOutput(final TFile file) throws IOException { assertInput(file); assertOutput(file); file.rm(); }
@Override public void setUp() throws IOException { super.setUp(); temp = createTempFile(); TFile.rm(temp); archive = new TFile(temp); }
@Test public final void testInputOutput() throws IOException { assertInputOutput(archive); final TFile archiveTest = new TFile(archive, "test"); assertInputOutput(archiveTest); final TFile archiveInner = new TFile(archive, "inner" + getSuffix()); final TFile archiveInnerTest = new TFile(archiveInner, "test"); assertInputOutput(archiveInnerTest); archiveInner.rm(); archive.rm(); }
@Test public void testCancelling() throws IOException { setAction(CANCEL); final TFile archive = getArchive(); assertFalse(archive.toNonArchiveFile().exists()); final TFile entry1 = new TFile(archive, "entry1"); assertTrue(entry1.mkdirs()); entry1.rm(); assertTrue(entry1.createNewFile()); entry1.rm(); final TFile entry2 = new TFile(archive, "entry2"); assertTrue(entry2.mkdirs()); entry2.rm(); assertTrue(entry2.createNewFile()); entry2.rm(); }
@Test public final void testCopyDelete() throws IOException { final String[] names = { "0" + getSuffix(), "1" + getSuffix(), //"2" + getSuffix(), }; assertTrue(archive.mkdir()); // create valid archive file assertCopyDelete(archive, names, 0); archive.rm(); assertTrue(archive.toNonArchiveFile().mkdir()); // create false positive archive file assertCopyDelete(archive, names, 0); archive.rm(); }
private void assertCopyDelete(final TFile parent, String[] names, int off) throws IOException { if (off >= names.length) return; final TFile dir = new TFile(parent, names[off]); assertTrue(dir.mkdir()); // create valid archive file assertCopyDelete(parent, dir); assertCopyDelete(dir, names, off + 1); // continue recursion dir.rm(); assertTrue(dir.toNonArchiveFile().mkdir()); // create false positive archive file assertCopyDelete(parent, dir); assertCopyDelete(dir, names, off + 1); // continue recursion dir.rm(); }
@Test public final void testFalsePositives() throws IOException { assertFalsePositive(archive); // Dito for entry. final TFile entry = new TFile(archive, "entry" + getSuffix()); assertTrue(archive.toNonArchiveFile().mkdir()); assertFalsePositive(entry); archive.rm(); assertTrue(archive.mkdir()); assertFalsePositive(entry); archive.rm(); }
@Test public final void testLenientFileOutputStream() throws IOException { TFile file = new TFile(archive, "dir/inner" + getSuffix() + "/dir/test.txt"); assertFileOutputStream(file); try { archive.rm(); fail("directory not empty"); } catch (IOException expected) { } umount(); // allow external modifications! TFile.rm(archive.toNonArchiveFile()); // use plain file to delete instead! assertFalse(archive.exists()); assertFalse(archive.isDirectory()); assertFalse(archive.isFile()); assertEquals(0, archive.length()); }
private void assertRm(final TFile file) throws IOException { file.rm(); assertFalse(file.exists()); assertFalse(file.isDirectory()); assertFalse(file.isFile()); assertEquals(0, file.length()); assertFalse(file.lastModified() > 0); }
private void roundTrip(final TFile outer) throws IOException { final TFile inner = new TFile(outer.getParentFile(), "inner" + getSuffix() + "/" + outer.getName()); // This particular sequence has been selected because of its increased // likeliness to fail in case the cache sync logic is not correct. create(inner); check(inner); inner.mv(outer); check(outer); outer.mv(inner); check(inner); inner.rm(); }
@Test public final void testStrictFileOutputStream() throws IOException { TFile file = new TFile(archive, "test.txt"); TConfig config = TConfig.push(); try { config.setLenient(false); try { assertFileOutputStream(file); fail("Creating ghost directories should not be allowed when File.isLenient() is false!"); } catch (FileNotFoundException expected) { } assertTrue(archive.mkdir()); assertFileOutputStream(file); archive.rm(); } finally { config.close(); } }