|
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 |
} |