More changes to support CDT Build in Container

- close LogStream when Log job completes
- make changes to ContainerCommandProcess
  - add keepContainer support
- make changes to ContainerLauncher
  - add optimization in CopyVolumesFromJob so we don't copy
    sub-directories of directories we have already copied
  - save the image id to compare in case the user has reloaded
    a different version and assume we have to copy everything
    if it has changed

Change-Id: I28016cbd1673a2c2d87e1dc5894d53293d94432b
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
index 4d6febe..345373b 100644
--- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
@@ -604,6 +604,7 @@
 
 		@Override
 		public void execute() throws InterruptedException, IOException {
+			LogStream stream = null;
 			try {
 				// Add timestamps to log based on user preference
 				IEclipsePreferences preferences = InstanceScope.INSTANCE
@@ -612,7 +613,6 @@
 				boolean timestamps = preferences.getBoolean(
 						"logTimestamp", true); //$NON-NLS-1$
 
-				LogStream stream = null;
 
 				if (timestamps)
 					stream = copyClient.logs(id, LogsParam.follow(),
@@ -626,7 +626,6 @@
 				int delayTime = 100;
 
 				do {
-					outputStream.write("Sleeping".getBytes());
 					Thread.sleep(delayTime);
 					// Second time in loop and following, pause a second to
 					// allow other threads to do meaningful work
@@ -657,6 +656,8 @@
 			} finally {
 				follow = false;
 				copyClient.close(); // we are done with copyClient..dispose
+				if (stream != null)
+					stream.close();
 				if (outputStream != null)
 					outputStream.close();
 			}
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/docker/ui/launch/ContainerLauncher.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/docker/ui/launch/ContainerLauncher.java
index ae97049..192ff97 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/docker/ui/launch/ContainerLauncher.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/docker/ui/launch/ContainerLauncher.java
@@ -196,7 +196,10 @@
 			try {
 				IDockerImage dockerImage = ((DockerConnection) connection)
 						.getImageByTag(image);
-				IPath imageFilePath = target.append(".IMAGE_ID");
+				// if there is a .image_id file, check the image id to ensure
+				// the user hasn't loaded a new version which may have
+				// different header files installed.
+				IPath imageFilePath = target.append(".image_id"); //$NON-NLS-1$
 				File imageFile = imageFilePath.toFile();
 				boolean needImageIdFile = !imageFile.exists();
 				if (!needImageIdFile) {
@@ -240,9 +243,16 @@
 						monitor.worked(1);
 						continue;
 					}
-					if (dirList.contains(volume)) {
-						monitor.worked(1);
-						continue;
+					// if we have already copied the directory either directly
+					// or as part of a parent directory copy, then skip to next
+					// volume.
+					for (String path : dirList) {
+						if (volume.equals(path)
+								|| (volume.startsWith(path) && volume.charAt(
+										path.length()) == File.separatorChar)) {
+							monitor.worked(1);
+							continue;
+						}
 					}
 					try {
 						monitor.setTaskName(Messages.getFormattedString(
@@ -300,18 +310,7 @@
 						}
 						k.close();
 					} catch (final DockerException e) {
-						// Display.getDefault()
-						// .syncExec(() -> MessageDialog.openError(
-						// PlatformUI.getWorkbench()
-						// .getActiveWorkbenchWindow()
-						// .getShell(),
-						// Messages.getFormattedString(
-						// ERROR_COPYING_VOLUME,
-						// new String[] { volume, target
-						// .toPortableString() }),
-						// e.getCause() != null
-						// ? e.getCause().getMessage()
-						// : e.getMessage()));
+						// ignore
 					}
 				}
 			} catch (InterruptedException e) {
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java
index 7c5e06a..f3e018f 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/ContainerCommandProcess.java
@@ -54,7 +54,9 @@
 		try {
 			try {
 				// TODO: see if there is a better way of draining the
-				// container output before closing the streams
+				// container output before closing the streams. Note
+				// that trying to join the attachLog thread does not
+				// work.
 				Thread.sleep(1000);
 			} catch (InterruptedException e1) {
 				// ignore
@@ -106,6 +108,9 @@
 			IDockerContainerExit exit = connection
 					.waitForContainer(containerId);
 			connection.stopLoggingThread(containerId);
+			if (!keepContainer) {
+				connection.removeContainer(containerId);
+			}
 			return exit.statusCode();
 		} catch (DockerException e) {
 			return -1;