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