Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 220647 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.xml (+22 lines)
Lines 75-78 Link Here
75
          version="1.0.0">
75
          version="1.0.0">
76
    </action>
76
    </action>
77
 </extension>
77
 </extension>
78
 <extension
79
       point="org.eclipse.equinox.p2.engine.actions">
80
    <action
81
          class="org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CopyAction"
82
          description="copy(source,target[,overwrite])"
83
          name="copy"
84
          touchpointType="org.eclipse.equinox.p2.native"
85
          touchpointVersion="1.0.0"
86
          version="1.0.0">
87
    </action>
88
 </extension>
89
 <extension
90
       point="org.eclipse.equinox.p2.engine.actions">
91
    <action
92
          class="org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CleanupcopyAction"
93
          description="cleanupcopy(source,target)"
94
          name="cleanupcopy"
95
          touchpointType="org.eclipse.equinox.p2.native"
96
          touchpointVersion="1.0.0"
97
          version="1.0.0">
98
    </action>
99
 </extension>
78
</plugin>
100
</plugin>
(-)src/org/eclipse/equinox/internal/p2/touchpoint/natives/actions/ActionConstants.java (+3 lines)
Lines 17-20 Link Here
17
	public static final String PARM_LINK_NAME = "linkName"; //$NON-NLS-1$
17
	public static final String PARM_LINK_NAME = "linkName"; //$NON-NLS-1$
18
	public static final String PARM_LINK_TARGET = "linkTarget"; //$NON-NLS-1$
18
	public static final String PARM_LINK_TARGET = "linkTarget"; //$NON-NLS-1$
19
	public static final String PARM_LINK_FORCE = "force"; //$NON-NLS-1$
19
	public static final String PARM_LINK_FORCE = "force"; //$NON-NLS-1$
20
	public static final String PARM_COPY_TARGET = "target"; //$NON-NLS-1$
21
	public static final String PARM_COPY_SOURCE = "source"; //$NON-NLS-1$
22
	public static final String PARM_COPY_OVERWRITE = "overwrite"; //$NON-NLS-1$
20
}
23
}
(-)src/org/eclipse/equinox/internal/p2/touchpoint/natives/Messages.java (+1 lines)
Lines 27-31 Link Here
27
	public static String unzipping;
27
	public static String unzipping;
28
	public static String restoring;
28
	public static String restoring;
29
	public static String param_not_set;
29
	public static String param_not_set;
30
	public static String copy_failed;
30
31
31
}
32
}
(-)src/org/eclipse/equinox/internal/p2/touchpoint/natives/messages.properties (+1 lines)
Lines 16-20 Link Here
16
artifact_repo_not_found=The artifact repository manager could not be found.
16
artifact_repo_not_found=The artifact repository manager could not be found.
17
could_not_obtain_download_cache=Could not obtain the download cache location.
17
could_not_obtain_download_cache=Could not obtain the download cache location.
18
download_cache_not_writeable=Agent download cache not writeable: {0}.
18
download_cache_not_writeable=Agent download cache not writeable: {0}.
19
copy_failed=I/O Error while copying {0} - see details.
19
20
20
21
(-)src/org/eclipse/equinox/internal/p2/touchpoint/natives/actions/CopyAction.java (+153 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Cloudsmith Inc. and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Cloudsmith Inc. - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.internal.p2.touchpoint.natives.actions;
12
13
import java.io.*;
14
import java.util.ArrayList;
15
import java.util.Map;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.equinox.internal.p2.engine.Profile;
19
import org.eclipse.equinox.internal.p2.touchpoint.natives.Activator;
20
import org.eclipse.equinox.internal.p2.touchpoint.natives.Messages;
21
import org.eclipse.equinox.internal.provisional.p2.engine.ProvisioningAction;
22
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
23
import org.eclipse.osgi.util.NLS;
24
25
/**
26
 * Copies from PARM_COPY_SOURCE to PARAM_COPY_TARGET
27
 * The optional parameter PARAM_COPY_OVERWRITE overwrites and existing file if set to true, else
28
 * and existing file with the same name is an error. The default is false.
29
 * If the source is a directory, a merge copy to the target is performed.
30
 * Copy will copy files and directories (recursively).
31
 *  
32
 * @author henrik.lindberg@cloudsmith.com
33
 */
34
public class CopyAction extends ProvisioningAction {
35
	public static final String ID = "cp"; //$NON-NLS-1$
36
37
	public IStatus execute(Map parameters) {
38
		return copy(parameters);
39
	}
40
41
	public static IStatus copy(Map parameters) {
42
		String target = (String) parameters.get(ActionConstants.PARM_COPY_TARGET);
43
		if (target == null)
44
			return new Status(IStatus.ERROR, Activator.ID, IStatus.OK, NLS.bind(Messages.param_not_set, ActionConstants.PARM_COPY_TARGET, ID), null);
45
46
		String source = (String) parameters.get(ActionConstants.PARM_COPY_SOURCE);
47
		if (source == null)
48
			return new Status(IStatus.ERROR, Activator.ID, IStatus.OK, NLS.bind(Messages.param_not_set, ActionConstants.PARM_COPY_SOURCE, ID), null);
49
50
		String overwrite = (String) parameters.get(ActionConstants.PARM_COPY_OVERWRITE);
51
		Profile profile = (Profile) parameters.get(ActionConstants.PARM_PROFILE);
52
		IInstallableUnit iu = (IInstallableUnit) parameters.get(ActionConstants.PARM_IU);
53
54
		File sourceFile = new File(source);
55
		File targetFile = new File(target);
56
		File[] copiedFiles = null;
57
		try {
58
			copiedFiles = mergeCopy(sourceFile, targetFile, Boolean.valueOf(overwrite).booleanValue());
59
		} catch (IOException e) {
60
			return new Status(IStatus.ERROR, Activator.ID, IStatus.OK, NLS.bind(Messages.copy_failed, sourceFile.getPath()), e);
61
		}
62
		// keep copied file in the profile as memento for CleanupCopy
63
		StringBuffer copiedFileNameBuffer = new StringBuffer();
64
		for (int i = 0; i < copiedFiles.length; i++)
65
			copiedFileNameBuffer.append(copiedFiles[i].getAbsolutePath()).append(ActionConstants.PIPE);
66
67
		profile.setInstallableUnitProperty(iu, "copied" + ActionConstants.PIPE + source + ActionConstants.PIPE + target, copiedFileNameBuffer.toString()); //$NON-NLS-1$
68
69
		return Status.OK_STATUS;
70
	}
71
72
	public IStatus undo(Map parameters) {
73
		return CleanupcopyAction.cleanupcopy(parameters);
74
	}
75
76
	/**
77
	 * Merge-copy file or directory.
78
	 * @param source
79
	 * @param target
80
	 * @param overwrite
81
	 * @throws IOException
82
	 */
83
	private static File[] mergeCopy(File source, File target, boolean overwrite) throws IOException {
84
		ArrayList copiedFiles = new ArrayList();
85
		xcopy(copiedFiles, source, target, overwrite);
86
		return (File[]) copiedFiles.toArray(new File[copiedFiles.size()]);
87
	}
88
89
	/**
90
	 * Merge-copy file or directory.
91
	 * @param copiedFiles - ArrayList where copied files are collected
92
	 * @param source
93
	 * @param target
94
	 * @param overwrite
95
	 * @throws IOException
96
	 */
97
	private static void xcopy(ArrayList copiedFiles, File source, File target, boolean overwrite) throws IOException {
98
		if (!source.exists())
99
			throw new IOException("Source: " + source + "does not exists"); //$NON-NLS-1$//$NON-NLS-2$
100
101
		if (source.isDirectory()) {
102
			if (target.exists() && target.isFile()) {
103
				if (!overwrite)
104
					throw new IOException("Target: " + target + " already exists"); //$NON-NLS-1$//$NON-NLS-2$
105
				target.delete();
106
			}
107
			if (!target.exists())
108
				target.mkdirs();
109
			copiedFiles.add(target);
110
			File[] children = source.listFiles();
111
			for (int i = 0; i < children.length; i++)
112
				xcopy(copiedFiles, children[i], new File(target, children[i].getName()), overwrite);
113
			return;
114
		} else if (target.exists() && !overwrite)
115
			throw new IOException("Target: " + target + " already exists"); //$NON-NLS-1$//$NON-NLS-2$
116
117
		InputStream input = null;
118
		OutputStream output = null;
119
		try {
120
			input = new BufferedInputStream(new FileInputStream(source));
121
			output = new BufferedOutputStream(new FileOutputStream(target));
122
123
			byte[] buffer = new byte[8192];
124
			int bytesRead = 0;
125
			while ((bytesRead = input.read(buffer)) != -1)
126
				output.write(buffer, 0, bytesRead);
127
		} catch (IOException e) {
128
			// get the original IOException to the log
129
			e.printStackTrace();
130
			throw new IOException("Error while copying:" + source.getAbsolutePath()); //$NON-NLS-1$
131
		} finally {
132
			if (input != null) {
133
				try {
134
					input.close();
135
				} catch (IOException e) {
136
					// Don't stop because of this - but log it
137
					System.err.println("Exception while trying to close input stream on: " + source.getAbsolutePath()); //$NON-NLS-1$
138
					e.printStackTrace();
139
				}
140
			}
141
			if (output != null) {
142
				try {
143
					output.close();
144
				} catch (IOException e) {
145
					// Don't stop because of this - but log it
146
					System.err.println("Exception while trying to close output stream on: " + target.getAbsolutePath()); //$NON-NLS-1$
147
					e.printStackTrace();
148
				}
149
			}
150
		}
151
		copiedFiles.add(target);
152
	}
153
}
(-)src/org/eclipse/equinox/internal/p2/touchpoint/natives/actions/CleanupcopyAction.java (+74 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.internal.p2.touchpoint.natives.actions;
12
13
import java.io.File;
14
import java.util.*;
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.core.runtime.Status;
17
import org.eclipse.equinox.internal.p2.touchpoint.natives.Messages;
18
import org.eclipse.equinox.internal.p2.touchpoint.natives.Util;
19
import org.eclipse.equinox.internal.provisional.p2.engine.IProfile;
20
import org.eclipse.equinox.internal.provisional.p2.engine.ProvisioningAction;
21
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
22
import org.eclipse.osgi.util.NLS;
23
24
public class CleanupcopyAction extends ProvisioningAction {
25
26
	public static final String ACTION_CLEANUPCOPY = "cleanupcopy"; //$NON-NLS-1$
27
28
	public IStatus execute(Map parameters) {
29
		return cleanupcopy(parameters);
30
	}
31
32
	public IStatus undo(Map parameters) {
33
		return CopyAction.copy(parameters);
34
	}
35
36
	public static IStatus cleanupcopy(Map parameters) {
37
		String source = (String) parameters.get(ActionConstants.PARM_SOURCE);
38
		if (source == null)
39
			return Util.createError(NLS.bind(Messages.param_not_set, ActionConstants.PARM_SOURCE, ACTION_CLEANUPCOPY));
40
		String target = (String) parameters.get(ActionConstants.PARM_TARGET);
41
		if (target == null)
42
			return Util.createError(NLS.bind(Messages.param_not_set, ActionConstants.PARM_TARGET, ACTION_CLEANUPCOPY));
43
44
		IInstallableUnit iu = (IInstallableUnit) parameters.get(ActionConstants.PARM_IU);
45
		IProfile profile = (IProfile) parameters.get(ActionConstants.PARM_PROFILE);
46
47
		String copied = profile.getInstallableUnitProperty(iu, "copied" + ActionConstants.PIPE + source + ActionConstants.PIPE + target); //$NON-NLS-1$
48
49
		if (copied == null)
50
			return Status.OK_STATUS;
51
52
		StringTokenizer tokenizer = new StringTokenizer(copied, ActionConstants.PIPE);
53
		List directories = new ArrayList();
54
		while (tokenizer.hasMoreTokens()) {
55
			String fileName = tokenizer.nextToken();
56
			File file = new File(fileName);
57
			if (!file.exists())
58
				continue;
59
60
			if (file.isDirectory())
61
				directories.add(file);
62
			else
63
				file.delete();
64
		}
65
66
		for (Iterator it = directories.iterator(); it.hasNext();) {
67
			File directory = (File) it.next();
68
			directory.delete();
69
		}
70
71
		return Status.OK_STATUS;
72
	}
73
74
}

Return to bug 220647