Skip to content

Fix data loss when adding an existing file to sketch #5786

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

Merged
merged 1 commit into from
Mar 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 44 additions & 41 deletions app/src/processing/app/SketchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,61 +495,64 @@ public boolean addFile(File sourceFile) {
isData = true;
}

// check whether this file already exists
if (destFile.exists()) {
Object[] options = { tr("OK"), tr("Cancel") };
String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename);
int result = JOptionPane.showOptionDialog(editor,
prompt,
tr("Replace"),
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (result == JOptionPane.YES_OPTION) {
replacement = true;
} else {
return false;
}
}
if (!sourceFile.equals(destFile)) {
// The typical case here is adding a file from somewhere else.
// This however fails if the source and destination are equal

// If it's a replacement, delete the old file first,
// otherwise case changes will not be preserved.
// http://dev.processing.org/bugs/show_bug.cgi?id=969
if (replacement) {
boolean muchSuccess = destFile.delete();
if (!muchSuccess) {
Base.showWarning(tr("Error adding file"),
I18n.format(tr("Could not delete the existing ''{0}'' file."), filename),
null);
return false;
// check whether this file already exists
if (destFile.exists()) {
Object[] options = { tr("OK"), tr("Cancel") };
String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename);
int result = JOptionPane.showOptionDialog(editor,
prompt,
tr("Replace"),
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (result == JOptionPane.YES_OPTION) {
replacement = true;
} else {
return false;
}
}
}

// make sure they aren't the same file
if (isData && sourceFile.equals(destFile)) {
Base.showWarning(tr("You can't fool me"),
tr("This file has already been copied to the\n" +
"location from which where you're trying to add it.\n" +
"I ain't not doin nuthin'."), null);
return false;
}
// If it's a replacement, delete the old file first,
// otherwise case changes will not be preserved.
// http://dev.processing.org/bugs/show_bug.cgi?id=969
if (replacement) {
if (!destFile.delete()) {
Base.showWarning(tr("Error adding file"),
I18n.format(tr("Could not delete the existing ''{0}'' file."), filename),
null);
return false;
}
}

// in case the user is "adding" the code in an attempt
// to update the sketch's tabs
if (!sourceFile.equals(destFile)) {
// perform the copy
try {
Base.copyFile(sourceFile, destFile);

} catch (IOException e) {
Base.showWarning(tr("Error adding file"),
I18n.format(tr("Could not add ''{0}'' to the sketch."), filename),
e);
e);
return false;
}
}
else {
// If the source and destination are equal, a code file is handled
// - as a replacement, if there is a corresponding tab,
// (eg. user wants to update the file after modifying it outside the editor)
// - as an addition, otherwise.
// (eg. the user copied the file to the sketch folder and wants to edit it)
// For a data file, this is a no-op.
if (editor.findTabIndex(destFile) >= 0)
replacement = true;
}

// open/refresh the tab
if (!isData) {
int tabIndex;
if (replacement) {
Expand Down