Bug 427148 - Unable to update from one version to the next,

On Windows only - can not remove eclipse.exe

When Eclipse is installed on a different volume than the users home
directory, an update of the eclipse.exe fails. This is because the exe
is locked as a running process, and cannot be 'moved' to the backup
store because it exists on a different volume. To fix this we rename the
file in-place. This moves the exe 'out-of-the-way' so it can be updated.

This only works for files named eclipse.exe or Eclipse.exe

For more information, see:
http://bugs.eclipse.org/427148
http://eclipsesource.com/blogs/2014/05/16/read-the-javadoc-then-test-your-assumptions/

Change-Id: Ic3b6a633aa0a25d84e0219934418c5aa245c666d
diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/touchpoint/natives/AllTests.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/touchpoint/natives/AllTests.java
index e66d400..077451a 100644
--- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/touchpoint/natives/AllTests.java
+++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/touchpoint/natives/AllTests.java
@@ -29,6 +29,7 @@
 		suite.addTestSuite(UnzipActionTest.class);
 		suite.addTestSuite(CopyActionTest.class);
 		suite.addTestSuite(RemoveActionTest.class);
+		suite.addTestSuite(BackupStoreTest.class);
 		return suite;
 	}
 }
diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/touchpoint/natives/BackupStoreTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/touchpoint/natives/BackupStoreTest.java
new file mode 100644
index 0000000..485ef3f
--- /dev/null
+++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/touchpoint/natives/BackupStoreTest.java
@@ -0,0 +1,162 @@
+/*******************************************************************************
+ * Copyright (c) 2014 EclipseSource and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     EclipseSource - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.equinox.p2.tests.touchpoint.natives;
+
+import java.io.*;
+import org.eclipse.equinox.internal.p2.touchpoint.natives.BackupStore;
+import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
+
+public class BackupStoreTest extends AbstractProvisioningTest {
+
+	private static final String BUPREFIX = "BackupTest";
+	private File sourceDir;
+	private File aDir;
+	private File aaDir;
+	private File aTxt;
+	private File bDir;
+	private File bTxt;
+
+	/**
+	 * Sets up directories and files under user.home
+	 * <ul><li>P2BUTEST/</li>
+	 *     <ul><li>A/</li>
+	 *         <ul><li>AA/</li>
+	 *             <ul><li>a.txt</li>
+	 *             </ul>
+	 *         </ul>
+	 *     </ul>
+	 * </ul>
+	 */
+	public void setUp() {
+		// create some test files under user.home
+		// do not want them under /tmp as it may be on its own file system (and even
+		// be an in-memory file system).
+		//
+		String userHome = System.getProperty("user.home");
+		sourceDir = new File(new File(userHome), "P2BUTEST");
+		fullyDelete(sourceDir);
+		aDir = new File(sourceDir, "A");
+		aDir.mkdirs();
+		aaDir = new File(aDir, "AA");
+		aaDir.mkdir();
+		aTxt = new File(aaDir, "eclipse.exe");
+		bDir = new File(sourceDir, "B");
+		bTxt = new File(bDir, "b.txt");
+		try {
+			writeToFile(aTxt, "A\nA file with an A");
+		} catch (IOException e) {
+			fail();
+		}
+	}
+
+	private void writeToFile(File file, String content) throws IOException {
+		file.getParentFile().mkdirs();
+		file.createNewFile();
+		Writer writer = new BufferedWriter(new FileWriter(file));
+		try {
+			writer.write(content);
+		} finally {
+			writer.close();
+		}
+	}
+
+	public void tearDown() {
+		fullyDelete(sourceDir);
+	}
+
+	/**
+	 * Deletes a file, or a directory with all of it's children.
+	 * @param file the file or directory to fully delete
+	 * @return true if, and only if the file is deleted
+	 */
+	private boolean fullyDelete(File file) {
+		if (!file.exists())
+			return true;
+		if (file.isDirectory()) {
+			File[] children = file.listFiles();
+			for (int i = 0; i < children.length; i++)
+				if (!fullyDelete(new File(file, children[i].getName())))
+					return false;
+		}
+		return file.delete();
+	}
+
+	public void testBackupByRenamingFile() {
+		String filePath = aTxt.getAbsolutePath();
+		new BackupStore(null, BUPREFIX) {
+			@Override
+			public void renameInPlace(File file) {
+				super.renameInPlace(file);
+			}
+
+			@Override
+			protected String getTimeStamp() {
+				return "-123";
+			}
+		}.renameInPlace(aTxt);
+
+		assertFalse(aTxt.exists());
+		assertTrue(new File(filePath + "-123.p2bu").exists());
+	}
+
+	public void testRenameIfMoveToBackupFails() throws IOException {
+		String filePath = aTxt.getAbsolutePath();
+		new BackupStore(null, BUPREFIX) {
+			@Override
+			public void renameInPlace(File file) {
+				super.renameInPlace(file);
+			}
+
+			@Override
+			public boolean moveToBackupStore(File a, File b) {
+				return false;
+			}
+
+			@Override
+			public void moveToBackup(File a, File b) throws IOException {
+				super.moveToBackup(a, b);
+			}
+
+			@Override
+			protected String getTimeStamp() {
+				return "-123";
+			}
+		}.moveToBackup(aTxt, bTxt);
+
+		assertFalse(aTxt.exists());
+		assertTrue(new File(filePath + "-123.p2bu").exists());
+		assertFalse(bTxt.exists());
+	}
+
+	public void testDoNotRenameIfMoveToBackupWorks() throws IOException {
+		String filePath = aTxt.getAbsolutePath();
+		new BackupStore(null, BUPREFIX) {
+			@Override
+			public void renameInPlace(File file) {
+				super.renameInPlace(file);
+			}
+
+			@Override
+			public boolean moveToBackupStore(File a, File b) {
+				return super.moveToBackupStore(a, b);
+			}
+
+			@Override
+			public void moveToBackup(File a, File b) throws IOException {
+				super.moveToBackup(a, b);
+			}
+		}.moveToBackup(aTxt, bTxt);
+
+		assertFalse(aTxt.exists());
+		assertFalse(new File(filePath + ".p2bu").exists());
+		assertTrue(bTxt.exists());
+	}
+}
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 0125ce0..5e452bc 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
@@ -15,6 +15,7 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import java.util.Map.Entry;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
@@ -92,6 +93,8 @@
  */
 public class BackupStore implements IBackupStore {
 
+	private static final String BACKUP_FILE_EXTENSION = ".p2bu"; //$NON-NLS-1$
+
 	/**
 	 * The name to use for a directory that represents leading separator (i.e. "/" or "\").
 	 */
@@ -137,6 +140,8 @@
 	 */
 	private boolean closed;
 
+	private Map<String, String> renamedInPlace = new HashMap<String, String>();
+
 	/**
 	 * Generates a BackupStore with a default prefix of ".p2bu" for backup directory and
 	 * probe file. 
@@ -144,7 +149,7 @@
 	 * - see {@link #genUnique()} for more info.
 	 */
 	public BackupStore() {
-		this(null, ".p2bu"); //$NON-NLS-1$
+		this(null, BACKUP_FILE_EXTENSION);
 	}
 
 	/**
@@ -253,12 +258,12 @@
 	 * @param buFile destination backup file to move to; should not exist and must be a directory
 	 * @throws IOException if the backup operation fails
 	 */
-	private void moveToBackup(File file, File buFile) throws IOException {
+	protected void moveToBackup(File file, File buFile) throws IOException {
 		// make sure all of the directories exist / gets created
 		buFile.getParentFile().mkdirs();
 		if (buFile.getParentFile().exists() && !buFile.getParentFile().isDirectory())
 			throw new IllegalArgumentException(NLS.bind(Messages.BackupStore_file_directory_mismatch, buFile.getParentFile().getAbsolutePath()));
-		if (file.renameTo(buFile)) {
+		if (moveToBackupStore(file, buFile)) {
 			backupCounter++;
 			return;
 		}
@@ -266,14 +271,46 @@
 		// that source is locked "in use" on a windows machine. The copy will work across volumes,
 		// but the locked file will fail on the subsequent delete.
 		//
-		Util.copyStream(new FileInputStream(file), true, new FileOutputStream(buFile), true);
-		backupCounter++;
-
-		// need to remove the backed up file
-		if (!file.delete())
+		// Rename in place
+		if (isEclipseExe(file))
+			renameInPlace(file);
+		else {
+			Util.copyStream(new FileInputStream(file), true, new FileOutputStream(buFile), true);
+			backupCounter++;
+		}
+		if (file.exists() && !file.delete())
 			throw new IOException(NLS.bind(Messages.BackupStore_can_not_delete_after_copy_0, file));
 	}
 
+	private boolean isEclipseExe(File file) {
+		return file.getName().equals("eclipse.exe") || file.getName().equals("Eclipse.exe"); //$NON-NLS-1$ //$NON-NLS-2$
+	}
+
+	protected boolean moveToBackupStore(File file, File buFile) {
+		if (file.renameTo(buFile)) {
+			// if the original file still exists, we have a problem.
+			if (file.exists()) {
+				// If the renamed work, but the file still exists, remove the backup
+				// and return false
+				if (buFile.exists())
+					buFile.delete();
+			} else {
+				return true;
+			}
+		}
+		return false;
+	}
+
+	protected void renameInPlace(File file) {
+		String newName = file.getAbsolutePath() + getTimeStamp() + BACKUP_FILE_EXTENSION;
+		renamedInPlace.put(file.getAbsolutePath(), newName);
+		file.renameTo(new File(newName));
+	}
+
+	protected String getTimeStamp() {
+		return "-" + new Date().getTime(); //$NON-NLS-1$
+	}
+
 	private File getBackupFile(File file) {
 		File buRoot = backupRoot;
 		File buDir = new File(buRoot, backupName);
@@ -647,6 +684,15 @@
 			// then perform a recursive restore
 			restore(target, bu, unrestorable);
 		}
+		restoreRenamedFiles(unrestorable);
+	}
+
+	private void restoreRenamedFiles(Set<File> unrestorable) {
+		for (Entry<String, String> entry : renamedInPlace.entrySet()) {
+			File bu = new File(entry.getValue());
+			if (!bu.renameTo(new File(entry.getKey())))
+				unrestorable.add(bu);
+		}
 	}
 
 	private static long msCounter = 0;