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 (+175 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.*;
20
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IFileArtifactRepository;
21
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
22
import org.eclipse.equinox.internal.provisional.p2.engine.ProvisioningAction;
23
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
24
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
25
import org.eclipse.osgi.util.NLS;
26
27
/**
28
 * Copies from PARM_COPY_SOURCE to PARAM_COPY_TARGET
29
 * The optional parameter PARAM_COPY_OVERWRITE overwrites and existing file if set to true, else
30
 * and existing file with the same name is an error. The default is false.
31
 * If the source is a directory, a merge copy to the target is performed.
32
 * Copy will copy files and directories (recursively).
33
 *  
34
 * @author henrik.lindberg@cloudsmith.com
35
 */
36
public class CopyAction extends ProvisioningAction {
37
	public static final String ID = "cp"; //$NON-NLS-1$
38
39
	public IStatus execute(Map parameters) {
40
		return copy(parameters);
41
	}
42
43
	public static IStatus copy(Map parameters) {
44
		String target = (String) parameters.get(ActionConstants.PARM_COPY_TARGET);
45
		if (target == null)
46
			return new Status(IStatus.ERROR, Activator.ID, IStatus.OK, NLS.bind(Messages.param_not_set, ActionConstants.PARM_COPY_TARGET, ID), null);
47
48
		String source = (String) parameters.get(ActionConstants.PARM_COPY_SOURCE);
49
		if (source == null)
50
			return new Status(IStatus.ERROR, Activator.ID, IStatus.OK, NLS.bind(Messages.param_not_set, ActionConstants.PARM_COPY_SOURCE, ID), null);
51
52
		String overwrite = (String) parameters.get(ActionConstants.PARM_COPY_OVERWRITE);
53
		Profile profile = (Profile) parameters.get(ActionConstants.PARM_PROFILE);
54
		IInstallableUnit iu = (IInstallableUnit) parameters.get(ActionConstants.PARM_IU);
55
56
		String originalSource = source;
57
		if (source.equals(ActionConstants.PARM_ARTIFACT)) {
58
			//TODO: fix wherever this occurs -- investigate as this is probably not desired
59
			if (iu.getArtifacts() == null || iu.getArtifacts().length == 0)
60
				return Status.OK_STATUS;
61
62
			IArtifactKey artifactKey = iu.getArtifacts()[0];
63
64
			IFileArtifactRepository downloadCache;
65
			try {
66
				downloadCache = Util.getDownloadCacheRepo();
67
			} catch (ProvisionException e) {
68
				return e.getStatus();
69
			}
70
			File fileLocation = downloadCache.getArtifactFile(artifactKey);
71
			if ((fileLocation == null) || !fileLocation.exists())
72
				return Util.createError(NLS.bind(Messages.artifact_not_available, artifactKey));
73
			source = fileLocation.getAbsolutePath();
74
		}
75
76
		File sourceFile = new File(source);
77
		File targetFile = new File(target);
78
		File[] copiedFiles = null;
79
		try {
80
			copiedFiles = mergeCopy(sourceFile, targetFile, Boolean.valueOf(overwrite).booleanValue());
81
		} catch (IOException e) {
82
			return new Status(IStatus.ERROR, Activator.ID, IStatus.OK, NLS.bind(Messages.copy_failed, sourceFile.getPath()), e);
83
		}
84
		// keep copied file in the profile as memento for CleanupCopy
85
		StringBuffer copiedFileNameBuffer = new StringBuffer();
86
		for (int i = 0; i < copiedFiles.length; i++)
87
			copiedFileNameBuffer.append(copiedFiles[i].getAbsolutePath()).append(ActionConstants.PIPE);
88
89
		profile.setInstallableUnitProperty(iu, "copied" + ActionConstants.PIPE + originalSource + ActionConstants.PIPE + target, copiedFileNameBuffer.toString()); //$NON-NLS-1$
90
91
		return Status.OK_STATUS;
92
	}
93
94
	public IStatus undo(Map parameters) {
95
		return CleanupcopyAction.cleanupcopy(parameters);
96
	}
97
98
	/**
99
	 * Merge-copy file or directory.
100
	 * @param source
101
	 * @param target
102
	 * @param overwrite
103
	 * @throws IOException
104
	 */
105
	private static File[] mergeCopy(File source, File target, boolean overwrite) throws IOException {
106
		ArrayList copiedFiles = new ArrayList();
107
		xcopy(copiedFiles, source, target, overwrite);
108
		return (File[]) copiedFiles.toArray(new File[copiedFiles.size()]);
109
	}
110
111
	/**
112
	 * Merge-copy file or directory.
113
	 * @param copiedFiles - ArrayList where copied files are collected
114
	 * @param source
115
	 * @param target
116
	 * @param overwrite
117
	 * @throws IOException
118
	 */
119
	private static void xcopy(ArrayList copiedFiles, File source, File target, boolean overwrite) throws IOException {
120
		if (!source.exists())
121
			throw new IOException("Source: " + source + "does not exists"); //$NON-NLS-1$//$NON-NLS-2$
122
123
		if (source.isDirectory()) {
124
			if (target.exists() && target.isFile()) {
125
				if (!overwrite)
126
					throw new IOException("Target: " + target + " already exists"); //$NON-NLS-1$//$NON-NLS-2$
127
				target.delete();
128
			}
129
			if (!target.exists())
130
				target.mkdirs();
131
			copiedFiles.add(target);
132
			File[] children = source.listFiles();
133
			for (int i = 0; i < children.length; i++)
134
				xcopy(copiedFiles, children[i], new File(target, children[i].getName()), overwrite);
135
			return;
136
		} else if (target.exists() && !overwrite)
137
			throw new IOException("Target: " + target + " already exists"); //$NON-NLS-1$//$NON-NLS-2$
138
139
		InputStream input = null;
140
		OutputStream output = null;
141
		try {
142
			input = new BufferedInputStream(new FileInputStream(source));
143
			output = new BufferedOutputStream(new FileOutputStream(target));
144
145
			byte[] buffer = new byte[8192];
146
			int bytesRead = 0;
147
			while ((bytesRead = input.read(buffer)) != -1)
148
				output.write(buffer, 0, bytesRead);
149
		} catch (IOException e) {
150
			// get the original IOException to the log
151
			e.printStackTrace();
152
			throw new IOException("Error while copying:" + source.getAbsolutePath()); //$NON-NLS-1$
153
		} finally {
154
			if (input != null) {
155
				try {
156
					input.close();
157
				} catch (IOException e) {
158
					// Don't stop because of this - but log it
159
					System.err.println("Exception while trying to close input stream on: " + source.getAbsolutePath()); //$NON-NLS-1$
160
					e.printStackTrace();
161
				}
162
			}
163
			if (output != null) {
164
				try {
165
					output.close();
166
				} catch (IOException e) {
167
					// Don't stop because of this - but log it
168
					System.err.println("Exception while trying to close output stream on: " + target.getAbsolutePath()); //$NON-NLS-1$
169
					e.printStackTrace();
170
				}
171
			}
172
		}
173
		copiedFiles.add(target);
174
	}
175
}
(-)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