|
From: <cr...@us...> - 2008-06-04 13:20:50
|
Revision: 4192
http://jnode.svn.sourceforge.net/jnode/?rev=4192&view=rev
Author: crawley
Date: 2008-06-04 06:20:48 -0700 (Wed, 04 Jun 2008)
Log Message:
-----------
Converted NFSMountCommand, fixed bugs and added flags for forcing readonly
or readwrite mounts. (The latter was previously implied by the uid/gid args.)
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.nfs.command.xml
trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSHostNameArgument.java
trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSMountCommand.java
trunk/fs/src/fs/org/jnode/fs/nfs/nfs2/NFS2Device.java
Modified: trunk/fs/descriptors/org.jnode.fs.nfs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.nfs.command.xml 2008-06-04 11:00:35 UTC (rev 4191)
+++ trunk/fs/descriptors/org.jnode.fs.nfs.command.xml 2008-06-04 13:20:48 UTC (rev 4192)
@@ -21,9 +21,25 @@
</runtime>
<extension point="org.jnode.shell.aliases">
- <alias name="nfsmount"
- class="org.jnode.fs.nfs.command.NFSMountCommand"/>
+ <alias name="nfsmount" class="org.jnode.fs.nfs.command.NFSMountCommand"/>
</extension>
+
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="nfsmount">
+ <sequence description="mount an NFS file system">
+ <argument argLabel="nfsFileSystem"/>
+ <argument argLabel="directory"/>
+ <optionSet>
+ <option argLabel="uid" shortName="u" longName="uid"/>
+ <option argLabel="gid" shortName="g" longName="gid"/>
+ <option argLabel="tcp" longName="tcp"/>
+ <option argLabel="udp" longName="udp"/>
+ <option argLabel="readOnly" longName="ro"/>
+ <option argLabel="readWrite" longName="rw"/>
+ </optionSet>
+ </sequence>
+ </syntax>
+ </extension>
<extension point="org.jnode.security.permissions">
<permission class="java.net.SocketPermission" name="*" actions="connect,resolve"/>
Modified: trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSHostNameArgument.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSHostNameArgument.java 2008-06-04 11:00:35 UTC (rev 4191)
+++ trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSHostNameArgument.java 2008-06-04 13:20:48 UTC (rev 4192)
@@ -6,7 +6,6 @@
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
-import java.util.ArrayList;
import java.util.List;
import org.jnode.driver.console.CompletionInfo;
@@ -14,29 +13,26 @@
import org.jnode.net.nfs.nfs2.mount.ExportEntry;
import org.jnode.net.nfs.nfs2.mount.Mount1Client;
import org.jnode.net.nfs.nfs2.mount.MountException;
-import org.jnode.shell.help.Argument;
-import org.jnode.shell.help.ParsedArguments;
+import org.jnode.shell.CommandLine.Token;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.CommandSyntaxException;
-public class NFSHostNameArgument extends Argument {
+public class NFSHostNameArgument extends Argument<String> {
- public NFSHostNameArgument(String name, String description, boolean multi) {
- super(name, description, multi);
+ public NFSHostNameArgument(String name, int flags, String description) {
+ super(name, flags, new String[0], description);
}
- public NFSHostNameArgument(String name, String description) {
- super(name, description);
- }
-
public void complete(CompletionInfo completion, String partial) {
-
int index = partial.indexOf(':');
- if (index == -1) {
+ if (index <= 0) {
return;
}
+ String hostName = partial.substring(0, index);
final InetAddress host;
try {
- host = InetAddress.getByName(partial.substring(0, index));
+ host = InetAddress.getByName(hostName);
} catch (UnknownHostException e) {
return;
}
@@ -73,43 +69,48 @@
for (int i = 0; i < exportEntryList.size(); i++) {
ExportEntry exportEntry = exportEntryList.get(i);
if (exportEntry.getDirectory().startsWith(partialDirectory)) {
- completion.addCompletion(partial.substring(0, index) + ":"
- + exportEntry.getDirectory());
+ completion.addCompletion(hostName + ":" + exportEntry.getDirectory());
}
}
}
- public InetAddress getAddress(ParsedArguments args)
- throws UnknownHostException {
- String value = getValue(args);
+ public InetAddress getAddress() throws UnknownHostException {
+ String value = getValue();
if (value == null) {
return null;
}
-
int index = value.indexOf(':');
- if (index == -1) {
- return InetAddress.getByName(value);
- } else {
- return InetAddress.getByName(value.substring(0, index));
- }
-
+ return InetAddress.getByName(index == -1 ? value : value.substring(0, index));
}
- public String getRemoteDirectory(ParsedArguments args) {
-
- String value = getValue(args);
-
+ public String getRemoteDirectory() {
+ String value = getValue();
if (value == null) {
return null;
}
+ int index = value.indexOf(':');
+ return (index == -1 || index == value.length() - 1) ? null : value.substring(index + 1);
+ }
- int index = value.indexOf(':');
+ @Override
+ protected String argumentKind() {
+ return "hostname:directory";
+ }
+
+ @Override
+ protected String doAccept(Token value) throws CommandSyntaxException {
+ int index = value.token.indexOf(':');
if (index == -1) {
- return null;
+ throw new CommandSyntaxException("missing ':'");
}
-
- return value.substring(index + 1);
-
+ else if (index == 0) {
+ throw new CommandSyntaxException("no hostname before ':'");
+ }
+ else if (index == value.token.length() - 1) {
+ throw new CommandSyntaxException("no directory after ':'");
+ }
+ else {
+ return value.token;
+ }
}
-
}
Modified: trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSMountCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSMountCommand.java 2008-06-04 11:00:35 UTC (rev 4191)
+++ trunk/fs/src/fs/org/jnode/fs/nfs/command/NFSMountCommand.java 2008-06-04 13:20:48 UTC (rev 4192)
@@ -21,14 +21,20 @@
package org.jnode.fs.nfs.command;
+import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.InetAddress;
+import java.net.UnknownHostException;
+import javax.naming.NameNotFoundException;
+
+import org.jnode.driver.DeviceAlreadyRegisteredException;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.DeviceUtils;
-import org.jnode.fs.FileSystem;
-import org.jnode.fs.FileSystemType;
+import org.jnode.driver.DriverException;
+import org.jnode.fs.FileSystemException;
import org.jnode.fs.nfs.nfs2.NFS2Device;
import org.jnode.fs.nfs.nfs2.NFS2Driver;
import org.jnode.fs.nfs.nfs2.NFS2FileSystem;
@@ -38,112 +44,100 @@
import org.jnode.net.nfs.Protocol;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.ParsedArguments;
-import org.jnode.shell.help.Syntax;
-import org.jnode.shell.help.argument.FileArgument;
-import org.jnode.shell.help.argument.IntegerArgument;
-import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.IntegerArgument;
/**
* @author Andrei Dore
+ * @author cr...@jn...
*/
public class NFSMountCommand extends AbstractCommand {
- private static final FileArgument MOUNTPOINT_ARG = new FileArgument(
- "directory", "the mountpoint");
- private static final NFSHostNameArgument HOST_ARG = new NFSHostNameArgument(
- "host:remoteDir", "NFS host");
+ private final FileArgument MOUNTPOINT_ARG = new FileArgument(
+ "directory", Argument.MANDATORY, "the mountpoint");
+
+ private final NFSHostNameArgument HOST_ARG = new NFSHostNameArgument(
+ "nfsFileSystem", Argument.MANDATORY, "remote NFS host and exported directory (host:dir)");
- private static final OptionArgument PROTOCOL_ARG = new OptionArgument(
- "protocol", "protocol", new OptionArgument.Option[] {
- new OptionArgument.Option("tcp", "tcp protocol"),
- new OptionArgument.Option("udp", "udp protocol") });
+ private final FlagArgument READ_ONLY_FLAG = new FlagArgument(
+ "readOnly", Argument.OPTIONAL, "if set, mount the file system read-only");
+
+ private final FlagArgument READ_WRITE_FLAG = new FlagArgument(
+ "readWrite", Argument.OPTIONAL, "if set, mount the file system read-write");
- private static final IntegerArgument USER_ID_ARG = new IntegerArgument(
- "uid", "user id");
- private static final IntegerArgument GROUP_ID_ARG = new IntegerArgument(
- "gid", "group id");
+ private final FlagArgument TCP_FLAG = new FlagArgument(
+ "tcp", Argument.OPTIONAL, "if set, use tcp protocol");
+
+ private final FlagArgument UDP_FLAG = new FlagArgument(
+ "udp", Argument.OPTIONAL, "if set, use udp protocol (default)");
- private static final Parameter PARAMETER_PROTOCOL = new Parameter(
- PROTOCOL_ARG, Parameter.OPTIONAL);
+ private final IntegerArgument USER_ID_ARG = new IntegerArgument(
+ "uid", Argument.OPTIONAL, "remote user id (default -1)");
+
+ private final IntegerArgument GROUP_ID_ARG = new IntegerArgument(
+ "gid", Argument.OPTIONAL, "remote group id (default -1)");
- private static final Parameter PARAMETER_USER_ID = new Parameter(
- USER_ID_ARG, Parameter.MANDATORY);
- private static final Parameter PARAMETER_GROUP_ID = new Parameter(
- GROUP_ID_ARG, Parameter.MANDATORY);
+ public NFSMountCommand() {
+ super("mount an NFS filesystem");
+ registerArguments(MOUNTPOINT_ARG, HOST_ARG, READ_ONLY_FLAG, READ_WRITE_FLAG,
+ TCP_FLAG, UDP_FLAG, USER_ID_ARG, GROUP_ID_ARG);
+ }
- static Help.Info HELP_INFO = new Help.Info("nfsmount",
-
- new Syntax("Mount a read only NFS filesystem", new Parameter[] {
- new Parameter(MOUNTPOINT_ARG, Parameter.MANDATORY),
- new Parameter(HOST_ARG, Parameter.MANDATORY),
-
- PARAMETER_PROTOCOL }),
-
- new Syntax("Mount a NFS filesystem", new Parameter[] {
- new Parameter(MOUNTPOINT_ARG, Parameter.MANDATORY),
- new Parameter(HOST_ARG, Parameter.MANDATORY), PARAMETER_USER_ID,
- PARAMETER_GROUP_ID, PARAMETER_PROTOCOL }));
-
public static void main(String[] args) throws Exception {
new NFSMountCommand().execute(args);
}
public void execute(CommandLine commandLine, InputStream in,
- PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
+ PrintStream out, PrintStream err)
+ throws NameNotFoundException, DriverException, DeviceAlreadyRegisteredException,
+ FileSystemException, IOException
+ {
+ final File mountPoint = MOUNTPOINT_ARG.getValue();
+ final InetAddress host = HOST_ARG.getAddress();
+ final String remoteDirectory = HOST_ARG.getRemoteDirectory();
- final String mount_point = MOUNTPOINT_ARG.getValue(cmdLine);
- final InetAddress host = HOST_ARG.getAddress(cmdLine);
- final String remoteDirectory = HOST_ARG.getRemoteDirectory(cmdLine);
+ // Choose the protocol (udp or tcp) the default value it is udp.
+ final Protocol protocol =
+ UDP_FLAG.isSet() ? Protocol.UDP :
+ TCP_FLAG.isSet() ? Protocol.TCP : Protocol.UDP;
- // select the protocol (udp or tcp) the default value it is udp.
- final Protocol protocol;
- if (PARAMETER_PROTOCOL.isSet(cmdLine)) {
+ int uid = USER_ID_ARG.isSet() ? USER_ID_ARG.getValue() : -1;
+ int gid = GROUP_ID_ARG.isSet() ? GROUP_ID_ARG.getValue() : -1;
+
+ // Choose read-only or read-write. If neither is specified, guess that the
+ // file system should be read-only if no uid/gid was specified.
+ boolean readOnly =
+ READ_ONLY_FLAG.isSet() ? true :
+ READ_WRITE_FLAG.isSet() ? false :
+ (uid == -1 && gid == -1);
- String protocolOption = PROTOCOL_ARG.getValue(cmdLine)
- .toLowerCase().intern();
-
- if (protocolOption == "tcp") {
- protocol = Protocol.TCP;
- } else {
- protocol = Protocol.UDP;
+ // Now do the work of mounting the file system, taking care to undo as much as
+ // we can in the event of a failure.
+ final DeviceManager dm = DeviceUtils.getDeviceManager();
+ final NFS2Device dev = new NFS2Device(host, remoteDirectory, protocol, uid, gid);
+ dev.setDriver(new NFS2Driver());
+ dm.register(dev);
+ boolean ok = false;
+ try {
+ final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
+ NFS2FileSystemType type = fss.getFileSystemType(NFS2FileSystemType.ID);
+ final NFS2FileSystem fs = type.create(dev, readOnly);
+ fss.registerFileSystem(fs);
+ try {
+ fss.mount(mountPoint.getAbsolutePath(), fs, null);
+ ok = true;
}
- } else {
- protocol = Protocol.UDP;
+ finally {
+ if (!ok) {
+ fss.unregisterFileSystem(dev);
+ }
+ }
}
-
- int uid = -1;
- int gid = -1;
- boolean readOnly;
- if (PARAMETER_USER_ID.isSet(cmdLine)
- && PARAMETER_GROUP_ID.isSet(cmdLine)) {
-
- uid = USER_ID_ARG.getInteger(cmdLine);
- gid = GROUP_ID_ARG.getInteger(cmdLine);
-
- readOnly = false;
-
- } else {
- readOnly = true;
+ finally {
+ if (!ok) {
+ dm.unregister(dev);
+ }
}
-
- final NFS2Device dev;
- if (!readOnly) {
- dev = new NFS2Device(host, remoteDirectory, protocol, uid, gid);
- } else {
- dev = new NFS2Device(host, remoteDirectory, protocol);
- }
-
- dev.setDriver(new NFS2Driver());
- final DeviceManager dm = DeviceUtils.getDeviceManager();
- dm.register(dev);
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
- NFS2FileSystemType type = fss.getFileSystemType(NFS2FileSystemType.ID);
- final NFS2FileSystem fs = type.create(dev, readOnly);
- fss.registerFileSystem(fs);
- fss.mount(mount_point, fs, null);
-
}
}
Modified: trunk/fs/src/fs/org/jnode/fs/nfs/nfs2/NFS2Device.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/nfs/nfs2/NFS2Device.java 2008-06-04 11:00:35 UTC (rev 4191)
+++ trunk/fs/src/fs/org/jnode/fs/nfs/nfs2/NFS2Device.java 2008-06-04 13:20:48 UTC (rev 4192)
@@ -42,22 +42,15 @@
private int gid;
public NFS2Device(InetAddress host, String remoteDirectory,
- Protocol protocol) {
- this(host, remoteDirectory, protocol, -1, -1);
-
- }
-
- public NFS2Device(InetAddress host, String remoteDirectory,
Protocol protocol, int uid, int gid) {
- super(null, "nfs2-(" + host.getHostName() + "," + remoteDirectory + ","
- + protocol + "," + uid + "," + gid + ")");
+ super(null, "nfs2-(" + host.getHostName() + "," + remoteDirectory + "," +
+ protocol + "," + uid + "," + gid + ")");
this.host = host;
this.remoteDirectory = remoteDirectory;
this.protocol = protocol;
this.uid = uid;
this.gid = gid;
-
}
public InetAddress getHost() {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|