Bug 537757 - Always attempt to delete old file when moving to backup
Since File.exist() is not a reliable check it is performed not as a
guard against deleting a non-existent file, but as confirmation that the
deletion indeed failed because the file was not there.
Change-Id: I77233458892083b3049c323cb004d97e195f475a
Signed-off-by: Todor Boev <rinsvind@gmail.com>
diff --git a/bundles/org.eclipse.equinox.p2.touchpoint.natives/src/org/eclipse/equinox/internal/p2/touchpoint/natives/BackupStore.java b/bundles/org.eclipse.equinox.p2.touchpoint.natives/src/org/eclipse/equinox/internal/p2/touchpoint/natives/BackupStore.java
index fc877d9..a02815c 100644
--- a/bundles/org.eclipse.equinox.p2.touchpoint.natives/src/org/eclipse/equinox/internal/p2/touchpoint/natives/BackupStore.java
+++ b/bundles/org.eclipse.equinox.p2.touchpoint.natives/src/org/eclipse/equinox/internal/p2/touchpoint/natives/BackupStore.java
@@ -113,12 +113,12 @@
/**
* The name of the backup directory (no path - relative to the backup root).
*/
- private String backupName;
+ private final String backupName;
/**
* The name of a dummy file used to backup empty directories
*/
- private String dummyName;
+ private final String dummyName;
/**
* A server socket that is used to obtain a port (a shared resource on this machine)
@@ -143,7 +143,7 @@
*/
private boolean closed;
- private Map<String, String> renamedInPlace = new HashMap<>();
+ private final Map<String, String> renamedInPlace = new HashMap<>();
/**
* Generates a BackupStore with a default prefix of ".p2bu" for backup directory and
@@ -284,7 +284,9 @@
Util.copyStream(new FileInputStream(file), true, new FileOutputStream(buFile), true);
backupCounter++;
}
- if (file.exists() && !file.delete())
+
+ // File.exists() is not reliable so always attempt to delete first and check why it may have failed second.
+ if (!file.delete() && file.exists())
throw new IOException(NLS.bind(Messages.BackupStore_can_not_delete_after_copy_0, file));
}