/** * Accommodate Windows bug encountered in both Sun and IBM JDKs. * Others possible. If the delete does not work, call System.gc(), * wait a little and try again. * * @param f File * @return whether deletion was successful * @since Ant 1.8.0 */ public boolean tryHardToDelete(File f) { return tryHardToDelete(f, ON_WINDOWS); }
/** * Accommodate Windows bug encountered in both Sun and IBM JDKs. * Others possible. If the delete does not work, call System.gc(), * wait a little and try again. */ private boolean delete(File f) { if (!FILE_UTILS.tryHardToDelete(f, performGc)) { if (deleteOnExit) { int level = quiet ? Project.MSG_VERBOSE : Project.MSG_INFO; log("Failed to delete " + f + ", calling deleteOnExit." + " This attempts to delete the file when the Ant jvm" + " has exited and might not succeed.", level); f.deleteOnExit(); return true; } return false; } return true; }
private void writeFile() throws BuildException { // Write to RAM first, as an OOME could otherwise produce a truncated file: ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { properties.store(baos, comment); } catch (IOException x) { // should not happen throw new BuildException(x, getLocation()); } try { try (OutputStream os = Files.newOutputStream(propertyfile.toPath())) { os.write(baos.toByteArray()); } catch (IOException x) { // possibly corrupt FileUtils.getFileUtils().tryHardToDelete(propertyfile); throw x; } } catch (IOException x) { // opening, writing, or closing throw new BuildException(x, getLocation()); } }
/** * Go and delete the directory tree. * @param d the directory to delete * @param deleteFiles whether to delete files */ protected void deleteDir(File d, boolean deleteFiles) { String[] list = d.list(); if (list == null) { return; } // on an io error list() can return null for (String s : list) { File f = new File(d, s); if (f.isDirectory()) { deleteDir(f); } else if (deleteFiles && !getFileUtils().tryHardToDelete(f, performGc)) { throw new BuildException("Unable to delete file %s", f.getAbsolutePath()); } else { throw new BuildException( "UNEXPECTED ERROR - The file %s should not exist!", f.getAbsolutePath()); } } log("Deleting directory " + d.getAbsolutePath(), verbosity); if (!getFileUtils().tryHardToDelete(d, performGc)) { throw new BuildException("Unable to delete directory %s", d.getAbsolutePath()); } }
return; if (to.exists() && !(areSame(from, to) || tryHardToDelete(to))) { throw new IOException("Failed to delete " + to + " while trying to rename " + from); if (!tryHardToDelete(from)) { throw new IOException("Failed to delete " + from + " while trying to rename it.");
/** * Try to move the file via a rename, but if this fails or filtering * is enabled, copy the file then delete the sourceFile. */ private void moveFile(File fromFile, File toFile, boolean filtering, boolean overwrite) { boolean moved = false; try { log("Attempting to rename: " + fromFile + " to " + toFile, verbosity); moved = renameFile(fromFile, toFile, filtering, forceOverwrite); } catch (IOException ioe) { throw new BuildException("Failed to rename " + fromFile + " to " + toFile + " due to " + ioe.getMessage(), ioe, getLocation()); } if (!moved) { copyFile(fromFile, toFile, filtering, overwrite); if (!getFileUtils().tryHardToDelete(fromFile, performGc)) { throw new BuildException("Unable to delete file %s", fromFile.getAbsolutePath()); } } }
"can't replace read-only destination file %s", destFile)); if (!getFileUtils().tryHardToDelete(destFile)) { throw new IOException(String.format( "failed to delete read-only destination file %s", && !getFileUtils().tryHardToDelete(destFile, performGc)) { throw new BuildException("Unable to remove existing file %s", destFile);
} finally { if (tmpFile != null && tmpFile.exists()) { FILE_UTILS.tryHardToDelete(tmpFile);
throw new ReadOnlyTargetFileException(destFile); if (!FILE_UTILS.tryHardToDelete(destFile)) { throw new IOException( "failed to delete read-only destination file " + destFile);
public static void writeToFile(Properties properties, File file, String comment) throws IOException { // write to memory first to prevent other side-effects ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { properties.store(baos, comment); } finally { baos.close(); } OutputStream os = new FileOutputStream(file); try { os.write(baos.toByteArray()); } catch (IOException ex) { FileUtils.getFileUtils().tryHardToDelete(file); throw ex; } finally { os.close(); } }
/** * Creates a copy of the target archive in update or recreate mode * because some entries may later be read and archived from it. */ private File maybeCopyTarget() { File copyOfDest = null; try { if (!Mode.FORCE_CREATE.equals(getMode().getValue()) && !Mode.CREATE.equals(getMode().getValue())) { copyOfDest = FILE_UTILS.createTempFile(getTaskName(), ".tmp", null, true, false); ResourceUtils.copyResource(getDest(), new FileResource(copyOfDest)); } } catch (IOException ioex) { if (copyOfDest != null && copyOfDest.exists()) { FILE_UTILS.tryHardToDelete(copyOfDest); } throw new BuildException("Failed to copy target archive", ioex); } return copyOfDest; }
FILE_UTILS.tryHardToDelete(copyOfDest);
} finally { try { FILE_UTILS.tryHardToDelete(casesFile); } catch (final Exception e) { log(e.toString(), Project.MSG_ERR);
outstream.close(); } catch (final IOException e) { FILE_UTILS.tryHardToDelete(propsFile); throw new BuildException("Error creating temporary properties " + "file.", e, getLocation()); FileUtils.close(br); if (vmWatcher.exists()) { FILE_UTILS.tryHardToDelete(vmWatcher); if (!FILE_UTILS.tryHardToDelete(propsFile)) { String msg = "Could not delete temporary properties file '" + propsFile.getAbsolutePath() + "'.";