1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-19 08:52:15 +01:00

Do not delete the source if source = destination

While the previous version could handle the case, it only did so after
deleting the destination file, therefore causing data loss.
This commit is contained in:
Petri Laarne 2017-01-01 17:36:08 +02:00 committed by Cristian Maglie
parent d3b3714802
commit 1fc0997f71

View File

@ -495,6 +495,10 @@ public class SketchController {
isData = true; isData = true;
} }
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
// check whether this file already exists // check whether this file already exists
if (destFile.exists()) { if (destFile.exists()) {
Object[] options = { tr("OK"), tr("Cancel") }; Object[] options = { tr("OK"), tr("Cancel") };
@ -518,8 +522,7 @@ public class SketchController {
// otherwise case changes will not be preserved. // otherwise case changes will not be preserved.
// http://dev.processing.org/bugs/show_bug.cgi?id=969 // http://dev.processing.org/bugs/show_bug.cgi?id=969
if (replacement) { if (replacement) {
boolean muchSuccess = destFile.delete(); if (!destFile.delete()) {
if (!muchSuccess) {
Base.showWarning(tr("Error adding file"), Base.showWarning(tr("Error adding file"),
I18n.format(tr("Could not delete the existing ''{0}'' file."), filename), I18n.format(tr("Could not delete the existing ''{0}'' file."), filename),
null); null);
@ -527,18 +530,7 @@ public class SketchController {
} }
} }
// make sure they aren't the same file // perform the copy
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;
}
// in case the user is "adding" the code in an attempt
// to update the sketch's tabs
if (!sourceFile.equals(destFile)) {
try { try {
Base.copyFile(sourceFile, destFile); Base.copyFile(sourceFile, destFile);
@ -549,7 +541,18 @@ public class SketchController {
return false; 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) { if (!isData) {
int tabIndex; int tabIndex;
if (replacement) { if (replacement) {