Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up delete dir function #3910

Merged
merged 2 commits into from Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 25 additions & 27 deletions app/src/processing/app/Util.java
Expand Up @@ -301,42 +301,40 @@ static public void copyDirNative(File sourceDir,

/**
* Remove all files in a directory and the directory itself.
* Prints error messages with failed filenames.
*/
static public void removeDir(File dir) {
if (dir.exists()) {
removeDescendants(dir);
if (!dir.delete()) {
System.err.println("Could not delete " + dir);
}
}
static public boolean removeDir(File dir) {
return removeDir(dir, true);
}


/**
* Recursively remove all files within a directory,
* used with removeDir(), or when the contents of a dir
* should be removed, but not the directory itself.
* (i.e. when cleaning temp files from lib/build)
* Remove all files in a directory and the directory itself.
* Optinally prints error messages with failed filenames.
*/
static public void removeDescendants(File dir) {
if (!dir.exists()) return;

String files[] = dir.list();
for (int i = 0; i < files.length; i++) {
if (files[i].equals(".") || files[i].equals("..")) continue;
File dead = new File(dir, files[i]);
if (!dead.isDirectory()) {
if (!Preferences.getBoolean("compiler.save_build_files")) {
if (!dead.delete()) {
// temporarily disabled
System.err.println("Could not delete " + dead);
static public boolean removeDir(File dir, boolean printErrorMessages) {
if (!dir.exists()) return true;

boolean result = true;
File[] files = dir.listFiles();
if (files != null) {
for (File child : files) {
if (child.isFile()) {
boolean deleted = child.delete();
if (!deleted && printErrorMessages) {
System.err.println("Could not delete " + child.getAbsolutePath());
}
result &= deleted;
} else if (child.isDirectory()) {
result &= removeDir(child, printErrorMessages);
}
} else {
removeDir(dead);
//dead.delete();
}
}
boolean deleted = dir.delete();
if (!deleted && printErrorMessages) {
System.err.println("Could not delete " + dir.getAbsolutePath());
}
result &= deleted;
return result;
}


Expand Down
4 changes: 2 additions & 2 deletions app/src/processing/app/contrib/AvailableContribution.java
Expand Up @@ -196,7 +196,7 @@ else if (newContrib.getType() == ContributionType.TOOL) {
}

// 4. Okay, now actually delete that temp folder
Util.removeDir(newContribFolder);
Util.removeDir(newContribFolder, false);

} else {
if (status != null) {
Expand All @@ -207,7 +207,7 @@ else if (newContrib.getType() == ContributionType.TOOL) {

// Remove any remaining boogers
if (tempFolder.exists()) {
Util.removeDir(tempFolder);
Util.removeDir(tempFolder, false);
}
return installedContrib;
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/processing/app/contrib/LocalContribution.java
Expand Up @@ -449,8 +449,7 @@ void remove(final Base base,
if (doBackup) {
success = backup(true, status);
} else {
Util.removeDir(getFolder());
success = !getFolder().exists();
success = Util.removeDir(getFolder(), false);
}

if (success) {
Expand Down