/*
* This file is part of CloudEclipse.
*
* Copyright (c) 2009 Chris Matthews
*
* CloudEclipse is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CloudEclipse is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CloudEclipse. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.eclipse.equinox.p2.virttools.touchpoint.actions;
import ca.uvic.cs.cloud.cloudwizard.BuildSettingsStore;
import java.io.File;
import java.io.IOException;
import java.security.KeyStore.Builder;
import java.util.ArrayList;
import java.util.Map;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.touchpoint.natives.Messages;
import org.eclipse.equinox.internal.p2.touchpoint.natives.Util;
import org.eclipse.equinox.internal.provisional.p2.engine.ProvisioningAction;
import org.eclipse.equinox.p2.virttools.RootExecWrapper;
import org.eclipse.osgi.util.NLS;
@SuppressWarnings("restriction")
public class MountAction extends ProvisioningAction {
public static final String ACTION_MOUNT = "mount"; //$NON-NLS-1$
public IStatus execute(Map parameters) {
BuildSettingsStore bs = null;
String buildFile = (String) parameters
.get(ActionConstants.PARM_BUILDPARAMS);
if (buildFile != null) {
bs = new BuildSettingsStore(buildFile);
}
String localRootPassword = bs.getValue(bs.YOUR_ROOT);
String optionsString = (String) parameters
.get(ActionConstants.PARM_OPTIONS);
String targetDir = (String) parameters
.get(ActionConstants.PARM_TARGET_DIR);
if (targetDir == null)
return Util.createError(NLS.bind(Messages.param_not_set,
ActionConstants.PARM_TARGET_DIR, ACTION_MOUNT));
String targetFile = (String) parameters
.get(ActionConstants.PARM_TARGET_FILE);
if (targetFile == null)
return Util.createError(NLS.bind(Messages.param_not_set,
ActionConstants.PARM_TARGET_FILE, ACTION_MOUNT));
String mountPoint = (String) parameters
.get(ActionConstants.PARM_MOUNT_MOUNTPOINT);
if (mountPoint == null)
return Util.createError(NLS.bind(Messages.param_not_set,
ActionConstants.PARM_MOUNT_MOUNTPOINT, ACTION_MOUNT));
File probe = new File(targetDir + IPath.SEPARATOR + targetFile);
if (!targetFile.equals("proc")) {
// Check that file exist
if (!probe.exists() || probe.isDirectory())
return Util.createError(NLS.bind(
Messages.action_0_failed_file_1_doesNotExist, ACTION_MOUNT,
probe.toString()));
}
// Check that dir exist
probe = new File(targetDir + IPath.SEPARATOR + mountPoint);
if (!probe.exists() || !probe.isDirectory())
return Util.createError(NLS.bind(
Messages.action_0_failed_file_1_doesNotExist, ACTION_MOUNT,
probe.toString()));
// check that there are no files in the dir
if (probe.list().length != 0) {
return Util.createError("The mountpoint directory is not empty");
}
String options[] = null;
if (optionsString != null) {
ArrayList<String> collect = new ArrayList<String>();
String r = optionsString.trim();
while (r.length() > 0) {
int spaceIdx = r.indexOf(' ');
if (spaceIdx < 0) {
collect.add(r);
r = ""; //$NON-NLS-1$
} else {
collect.add(r.substring(0, spaceIdx));
r = r.substring(spaceIdx + 1);
r = r.trim();
}
}
if (collect.size() > 0) {
options = new String[collect.size()];
collect.toArray(options);
}
}
return mount(targetDir, targetFile, mountPoint, options, localRootPassword);
}
public IStatus undo(Map parameters) {
parameters.remove(ActionConstants.PARM_OPTIONS);
return new UmountAction().execute(parameters);
}
protected IStatus mount(String targetDir, String targetFile, String targetMountPoint, String[] options, String rootPassword) {
// Note: 3 is from chmod, permissions, and target
String[] args = new String[3 + (options == null ? 0
: options.length)];
int i = 0;
args[i++] = "mount"; //$NON-NLS-1$
if (options != null) {
for (int j = 0; j < options.length; j++)
args[i++] = options[j];
}
args[i++] = targetDir + IPath.SEPARATOR + targetFile;
args[i] = targetDir + IPath.SEPARATOR + targetMountPoint;
try {
Process process = RootExecWrapper.execAsRoot(args, rootPassword);
try {
process.waitFor();
ActionConstants.readOffStream(process.getErrorStream(), true);
ActionConstants.readOffStream(process.getInputStream(), false);
} catch (InterruptedException e) {
// mark thread interrupted and continue
Thread.currentThread().interrupt();
}
if (process.exitValue() != 0) {
return Util.createError("Mouting failed with return code: " + process.exitValue());
}
} catch (IOException e) {
e.printStackTrace();
for (int j = 0; j < args.length; j++) {
System.out.println(args[j]);
}
return Util.createError("Mouting failed with IOException: " + e.getMessage());
}
return Status.OK_STATUS;
}
}