|
From: <fd...@us...> - 2007-02-11 22:19:11
|
Revision: 3117
http://jnode.svn.sourceforge.net/jnode/?rev=3117&view=rev
Author: fduminy
Date: 2007-02-11 14:19:06 -0800 (Sun, 11 Feb 2007)
Log Message:
-----------
- IBMPartitionTypes is now an enum instead of an interface
- added some functions for disk partitionning
- extracted partionning functions from FDiskCommand to put them in the class PartitionHelper
Modified Paths:
--------------
trunk/fs/build.xml
trunk/fs/descriptors/org.jnode.partitions.command.xml
trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockAlignmentSupport.java
trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockDeviceAPI.java
trunk/fs/src/driver/org/jnode/driver/block/ide/disk/IDEDiskDriver.java
trunk/fs/src/driver/org/jnode/driver/bus/ide/IDEDeviceAPI.java
trunk/fs/src/fs/org/jnode/fs/fat/BootSector.java
trunk/fs/src/fs/org/jnode/fs/fat/FatFileSystemType.java
trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java
trunk/fs/src/fs/org/jnode/fs/ntfs/NTFSFileSystemType.java
trunk/fs/src/fs/org/jnode/partitions/PartitionTable.java
trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java
trunk/fs/src/fs/org/jnode/partitions/ibm/IBMPartitionTable.java
trunk/fs/src/fs/org/jnode/partitions/ibm/IBMPartitionTableEntry.java
trunk/fs/src/fs/org/jnode/partitions/ibm/IBMPartitionTypes.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/AbstractFSTest.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/FSConfigurations.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/FSContext.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/FSTestSuite.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/config/DeviceParam.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/config/FileParam.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/config/JNodeDeviceParam.java
trunk/fs/src/test/org/jnode/test/fs/filesystem/config/ResourceParam.java
Modified: trunk/fs/build.xml
===================================================================
--- trunk/fs/build.xml 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/build.xml 2007-02-11 22:19:06 UTC (rev 3117)
@@ -63,6 +63,16 @@
<target name="clean">
<jnode.clean/>
</target>
+
+<!-- tests FileSystems -->
+ <target name="tests" depends="compile" description="Run FS tests with JUnit">
+ <junit fork="yes" haltonerror="false" haltonfailure="false" printsummary="on" includeantruntime="true">
+ <classpath refid="cp-jnode" />
+
+ <formatter type="plain" usefile="false" />
+ <test name="org.jnode.test.fs.AllFSTest" />
+ </junit>
+ </target>
</project>
Modified: trunk/fs/descriptors/org.jnode.partitions.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.partitions.command.xml 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/descriptors/org.jnode.partitions.command.xml 2007-02-11 22:19:06 UTC (rev 3117)
@@ -17,6 +17,7 @@
<runtime>
<library name="jnode-fs.jar">
<export name="org.jnode.partitions.command.*"/>
+ <export name="org.jnode.partitions.help.argument.*"/>
</library>
</runtime>
Modified: trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockAlignmentSupport.java
===================================================================
--- trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockAlignmentSupport.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockAlignmentSupport.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -24,20 +24,22 @@
import java.io.IOException;
import org.jnode.partitions.PartitionTable;
+import org.jnode.partitions.PartitionTableEntry;
/**
* @author Ewout Prangsma (ep...@us...)
*/
-public class PartitionableBlockAlignmentSupport extends BlockAlignmentSupport
- implements PartitionableBlockDeviceAPI {
+public class PartitionableBlockAlignmentSupport <PTE extends PartitionTableEntry>
+ extends BlockAlignmentSupport
+ implements PartitionableBlockDeviceAPI<PTE> {
- private final PartitionableBlockDeviceAPI parentApi;
+ private final PartitionableBlockDeviceAPI<PTE> parentApi;
/**
* @param parentApi
* @param alignment
*/
- public PartitionableBlockAlignmentSupport(PartitionableBlockDeviceAPI parentApi, int alignment) {
+ public PartitionableBlockAlignmentSupport(PartitionableBlockDeviceAPI<PTE> parentApi, int alignment) {
super(parentApi, alignment);
this.parentApi = parentApi;
}
@@ -54,7 +56,7 @@
* @return Null if no partition table is found.
* @throws IOException
*/
- public PartitionTable getPartitionTable() throws IOException {
+ public PartitionTable<PTE> getPartitionTable() throws IOException {
return parentApi.getPartitionTable();
}
}
Modified: trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockDeviceAPI.java
===================================================================
--- trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockDeviceAPI.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/driver/org/jnode/driver/block/PartitionableBlockDeviceAPI.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -24,6 +24,7 @@
import java.io.IOException;
import org.jnode.partitions.PartitionTable;
+import org.jnode.partitions.PartitionTableEntry;
/**
* This device API is implemented by block devices that
@@ -31,7 +32,9 @@
*
* @author Ewout Prangsma (ep...@us...)
*/
-public interface PartitionableBlockDeviceAPI extends BlockDeviceAPI {
+public interface PartitionableBlockDeviceAPI
+ <PTE extends PartitionTableEntry>
+ extends BlockDeviceAPI {
/**
* Gets the sector size for this device.
@@ -45,5 +48,5 @@
* @return Null if no partition table is found.
* @throws IOException
*/
- public PartitionTable getPartitionTable() throws IOException;
+ public PartitionTable<PTE> getPartitionTable() throws IOException;
}
Modified: trunk/fs/src/driver/org/jnode/driver/block/ide/disk/IDEDiskDriver.java
===================================================================
--- trunk/fs/src/driver/org/jnode/driver/block/ide/disk/IDEDiskDriver.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/driver/org/jnode/driver/block/ide/disk/IDEDiskDriver.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -52,11 +52,9 @@
import org.jnode.driver.bus.ide.command.IDEReadSectorsCommand;
import org.jnode.driver.bus.ide.command.IDEWriteSectorsCommand;
import org.jnode.naming.InitialNaming;
-import org.jnode.partitions.PartitionTable;
import org.jnode.partitions.ibm.IBMPartitionTable;
import org.jnode.partitions.ibm.IBMPartitionTableEntry;
import org.jnode.system.BootLog;
-import org.jnode.util.ByteBufferUtils;
import org.jnode.util.TimeoutException;
/**
@@ -64,7 +62,8 @@
*
* @author epr
*/
-public class IDEDiskDriver extends Driver implements IDEDeviceAPI, IDEConstants {
+public class IDEDiskDriver extends Driver
+ implements IDEDeviceAPI<IBMPartitionTableEntry>, IDEConstants {
/** My logger */
private static final Logger log = Logger.getLogger(IDEDiskDriver.class);
@@ -115,35 +114,23 @@
this.pt = factory.createIBMPartitionTable(bs, dev);
int partIndex = 0;
- final int max = pt.getLength();
- for (int i = 0; i < max; i++) {
- final IBMPartitionTableEntry pte = (IBMPartitionTableEntry)pt.getEntry(i);
+ int i = 0;
+ for (IBMPartitionTableEntry pte : pt) {
if(pte == null)
{
BootLog.warn("PartitionTableEntry #"+i+" is null");
}
else if (pte.isValid()) {
if (pte.isExtended()) {
- //now we should have an filled vector in the pt
- final List<IBMPartitionTableEntry> extendedPartitions = pt.getExtendedPartitions();
- log.info("Have "+ extendedPartitions.size()+ " Extended partitions found");
-
- for(int iPart = 0 ; iPart < extendedPartitions.size() ; iPart++)
- {
- IBMPartitionTableEntry pteExt =
- extendedPartitions.get(iPart);
- registerPartition(devMan, dev, pteExt, partIndex);
-
- if(iPart < (extendedPartitions.size() -1))
- partIndex++;
- }
-
+ // Create partition devices for the extended partition
+ partIndex = registerExtendedPartition(devMan, dev, partIndex);
} else {
// Create a partition device.
registerPartition(devMan, dev, pte, partIndex);
}
}
partIndex++;
+ i++;
}
} catch (DeviceAlreadyRegisteredException ex) {
throw new DriverException("Partition device is already known???? Probably a bug", ex);
@@ -314,6 +301,34 @@
pdev.setDriver(new IDEDiskPartitionDriver());
devMan.register(pdev);
}
+
+ /**
+ * register all the partitions included in the extended partition
+ * @param devMan
+ * @param dev
+ * @param partIndex
+ * @return
+ * @throws DeviceAlreadyRegisteredException
+ * @throws DriverException
+ */
+ private int registerExtendedPartition(DeviceManager devMan, IDEDevice dev,
+ int partIndex) throws DeviceAlreadyRegisteredException, DriverException
+ {
+ //now we should have an filled vector in the pt
+ final List<IBMPartitionTableEntry> extendedPartitions = pt.getExtendedPartitions();
+ log.info("Have "+ extendedPartitions.size()+ " Extended partitions found");
+
+ for(int iPart = 0 ; iPart < extendedPartitions.size() ; iPart++)
+ {
+ IBMPartitionTableEntry pteExt =
+ extendedPartitions.get(iPart);
+ registerPartition(devMan, dev, pteExt, partIndex);
+
+ if(iPart < (extendedPartitions.size() -1))
+ partIndex++;
+ }
+ return partIndex;
+ }
/**
* @see org.jnode.driver.block.PartitionableBlockDeviceAPI#getSectorSize()
@@ -327,7 +342,7 @@
* @return Null if no partition table is found.
* @throws IOException
*/
- public PartitionTable getPartitionTable() throws IOException {
+ public IBMPartitionTable getPartitionTable() throws IOException {
return pt;
}
}
Modified: trunk/fs/src/driver/org/jnode/driver/bus/ide/IDEDeviceAPI.java
===================================================================
--- trunk/fs/src/driver/org/jnode/driver/bus/ide/IDEDeviceAPI.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/driver/org/jnode/driver/bus/ide/IDEDeviceAPI.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -22,10 +22,12 @@
package org.jnode.driver.bus.ide;
import org.jnode.driver.block.PartitionableBlockDeviceAPI;
+import org.jnode.partitions.PartitionTableEntry;
/**
* @author epr
*/
-public interface IDEDeviceAPI extends PartitionableBlockDeviceAPI {
+public interface IDEDeviceAPI <PTE extends PartitionTableEntry>
+ extends PartitionableBlockDeviceAPI<PTE> {
// nothing different
}
Modified: trunk/fs/src/fs/org/jnode/fs/fat/BootSector.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/fat/BootSector.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/fs/org/jnode/fs/fat/BootSector.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -356,6 +356,11 @@
return dirty;
}
+ public int getNbPartitions()
+ {
+ return partitions.length;
+ }
+
public synchronized IBMPartitionTableEntry getPartition(int partNr) {
if (partitions[partNr] == null) {
partitions[partNr] = new IBMPartitionTableEntry(null, data, partNr);
Modified: trunk/fs/src/fs/org/jnode/fs/fat/FatFileSystemType.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/fat/FatFileSystemType.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/fs/org/jnode/fs/fat/FatFileSystemType.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -69,15 +69,17 @@
return false;
}
final IBMPartitionTableEntry ipte = (IBMPartitionTableEntry)pte;
- final int type = ipte.getSystemIndicator();
- switch (type) {
- case IBMPartitionTypes.PARTTYPE_DOS_FAT12 :
- case IBMPartitionTypes.PARTTYPE_DOS_FAT16_LT32M :
- case IBMPartitionTypes.PARTTYPE_DOS_FAT16_GT32M :
- return true;
- default :
- return false;
+ final IBMPartitionTypes type = ipte.getSystemIndicator();
+ if((type == IBMPartitionTypes.PARTTYPE_DOS_FAT12) ||
+ (type == IBMPartitionTypes.PARTTYPE_DOS_FAT16_LT32M) ||
+ (type == IBMPartitionTypes.PARTTYPE_DOS_FAT16_GT32M) )
+ {
+ return true;
}
+ else
+ {
+ return false;
+ }
}
Modified: trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -43,18 +43,18 @@
return false;
final IBMPartitionTableEntry ipte =
- (IBMPartitionTableEntry)pte;
+ (IBMPartitionTableEntry)pte;
- final int type = ipte.getSystemIndicator();
-
- switch ( type ) {
- case IBMPartitionTypes.PARTTYPE_WIN95_FAT32:
- case IBMPartitionTypes.PARTTYPE_WIN95_FAT32_LBA:
- return true;
-
- default:
- return false;
- }
+ final IBMPartitionTypes type = ipte.getSystemIndicator();
+ if((type == IBMPartitionTypes.PARTTYPE_WIN95_FAT32) ||
+ (type == IBMPartitionTypes.PARTTYPE_WIN95_FAT32_LBA) )
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
}
return false;
Modified: trunk/fs/src/fs/org/jnode/fs/ntfs/NTFSFileSystemType.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/ntfs/NTFSFileSystemType.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/fs/org/jnode/fs/ntfs/NTFSFileSystemType.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -52,8 +52,10 @@
FSBlockDeviceAPI devApi) {
if (pte instanceof IBMPartitionTableEntry) {
IBMPartitionTableEntry iPte = (IBMPartitionTableEntry) pte;
- if (iPte.getSystemIndicator() == IBMPartitionTypes.PARTTYPE_NTFS) { return new String(
- firstSector, 0x03, 8).startsWith(TAG); }
+ if (iPte.getSystemIndicator() == IBMPartitionTypes.PARTTYPE_NTFS)
+ {
+ return new String(firstSector, 0x03, 8).startsWith(TAG);
+ }
}
return false;
}
Modified: trunk/fs/src/fs/org/jnode/partitions/PartitionTable.java
===================================================================
--- trunk/fs/src/fs/org/jnode/partitions/PartitionTable.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/fs/org/jnode/partitions/PartitionTable.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -24,21 +24,11 @@
/**
* @author epr
*/
-public interface PartitionTable {
+public interface PartitionTable <PTE extends PartitionTableEntry>
+ extends Iterable<PTE> {
/**
* Gets the type of this partition table
*/
public PartitionTableType getType();
-
- /**
- * Gets the number of entries in this table
- */
- public int getLength();
-
- /**
- * Gets the partition table entry at the given index.
- * @param index
- */
- public PartitionTableEntry getEntry(int index);
}
Modified: trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java 2007-02-11 22:12:18 UTC (rev 3116)
+++ trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java 2007-02-11 22:19:06 UTC (rev 3117)
@@ -25,7 +25,7 @@
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
-import java.util.StringTokenizer;
+import java.util.NoSuchElementException;
import javax.naming.NameNotFoundException;
@@ -33,14 +33,12 @@
import org.jnode.driver.Device;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.DeviceNotFoundException;
-import org.jnode.driver.DriverException;
import org.jnode.driver.block.BlockDeviceAPI;
import org.jnode.driver.bus.ide.IDEConstants;
import org.jnode.driver.bus.ide.IDEDevice;
import org.jnode.driver.bus.ide.IDEDriveDescriptor;
-import org.jnode.fs.fat.BootSector;
-import org.jnode.fs.fat.GrubBootSector;
import org.jnode.naming.InitialNaming;
+import org.jnode.partitions.help.argument.IBMPartitionTypeArgument;
import org.jnode.partitions.ibm.IBMPartitionTable;
import org.jnode.partitions.ibm.IBMPartitionTableEntry;
import org.jnode.partitions.ibm.IBMPartitionTableType;
@@ -51,8 +49,11 @@
import org.jnode.shell.help.Syntax;
import org.jnode.shell.help.SyntaxErrorException;
import org.jnode.shell.help.argument.DeviceArgument;
+import org.jnode.shell.help.argument.IntegerArgument;
+import org.jnode.shell.help.argument.LongArgument;
import org.jnode.shell.help.argument.OptionArgument;
-import org.jnode.shell.help.argument.StringArgument;
+import org.jnode.shell.help.argument.SizeArgument;
+
/**
* @author gbin
* @author Trickkiste
@@ -72,50 +73,54 @@
"Action on a specified partition",
new OptionArgument.Option[] {
new OptionArgument.Option("-d", "Delete a partition"),
- new OptionArgument.Option("-b", "Switch the bootable flag of a partition"),
+ new OptionArgument.Option("-b", "Switch the bootable flag of a partition")});
+
+ static final OptionArgument ACTION_MODIFY =
+ new OptionArgument(
+ "action",
+ "Action on a specified partition",
+ new OptionArgument.Option[] {
new OptionArgument.Option("-m", "Modify/create a partition")});
- static final StringArgument PARTITION = new StringArgument("partition number", "Targeted partition");
- static final StringArgument PARTITION_DESCRIPTION = new StringArgument("description", "Partition description" +
- " \"ID:start:size:filesystem\"" +
- " ID : ID of the partition" +
- " start : Sector where the partition starts" +
- " size : Size of the partition in sectors" +
- " filesystem : Number of the filesystem type");
+ static final IntegerArgument PARTITION = new IntegerArgument("partition number", "Targeted partition");
+ static final LongArgument START = new LongArgument("start", "Sector where the partition starts");
+ static final SizeArgument SIZE = new SizeArgument("size", "Size of the partition in sectors or in bytes(use prefixes K, M, G, ...)");
+ static final IBMPartitionTypeArgument PARTITION_TYPE = new IBMPartitionTypeArgument(
+ "partition type", "partition type code");
static final DeviceArgument ARG_DEVICE =
new DeviceArgument("device-id", "the device on which you want to change/create the partition");
static final Parameter PARAM_INITMBR = new Parameter(INITMBR, Parameter.MANDATORY);
static final Parameter PARAM_ACTION = new Parameter(ACTION, Parameter.MANDATORY);
+ static final Parameter PARAM_ACTION_MODIFY = new Parameter(ACTION_MODIFY, Parameter.MANDATORY);
static final Parameter PARAM_DEVICE = new Parameter(ARG_DEVICE, Parameter.MANDATORY);
static final Parameter PARAM_PARTITION = new Parameter(PARTITION, Parameter.MANDATORY);
- static final Parameter PARAM_PARTITION_DESCRIPTION = new Parameter(PARTITION_DESCRIPTION, Parameter.MANDATORY);
+ static final Parameter PARAM_START = new Parameter(START, Parameter.MANDATORY);
+ static final Parameter PARAM_SIZE = new Parameter(SIZE, Parameter.MANDATORY);
+ static final Parameter PARAM_PARTITION_TYPE = new Parameter(PARTITION_TYPE, Parameter.MANDATORY);
public static Help.Info HELP_INFO =
new Help.Info(
"fdisk",
new Syntax[] {
new Syntax("Lists the available devices"),
- new Syntax("Print the partition table of a device", new Parameter[] { PARAM_DEVICE }),
- new Syntax("Initialize the MBR of a device", new Parameter[] { PARAM_INITMBR, PARAM_DEVICE }),
- new Syntax(
- "Create / Delete / change a partition",
- new Parameter[] { PARAM_ACTION, PARAM_PARTITION, PARAM_DEVICE }),
- new Syntax(
- "Create / Delete / change a partition",
- new Parameter[] { PARAM_ACTION, PARAM_PARTITION_DESCRIPTION, PARAM_DEVICE })
+ new Syntax("Print the partition table of a device",
+ new Parameter[] {
+ PARAM_DEVICE }),
+ new Syntax("Initialize the MBR of a device",
+ new Parameter[] {
+ PARAM_INITMBR, PARAM_DEVICE }),
+ new Syntax("Change a partition",
+ new Parameter[] {
+ PARAM_ACTION_MODIFY, PARAM_PARTITION, PARAM_START,
+ PARAM_SIZE, PARAM_PARTITION_TYPE,
+ PARAM_DEVICE }),
+ new Syntax("Delete a partition / switch bootable flag",
+ new Parameter[] {
+ PARAM_ACTION, PARAM_PARTITION, PARAM_DEVICE }),
});
- /*
- * public static Help.Info HELP_INFO = new Help.Info( "fdisk", "With no
- * argument, it lists the available devices\n" + "With only the device, it
- * lists the current partitions on the device\n" + "--initmbr initialize the
- * Master Boot Record of the device\n" + "-m add or modify the id partition
- * with first sector at start, size of size sectors and fs id fs\n" + "-d
- * delete the partition with id id" + "-b switch boot flag on parition id",
- */
-
public static void main(String[] args) throws SyntaxErrorException {
ParsedArguments cmdLine = HELP_INFO.parse(args);
@@ -124,6 +129,7 @@
dm = InitialNaming.lookup(DeviceManager.NAME);
boolean isAction = PARAM_ACTION.isSet(cmdLine);
+ boolean isActionModify = PARAM_ACTION_MODIFY.isSet(cmdLine);
boolean isInitMBR = PARAM_INITMBR.isSet(cmdLine);
boolean isDevice = PARAM_DEVICE.isSet(cmdLine);
@@ -134,56 +140,41 @@
}
// only device is set
- if (!isAction && !isInitMBR && isDevice) {
+ if (!isAction && !isActionModify && !isInitMBR && isDevice) {
printTable(ARG_DEVICE.getValue(cmdLine), dm);
return;
}
+ final String deviceId = ARG_DEVICE.getValue(cmdLine);
+ final PartitionHelper helper = new PartitionHelper(deviceId, dm);
+
// initMBR
if (isInitMBR) {
- initMbr(ARG_DEVICE.getValue(cmdLine), dm);
+ helper.initMbr();
+ helper.write();
return;
}
- // now it is a change on a specific partition so read the table
+ int partNumber = getPartitionNumber(helper, cmdLine);
- IDEDevice current = (IDEDevice)dm.getDevice(ARG_DEVICE.getValue(cmdLine));
- BlockDeviceAPI api = current.getAPI(BlockDeviceAPI.class);
- ByteBuffer mbr = ByteBuffer.allocate(IDEConstants.SECTOR_SIZE);
- api.read(0, mbr);
- if (!IBMPartitionTable.containsPartitionTable(mbr.array()))
- throw new IOException("This device doesn't contain a valid MBR, use --initmbr.");
-
- BootSector bs = new BootSector(mbr.array());
-
- if (ACTION.getValue(cmdLine).intern() == "-m") {
- modifyPartition(PARTITION.getValue(cmdLine), api, bs, current);
- bs.write(api);
+ // modify a partition ?
+ if (ACTION_MODIFY.getValue(cmdLine).intern() == "-m") {
+ modifyPartition(helper, partNumber, cmdLine);
+ helper.write();
return;
}
- // it is not a modify so the PARTITION parameter is only a partition
- // number
-
- int partNumber;
- try {
- partNumber = Integer.parseInt(PARTITION.getValue(cmdLine));
- } catch (NumberFormatException f) {
- throw new IllegalArgumentException("Partition number is invalid");
- }
-
- if (partNumber > 3 || partNumber < 0)
- throw new IllegalArgumentException("Partition number is invalid");
-
+ // delete a partition ?
if (ACTION.getValue(cmdLine).intern() == "-d") {
- deletePartition(bs, partNumber);
- bs.write(api);
+ helper.deletePartition(partNumber);
+ helper.write();
return;
}
+ // toggle boot flag for a partition ?
if (ACTION.getValue(cmdLine).intern() == "-b") {
- toggleBootable(bs, partNumber);
- bs.write(api);
+ helper.toggleBootable(partNumber);
+ helper.write();
}
} catch (IOException e) {
e.printStackTrace();
@@ -194,99 +185,56 @@
} catch (DeviceNotFoundException e) {
e.printStackTrace();
}
-
}
+
+ private static int getPartitionNumber(PartitionHelper helper, ParsedArguments cmdLine)
+ {
+ int partNumber = PARTITION.getInteger(cmdLine);
- private static void toggleBootable(BootSector bs, int partNumber) {
- // save the current state for the targeted partition
- boolean currentStatus = bs.getPartition(partNumber).getBootIndicator();
-
- // erase all the states
- for (int i = 0; i < 4; i++) {
- bs.getPartition(i).setBootIndicator(false);
- }
-
- // put back the reversed state for the targeted partition
- bs.getPartition(partNumber).setBootIndicator(!currentStatus);
+ if ((partNumber >= helper.getNbPartitions()) ||
+ (partNumber < 0) )
+ throw new IllegalArgumentException("Partition number is invalid");
+
+ return partNumber;
}
- private static void deletePartition(BootSector bs, int partNumber) {
- bs.getPartition(partNumber).setSystemIndicator(IBMPartitionTypes.PARTTYPE_EMPTY);
- }
-
- private static void modifyPartition(String description, BlockDeviceAPI api, BootSector bs, Device dev) throws IOException {
- // arg 1 should be in the form id:start:size:fs
- StringTokenizer st = new StringTokenizer(description, ":");
- int id = Integer.parseInt(st.nextToken());
- //BUG in long
- //long start = Long.parseLong(st.nextToken());
- //long size = Long.parseLong(st.nextToken());
- int start = Integer.parseInt(st.nextToken());
- int size = Integer.parseInt(st.nextToken());
- int fs = Integer.parseInt(st.nextToken(), 16);
- System.out.println(
- "Init " + id + " with start = " + start + ", size = " + size + ", fs = " + Integer.toHexString(fs & 0xff));
- IBMPartitionTableEntry entry = bs.getPartition(id);
- entry.setBootIndicator(false);
- entry.setSystemIndicator(fs);
- entry.setStartLba(start);
- entry.setNrSectors(size);
- bs.write(api);
-
-// restart the device
- DeviceManager dm = null;
-
- try {
- dm = InitialNaming.lookup(DeviceManager.NAME);
- dm.stop(dev);
- dm.start(dev);
+ private static void modifyPartition(PartitionHelper helper,
+ int id,
+ ParsedArguments cmdLine)
+ throws IOException
+ {
+ long start = START.getLong(cmdLine);
+ long size = SIZE.getLong(cmdLine);
+ IBMPartitionTypes type = PARTITION_TYPE.getArgValue(cmdLine);
- } catch (NameNotFoundException e) {
-
- e.printStackTrace();
- } catch (DeviceNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (DriverException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- return;
+// try {
+ System.out.println("D");
+ System.out.println("Init " + id + " with start = " + start
+ + ", size = " + size + ", fs = "
+ + Integer.toHexString(type.getCode() & 0xff));
+ System.out.println("E");
+ boolean sizeUnit = SIZE.hasSizeUnit(cmdLine) ?
+ PartitionHelper.BYTES : PartitionHelper.SECTORS;
+ helper.modifyPartition(id, false, start, size, sizeUnit, type);
+ System.out.println("F");
+// }
+// catch (NumberFormatException nfe)
+// {
+// System.err.println("not an integer");
+// System.err.println(helpMsg);
+// }
+// catch (NoSuchElementException nsee)
+// {
+// System.err.println("not enough elements");
+// System.err.println(helpMsg);
+// }
+// catch (IllegalArgumentException iae)
+// {
+// System.err.println(iae.getMessage());
+// System.err.println(helpMsg);
+// }
}
-
- private static void initMbr(String device, DeviceManager dm)
- throws DeviceNotFoundException, ApiNotFoundException, IOException {
- IDEDevice current = (IDEDevice)dm.getDevice(device);
- BlockDeviceAPI api = current.getAPI(BlockDeviceAPI.class);
- ByteBuffer MBR = ByteBuffer.allocate(IDEConstants.SECTOR_SIZE);
- api.read(0, MBR);
-
- System.out.println("Initialize MBR ...");
-
- GrubBootSector newMBR = new GrubBootSector(PLAIN_MASTER_BOOT_SECTOR);
-
- if (IBMPartitionTable.containsPartitionTable(MBR.array())) {
- BootSector oldMBR = new BootSector(MBR.array());
- System.out.println("This device already contains a partition table. Copy the already existing partitions.");
- for (int i = 0; i < 4; i++) {
- IBMPartitionTableEntry entry = newMBR.getPartition(i);
- entry.setBootIndicator(oldMBR.getPartition(i).getBootIndicator());
- entry.setStartLba(oldMBR.getPartition(i).getStartLba());
- entry.setNrSectors(oldMBR.getPartition(i).getNrSectors());
- entry.setSystemIndicator(oldMBR.getPartition(i).getSystemIndicator());
-
- }
- } else {
- newMBR.getPartition(0).setSystemIndicator(IBMPartitionTypes.PARTTYPE_EMPTY);
- newMBR.getPartition(1).setSystemIndicator(IBMPartitionTypes.PARTTYPE_EMPTY);
- newMBR.getPartition(2).setSystemIndicator(IBMPartitionTypes.PARTTYPE_EMPTY);
- newMBR.getPartition(3).setSystemIndicator(IBMPartitionTypes.PARTTYPE_EMPTY);
- }
- newMBR.write(api);
- }
-
+
private static void printTable(String deviceName, DeviceManager dm)
throws DeviceNotFoundException, ApiNotFoundException, IOException {
{
@@ -297,16 +245,16 @@
api.read(0, MBR);
if (IBMPartitionTable.containsPartitionTable(MBR.array())) {
IBMPartitionTable partitionTable = new IBMPartitionTable(new IBMPartitionTableType(), MBR.array(), current);
- int nbPartitions = partitionTable.getLength();
System.out.println(
"Disk : " + current.getId() + ": " + descriptor.getSectorsIn28bitAddressing() * 512 + " bytes");
System.out.println("Device Boot Start End Blocks System");
- for (int i = 0; i < nbPartitions; i++) {
- IBMPartitionTableEntry entry = (IBMPartitionTableEntry)partitionTable.getEntry(i);
- int si = entry.getSystemIndicator();
- if (si != 0)
+ int i = 0;
+ for (IBMPartitionTableEntry entry : partitionTable) {
+ //IBMPartitionTableEntry entry = (IBMPartitionTableEntry)partitionTable.getEntry(i);
+ IBMPartitionTypes si = entry.getSystemIndicator();
+ if (si != IBMPartitionTypes.PARTTYPE_EMPTY)
System.out.println(
"ID "
+ i
@@ -319,7 +267,7 @@
+ " "
+ entry.getNrSectors()
+ " "
- + Integer.toHexString(si));
+ + si);
if(entry.isExtended()) {
final List<IBMPartitionTableEntry> exPartitions = partitionTabl...
[truncated message content] |
|
From: <hag...@us...> - 2008-04-22 12:33:25
|
Revision: 3999
http://jnode.svn.sourceforge.net/jnode/?rev=3999&view=rev
Author: hagar-wize
Date: 2008-04-22 05:33:21 -0700 (Tue, 22 Apr 2008)
Log Message:
-----------
added new syntax and recurcive deleting
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-04-22 10:59:29 UTC (rev 3998)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-04-22 12:33:21 UTC (rev 3999)
@@ -35,6 +35,18 @@
<alias name="hexdump" class="org.jnode.fs.command.HexdumpCommand"/>
</extension>
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="del">
+ <sequence description="delete file or directory">
+ <optionSet>
+ <option argLabel="recurcive" shortName="r" description="delete recurcive" />
+ </optionSet>
+ <argument argLabel="file/dir"/>
+ </sequence>
+ </syntax>
+ </extension>
+
+
<extension point="org.jnode.security.permissions">
<permission class="java.io.FilePermission" name="<<ALL FILES>>" actions="read,write,delete"/>
<permission class="java.net.SocketPermission" name="*:0-" actions="connect,resolve"/>
Modified: trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-04-22 10:59:29 UTC (rev 3998)
+++ trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-04-22 12:33:21 UTC (rev 3999)
@@ -21,20 +21,18 @@
package org.jnode.fs.command;
-import java.io.File;
-import java.io.InputStream;
-import java.io.PrintStream;
-
import org.jnode.fs.service.FileSystemService;
import org.jnode.naming.InitialNaming;
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.argument.FileArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.FlagArgument;
import javax.naming.NameNotFoundException;
+import java.io.File;
+import java.io.InputStream;
+import java.io.PrintStream;
/**
* Delete a file or a empty directory
@@ -42,37 +40,45 @@
* @author Guillaume BINET (gb...@us...)
* @author Andreas H\u00e4nel
* @author Levente S\u00e1ntha
+ * @author Martin Husted Hartvig (hagar at jnode.org)
*/
public class DeleteCommand extends AbstractCommand {
- static final FileArgument ARG_DIR = new FileArgument("file/dir",
- "delete the file or directory", true);
+ private final FileArgument ARG_DIR;
+ private final FlagArgument ARG_OPTION;
- public static Help.Info HELP_INFO = new Help.Info("del",
- "delete a file or directory", new Parameter[]{new Parameter(
- ARG_DIR, Parameter.MANDATORY)});
- public static void main(String[] args) throws Exception {
+ public DeleteCommand() {
+ super("delete files or directories");
+
+ ARG_DIR = new FileArgument("file/dir",Argument.MANDATORY, "delete the file or directory");
+ ARG_OPTION = new FlagArgument("recurcive", Argument.OPTIONAL, "recurcive deleting");
+
+ registerArguments(ARG_DIR, ARG_OPTION);
+ }
+
+ public static void main(String[] args) throws Exception {
new DeleteCommand().execute(args);
}
public void execute(CommandLine commandLine, InputStream in,
PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- File[] file_arr = ARG_DIR.getFiles(cmdLine);
+ boolean recurcive = ARG_OPTION.isSet();
+
+ File[] file_arr = ARG_DIR.getValues();
+
boolean ok = true;
for (File file : file_arr) {
- boolean tmp = deleteFile(file, err);
+ boolean tmp = deleteFile(file, err, recurcive);
ok &= tmp;
}
+
if (!ok) {
exit(1);
}
}
- private boolean deleteFile(File file, PrintStream err) throws NameNotFoundException {
- // for this time, delete only empty directory (wait implementation of -r
- // option)
+ private boolean deleteFile(File file, PrintStream err, boolean recurcive) throws NameNotFoundException {
boolean deleteOk = true;
try {
if (!file.exists()) {
@@ -86,12 +92,19 @@
if (file.isDirectory() && !fss.isMount(file.getAbsolutePath())) {
final File[] subFiles = file.listFiles();
for (File f : subFiles) {
- final String name = f.getName();
- if (!name.equals(".") && !name.equals("..")) {
- err.println("Directory is not empty " + file);
- deleteOk = false;
- break;
+ final String name = f.getName();
+
+ if (!name.equals(".") && !name.equals("..")) {
+ if (!recurcive) {
+ err.println("Directory is not empty " + file);
+ deleteOk = false;
+ break;
}
+ }
+ else
+ {
+ deleteFile(f, err, recurcive);
+ }
}
}
} catch (Exception e) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-04-23 11:42:12
|
Revision: 4004
http://jnode.svn.sourceforge.net/jnode/?rev=4004&view=rev
Author: crawley
Date: 2008-04-23 04:42:09 -0700 (Wed, 23 Apr 2008)
Log Message:
-----------
Correct spelling and clarified some help descriptions.
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-04-22 15:39:41 UTC (rev 4003)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-04-23 11:42:09 UTC (rev 4004)
@@ -37,9 +37,9 @@
<extension point="org.jnode.shell.syntaxes">
<syntax alias="del">
- <sequence description="delete file or directory">
+ <sequence description="delete a file or directory">
<optionSet>
- <option argLabel="recurcive" shortName="r" description="delete recurcive" />
+ <option argLabel="recursive" shortName="r" description="recursively delete a directory's contents" />
</optionSet>
<argument argLabel="file/dir"/>
</sequence>
Modified: trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-04-22 15:39:41 UTC (rev 4003)
+++ trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-04-23 11:42:09 UTC (rev 4004)
@@ -51,8 +51,10 @@
public DeleteCommand() {
super("delete files or directories");
- ARG_DIR = new FileArgument("file/dir",Argument.MANDATORY, "delete the file or directory");
- ARG_OPTION = new FlagArgument("recurcive", Argument.OPTIONAL, "recurcive deleting");
+ ARG_DIR = new FileArgument("file/dir", Argument.MANDATORY,
+ "the file or directory to be deleted");
+ ARG_OPTION = new FlagArgument("recursive", Argument.OPTIONAL,
+ "if set, any directories are deleted recursively");
registerArguments(ARG_DIR, ARG_OPTION);
}
@@ -64,13 +66,13 @@
public void execute(CommandLine commandLine, InputStream in,
PrintStream out, PrintStream err) throws Exception {
- boolean recurcive = ARG_OPTION.isSet();
+ boolean recursive = ARG_OPTION.isSet();
File[] file_arr = ARG_DIR.getValues();
boolean ok = true;
for (File file : file_arr) {
- boolean tmp = deleteFile(file, err, recurcive);
+ boolean tmp = deleteFile(file, err, recursive);
ok &= tmp;
}
@@ -79,7 +81,7 @@
}
}
- private boolean deleteFile(File file, PrintStream err, boolean recurcive) throws NameNotFoundException {
+ private boolean deleteFile(File file, PrintStream err, boolean recursive) throws NameNotFoundException {
boolean deleteOk = true;
try {
if (!file.exists()) {
@@ -97,13 +99,13 @@
final String name = f.getName();
if (!name.equals(".") && !name.equals("..")) {
- if (!recurcive) {
+ if (!recursive) {
err.println("Directory is not empty " + file);
deleteOk = false;
break;
}
else {
- deleteFile(f, err, recurcive);
+ deleteFile(f, err, recursive);
}
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-03 06:24:28
|
Revision: 4042
http://jnode.svn.sourceforge.net/jnode/?rev=4042&view=rev
Author: crawley
Date: 2008-05-02 23:24:24 -0700 (Fri, 02 May 2008)
Log Message:
-----------
Converted 'cat' command
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-03 02:39:44 UTC (rev 4041)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-03 06:24:24 UTC (rev 4042)
@@ -36,6 +36,18 @@
</extension>
<extension point="org.jnode.shell.syntaxes">
+ <syntax alias="cat">
+ <empty description="copy standard input to standard output"/>
+ <sequence description="concatenate urls to standard output">
+ <option argLabel="urls" shortName="u" longName="urls"/>
+ <repeat minCount="1">
+ <argument argLabel="url"/>
+ </repeat>
+ </sequence>
+ <repeat minCount="1" description="concatenate files to standard output">
+ <argument argLabel="file"/>
+ </repeat>
+ </syntax>
<syntax alias="del">
<sequence description="delete a file or directory">
<optionSet>
Modified: trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java 2008-05-03 02:39:44 UTC (rev 4041)
+++ trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java 2008-05-03 06:24:24 UTC (rev 4042)
@@ -32,12 +32,7 @@
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.URLArgument;
+import org.jnode.shell.syntax.*;
/**
* @author epr
@@ -47,28 +42,23 @@
*/
public class CatCommand extends AbstractCommand {
- static final FileArgument ARG_FILE = new FileArgument("file",
- "the files to be concatenated", true);
+ private final FileArgument ARG_FILE =
+ new FileArgument("file", Argument.OPTIONAL | Argument.MULTIPLE,
+ "the files to be concatenated");
- static final URLArgument ARG_URL = new URLArgument("url",
- "the files to be concatenated", true);
+ private final URLArgument ARG_URL =
+ new URLArgument("url", Argument.OPTIONAL | Argument.MULTIPLE,
+ "the urls to be concatenated");
- public static Help.Info HELP_INFO = new Help.Info("cat",
- new Syntax[] {
- new Syntax(
- "Fetch the argument urls and copy their contents to standard output.",
- new Parameter[] {
- new Parameter("u",
- "selects urls rather than pathnames",
- ARG_URL, Parameter.MANDATORY)}),
- new Syntax(
- "Read the argument files, copying their contents to standard output. " +
- "If there are no arguments, standard input is read until EOF is reached; " +
- "e.g. ^D when reading keyboard input.",
- new Parameter[] {
- new Parameter(ARG_FILE, Parameter.OPTIONAL) })
-
- });
+ private final FlagArgument FLAG_URLS =
+ new FlagArgument("urls", Argument.OPTIONAL, "If set, arguments will be urls");
+
+ public CatCommand() {
+ super("Read the argument files or urls, copying their contents to standard output. " +
+ "If there are no arguments, standard input is read until EOF is reached; " +
+ "e.g. ^D when reading keyboard input.");
+ registerArguments(ARG_FILE, ARG_URL, FLAG_URLS);
+ }
private static final int BUFFER_SIZE = 1024;
@@ -77,10 +67,10 @@
new CatCommand().execute(args);
}
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments args = HELP_INFO.parse(commandLine);
- File[] files = ARG_FILE.getFiles(args);
- String[] urls = ARG_URL.getValues(args);
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws Exception {
+ File[] files = ARG_FILE.getValues();
+ String[] urls = ARG_URL.getValues();
boolean ok = true;
try {
if (urls != null && urls.length > 0) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-18 04:37:53
|
Revision: 4106
http://jnode.svn.sourceforge.net/jnode/?rev=4106&view=rev
Author: crawley
Date: 2008-05-17 21:37:38 -0700 (Sat, 17 May 2008)
Log Message:
-----------
Rewrote CpCommand. It now supports multiple arguments, and uses the
new syntax mechanism.
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-18 04:36:21 UTC (rev 4105)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-18 04:37:38 UTC (rev 4106)
@@ -48,6 +48,19 @@
<argument argLabel="file"/>
</repeat>
</syntax>
+ <syntax alias="cp">
+ <sequence description="copy files or directories">
+ <optionSet>
+ <option argLabel="force" shortName="f" longName="force"/>
+ <option argLabel="update" shortName="u" longName="update"/>
+ <option argLabel="interactive" shortName="i" longName="interactive"/>
+ <option argLabel="recursive" shortName="r" longName="recursive"/>
+ <option argLabel="verbose" shortName="v" longName="verbose"/>
+ </optionSet>
+ <repeat minCount="1"><argument argLabel="source"/></repeat>
+ <argument argLabel="target"/>
+ </sequence>
+ </syntax>
<syntax alias="del">
<sequence description="delete a file or directory">
<optional>
Modified: trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java 2008-05-18 04:36:21 UTC (rev 4105)
+++ trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java 2008-05-18 04:37:38 UTC (rev 4106)
@@ -21,6 +21,7 @@
package org.jnode.fs.command;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -29,554 +30,365 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
+import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.Syntax;
-import org.jnode.shell.help.argument.FileArgument;
-import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.syntax.*;
/**
+ * File copy utility. This utility copies one file to another file, or multiple files or directories
+ * into a directory. Recursive directory copy is supported.
*
- * Copy a File to a File or Directory
- *
- * Syntaxe : cp [-u | -f | -i] [-v] [-r] file/url-src file-dst Exemples cp -ur
- * -v d:/adir d:/temp
- *
- *
- * TODO : support of patern file source (cp *.j*g /dir/) support file name with
- * strange char like * \ /
- *
- * @author Yves Galante (yve...@jm...)
- * @version 0.1
- *
+ * @author cr...@jn...
*/
-public class CpCommand {
+public class CpCommand extends AbstractCommand {
- static final byte MODE_BASE = 0;
-
- static final byte MODE_ITERATIVE = 1;
-
+ static final byte MODE_NORMAL = 0;
+ static final byte MODE_INTERACTIVE = 1;
static final byte MODE_FORCE = 2;
-
static final byte MODE_UPDATE = 3;
- static final char MODE_FORCE_C = 'f';
+ private final FileArgument ARG_SOURCE =
+ new FileArgument("source", Argument.MANDATORY + Argument.MULTIPLE, "source files or directories");
- static final char MODE_INTER_C = 'i';
+ private final FileArgument ARG_TARGET =
+ new FileArgument("target", Argument.MANDATORY, "target file or directory");
- static final char MODE_UPDATE_C = 'u';
+ private final FlagArgument FLAG_FORCE =
+ new FlagArgument("force", Argument.OPTIONAL, "if set, force overwrite of existing files");
+
+ private final FlagArgument FLAG_INTERACTIVE =
+ new FlagArgument("interactive", Argument.OPTIONAL, "if set, ask before overwriting existing files");
+
+ private final FlagArgument FLAG_UPDATE =
+ new FlagArgument("update", Argument.OPTIONAL, "if set, overwrite existing files older than their source");
+
+ private final FlagArgument FLAG_RECURSIVE =
+ new FlagArgument("recursive", Argument.OPTIONAL, "if set, recursively copy source directories");
+
+ private final FlagArgument FLAG_VERBOSE =
+ new FlagArgument("verbose", Argument.OPTIONAL, "if set, output a line for each file copied");
- static final char OPTION_VERBOSE_C = 'v';
+ private byte mode = MODE_NORMAL;
+ private boolean recursive = false;
+ private boolean verbose = false;
+ private int filesCopied = 0;
+ private int directoriesCreated = 0;
+ private BufferedReader in;
+ private PrintStream out;
+ private PrintStream err;
+ private byte[] buffer = new byte[1024 * 8];
- static final char OPTION_RECURCIVE_C = 'r';
-
- static final FileArgument ARG_FSRC = new FileArgument("file or url",
- "file or url source");
-
- static final FileArgument ARG_FDST = new FileArgument("file", "file dest");
-
- static final OptionArgument ARG_OPT1 = new OptionArgument(
- "option",
- "a option",
- new OptionArgument.Option[] {
- new OptionArgument.Option("-" + MODE_FORCE_C + "",
- "if an existing destination file overwrite"),
- new OptionArgument.Option("-" + MODE_INTER_C + "",
- "if an existing destination file ask"),
- new OptionArgument.Option("-" + MODE_UPDATE_C + "",
- "if an existing destination file overwrite only if\ndate is older")});
-
- static final OptionArgument ARG_OPT2 = new OptionArgument("option",
- "a option",
- new OptionArgument.Option[] { new OptionArgument.Option("-"
- + OPTION_VERBOSE_C + "", "verbose")});
-
- static final OptionArgument ARG_OPT3 = new OptionArgument("option",
- "a option",
- new OptionArgument.Option[] { new OptionArgument.Option("-"
- + OPTION_RECURCIVE_C + "", "recursie")});
-
- public static Help.Info HELP_INFO = new Help.Info("cp",
- new Syntax[] { new Syntax("copy a file or url to a file",
- new Parameter[] {
- new Parameter(ARG_OPT1, Parameter.OPTIONAL),
- new Parameter(ARG_OPT2, Parameter.OPTIONAL),
- new Parameter(ARG_OPT3, Parameter.OPTIONAL),
- new Parameter(ARG_FSRC, Parameter.MANDATORY),
- new Parameter(ARG_FDST, Parameter.MANDATORY)})});
-
- /**
- * ****************************** non static filed
- * ************************************
- */
-
- private URL _urlSrc;
-
- private File _fileSrc;
-
- private File _fileDst;
-
- private byte _mode = MODE_BASE;
-
- private boolean _recursive = false;
-
- private boolean _verbose = false;
-
- private boolean _srcIsDir = false;
-
- private boolean _argsOk = true;
-
- final byte[] buf = new byte[128*1024];
-
- /**
- * Construct this class with args[] [-u | -f | -i] [-v] [-r] file/url-src
- * file-dst
- *
- * @param args
- * commande argument
- */
- public CpCommand(String[] args) {
- _argsOk = parseOption(args);
+ public CpCommand() {
+ super("Copy files or directories");
+ registerArguments(ARG_SOURCE, ARG_TARGET, FLAG_FORCE, FLAG_INTERACTIVE, FLAG_RECURSIVE,
+ FLAG_UPDATE, FLAG_VERBOSE);
}
-
- /**
- * Constructor with file
- *
- * @param fileSrc
- * @param fileDst
- * @param mode
- * @param recursive
- * @param verbose
- */
- public CpCommand(File fileSrc, File fileDst, byte mode, boolean recursive,
- boolean verbose) {
- _fileSrc = fileSrc;
- _fileDst = fileDst;
- _mode = mode;
- _recursive = recursive;
- _verbose = verbose;
+
+ public static void main(String[] args) throws Exception {
+ new CpCommand().execute(args);
}
- /**
- * Constructor with URL
- *
- * @param urlSrc
- * @param fileDst
- * @param mode
- * @param verbose
- */
- public CpCommand(URL urlSrc, File fileDst, byte mode, boolean verbose) {
- _urlSrc = urlSrc;
- _fileDst = fileDst;
- _mode = mode;
- _verbose = verbose;
+ public void execute(CommandLine commandLine, InputStream in,
+ PrintStream out, PrintStream err) throws Exception {
+ this.out = out;
+ this.err = err;
+ processFlags();
+ if (mode == MODE_INTERACTIVE) {
+ this.in = new BufferedReader(new InputStreamReader(in));
+ }
+ File[] sources = ARG_SOURCE.getValues();
+ File target = ARG_TARGET.getValue();
+ if (sources.length == 0) {
+ error("No source files or directories supplied");
+ }
+ if (target.isDirectory()) {
+ if (!target.canWrite()) {
+ error("Target directory is not writable");
+ }
+ for (File source : sources) {
+ if (checkSafe(source, target)) {
+ copyIntoDirectory(source, target);
+ }
+ }
+ }
+ else if (sources.length > 1) {
+ error("Multi-file copy requires the target to be a directory");
+ }
+ else {
+ File source = sources[0];
+ if (source.isDirectory()) {
+ error("Cannot copy a directory to a file");
+ }
+ else if (target.exists() && !target.isFile()) {
+ error("Cannot copy to a device");
+ }
+ else {
+ if (checkSafe(source, target)) {
+ copyToFile(source, target);
+ }
+ }
+ }
+ if (verbose) {
+ out.println("Files copied: " + filesCopied + ", directories created: " + directoriesCreated);
+ }
}
- /**
- * Copy
- *
- * @return Return the number of file copied
- */
- public int copy() throws Exception {
- if (!_argsOk) {
- HELP_INFO.parse(new String[ 0]);
- return 0;
+ private void processFlags() {
+ recursive = FLAG_RECURSIVE.isSet();
+ verbose = FLAG_VERBOSE.isSet();
+ // The mode flags are mutually exclusive ...
+ if (FLAG_FORCE.isSet()) {
+ mode = MODE_FORCE;
}
- if (_fileSrc != null) {
- if (!check()) { return 0; }
- if (_srcIsDir) {
- return copyDir();
- } else {
- return copyFile();
+ if (FLAG_INTERACTIVE.isSet()) {
+ if (mode != MODE_NORMAL) {
+ error("The 'force', 'interactive' and 'update' flags are mutually exclusive");
}
- } else {
- if (!checkURL()) { return 0; }
- return copyURL();
+ mode = MODE_INTERACTIVE;
}
-
+ if (FLAG_UPDATE.isSet()) {
+ if (mode != MODE_NORMAL) {
+ error("The 'force', 'interactive' and 'update' flags are mutually exclusive");
+ }
+ mode = MODE_UPDATE;
+ }
}
-
+
/**
- * Syntaxe [-u | -f | -i] [-v] [-r] filesrc filedst Exemple cp -uv file1
- * file2 cp -f -r file1 file2
+ * Copy a file or directory into a supplied target directory.
*
- * @param args
- *
- * @since 30 mars 04
+ * @param source the name of the object to be copied
+ * @param targetDir the destination directory
+ * @throws IOException
*/
- private boolean parseOption(String[] args) {
-
- String options;
- StringBuilder sb = new StringBuilder(5);
-
- if (args.length < 2 || args.length > 10) { return false; }
-
- // concat all options
- for (int i = 0; i < args.length - 2; i++) {
- if (args[ i].charAt(0) != '-') return false;
- sb.append(args[ i].substring(1));
+ private void copyIntoDirectory(File source, File targetDir) throws IOException {
+ if (!targetDir.canWrite()) {
+ skip("directory '" + targetDir + "' is not writable");
}
- options = sb.toString();
- sb = null;
- // parse options
- for (int i = 0; i < options.length(); i++) {
- char option = options.charAt(i);
- switch (option) {
- case MODE_FORCE_C:
- if (_mode == MODE_BASE) {
- _mode = MODE_FORCE;
- } else {
- return false;
+ else if (source.isDirectory()) {
+ if (recursive) {
+ File newDir = new File(targetDir, source.getName());
+ if (!newDir.exists()) {
+ if (verbose) {
+ out.println("Creating directory '" + newDir + "'");
+ }
+ newDir.mkdir();
+ directoriesCreated++;
}
- break;
- case MODE_UPDATE_C:
- if (_mode == MODE_BASE) {
- _mode = MODE_UPDATE;
- } else {
- return false;
+ else if (!newDir.isDirectory()) {
+ if (mode == MODE_FORCE) {
+ if (verbose) {
+ out.println("Replacing file '" + newDir + "' with a directory");
+ }
+ newDir.delete();
+ newDir.mkdir();
+ directoriesCreated++;
+ }
+ else {
+ skip("not overwriting '" + newDir + "' with a directory");
+ return;
+ }
}
- break;
- case MODE_INTER_C:
- if (_mode == MODE_BASE) {
- _mode = MODE_ITERATIVE;
- } else {
- return false;
+ String[] contents = source.list();
+ for (String name : contents) {
+ if (name.equals(".") || name.equals("..")) {
+ continue;
+ }
+ copyIntoDirectory(new File(source, name), newDir);
}
- break;
- case OPTION_VERBOSE_C:
- if (!_verbose) {
- _verbose = true;
- } else {
- return false;
- }
- break;
- case OPTION_RECURCIVE_C:
- if (!_recursive) {
- _recursive = true;
- } else {
- return false;
- }
- break;
- default:
- return false;
}
-
- }
-
- if (args[ args.length - 2].indexOf("://") >= 0) {
- try {
- _urlSrc = new URL(args[ args.length - 2]);
- } catch (MalformedURLException urlEx) {
- _urlSrc = null;
- System.err.println(args[ args.length - 2] + " is malformed");
- return false;
+ else {
+ skip("'" + source + "' is a directory");
}
}
- if (_urlSrc == null) {
- _fileSrc = new File(args[ args.length - 2]);
+ else {
+ File newFile = new File(targetDir, source.getName());
+ copyToFile(source, newFile);
}
- _fileDst = new File(args[ args.length - 1]);
- return true;
}
-
+
/**
- * Make some check on src url and on dst file
+ * Copy a file to (as) a file
*
- * @return
+ * @param sourceFile
+ * @param targetFile
+ * @throws IOException
*/
- private boolean checkURL() {
-
- if (_urlSrc == null || _fileDst == null) { return false; }
- // now file is converted to ftp
- //if(_urlSrc.getProtocol().equalsIgnoreCase("file")){
- // _urlSrc=null;
- // System.err.println(_urlSrc.toExternalForm()+" file protocol not
- // supported!");
- // return false;
- //}
- if (_fileDst.isDirectory()) { // directory not supported
- System.err.println(_fileDst.getAbsolutePath() + " is a directory!");
- return false;
+ private void copyToFile(File sourceFile, File targetFile) throws IOException {
+ if (!checkSafe(sourceFile, targetFile) ||
+ !checkSource(sourceFile) ||
+ !checkTarget(targetFile, sourceFile)) {
+ return;
}
-
- return checkDstOverwrite();
-
- }
-
- /**
- * Make some check on src file and on dst file
- *
- */
- private boolean check() {
-
- if (_fileSrc == null || _fileDst == null) { return false; }
-
- // same file check
- if (_fileSrc.toString().equalsIgnoreCase(_fileDst.toString())) {
- System.err.println("Same file !!!");
- return false;
+ if (verbose) {
+ out.println("Copying file '" + sourceFile + "' as '" + targetFile + "'");
}
-
- // file src check
- if (!_fileSrc.exists()) {
- System.err.println(_fileSrc.getAbsolutePath() + " not found");
- return false;
+
+ InputStream sin = null;
+ OutputStream tout = null;
+ try {
+ sin = new FileInputStream(sourceFile);
+ tout = new FileOutputStream(targetFile);
+ while (true) {
+ int nosBytesRead = sin.read(buffer);
+ if (nosBytesRead <= 0) {
+ break;
+ }
+ tout.write(buffer, 0, nosBytesRead);
+ }
}
- if (!_fileSrc.canRead()) {
- System.err.println("You havn't right too read "
- + _fileSrc.getAbsolutePath());
- return false;
- }
-
- // Source file is a directory ?
- if (_fileSrc.isDirectory()) {
- if (!_recursive) {
- System.err.println("For recursive copy, please use option -r");
- return false;
+ finally {
+ if (sin != null) {
+ try {
+ sin.close();
+ }
+ catch (IOException ex) {
+ // ignore
+ }
}
- if (_fileDst.isFile()) {
- System.err.println(_fileDst.getAbsolutePath()
- + " is not a directory ");
- return false;
+ if (tout != null) {
+ try {
+ tout.close();
+ }
+ catch (IOException ex) {
+ // ignore
+ }
}
- _srcIsDir = true;
- return true; // OK !
}
- // src not a dir, need more check
- _srcIsDir = false;
- // dst file is a directory ?
- if (_fileDst.isDirectory()) {
- _fileDst = new File(_fileDst.getAbsolutePath() + File.separator
- + _fileSrc.getName());
- }
-
- return checkDstOverwrite();
+ filesCopied++;
}
/**
- * We can overwrite the dst file ?
+ * Check that a source object exists, is readable and is either
+ * a file or a directory.
*
+ * @param source
* @return
*/
- private boolean checkDstOverwrite() {
- if (_fileDst.isFile()) {
- switch (_mode) {
- case MODE_BASE:
- System.err.println(_fileDst.getAbsolutePath()
- + " already exist");
- return false;
- case MODE_UPDATE:
- if (_fileSrc == null
- || _fileSrc.lastModified() <= _fileDst.lastModified()) { return false; }
- break;
- case MODE_ITERATIVE:
- if (!overWriteQuerry(_fileDst.getAbsolutePath())) { return false; }
- break;
- case MODE_FORCE:
- break;
- default:
- return false;
- }
- if (!_fileDst.canWrite()) {
- System.err.println(_fileDst.getAbsolutePath()
- + " cannot overwrite");
- return false;
- }
+ private boolean checkSource(File source) {
+ if (!source.exists()) {
+ return skip("'" + source + "' does not exist");
}
- return true;
- }
-
- /**
- * Copy the file src to the dst file
- *
- * @return number of copied file
- */
- private int copyFile() {
- InputStream is;
- FileOutputStream os;
- try {
- is = new FileInputStream(_fileSrc);
- } catch (FileNotFoundException fEx) {
- System.err.println(_fileSrc.getAbsolutePath() + " not found");
- return 0;
+ else if (!source.canRead()) {
+ return skip("'" + source + "' cannot be read");
}
- os = initDstFile();
- if (os == null) return 0;
-
- if (!copyData(is, os)) { return 0; }
- if (_verbose) {
- System.out.println("Copie : " + _fileSrc.getAbsolutePath() + " to "
- + _fileDst.getAbsolutePath());
+ else if (!(source.isFile() || source.isDirectory())) {
+ return vskip("'" + source + "' is a device");
}
- return 1;
- }
-
- private int copyDir() throws Exception {
- final File dstDir = new File(_fileDst.getAbsoluteFile()
- + File.separator);//+_fileSrc.getName()+File.separator);
- final File[] subFile = _fileSrc.listFiles();
- int numberFileCopied = 0;
-
- if (!dstDir.exists()) {
- if (!dstDir.mkdir()) {
- System.err.println(_fileDst.getParentFile().getAbsolutePath()
- + " cannot create ");
- return 0;
- }
+ else {
+ return true;
}
- for (int i = 0; i < subFile.length; i++) {
- final String name = subFile[ i].getName();
- if (name.equals(".") || name.equals("..")) continue;
- File subDstDir = new File(dstDir.getAbsoluteFile() + File.separator
- + name);
- CpCommand cp = new CpCommand(subFile[ i], subDstDir, _mode,
- _recursive, _verbose);
- numberFileCopied += cp.copy();
- }
- return numberFileCopied;
}
/**
- * Copy the url src to the dst file
+ * Check that a copy is going to be safe. Unsafe things are copying a
+ * file to itself and copying a directory into itself or a subdirectory.
*
- * @return number of copied file
- */
- private int copyURL() {
- InputStream is;
- FileOutputStream os;
-
- os = initDstFile();
- if (os == null) return 0;
- try {
- is = _urlSrc.openStream();
- } catch (IOException fEx) {
- System.err.println("File not found " + _urlSrc.toExternalForm());
- return 0;
- }
-
- if (!copyData(is, os)) { return 0; }
- if (_verbose) {
- System.out.println("Copie : " + _urlSrc.toExternalForm() + " to "
- + _fileDst.getAbsolutePath());
- }
- return 1;
- }
-
- /**
- * Copy an inputstream to a outputstream
- *
- * @param is
- * @param os
+ * @param source
+ * @param target
* @return
+ * @throws IOException
*/
- private boolean copyData(InputStream is, OutputStream os) {
- try {
- int len = 0;
- //final byte[] buf = new byte[128*1024];
- while ((len = is.read(buf)) > 0) {
- os.write(buf, 0, len);
+ private boolean checkSafe(File source, File target) throws IOException {
+ // These checks must be done with canonical paths. But fortunately they
+ // don't need to be repeated for every file/directory in a recursive copy.
+ String sourcePath = source.getCanonicalPath();
+ String targetPath = target.getCanonicalPath();
+ if (target.isDirectory()) {
+ if (recursive && source.isDirectory()) {
+ if (sourcePath.equals(targetPath)) {
+ return skip("Cannot copy directory '" + source + "' into itself");
+ }
+ if (!sourcePath.endsWith(File.separator)) {
+ sourcePath = sourcePath + File.separatorChar;
+ }
+ if (targetPath.startsWith(sourcePath)) {
+ return skip("Cannot copy directory '" + source +
+ "' into a subdirectory ('" + target + "')");
+ }
}
- os.close();
- is.close();
- } catch (IOException io) {
- System.err.println("An error is occure : copy aborted");
- io.printStackTrace();
- try {
- _fileDst.delete(); // clean
- } catch (Exception ex) {
+ }
+ else {
+ if (sourcePath.equals(targetPath)) {
+ return skip("Cannot copy file '" + source + "' to itself");
}
- return false;
}
return true;
}
/**
- * Open the dst file Make parent is need Delete if exist
+ * Check that the target can be written / overwritten. In interactive mode,
+ * the user is asked about clobbering existing files. In update mode, they
+ * are overwritten if the source is newer than the target. In force mode, they
+ * are clobbered without asking. In normal mode, existing target files are
+ * skipped.
*
- * @return the file outputstream
+ * @param target
+ * @param source
+ * @return
*/
- private FileOutputStream initDstFile() {
- final File dstParent = _fileDst.getParentFile();
- if (dstParent != null && !dstParent.exists()) { // make parent dir
- if (!dstParent.mkdirs()) {
- System.err.println("Directory can't create "
- + _fileDst.getParentFile().getAbsolutePath());
- return null;
- }
+ private boolean checkTarget(File target, File source) {
+ if (!target.exists()) {
+ return true;
}
- if (_fileDst.exists() && !_fileDst.delete()) {
- System.err.println("File can't overwrite "
- + _fileDst.getAbsolutePath());
- return null;
+ if (target.isDirectory() && !source.isDirectory()) {
+ return skip("Cannot copy '" + source + "' onto directory '" + target + "'");
}
-
- try {
- if (!_fileDst.createNewFile()) {
- System.err.println("File can't create "
- + _fileDst.getAbsolutePath());
- return null;
- }
- return new FileOutputStream(_fileDst);
- } catch (IOException io) {
- System.err.println("File can't create "
- + _fileDst.getAbsolutePath());
- } catch (SecurityException sec) {
- System.err.println("You havn't right too create "
- + _fileDst.getAbsolutePath());
+ if (!target.isFile()) {
+ return vskip("Cannot copy '" + source + "' to device '" + target + "'");
}
- return null;
-
- }
-
- // STATIC METHODES
-
- public static void main(String[] args) throws Exception {
- CpCommand cpCommand = new CpCommand(args);
- int nbCopiedFile = cpCommand.copy();
- System.out.println(nbCopiedFile + " file(s) copied");
- }
-
- /**
- * Ask user for owerwrite a file
- *
- * @param fileName
- * @return
- */
- private static boolean overWriteQuerry(String fileName) {
-
- final StringBuilder sbRead = new StringBuilder(256);
- final InputStreamReader reader = new InputStreamReader(System.in);
-
- String reponse = null;
-
- System.out.print("Overwrite " + fileName + " [N] ? ");
- try {
- int readInt;
- while ((readInt = reader.read()) > 0) {
- char ch = (char) readInt;
- if (ch == '\r') {
- continue;
+ switch (mode) {
+ case MODE_NORMAL:
+ return vskip("'" + target + "' already exists");
+ case MODE_FORCE:
+ return true;
+ case MODE_UPDATE:
+ return (source.lastModified() > target.lastModified() ||
+ vskip("'" + target + "' is newer than '" + source + "'"));
+ case MODE_INTERACTIVE:
+ out.print("Overwrite '" + target + "' with '" + source + "'? [y/n]");
+ while (true) {
+ try {
+ String line = in.readLine();
+ if (line == null) {
+ error("EOF - abandoning copying");
+ }
+ if (line.length() > 0) {
+ if (line.charAt(0) == 'y' || line.charAt(0) == 'Y') {
+ return true;
+ }
+ else if (line.charAt(0) == 'n' || line.charAt(0) == 'N') {
+ return vskip("'" + target + "'");
+ }
+ }
+ out.print("Answer 'y' or 'n'");
}
- if (ch == '\n') {
- break;
+ catch (IOException ex) {
+ error("IO Error - abandoning copying");
}
- sbRead.append(ch);
- System.out.print(ch); // Console echo
}
- System.out.println();...
[truncated message content] |
|
From: <cr...@us...> - 2008-05-23 15:48:33
|
Revision: 4118
http://jnode.svn.sourceforge.net/jnode/?rev=4118&view=rev
Author: crawley
Date: 2008-05-23 08:48:30 -0700 (Fri, 23 May 2008)
Log Message:
-----------
Converted the format commands
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.ext2.command.xml
trunk/fs/descriptors/org.jnode.fs.fat.command.xml
trunk/fs/descriptors/org.jnode.fs.jfat.command.xml
trunk/fs/src/fs/org/jnode/fs/command/AbstractFormatCommand.java
trunk/fs/src/fs/org/jnode/fs/ext2/command/FormatExt2Command.java
trunk/fs/src/fs/org/jnode/fs/fat/command/FormatFatCommand.java
trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.ext2.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.ext2.command.xml 2008-05-22 13:39:44 UTC (rev 4117)
+++ trunk/fs/descriptors/org.jnode.fs.ext2.command.xml 2008-05-23 15:48:30 UTC (rev 4118)
@@ -22,4 +22,13 @@
<extension point="org.jnode.shell.aliases">
<alias name="formatExt2" class="org.jnode.fs.ext2.command.FormatExt2Command"/>
</extension>
+
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="formatExt2">
+ <sequence description="format an EXT2 file system">
+ <optional><option argLabel="blockSize" shortName="b" longName="blocksize"/></optional>
+ <argument argLabel="device"/>
+ </sequence>
+ </syntax>
+ </extension>
</plugin>
\ No newline at end of file
Modified: trunk/fs/descriptors/org.jnode.fs.fat.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.fat.command.xml 2008-05-22 13:39:44 UTC (rev 4117)
+++ trunk/fs/descriptors/org.jnode.fs.fat.command.xml 2008-05-23 15:48:30 UTC (rev 4118)
@@ -22,4 +22,13 @@
<extension point="org.jnode.shell.aliases">
<alias name="formatFat" class="org.jnode.fs.fat.command.FormatFatCommand"/>
</extension>
+
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="formatFat">
+ <sequence description="format a FAT32 file system">
+ <optional><option argLabel="fsType" shortName="t" longName="fstype"/></optional>
+ <argument argLabel="device"/>
+ </sequence>
+ </syntax>
+ </extension>
</plugin>
\ No newline at end of file
Modified: trunk/fs/descriptors/org.jnode.fs.jfat.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.jfat.command.xml 2008-05-22 13:39:44 UTC (rev 4117)
+++ trunk/fs/descriptors/org.jnode.fs.jfat.command.xml 2008-05-23 15:48:30 UTC (rev 4118)
@@ -24,6 +24,15 @@
<alias name="grub" class="org.jnode.fs.jfat.command.JGrubInstallCommand"/>
<alias name="formatJFat" class="org.jnode.fs.jfat.command.FatFormatCommand"/>
</extension>
+
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="formatJFat">
+ <sequence description="format a JFat file system">
+ <optional><option argLabel="clusterSize" shortName="c" longName="clustersize"/></optional>
+ <argument argLabel="device"/>
+ </sequence>
+ </syntax>
+ </extension>
<extension point="org.jnode.security.permissions">
<permission class="java.io.FilePermission" name="<<ALL FILES>>" actions="read,write,delete"/>
Modified: trunk/fs/src/fs/org/jnode/fs/command/AbstractFormatCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/AbstractFormatCommand.java 2008-05-22 13:39:44 UTC (rev 4117)
+++ trunk/fs/src/fs/org/jnode/fs/command/AbstractFormatCommand.java 2008-05-23 15:48:30 UTC (rev 4118)
@@ -37,56 +37,35 @@
import org.jnode.naming.InitialNaming;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.ParsedArguments;
-import org.jnode.shell.help.SyntaxErrorException;
-import org.jnode.shell.help.argument.DeviceArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.DeviceArgument;
/**
* @author Fabien DUMINY (fduminy at jnode.org)
- *
+ * @author cr...@jn...
*/
-abstract public class AbstractFormatCommand<T extends FileSystem<?>> extends AbstractCommand {
- private static final DeviceArgument ARG_DEVICE = new DeviceArgument("device-id",
- "the device to format");
+public abstract class AbstractFormatCommand<T extends FileSystem<?>> extends AbstractCommand {
+
+ protected final DeviceArgument ARG_DEVICE =
+ new DeviceArgument("device", Argument.MANDATORY, "the device to format", FSBlockDeviceAPI.class);
+
+ public AbstractFormatCommand(String description) {
+ super(description);
+ registerArguments(ARG_DEVICE);
+ }
- protected static final Parameter PARAM_DEVICE = new Parameter(ARG_DEVICE,
- Parameter.MANDATORY);
+ abstract protected Formatter<T> getFormatter();
- abstract protected ParsedArguments parse(CommandLine commandLine) throws SyntaxErrorException;
- abstract protected Formatter<T> getFormatter(ParsedArguments cmdLine);
+ final public void execute(CommandLine commandLine, InputStream in,
+ PrintStream out, PrintStream err)
+ throws FileSystemException, NameNotFoundException, DeviceNotFoundException, DriverException {
+ Device dev = ARG_DEVICE.getValue();
+ Formatter<T> formatter = getFormatter();
+ formatter.format(dev);
- final public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- try {
- ParsedArguments cmdLine = parse(commandLine);
-
- String device = ARG_DEVICE.getValue(cmdLine);
- Formatter<T> formatter = getFormatter(cmdLine);
-
- DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
-
- Device dev = dm.getDevice(device);
- if(!(dev.getDriver() instanceof FSBlockDeviceAPI)){
- throw new FileSystemException(
- "device unsupported by format command");
- }
- formatter.format(dev);
-
- // restart the device
- dm.stop(dev);
- dm.start(dev);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- exit(1);
- } catch (DeviceNotFoundException e) {
- e.printStackTrace();
- exit(2);
- } catch (DriverException e) {
- e.printStackTrace();
- exit(3);
- } catch (FileSystemException e) {
- e.printStackTrace();
- exit(4);
- }
- }
+ // restart the device
+ final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
+ dm.stop(dev);
+ dm.start(dev);
+ }
}
Modified: trunk/fs/src/fs/org/jnode/fs/ext2/command/FormatExt2Command.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/ext2/command/FormatExt2Command.java 2008-05-22 13:39:44 UTC (rev 4117)
+++ trunk/fs/src/fs/org/jnode/fs/ext2/command/FormatExt2Command.java 2008-05-23 15:48:30 UTC (rev 4118)
@@ -21,52 +21,55 @@
package org.jnode.fs.ext2.command;
+import java.util.HashMap;
+import java.util.Map;
+
import org.jnode.fs.command.AbstractFormatCommand;
import org.jnode.fs.ext2.BlockSize;
import org.jnode.fs.ext2.Ext2FileSystem;
import org.jnode.fs.ext2.Ext2FileSystemFormatter;
-import org.jnode.fs.fat.FatType;
-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.SyntaxErrorException;
-import org.jnode.shell.help.argument.EnumOptionArgument;
-import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.fs.jfat.ClusterSize;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.EnumArgument;
+import org.jnode.shell.syntax.MappedArgument;
/**
* @author gbin
+ * @author cr...@jn...
*/
-@SuppressWarnings("unchecked")
public class FormatExt2Command extends AbstractFormatCommand<Ext2FileSystem> {
- static final EnumOptionArgument<BlockSize> BS_VAL = new EnumOptionArgument<BlockSize>("blocksize",
- "block size for ext2 filesystem",
- new EnumOptionArgument.EnumOption<BlockSize>("1", "1Kb", BlockSize._1Kb),
- new EnumOptionArgument.EnumOption<BlockSize>("2", "2Kb", BlockSize._2Kb),
- new EnumOptionArgument.EnumOption<BlockSize>("4", "4Kb", BlockSize._4Kb));
+
+ private static class BlockSizeArgument extends MappedArgument<BlockSize> {
+ private static final Map<String, BlockSize> MAP = new HashMap<String, BlockSize> ();
+ static {
+ MAP.put("1k", BlockSize._1Kb);
+ MAP.put("2k", BlockSize._2Kb);
+ MAP.put("4k", BlockSize._4Kb);
+ }
+ public BlockSizeArgument() {
+ super("blockSize", Argument.OPTIONAL, new BlockSize[0],
+ MAP, true, "block size for EXT2 filesystem (default 4k)");
+ }
- static final Parameter PARAM_BS_VAL = new Parameter(BS_VAL,
- Parameter.OPTIONAL);
+ @Override
+ protected String argumentKind() {
+ return "block size";
+ }
+ }
+
+ private final BlockSizeArgument ARG_BLOCK_SIZE = new BlockSizeArgument();
- public static Help.Info HELP_INFO = new Help.Info("format",
- new Syntax[] { new Syntax(
- "Format a block device with ext2 filesystem",
- new Parameter[] { PARAM_DEVICE, PARAM_BS_VAL }) });
+ public FormatExt2Command() {
+ super("Format a block device with ext2 filesystem");
+ registerArguments(ARG_BLOCK_SIZE);
+ }
public static void main(String[] args) throws Exception {
new FormatExt2Command().execute(args);
}
@Override
- protected Ext2FileSystemFormatter getFormatter(ParsedArguments cmdLine) {
- BlockSize bsize = BS_VAL.getEnum(cmdLine, BlockSize.class);
- return new Ext2FileSystemFormatter(bsize);
+ protected Ext2FileSystemFormatter getFormatter() {
+ return new Ext2FileSystemFormatter(ARG_BLOCK_SIZE.getValue());
}
-
- @Override
- protected ParsedArguments parse(CommandLine commandLine)
- throws SyntaxErrorException {
- return HELP_INFO.parse(commandLine);
- }
}
Modified: trunk/fs/src/fs/org/jnode/fs/fat/command/FormatFatCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/fat/command/FormatFatCommand.java 2008-05-22 13:39:44 UTC (rev 4117)
+++ trunk/fs/src/fs/org/jnode/fs/fat/command/FormatFatCommand.java 2008-05-23 15:48:30 UTC (rev 4118)
@@ -21,59 +21,54 @@
package org.jnode.fs.fat.command;
+import java.util.HashMap;
+import java.util.Map;
+
import org.jnode.fs.command.AbstractFormatCommand;
-import org.jnode.fs.fat.Fat;
import org.jnode.fs.fat.FatFileSystem;
import org.jnode.fs.fat.FatFileSystemFormatter;
import org.jnode.fs.fat.FatType;
-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.SyntaxErrorException;
-import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.MappedArgument;
/**
+ * This command formats a FAT12 or FAT16 filesystem.
+ *
* @author gbin
+ * @author cr...@jn...
*/
public class FormatFatCommand extends AbstractFormatCommand<FatFileSystem> {
+
+ private static class FatTypeArgument extends MappedArgument<FatType> {
+ private static final Map<String, FatType> MAP = new HashMap<String, FatType>();
+ static {
+ MAP.put("fat12", FatType.FAT12);
+ MAP.put("fat16", FatType.FAT16);
+ }
+
+ public FatTypeArgument() {
+ super("fsType", Argument.MANDATORY, new FatType[0], MAP,
+ true, "the kind of FAT filesystem to create");
+ }
+ @Override
+ protected String argumentKind() {
+ return "fat type";
+ }
+ }
+
+ private final FatTypeArgument ARG_FS_TYPE = new FatTypeArgument();
- static final OptionArgument FS = new OptionArgument("fstype",
- "FAT type", new OptionArgument.Option[] {
- new OptionArgument.Option("fat16", "FAT 16 filesystem"),
- new OptionArgument.Option("fat12", "FAT 12 filesystem")});
+ public FormatFatCommand() {
+ super("Format a block device with a FAT12 or FAT16 filesystem");
+ registerArguments(ARG_FS_TYPE);
+ }
- static final Parameter PARAM_FS = new Parameter(FS, Parameter.MANDATORY);
-
- public static Help.Info HELP_INFO = new Help.Info("format",
- new Syntax[] { new Syntax(
- "Format a block device with fat filesystem",
- new Parameter[] { PARAM_DEVICE, PARAM_FS}) });
-
public static void main(String[] args) throws Exception {
new FormatFatCommand().execute(args);
}
@Override
- protected FatFileSystemFormatter getFormatter(ParsedArguments cmdLine) {
- String FSType = FS.getValue(cmdLine).intern();
-
- FatFileSystemFormatter formatter = null;
- if (FSType == "fat16") {
- formatter = new FatFileSystemFormatter(FatType.FAT16);
- } else if (FSType == "fat12") {
- formatter = new FatFileSystemFormatter(FatType.FAT32);
- } else
- throw new IllegalArgumentException(
- "invalid fat type");
-
- return formatter;
+ protected FatFileSystemFormatter getFormatter() {
+ return new FatFileSystemFormatter(ARG_FS_TYPE.getValue());
}
-
- @Override
- protected ParsedArguments parse(CommandLine commandLine)
- throws SyntaxErrorException {
- return HELP_INFO.parse(commandLine);
- }
}
Modified: trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java 2008-05-22 13:39:44 UTC (rev 4117)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java 2008-05-23 15:48:30 UTC (rev 4118)
@@ -20,78 +20,65 @@
*/
package org.jnode.fs.jfat.command;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.log4j.Logger;
import org.jnode.fs.command.AbstractFormatCommand;
-import org.jnode.fs.ext2.BlockSize;
import org.jnode.fs.jfat.ClusterSize;
import org.jnode.fs.jfat.FatFileSystem;
import org.jnode.fs.jfat.FatFileSystemFormatter;
-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.SyntaxErrorException;
-import org.jnode.shell.help.argument.EnumOptionArgument;
-import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.MappedArgument;
/**
- * @author Tango
- * <p>
- * The FAT32 formating command.
+ * This command formats a FAT32 file system.
*
+ * @author Tango
+ * @author cr...@jn...
*/
public class FatFormatCommand extends AbstractFormatCommand<FatFileSystem> {
private static final Logger log = Logger.getLogger(FatFormatCommand.class);
+
+ private static class ClusterSizeArgument extends MappedArgument<ClusterSize> {
+ private static final Map<String, ClusterSize> MAP = new HashMap<String, ClusterSize> ();
+ static {
+ MAP.put("1k", ClusterSize._1Kb);
+ MAP.put("2k", ClusterSize._2Kb);
+ MAP.put("4k", ClusterSize._4Kb);
+ MAP.put("8k", ClusterSize._8Kb);
+ MAP.put("16k", ClusterSize._16Kb);
+ MAP.put("32k", ClusterSize._32Kb);
+ // FIXME - should we enable this?
+ // MAP.put("64k", ClusterSize._64Kb);
+ }
+ public ClusterSizeArgument() {
+ super("clusterSize", Argument.OPTIONAL, new ClusterSize[0],
+ MAP, true, "cluster size for FAT32 filesystem (default 4k)");
+ }
- static final OptionArgument TYPE =
- new OptionArgument("action", "Type parameter",
- new OptionArgument.Option[] { new OptionArgument.Option(
- "-c", "Specify Sector Per Cluster Value") });
- static final Parameter PARAM_TYPE = new Parameter(TYPE, Parameter.OPTIONAL);
+ @Override
+ protected String argumentKind() {
+ return "cluster size";
+ }
+ }
- static final EnumOptionArgument<ClusterSize> BS_VAL =
- new EnumOptionArgument<ClusterSize>("clusterSize",
- "cluster size for fat filesystem",
- new EnumOptionArgument.EnumOption<ClusterSize>("1", "1Kb",
- ClusterSize._1Kb),
- new EnumOptionArgument.EnumOption<ClusterSize>("2", "2Kb",
- ClusterSize._2Kb),
- new EnumOptionArgument.EnumOption<ClusterSize>("4", "4Kb",
- ClusterSize._4Kb),
- new EnumOptionArgument.EnumOption<ClusterSize>("8", "8Kb",
- ClusterSize._8Kb),
- new EnumOptionArgument.EnumOption<ClusterSize>("16",
- "16Kb", ClusterSize._16Kb),
- new EnumOptionArgument.EnumOption<ClusterSize>("32",
- "32Kb", ClusterSize._32Kb),
- new EnumOptionArgument.EnumOption<ClusterSize>("32",
- "64Kb", ClusterSize._64Kb));
+ private final ClusterSizeArgument ARG_CLUSTER_SIZE = new ClusterSizeArgument();
+
+ public FatFormatCommand() {
+ super("Format a FAT32 file system");
+ registerArguments(ARG_CLUSTER_SIZE);
+ }
- static final Parameter PARAM_BS_VAL =
- new Parameter(BS_VAL, Parameter.OPTIONAL);
-
- public static Help.Info HELP_INFO =
- new Help.Info(
- "mkjfat",
- new Syntax[] { new Syntax(
- "Format a block device with a specified type.Enter the Cluster Size as 1 for 1KB. ",
- new Parameter[] { PARAM_TYPE, PARAM_BS_VAL,
- PARAM_DEVICE }) });
-
public static void main(String[] args) throws Exception {
new FatFormatCommand().execute(args);
}
-
- protected ParsedArguments parse(CommandLine commandLine)
- throws SyntaxErrorException {
- return HELP_INFO.parse(commandLine);
+
+ protected FatFileSystemFormatter getFormatter() {
+ ClusterSize clusterSize = ARG_CLUSTER_SIZE.isSet() ?
+ ARG_CLUSTER_SIZE.getValue() : ClusterSize._4Kb;
+ return new FatFileSystemFormatter(clusterSize);
}
-
- protected FatFileSystemFormatter getFormatter(ParsedArguments cmdLine) {
- ClusterSize bsize = BS_VAL.getEnum(cmdLine, ClusterSize.class);
- return new FatFileSystemFormatter(bsize);
- }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-24 03:13:15
|
Revision: 4120
http://jnode.svn.sourceforge.net/jnode/?rev=4120&view=rev
Author: crawley
Date: 2008-05-23 20:13:12 -0700 (Fri, 23 May 2008)
Log Message:
-----------
Completed support for multi-argument delete: fixed the syntax XML and added
Argument.MULTIPLE to "path" Argument.
Got rid of Exception catches / stack traces (with output to System.err).
Fixed bug in recursive delete so that failure to delete a child file / dir
stops parent from being deleted.
Various stylistic tidy ups.
Added FIXMEs about "del"s handling of mounted filesystems
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-24 01:55:00 UTC (rev 4119)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-24 03:13:12 UTC (rev 4120)
@@ -38,7 +38,7 @@
<extension point="org.jnode.shell.syntaxes">
<syntax alias="cat">
<empty description="copy standard input to standard output"/>
- <sequence description="concatenate urls to standard output">
+ <sequence description="fetch and concatenate urls to standard output">
<option argLabel="urls" shortName="u" longName="urls"/>
<repeat minCount="1">
<argument argLabel="url"/>
@@ -62,11 +62,11 @@
</sequence>
</syntax>
<syntax alias="del">
- <sequence description="delete a file or directory">
- <optional>
- <option argLabel="recursive" shortName="r" description="recursively delete a directory's contents" />
- </optional>
- <argument argLabel="file/dir"/>
+ <sequence description="delete files and directories">
+ <optionSet>
+ <option argLabel="recursive" shortName="r" longName="recursive"/>
+ </optionSet>
+ <repeat minCount="1"><argument argLabel="paths"/></repeat>
</sequence>
</syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-05-24 01:55:00 UTC (rev 4119)
+++ trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-05-24 03:13:12 UTC (rev 4120)
@@ -41,22 +41,25 @@
* @author Andreas H\u00e4nel
* @author Levente S\u00e1ntha
* @author Martin Husted Hartvig (hagar at jnode.org)
+ * @author cr...@jn...
*/
public class DeleteCommand extends AbstractCommand {
- private final FileArgument ARG_DIR;
- private final FlagArgument ARG_OPTION;
+ private final FileArgument ARG_PATHS = new FileArgument(
+ "paths", Argument.MANDATORY | Argument.MULTIPLE,
+ "the files or directories to be deleted");
+ private final FlagArgument FLAG_RECURSIVE = new FlagArgument(
+ "recursive", Argument.OPTIONAL,
+ "if set, any directories are deleted recursively");
+
+ private FileSystemService fss;
+ private boolean recursive;
+ private PrintStream err;
+
-
public DeleteCommand() {
super("delete files or directories");
-
- ARG_DIR = new FileArgument(
- "file/dir", Argument.MANDATORY, "the file or directory to be deleted");
- ARG_OPTION = new FlagArgument(
- "recursive", Argument.OPTIONAL, "if set, any directories are deleted recursively");
-
- registerArguments(ARG_DIR, ARG_OPTION);
+ registerArguments(ARG_PATHS, FLAG_RECURSIVE);
}
public static void main(String[] args) throws Exception {
@@ -64,67 +67,58 @@
}
public void execute(CommandLine commandLine, InputStream in,
- PrintStream out, PrintStream err) throws Exception {
-
- boolean recursive = ARG_OPTION.isSet();
-
- File[] file_arr = ARG_DIR.getValues();
-
+ PrintStream out, PrintStream err) throws NameNotFoundException {
+ // Lookup the Filesystem service
+ fss = InitialNaming.lookup(FileSystemService.NAME);
+ recursive = FLAG_RECURSIVE.isSet();
+ File[] paths = ARG_PATHS.getValues();
+ this.err = err;
boolean ok = true;
- for (File file : file_arr) {
- boolean tmp = deleteFile(file, err, recursive);
- ok &= tmp;
+ for (File file : paths) {
+ ok &= deleteFile(file);
}
-
if (!ok) {
exit(1);
}
}
- private boolean deleteFile(File file, PrintStream err, boolean recursive) throws NameNotFoundException {
+ private boolean deleteFile(File file) {
+ if (!file.exists()) {
+ err.println(file + " does not exist");
+ return false;
+ }
boolean deleteOk = true;
- try {
- if (!file.exists()) {
- err.println(file + " does not exist");
- return false;
- }
- // Lookup the Filesystem service
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
+ // FIXME the following doesn't handle mounted filesystems correctly (I think).
+ // Recursive delete should not recurse >>into<< a mounted filesystem, but should
+ // give an error message and then refuse to delete the parent directory because
+ // it cannot be emptied.
+ if (file.isDirectory() && !fss.isMount(file.getAbsolutePath())) {
+ for (File f : file.listFiles()) {
+ final String name = f.getName();
- if (file.isDirectory() && !fss.isMount(file.getAbsolutePath())) {
- final File[] subFiles = file.listFiles();
-
- for (File f : subFiles) {
- final String name = f.getName();
-
- if (!name.equals(".") && !name.equals("..")) {
- if (!recursive) {
- err.println("Directory is not empty " + file);
- deleteOk = false;
- break;
- }
- else {
- deleteFile(f, err, recursive);
- }
+ if (!name.equals(".") && !name.equals("..")) {
+ if (!recursive) {
+ err.println("Directory is not empty " + file);
+ deleteOk = false;
+ break;
}
+ else {
+ deleteOk &= deleteFile(f);
+ }
}
}
- } catch (Exception e) {
- System.err.println("Failed to check file properties");
- e.printStackTrace();
- System.err.println("Trying to delete it directly");
}
- try {
- if (deleteOk) {
- deleteOk = file.delete();
- if (!deleteOk) {
- err.println(file + " was not deleted");
- }
+ if (deleteOk) {
+ // FIXME ... this is going to attempt to delete "directories" that are
+ // mounted filesystems. Is this right? What will it do?
+ // FIXME ... this does not report the reason that the delete failed.
+ // How should we do that?
+ deleteOk = file.delete();
+ if (!deleteOk) {
+ err.println(file + " was not deleted");
}
- } catch (Exception e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return deleteOk;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-24 12:48:04
|
Revision: 4122
http://jnode.svn.sourceforge.net/jnode/?rev=4122&view=rev
Author: crawley
Date: 2008-05-24 05:47:57 -0700 (Sat, 24 May 2008)
Log Message:
-----------
Converted RamDiskCommand
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.driver.block.ramdisk.command.xml
trunk/fs/src/driver/org/jnode/driver/block/ramdisk/command/RamDiskCommand.java
Modified: trunk/fs/descriptors/org.jnode.driver.block.ramdisk.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.driver.block.ramdisk.command.xml 2008-05-24 03:35:13 UTC (rev 4121)
+++ trunk/fs/descriptors/org.jnode.driver.block.ramdisk.command.xml 2008-05-24 12:47:57 UTC (rev 4122)
@@ -21,5 +21,14 @@
<extension point="org.jnode.shell.aliases">
<alias name="ramdisk" class="org.jnode.driver.block.ramdisk.command.RamDiskCommand"/>
</extension>
+
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="ramdisk">
+ <sequence description="create a ramdisk">
+ <option argLabel="create" shortName="c" longName="create"/>
+ <optional><option argLabel="size" shortName="s" longName="size"/></optional>
+ </sequence>
+ </syntax>
+ </extension>
</plugin>
Modified: trunk/fs/src/driver/org/jnode/driver/block/ramdisk/command/RamDiskCommand.java
===================================================================
--- trunk/fs/src/driver/org/jnode/driver/block/ramdisk/command/RamDiskCommand.java 2008-05-24 03:35:13 UTC (rev 4121)
+++ trunk/fs/src/driver/org/jnode/driver/block/ramdisk/command/RamDiskCommand.java 2008-05-24 12:47:57 UTC (rev 4122)
@@ -21,61 +21,52 @@
package org.jnode.driver.block.ramdisk.command;
+import java.io.InputStream;
+import java.io.PrintStream;
+
+import javax.naming.NameNotFoundException;
+
+import org.jnode.driver.DeviceAlreadyRegisteredException;
import org.jnode.driver.DeviceManager;
+import org.jnode.driver.DriverException;
import org.jnode.driver.block.ramdisk.RamDiskDevice;
import org.jnode.driver.block.ramdisk.RamDiskDriver;
import org.jnode.naming.InitialNaming;
-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.DeviceArgument;
-import org.jnode.shell.help.argument.IntegerArgument;
-import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.IntegerArgument;
/**
* @author Ewout Prangsma (ep...@us...)
*/
-public class RamDiskCommand {
+public class RamDiskCommand extends AbstractCommand {
+ private final FlagArgument FLAG_CREATE = new FlagArgument(
+ "create", Argument.MANDATORY, "if set, create the ramdisk");
+ private final IntegerArgument ARG_SIZE = new IntegerArgument(
+ "size", Argument.OPTIONAL, "the size of the ramdisk");
- static final OptionArgument ARG_ACTION =
- new OptionArgument(
- "action",
- "action to do on the ramdisk",
- new OptionArgument.Option[] {
- new OptionArgument.Option("create", "Create a ramdisk"),
- });
+ public RamDiskCommand() {
+ super("Manage RAM 'disks'");
+ registerArguments(FLAG_CREATE, ARG_SIZE);
+ }
- static final IntegerArgument ARG_SIZE = new IntegerArgument("size", "the size of the ramdisk");
-
- static final Parameter PARAM_ACTION = new Parameter(ARG_ACTION, Parameter.MANDATORY);
- static final Parameter PARAM_SIZE = new Parameter(ARG_SIZE, Parameter.OPTIONAL);
-
- public static Help.Info HELP_INFO =
- new Help.Info(
- "ramdisk",
- new Syntax[] {
- new Syntax("Create a ramdisk", new Parameter[] { PARAM_ACTION, PARAM_SIZE })
- });
-
- public static void main(String[] args)
- throws Exception {
- final ParsedArguments cmdLine = HELP_INFO.parse(args);
-
- final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
- if (PARAM_ACTION.isSet(cmdLine)) {
+ public static void main(String[] args) throws Exception {
+ new RamDiskCommand().execute(args);
+ }
+
+ public void execute(CommandLine commandLine, InputStream in,
+ PrintStream out, PrintStream err)
+ throws NameNotFoundException, DriverException, DeviceAlreadyRegisteredException {
+ final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
+ if (FLAG_CREATE.isSet()) {
// Create
- final int size;
- if (PARAM_SIZE.isSet(cmdLine)) {
- size = ARG_SIZE.getInteger(cmdLine);
- } else {
- size = 4*4096;
- }
+ final int size = ARG_SIZE.isSet() ? ARG_SIZE.getValue() : 4 * 4096;
RamDiskDevice dev = new RamDiskDevice(null, "dummy", size);
dev.setDriver(new RamDiskDriver(null));
dm.register(dev);
}
-
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-24 13:36:47
|
Revision: 4123
http://jnode.svn.sourceforge.net/jnode/?rev=4123&view=rev
Author: crawley
Date: 2008-05-24 06:36:45 -0700 (Sat, 24 May 2008)
Log Message:
-----------
Converted DFCommand and added support for a device argument
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-24 12:47:57 UTC (rev 4122)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-24 13:36:45 UTC (rev 4123)
@@ -69,6 +69,10 @@
<repeat minCount="1"><argument argLabel="paths"/></repeat>
</sequence>
</syntax>
+ <syntax alias="df">
+ <empty description="display disk usage for all filesystems"/>
+ <argument argLabel="device" description="display disk usage for the filesystem on a device"/>
+ </syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java 2008-05-24 12:47:57 UTC (rev 4122)
+++ trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java 2008-05-24 13:36:45 UTC (rev 4123)
@@ -1,72 +1,95 @@
+/*
+ * $Id: Help.java 3962 2008-04-17 14:33:02Z crawley $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library 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 Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
package org.jnode.fs.command;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
-import java.io.IOException;
import javax.naming.NameNotFoundException;
-import org.apache.log4j.Logger;
import org.jnode.driver.Device;
import org.jnode.driver.DeviceManager;
import org.jnode.fs.FileSystem;
-import org.jnode.fs.jfat.FatFileSystem;
import org.jnode.fs.service.FileSystemService;
import org.jnode.naming.InitialNaming;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Syntax;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.DeviceArgument;
+/**
+ * The DF command prints disk usage information for devices with filesystems.
+ *
+ * @author ga...@jn...
+ * @author cr...@jn...
+ *
+ */
public class DFCommand extends AbstractCommand {
+
+ private final DeviceArgument ARG_DEVICE = new DeviceArgument(
+ "device", Argument.OPTIONAL,
+ "The device for which disk usage inforrmation should be displayed");
- private static final Logger log = Logger.getLogger ( DFCommand.class );
-
- public static Help.Info HELP_INFO =
- new Help.Info(
- "device",
- new Syntax[] {
- new Syntax("Print disk usage about all devices")});
-
- public void execute(CommandLine commandLine, InputStream in,
- PrintStream out, PrintStream err) throws Exception {
- //ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- print(out);
- }
-
- private void print(PrintStream out) throws NameNotFoundException {
- final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
- StringBuffer b = new StringBuffer();
- b.append("ID")
- .append("\t").append("Total")
- .append("\t").append("Use")
- .append("\t").append("Free")
- .append("\n");
- FileSystem fs;
- for (Device dev : dm.getDevices()) {
- // Is device contains a filesystem ?
- fs = fss.getFileSystem(dev);
- long total, free, use;
- if (fs != null) {
- log.debug("Check device : " + dev.getId());
- try {
- total = fs.getTotalSpace();
- if(total > 0){
- free = fs.getFreeSpace();
- use = total - free;
- b.append(dev.getId())
- .append("\t").append(total)
- .append("\t").append(use)
- .append("\t").append(free)
- .append("\n");
- }
- } catch (IOException x){
- b.append("\t").append("Error getting disk usage information.").append("\n");
+ public DFCommand() {
+ super("Print file system usage information");
+ registerArguments(ARG_DEVICE);
+ }
+
+ public void execute(CommandLine commandLine, InputStream in,
+ PrintStream out, PrintStream err) throws NameNotFoundException {
+ final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
+ out.println("ID\tTotal\tUse\tFree");
+ if (ARG_DEVICE.isSet()) {
+ final Device dev = ARG_DEVICE.getValue();
+ FileSystem fs = fss.getFileSystem(dev);
+ if (fs == null) {
+ out.println("No filesystem on device");
+ }
+ else {
+ displayInfo(out, dev, fs);
+ }
+ }
+ else {
+ final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
+ for (Device dev : dm.getDevices()) {
+ FileSystem fs = fss.getFileSystem(dev);
+ if (fs != null) {
+ displayInfo(out, dev, fs);
}
}
- }
- out.print(b.toString());
- }
+ }
+ }
+ private void displayInfo(PrintStream out, Device dev, FileSystem fs) {
+ try {
+ long total = fs.getTotalSpace();
+ if (total > 0) {
+ long free = fs.getFreeSpace();
+ long use = total - free;
+ out.println(dev.getId() + "\t" + total + "\t" + use + "\t" + free);
+ }
+ } catch (IOException ex) {
+ out.println("\tError getting disk usage information: " + ex.getLocalizedMessage());
+ }
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-26 12:36:39
|
Revision: 4130
http://jnode.svn.sourceforge.net/jnode/?rev=4130&view=rev
Author: crawley
Date: 2008-05-26 05:36:31 -0700 (Mon, 26 May 2008)
Log Message:
-----------
Converted CdCommand and fixed bugs in no-argument form.
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-26 08:49:57 UTC (rev 4129)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-26 12:36:31 UTC (rev 4130)
@@ -48,6 +48,10 @@
<argument argLabel="file"/>
</repeat>
</syntax>
+ <syntax alias="cd">
+ <empty description="change the current directory to the 'user.home' directory"/>
+ <argument argLabel="directory" description="change the current directory to 'directory'"/>
+ </syntax>
<syntax alias="cp">
<sequence description="copy files or directories">
<optionSet>
Modified: trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java 2008-05-26 08:49:57 UTC (rev 4129)
+++ trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java 2008-05-26 12:36:31 UTC (rev 4130)
@@ -22,54 +22,56 @@
package org.jnode.fs.command;
import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import org.jnode.shell.AbstractCommand;
-import org.jnode.shell.Command;
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.argument.FileArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.test.AnnotationTest.A;
/**
+ * The CdCommand class changes the current directory as given by the "user.dir" property.
+ *
* @author Ewout Prangsma (ep...@us...)
* @author Andreas H\u00e4nel
+ * @author cr...@jn...
*/
public class CdCommand extends AbstractCommand {
- static final FileArgument ARG_DIR = new FileArgument("directory", "the directory to switch to");
- public static Help.Info HELP_INFO = new Help.Info("cd", "Go to the given directory",
- new Parameter[] { new Parameter(ARG_DIR, Parameter.MANDATORY)});
+ private final FileArgument ARG_DIR = new FileArgument(
+ "directory", Argument.OPTIONAL, "the directory to change to");
+ public CdCommand() {
+ super("Change the current directory");
+ registerArguments(ARG_DIR);
+ }
+
public static void main(String[] args) throws Exception {
new CdCommand().execute(args);
}
public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
- throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- String dir_str = ARG_DIR.getValue(cmdLine);
-
- // FIXME the following seems to cater for an optional <directory> argument, but the
- // HELP_INFO syntax declares it as mandatoty.
- if ((dir_str == null && System.getProperty("user.dir").equals("/")) ||
- (dir_str != null && dir_str.equals("/"))) {
- System.setProperty("user.dir", "/");
- } else {
- File dir = ARG_DIR.getFile(cmdLine);
- if (dir == null) {
- dir = new File(System.getProperty("user.dir"));
- }
- if (dir.exists() && dir.isDirectory()) {
- System.setProperty("user.dir", dir.getAbsoluteFile().getCanonicalPath());
- } else {
- System.err.println(dir + " is not a valid directory");
- exit(1);
- }
- }
-
+ throws IOException {
+ File dir = ARG_DIR.getValue();
+
+ if (dir == null) {
+ // If no directory argument was given, change to the "user.home" directory.
+ String home = System.getProperty("user.home");
+ if (home == null || home.isEmpty()) {
+ err.println("user.home is not set");
+ exit(1);
+ }
+ dir = new File(home);
+ }
+ if (dir.exists() && dir.isDirectory()) {
+ System.setProperty("user.dir", dir.getAbsoluteFile().getCanonicalPath());
+ } else {
+ err.println(dir + " is not a valid directory");
+ exit(1);
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-27 11:54:02
|
Revision: 4131
http://jnode.svn.sourceforge.net/jnode/?rev=4131&view=rev
Author: crawley
Date: 2008-05-27 04:53:58 -0700 (Tue, 27 May 2008)
Log Message:
-----------
Convert DirCommand and add support for multiple arguments
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-26 12:36:31 UTC (rev 4130)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-27 11:53:58 UTC (rev 4131)
@@ -77,6 +77,14 @@
<empty description="display disk usage for all filesystems"/>
<argument argLabel="device" description="display disk usage for the filesystem on a device"/>
</syntax>
+ <syntax alias="dir">
+ <empty description="list the current directory"/>
+ <argument argLabel="path" description="list a file or directory"/>
+ </syntax>
+ <syntax alias="ls">
+ <empty description="list the current directory"/>
+ <repeat><argument argLabel="path" description="list files or directories"/></repeat>
+ </syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java 2008-05-26 12:36:31 UTC (rev 4130)
+++ trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java 2008-05-27 11:53:58 UTC (rev 4131)
@@ -21,15 +21,8 @@
package org.jnode.fs.command;
-import org.jnode.shell.AbstractCommand;
-import org.jnode.shell.Command;
-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.argument.FileArgument;
-
import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
@@ -37,42 +30,56 @@
import java.util.Comparator;
import java.util.Date;
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+
/**
* @author epr
* @author Andreas H\u00e4nel
* @author Martin Husted Hartvig (ha...@jn...)
* @author Levente S\u00e1ntha
+ * @author cr...@jn...
*/
public class DirCommand extends AbstractCommand {
private static final int LEFT_MARGIN = 14;
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm");
+ private final FileArgument ARG_PATH = new FileArgument(
+ "path", Argument.OPTIONAL + Argument.MULTIPLE, "the file or directory to list");
+
+ public DirCommand() {
+ super("List files or directories");
+ registerArguments(ARG_PATH);
+ }
- static final FileArgument ARG_PATH = new FileArgument("path", "the path to list contents of");
- public static Help.Info HELP_INFO =
- new Help.Info(
- "dir",
- "List the entries of the given path",
- new Parameter[]{new Parameter(ARG_PATH, Parameter.OPTIONAL)});
-
public static void main(String[] args) throws Exception {
new DirCommand().execute(args);
}
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- String userDir = System.getProperty("user.dir");
- File path = ARG_PATH.getFile(cmdLine);
- if (path == null) path = new File(userDir);
- if (path.exists() && path.isDirectory()) {
- final File[] list = path.listFiles();
- this.printList(list, out);
- } else if (path.exists() && path.isFile()) {
- this.printList(new File[]{path}, out);
- } else {
- err.println("No such path " + path);
- exit(1);
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws IOException {
+ File[] paths = ARG_PATH.getValues();
+ if (paths.length == 0) {
+ paths = new File[] {new File(System.getProperty("user.dir"))};
}
+ for (File path : paths) {
+ if (!path.exists()) {
+ err.println("No such path: " + path);
+ }
+ else {
+ if (paths.length > 1) {
+ out.println(path + ":");
+ }
+ if (path.isDirectory()) {
+ final File[] list = path.listFiles();
+ printList(list, out);
+ } else if (path.isFile()) {
+ printList(new File[]{path}, out);
+ }
+ }
+ }
}
private void printList(File[] list, PrintStream out) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-27 12:33:59
|
Revision: 4132
http://jnode.svn.sourceforge.net/jnode/?rev=4132&view=rev
Author: crawley
Date: 2008-05-27 05:33:57 -0700 (Tue, 27 May 2008)
Log Message:
-----------
Converted EjectCommand
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-27 11:53:58 UTC (rev 4131)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-27 12:33:57 UTC (rev 4132)
@@ -81,6 +81,9 @@
<empty description="list the current directory"/>
<argument argLabel="path" description="list a file or directory"/>
</syntax>
+ <syntax alias="eject">
+ <argument argLabel="device" description="eject a device with a removable medium"/>
+ </syntax>
<syntax alias="ls">
<empty description="list the current directory"/>
<repeat><argument argLabel="path" description="list files or directories"/></repeat>
Modified: trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java 2008-05-27 11:53:58 UTC (rev 4131)
+++ trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java 2008-05-27 12:33:57 UTC (rev 4132)
@@ -21,18 +21,17 @@
package org.jnode.fs.command;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
+import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.RemovableDeviceAPI;
import org.jnode.shell.AbstractCommand;
-import org.jnode.shell.Command;
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.argument.DeviceArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.DeviceArgument;
/**
@@ -40,18 +39,29 @@
*/
public class EjectCommand extends AbstractCommand {
- static final DeviceArgument ARG_DEVICE = new DeviceArgument("device", "device to eject the medium from");
- public static Help.Info HELP_INFO = new Help.Info("eject", "Eject the medium from a given device", new Parameter[] { new Parameter(ARG_DEVICE, Parameter.MANDATORY)});
-
+ private final DeviceArgument ARG_DEVICE = new DeviceArgument(
+ "device", Argument.MANDATORY, "device to eject the medium from",
+ RemovableDeviceAPI.class);
+
+ public EjectCommand() {
+ super("Eject the medium from a given device");
+ registerArguments(ARG_DEVICE);
+ }
+
public static void main(String[] args) throws Exception {
new EjectCommand().execute(args);
}
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
-
- final Device dev = ARG_DEVICE.getDevice(cmdLine);
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws ApiNotFoundException, IOException {
+ final Device dev = ARG_DEVICE.getValue();
final RemovableDeviceAPI api = dev.getAPI(RemovableDeviceAPI.class);
- api.eject();
+ try {
+ api.eject();
+ }
+ catch (IOException ex) {
+ err.println("eject failed for " + dev.getId() + ": " + ex.getMessage());
+ exit(1);
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-27 13:42:30
|
Revision: 4133
http://jnode.svn.sourceforge.net/jnode/?rev=4133&view=rev
Author: crawley
Date: 2008-05-27 06:42:26 -0700 (Tue, 27 May 2008)
Log Message:
-----------
Converted HexdumpCommand and added filter support.
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-27 12:33:57 UTC (rev 4132)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-27 13:42:26 UTC (rev 4133)
@@ -84,6 +84,11 @@
<syntax alias="eject">
<argument argLabel="device" description="eject a device with a removable medium"/>
</syntax>
+ <syntax alias="hexdump">
+ <empty description="print a hex dump of standard input"/>
+ <argument argLabel="file" description="print a hex dump of a file"/>
+ <option argLabel="url" shortName="u" longName="url" description="print a hex dump of a URL"/>
+ </syntax>
<syntax alias="ls">
<empty description="list the current directory"/>
<repeat><argument argLabel="path" description="list files or directories"/></repeat>
Modified: trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java 2008-05-27 12:33:57 UTC (rev 4132)
+++ trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java 2008-05-27 13:42:26 UTC (rev 4133)
@@ -4,46 +4,77 @@
package org.jnode.fs.command;
-import org.jnode.shell.AbstractCommand;
-import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Argument;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.ParsedArguments;
-import org.jnode.shell.help.argument.FileArgument;
-import org.jnode.util.NumberUtils;
-
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.URLArgument;
+import org.jnode.util.NumberUtils;
+
/**
* @author gvt
+ * @author cr...@jn...
*/
public class HexdumpCommand extends AbstractCommand {
- static final Argument ARG_FILE = new FileArgument("file",
- "the file (or URL) to print out");
+ private final FileArgument ARG_FILE = new FileArgument(
+ "file", Argument.OPTIONAL, "the file to print out");
- public static Help.Info HELP_INFO = new Help.Info("hexdump",
- "hexadecimal dump of the given file (or URL)",
- new Parameter[]{new Parameter(ARG_FILE, Parameter.MANDATORY)});
+ private final URLArgument ARG_URL = new URLArgument(
+ "url", Argument.OPTIONAL, "the url to print out");
+ public HexdumpCommand() {
+ super("Print a hexadecimal dump of a given file (or URL)");
+ registerArguments(ARG_FILE, ARG_URL);
+ }
+
public static void main(String[] args) throws Exception {
new HexdumpCommand().execute(args);
}
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws IOException {
+ InputStream is = null;
+ try {
+ // Set up the stream to be dumped.
+ File file = ARG_FILE.getValue();
+ if (ARG_FILE.isSet()) {
+ try {
+ is = new FileInputStream(file);
+ }
+ catch (FileNotFoundException ex) {
+ err.println("Cannot open " + file + ": " + ex.getMessage());
+ exit(1);
+ }
+ }
+ else if (ARG_URL.isSet()) {
+ String urlStr = ARG_URL.getValue();
+ try {
+ URL url = new URL(urlStr);
+ is = url.openStream();
+ }
+ catch (MalformedURLException ex) {
+ err.println("Malformed URL '" + urlStr + "': " + ex.getMessage());
+ exit(1);
+ }
+ catch (IOException ex) {
+ err.println("Cannot access URL '" + urlStr + "': " + ex.getMessage());
+ exit(1);
+ }
+ }
+ else {
+ is = in;
+ }
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- URL url = openURL(ARG_FILE.getValue(cmdLine));
- InputStream is = url.openStream();
-
- if (is == null) {
- err.println("Not found " + ARG_FILE.getValue(cmdLine));
- exit(1);
- } else {
+ // Now do the work
final int rowlen = 16;
int prt = 0;
int len;
@@ -61,14 +92,17 @@
sb.append(NumberUtils.hex(prt, 8)).append(" ");
for (int i = 0; i < rowlen; i++) {
- if (ofs + i < len)
+ if (ofs + i < len) {
sb.append(NumberUtils.hex(buf[ofs + i], 2));
- else
+ } else {
sb.append(" ");
- if ((i + 1) < rowlen)
+ }
+ if ((i + 1) < rowlen) {
sb.append(" ");
- if ((i + 1) == rowlen / 2)
+ }
+ if ((i + 1) == rowlen / 2) {
sb.append(" ");
+ }
}
sb.append(" |");
@@ -76,12 +110,14 @@
for (int i = 0; i < rowlen; i++) {
if (ofs + i < len) {
char c = (char) buf[ofs + i];
- if ((c >= ' ') && (c < (char) 0x7f))
+ if ((c >= ' ') && (c < (char) 0x7f)) {
sb.append(c);
- else
+ } else {
sb.append(".");
- } else
+ }
+ } else {
sb.append(" ");
+ }
}
sb.append("|");
@@ -94,18 +130,17 @@
out.flush();
}
}
-
out.flush();
- is.close();
}
- }
-
- private URL openURL(String fname) throws MalformedURLException {
- try {
- return new URL(fname);
- } catch (MalformedURLException ex) {
- return new File(fname).toURL();
+ finally {
+ if (is != null && is != in) {
+ try {
+ is.close();
+ }
+ catch (IOException ex) {
+ /* ignore */
+ }
+ }
}
}
-
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-28 10:20:43
|
Revision: 4138
http://jnode.svn.sourceforge.net/jnode/?rev=4138&view=rev
Author: crawley
Date: 2008-05-28 03:20:40 -0700 (Wed, 28 May 2008)
Log Message:
-----------
Converted MkdirCommand
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-27 19:18:48 UTC (rev 4137)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-28 10:20:40 UTC (rev 4138)
@@ -93,6 +93,9 @@
<empty description="list the current directory"/>
<repeat><argument argLabel="path" description="list files or directories"/></repeat>
</syntax>
+ <syntax alias="mkdir">
+ <argument argLabel="directory" description="create a new directory"/>
+ </syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java 2008-05-27 19:18:48 UTC (rev 4137)
+++ trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java 2008-05-28 10:20:40 UTC (rev 4138)
@@ -27,10 +27,7 @@
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.argument.FileArgument;
+import org.jnode.shell.syntax.*;
/**
* @author Guillaume BINET (gb...@us...)
@@ -38,21 +35,24 @@
*/
public class MkdirCommand extends AbstractCommand {
- static final FileArgument ARG_DIR = new FileArgument("directory", "the directory to create");
- public static Help.Info HELP_INFO =
- new Help.Info(
- "dir",
- "create a directory",
- new Parameter[] { new Parameter(ARG_DIR, Parameter.MANDATORY)});
+ private final FileArgument ARG_DIR = new FileArgument(
+ "directory", Argument.MANDATORY, "the directory to create");
+
+ public MkdirCommand() {
+ super("Create a new directory");
+ registerArguments(ARG_DIR);
+ }
public static void main(String[] args) throws Exception {
new MkdirCommand().execute(args);
}
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- File dir = ARG_DIR.getFile(cmdLine);
-
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) {
+ File dir = ARG_DIR.getValue();
+ if (dir.exists()) {
+ err.println(dir.getPath() + " already exists.");
+ exit(1);
+ }
if (!dir.mkdir()) {
err.println("Can't create directory.");
exit(1);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-29 12:59:12
|
Revision: 4139
http://jnode.svn.sourceforge.net/jnode/?rev=4139&view=rev
Author: crawley
Date: 2008-05-29 05:59:10 -0700 (Thu, 29 May 2008)
Log Message:
-----------
Converted MouontCommand
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/MountCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-28 10:20:40 UTC (rev 4138)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-29 12:59:10 UTC (rev 4139)
@@ -96,6 +96,14 @@
<syntax alias="mkdir">
<argument argLabel="directory" description="create a new directory"/>
</syntax>
+ <syntax alias="mount">
+ <empty description="list all mounted filesystems"/>
+ <sequence description="mount a filesystem">
+ <argument argLabel="device" description="the device holding the file system"/>
+ <argument argLabel="directory" description="the mountpoint"/>
+ <argument argLabel="fsPath" description="the file system's root directory"/>
+ </sequence>
+ </syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/MountCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/MountCommand.java 2008-05-28 10:20:40 UTC (rev 4138)
+++ trunk/fs/src/fs/org/jnode/fs/command/MountCommand.java 2008-05-29 12:59:10 UTC (rev 4139)
@@ -21,6 +21,7 @@
package org.jnode.fs.command;
+import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Map;
@@ -32,30 +33,26 @@
import org.jnode.naming.InitialNaming;
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.argument.DeviceArgument;
-import org.jnode.shell.help.argument.FileArgument;
+import org.jnode.shell.syntax.*;
/**
* @author Ewout Prangsma (ep...@us...)
*/
public class MountCommand extends AbstractCommand {
- private static final DeviceArgument ARG_DEV = new DeviceArgument("device",
- "the device to mount", BlockDeviceAPI.class);
+ private final DeviceArgument ARG_DEV = new DeviceArgument("device",
+ Argument.OPTIONAL, "the device to mount", BlockDeviceAPI.class);
- private static final FileArgument ARG_DIR = new FileArgument("directory",
- "the mountpoint");
+ private final FileArgument ARG_DIR = new FileArgument("directory",
+ Argument.OPTIONAL, "the mount point");
- private static final FileArgument ARG_FSPATH = new FileArgument("fspath",
- "the subdirectory within the filesystem to use as root");
+ private final FileArgument ARG_FSPATH = new FileArgument("fsPath",
+ Argument.OPTIONAL, "the subdirectory within the filesystem to use as root");
- static Help.Info HELP_INFO = new Help.Info("mount", "Mount a filesystem",
- new Parameter[] { new Parameter(ARG_DEV, Parameter.OPTIONAL),
- new Parameter(ARG_DIR, Parameter.OPTIONAL),
- new Parameter(ARG_FSPATH, Parameter.OPTIONAL) });
+ public MountCommand() {
+ super("Mount a filesystem");
+ registerArguments(ARG_DEV, ARG_DIR, ARG_FSPATH);
+ }
public static void main(String[] args) throws Exception {
new MountCommand().execute(args);
@@ -63,33 +60,26 @@
public void execute(CommandLine commandLine, InputStream in,
PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
+ // Find the filesystem service
+ final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
- if(ARG_DEV.getValue(cmdLine) == null)
- {
- // Find the filesystem service
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
-
+ if (!ARG_DEV.isSet()) {
+ // List all mounted file systems
Map<String, FileSystem<?>> filesystems = fss.getMountPoints();
- for(String mountPoint : filesystems.keySet())
- {
+ for (String mountPoint : filesystems.keySet()) {
FileSystem<?> fs = filesystems.get(mountPoint);
Device device = fs.getDevice();
String mode = fs.isReadOnly() ? "ro" : "rw";
String type = fs.getType().getName();
- System.out.println(device.getId() + " on " + mountPoint + " type " + type + " (" + mode + ')');
+ out.println(device.getId() + " on " + mountPoint + " type " + type + " (" + mode + ')');
}
}
- else
- {
+ else {
// Get the parameters
- final Device dev = ARG_DEV.getDevice(cmdLine);
- final String mountPoint = ARG_DIR.getValue(cmdLine);
- final String fsPath = ARG_FSPATH.getValue(cmdLine);
+ final Device dev = ARG_DEV.getValue();
+ final File mountPoint = ARG_DIR.getValue();
+ final File fsPath = ARG_FSPATH.getValue();
- // Find the filesystem service
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
-
// Find the filesystem
final FileSystem<?> fs = fss.getFileSystem(dev);
if (fs == null) {
@@ -97,7 +87,7 @@
exit(1);
} else {
// Mount it
- fss.mount(mountPoint, fs, fsPath);
+ fss.mount(mountPoint.toString(), fs, fsPath.toString());
}
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-29 13:47:56
|
Revision: 4140
http://jnode.svn.sourceforge.net/jnode/?rev=4140&view=rev
Author: crawley
Date: 2008-05-29 06:47:51 -0700 (Thu, 29 May 2008)
Log Message:
-----------
Converted PwdCommand
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/PwdCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-29 12:59:10 UTC (rev 4139)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-29 13:47:51 UTC (rev 4140)
@@ -104,6 +104,9 @@
<argument argLabel="fsPath" description="the file system's root directory"/>
</sequence>
</syntax>
+ <syntax alias="pwd">
+ <empty description="show the pathname for the current directory"/>
+ </syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/PwdCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/PwdCommand.java 2008-05-29 12:59:10 UTC (rev 4139)
+++ trunk/fs/src/fs/org/jnode/fs/command/PwdCommand.java 2008-05-29 13:47:51 UTC (rev 4140)
@@ -27,23 +27,22 @@
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Help;
/**
* @author Martin Husted Hartvig (ha...@jn...)
*/
public class PwdCommand extends AbstractCommand
{
- public static Help.Info HELP_INFO = new Help.Info("pwd", "show the name of current working directory");
+ public PwdCommand() {
+ super("show the name of current working directory");
+ }
- public static void main(String[] args) throws Exception
- {
- new PwdCommand().execute(args);
- }
+ public static void main(String[] args) throws Exception {
+ new PwdCommand().execute(args);
+ }
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception
- {
- File file = new File("");
- out.println(file.getAbsolutePath());
- }
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) {
+ File file = new File("");
+ out.println(file.getAbsolutePath());
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-05-31 03:45:30
|
Revision: 4162
http://jnode.svn.sourceforge.net/jnode/?rev=4162&view=rev
Author: crawley
Date: 2008-05-30 20:45:28 -0700 (Fri, 30 May 2008)
Log Message:
-----------
Converted TouchCommand (and tweaked error messages)
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/TouchCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-31 03:19:24 UTC (rev 4161)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-31 03:45:28 UTC (rev 4162)
@@ -107,6 +107,9 @@
<syntax alias="pwd">
<empty description="show the pathname for the current directory"/>
</syntax>
+ <syntax alias="touch">
+ <argument argLabel="file" description="touch the given file"/>
+ </syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/TouchCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/TouchCommand.java 2008-05-31 03:19:24 UTC (rev 4161)
+++ trunk/fs/src/fs/org/jnode/fs/command/TouchCommand.java 2008-05-31 03:45:28 UTC (rev 4162)
@@ -27,52 +27,51 @@
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.argument.FileArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
/**
- * Touch a file
+ * Touch a file; i.e. create it if it doesn't exist and change its
+ * modification timestamp if it does exist.
*
- * TODO if file exist change modified date
- *
* @author Yves Galante (yve...@jm...)
* @author Andreas H\u00e4nel
*/
public class TouchCommand extends AbstractCommand {
- static final FileArgument ARG_TOUCH = new FileArgument("file",
- "the file to touch");
+ private final FileArgument ARG_FILE = new FileArgument(
+ "file", Argument.MANDATORY, "the file to touch");
- public static Help.Info HELP_INFO = new Help.Info("touch",
- "touch a file", new Parameter[] { new Parameter(ARG_TOUCH,
- Parameter.MANDATORY)});
-
+ public TouchCommand() {
+ super("touch a file");
+ registerArguments(ARG_FILE);
+ }
+
public static void main(String[] args) throws Exception {
new TouchCommand().execute(args);
}
public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- File file = ARG_TOUCH.getFile(cmdLine);
-
+ File file = ARG_FILE.getValue();
if (!file.exists()) {
File parentFile = file.getParentFile();
- if (parentFile!=null && !parentFile.exists()) {
+ if (parentFile != null && !parentFile.exists()) {
+ // FIXME ... this is wrong. Touch should not do this.
if (!parentFile.mkdirs()) {
- err.println("Parent dirs can't create");
+ err.println("Cannot create parent directories");
exit(2);
}
}
if (file.createNewFile()) {
out.println("File created");
} else {
- err.println("File can't create");
+ err.println("Cannot create file");
exit(1);
}
}
- file.setLastModified(System.currentTimeMillis());
+ else {
+ file.setLastModified(System.currentTimeMillis());
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-06-03 14:02:40
|
Revision: 4186
http://jnode.svn.sourceforge.net/jnode/?rev=4186&view=rev
Author: crawley
Date: 2008-06-03 07:02:38 -0700 (Tue, 03 Jun 2008)
Log Message:
-----------
Converted FTPMountCommand & tried to get it to undo stuff if it fails.
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.ftpfs.command.xml
trunk/fs/src/fs/org/jnode/fs/ftpfs/command/FTPMountCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.ftpfs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.ftpfs.command.xml 2008-06-03 10:26:00 UTC (rev 4185)
+++ trunk/fs/descriptors/org.jnode.fs.ftpfs.command.xml 2008-06-03 14:02:38 UTC (rev 4186)
@@ -23,6 +23,17 @@
<alias name="ftpmount" class="org.jnode.fs.ftpfs.command.FTPMountCommand"/>
</extension>
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="ftpmount">
+ <sequence description="mount a remote FTP service as a file system">
+ <argument argLabel="directory"/>
+ <argument argLabel="host"/>
+ <argument argLabel="userName"/>
+ <optional><argument argLabel="password"/></optional>
+ </sequence>
+ </syntax>
+ </extension>
+
<extension point="org.jnode.security.permissions">
<permission class="java.net.SocketPermission" name="*:0-" actions="connect,resolve"/>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/ftpfs/command/FTPMountCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/ftpfs/command/FTPMountCommand.java 2008-06-03 10:26:00 UTC (rev 4185)
+++ trunk/fs/src/fs/org/jnode/fs/ftpfs/command/FTPMountCommand.java 2008-06-03 14:02:38 UTC (rev 4186)
@@ -21,9 +21,13 @@
package org.jnode.fs.ftpfs.command;
+import org.apache.log4j.Logger;
+import org.jnode.driver.DeviceAlreadyRegisteredException;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.DeviceUtils;
+import org.jnode.driver.DriverException;
import org.jnode.fs.FileSystem;
+import org.jnode.fs.FileSystemException;
import org.jnode.fs.FileSystemType;
import org.jnode.fs.ftpfs.FTPFSDevice;
import org.jnode.fs.ftpfs.FTPFSDriver;
@@ -33,49 +37,77 @@
import org.jnode.naming.InitialNaming;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Argument;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.ParsedArguments;
-import org.jnode.shell.help.argument.FileArgument;
+import org.jnode.shell.syntax.*;
+import java.io.File;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
+import javax.naming.NameNotFoundException;
+
/**
* @author Levente S\u00e1ntha
*/
public class FTPMountCommand extends AbstractCommand {
- private static final FileArgument MOUNTPOINT_ARG = new FileArgument("directory", "the mountpoint");
- private static final Argument HOST_ARG = new Argument("host", "FTP host");
- private static final Argument USERNAME_ARG = new Argument("username", "FTP user");
- private static final Argument PASSWORD_ARG = new Argument("password", "FTP password");
- static Help.Info HELP_INFO = new Help.Info("mount", "Mount an FTP filesystem",
- new Parameter[]{new Parameter(MOUNTPOINT_ARG, Parameter.MANDATORY),
- new Parameter(HOST_ARG, Parameter.MANDATORY),
- new Parameter(USERNAME_ARG, Parameter.MANDATORY),
- new Parameter(PASSWORD_ARG, Parameter.OPTIONAL)});
+ private final FileArgument MOUNTPOINT_ARG =
+ new FileArgument("directory", Argument.MANDATORY, "the mountpoint");
+
+ private static final HostNameArgument HOST_ARG =
+ new HostNameArgument("host", Argument.MANDATORY, "FTP host");
+
+ private static final StringArgument USERNAME_ARG =
+ new StringArgument("userName", Argument.MANDATORY, "FTP user");
+
+ private static final StringArgument PASSWORD_ARG =
+ new StringArgument("password", Argument.OPTIONAL, "FTP password");
+
+ public FTPMountCommand() {
+ super("Mount an FTP filesystem");
+ registerArguments(MOUNTPOINT_ARG, HOST_ARG, USERNAME_ARG, PASSWORD_ARG);
+ }
public static void main(String[] args) throws Exception {
new FTPMountCommand().execute(args);
}
public void execute(CommandLine commandLine, InputStream in,
- PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
-
- final String mount_point = MOUNTPOINT_ARG.getValue(cmdLine);
- final String host = HOST_ARG.getValue(cmdLine);
- final String user = USERNAME_ARG.getValue(cmdLine);
- final String password = PASSWORD_ARG.getValue(cmdLine);
+ PrintStream out, PrintStream err)
+ throws DriverException, NameNotFoundException, DeviceAlreadyRegisteredException,
+ FileSystemException, IOException {
+ final File mountPoint = MOUNTPOINT_ARG.getValue();
+ final String host = HOST_ARG.getValue();
+ final String user = USERNAME_ARG.getValue();
+ final String password = PASSWORD_ARG.getValue();
+ boolean ok = false;
+
+ final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
+ FTPFileSystemType type = fss.getFileSystemType(FTPFileSystemType.ID);
+ final DeviceManager dm = DeviceUtils.getDeviceManager();
final FTPFSDevice dev = new FTPFSDevice(host, user, password);
dev.setDriver(new FTPFSDriver());
- final DeviceManager dm = DeviceUtils.getDeviceManager();
- dm.register(dev);
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
- FTPFileSystemType type = fss.getFileSystemType(FTPFileSystemType.ID);
- final FTPFileSystem fs = type.create(dev, true);
- fss.registerFileSystem(fs);
- fss.mount(mount_point, fs, null);
+ FTPFileSystem fs = null;
+ try {
+ dm.register(dev);
+ fs = type.create(dev, true);
+ fss.registerFileSystem(fs);
+ fss.mount(mountPoint.getAbsolutePath(), fs, null);
+ ok = true;
+ }
+ finally {
+ if (!ok) {
+ try {
+ // If we failed, try to undo the changes that we managed to make
+ if (fs != null) {
+ fss.unregisterFileSystem(dev);
+ }
+ dm.unregister(dev);
+ }
+ catch (Exception ex) {
+ Logger log = Logger.getLogger(FTPMountCommand.class);
+ log.fatal("Cannot undo failed mount attempt", ex);
+ }
+ }
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-06-04 11:00:37
|
Revision: 4191
http://jnode.svn.sourceforge.net/jnode/?rev=4191&view=rev
Author: crawley
Date: 2008-06-04 04:00:35 -0700 (Wed, 04 Jun 2008)
Log Message:
-----------
Converted JGrubCommand
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.jfat.command.xml
trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.jfat.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.jfat.command.xml 2008-06-04 10:43:49 UTC (rev 4190)
+++ trunk/fs/descriptors/org.jnode.fs.jfat.command.xml 2008-06-04 11:00:35 UTC (rev 4191)
@@ -32,6 +32,11 @@
<argument argLabel="device"/>
</sequence>
</syntax>
+ <syntax alias="grub">
+ <sequence description="install a grub stage2 loader">
+ <argument argLabel="device"/>
+ </sequence>
+ </syntax>
</extension>
<extension point="org.jnode.security.permissions">
Modified: trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java 2008-06-04 10:43:49 UTC (rev 4190)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/command/JGrubInstallCommand.java 2008-06-04 11:00:35 UTC (rev 4191)
@@ -29,40 +29,33 @@
import org.jnode.driver.block.BlockDeviceAPI;
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.argument.DeviceArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.DeviceArgument;
/**
* Grub Installer command for JNode.
- * example of usage : grub hdb0
*
* TODO: Add more options for supporting Grub with user specified file
* TODO: Add more command support for grub installation.
* @author Tango Devian
*/
public class JGrubInstallCommand extends AbstractCommand {
- static final DeviceArgument ARG_DEVICE = new DeviceArgument("device partition", "device where grub stage 2 will be installed", BlockDeviceAPI.class);
+ private final DeviceArgument ARG_DEVICE = new DeviceArgument(
+ "device", Argument.MANDATORY,
+ "device where grub stage 2 will be installed", BlockDeviceAPI.class);
-
- static final Help.Info HELP_INFO = new Help.Info("grub",
- "Install the grub to the specified location.",
- new Parameter(ARG_DEVICE, Parameter.MANDATORY));
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String... args) throws Exception {
+ public JGrubInstallCommand() {
+ super("Install a Grub stage2 loader on a disc device");
+ registerArguments(ARG_DEVICE);
+ }
+
+ public static void main(String[] args) throws Exception {
new JGrubInstallCommand().execute(args);
}
- /**
- *
- */
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- Device device = ARG_DEVICE.getDevice(cmdLine);
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws GrubException {
+ Device device = ARG_DEVICE.getValue();
JGrub jgrub = new JGrub(out, err, device);
jgrub.install();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
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.
|
|
From: <ls...@us...> - 2008-06-06 20:52:06
|
Revision: 4205
http://jnode.svn.sourceforge.net/jnode/?rev=4205&view=rev
Author: lsantha
Date: 2008-06-06 13:52:02 -0700 (Fri, 06 Jun 2008)
Log Message:
-----------
SMB FS improvements.
Modified Paths:
--------------
trunk/fs/fs.iml
trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSDirectory.java
trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSEntry.java
trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSFile.java
trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFileSystem.java
Modified: trunk/fs/fs.iml
===================================================================
--- trunk/fs/fs.iml 2008-06-06 20:07:37 UTC (rev 4204)
+++ trunk/fs/fs.iml 2008-06-06 20:52:02 UTC (rev 4205)
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module relativePaths="true" type="JAVA_MODULE" version="4">
+ <component name="DBNavigator.Module.ConnectionManager">
+ <connections />
+ </component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/classes" />
<exclude-output />
@@ -26,11 +29,10 @@
<orderEntry type="module-library">
<library>
<CLASSES>
- <root url="file://$MODULE_DIR$/lib" />
+ <root url="jar://$MODULE_DIR$/lib/jcifs-1.2.6.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
- <jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library>
</orderEntry>
<orderEntryProperties />
Modified: trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSDirectory.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSDirectory.java 2008-06-06 20:07:37 UTC (rev 4204)
+++ trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSDirectory.java 2008-06-06 20:52:02 UTC (rev 4205)
@@ -86,7 +86,7 @@
* All elements returned by the iterator must be instanceof FSEntry.
*/
public Iterator<? extends SMBFSEntry> iterator() throws IOException {
- SmbFile[] smb_list = null;
+ SmbFile[] smb_list;
try{
smb_list = smbFile.listFiles();
} catch(SmbException e){
@@ -94,13 +94,17 @@
throw e;
}
entries.clear();
+
for(SmbFile f : smb_list){
if(f.isDirectory()){
- entries.put(f.getName(), new SMBFSDirectory(this, f));
+ String name = getSimpleName(f);
+ entries.put(name, new SMBFSDirectory(this, f));
} else if(f.isFile()){
- entries.put(f.getName(), new SMBFSFile(this, f));
+ String name = getSimpleName(f);
+ entries.put(name, new SMBFSFile(this, f));
}
}
+
return entries.values().iterator();
}
Modified: trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSEntry.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSEntry.java 2008-06-06 20:07:37 UTC (rev 4204)
+++ trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSEntry.java 2008-06-06 20:52:02 UTC (rev 4205)
@@ -18,7 +18,7 @@
* along with this library; If not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-
+
package org.jnode.fs.smbfs;
import org.jnode.fs.FSEntry;
@@ -84,9 +84,16 @@
* Gets the name of this entry.
*/
public String getName() {
- return smbFile.getName();
+ return getSimpleName(smbFile);
}
+ static String getSimpleName(SmbFile smbFile) {
+ String name = smbFile.getName();
+ if(name.endsWith("/"))
+ name = name.substring(0, name.length() - 1);
+ return name;
+ }
+
/**
* Gets the directory this entry is a part of.
*/
Modified: trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSFile.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSFile.java 2008-06-06 20:07:37 UTC (rev 4204)
+++ trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFSFile.java 2008-06-06 20:52:02 UTC (rev 4205)
@@ -24,9 +24,11 @@
import org.jnode.fs.FSFile;
import java.io.IOException;
+import java.io.InputStream;
import java.nio.ByteBuffer;
import jcifs.smb.SmbFile;
+import jcifs.smb.SmbFileInputStream;
/**
* @author Levente S\u00e1ntha
@@ -65,9 +67,24 @@
* @throws java.io.IOException
*/
public void read(long fileOffset, ByteBuffer dest) throws IOException {
- byte[] data = new byte[(int) getLength()];
- smbFile.getInputStream().read(data);
- dest.put(data, (int) fileOffset, dest.remaining());
+ if(fileOffset > smbFile.length())
+ return;
+
+ int b_len = 32 * 1024;
+ byte[] buf = new byte[b_len];
+
+ SmbFileInputStream is = (SmbFileInputStream) smbFile.getInputStream();
+
+ long s = is.skip(fileOffset);
+ if(s < fileOffset)
+ is.skip(fileOffset);
+
+ int bc;
+ int rem = 1;
+ while((bc = is.read(buf)) > 0 && rem > 0){
+ dest.put(buf, 0, Math.min(bc, dest.remaining()));
+ rem = dest.remaining();
+ }
}
/**
Modified: trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFileSystem.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFileSystem.java 2008-06-06 20:07:37 UTC (rev 4204)
+++ trunk/fs/src/fs/org/jnode/fs/smbfs/SMBFileSystem.java 2008-06-06 20:52:02 UTC (rev 4205)
@@ -26,6 +26,7 @@
import jcifs.smb.NtlmAuthenticator;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
+import jcifs.smb.SmbException;
import org.jnode.fs.FileSystem;
@@ -99,9 +100,12 @@
}
public long getFreeSpace() {
- // TODO implement me
- return 0;
- }
+ try {
+ return root.smbFile.getDiskFreeSpace();
+ } catch (SmbException e){
+ return 0;
+ }
+ }
public long getTotalSpace() {
// TODO implement me
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-06-10 11:45:48
|
Revision: 4230
http://jnode.svn.sourceforge.net/jnode/?rev=4230&view=rev
Author: crawley
Date: 2008-06-10 04:45:45 -0700 (Tue, 10 Jun 2008)
Log Message:
-----------
Converted CreateJIFSCommand
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.jifs.command.xml
trunk/fs/src/fs/org/jnode/fs/jifs/command/CreateJIFSCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.jifs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.jifs.command.xml 2008-06-10 11:44:50 UTC (rev 4229)
+++ trunk/fs/descriptors/org.jnode.fs.jifs.command.xml 2008-06-10 11:45:45 UTC (rev 4230)
@@ -10,6 +10,7 @@
<requires>
<import plugin="org.jnode.fs.jifs"/>
<import plugin="org.jnode.shell"/>
+ <import plugin="org.jnode.shell.syntax"/>
</requires>
<runtime>
@@ -22,6 +23,12 @@
<alias name="jifs" class="org.jnode.fs.jifs.command.CreateJIFSCommand"/>
</extension>
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="jifs">
+ <argument argLabel="action" description="Manage the JIFS filesystem plugin"/>
+ </syntax>
+ </extension>
+
<extension point="org.jnode.security.permissions">
<permission class="org.jnode.security.JNodePermission" name="stopPlugin"/>
<permission class="org.jnode.security.JNodePermission" name="startPlugin"/>
Modified: trunk/fs/src/fs/org/jnode/fs/jifs/command/CreateJIFSCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jifs/command/CreateJIFSCommand.java 2008-06-10 11:44:50 UTC (rev 4229)
+++ trunk/fs/src/fs/org/jnode/fs/jifs/command/CreateJIFSCommand.java 2008-06-10 11:45:45 UTC (rev 4230)
@@ -28,14 +28,12 @@
import org.jnode.naming.InitialNaming;
import org.jnode.plugin.Plugin;
+import org.jnode.plugin.PluginException;
import org.jnode.plugin.PluginManager;
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.OptionArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.EnumArgument;
// TODO fix class name
// TODO this class is actually just PluginCommand specialized for JIFS ...
@@ -46,41 +44,44 @@
*/
public class CreateJIFSCommand extends AbstractCommand {
- static final OptionArgument ACTION =
- new OptionArgument("action", "Action to perform", new OptionArgument.Option[] {
- new OptionArgument.Option("start", "start the jifs"),
- new OptionArgument.Option("stop", "stop the jifs"),
- new OptionArgument.Option("restart", "restart the jifs")});
+ private static enum Action {
+ start, stop, restart;
+ }
- static final Parameter PARAM_ACTION = new Parameter(ACTION, Parameter.MANDATORY);
+ private static class ActionArgument extends EnumArgument<Action> {
+ public ActionArgument() {
+ super("action", Argument.MANDATORY, Action.class, "action to be performed");
+ }
- public static Help.Info HELP_INFO =
- new Help.Info("jifs", new Syntax[] {new Syntax("JIFS - Jnode Information FileSystem",
- new Parameter[] {PARAM_ACTION})});
+ @Override
+ protected String argumentKind() {
+ return "{start,stop,restart}";
+ }
+ }
+
+ private final ActionArgument ARG_ACTION = new ActionArgument();
+
+ public CreateJIFSCommand() {
+ super("Manage the JIFS filesystem plugin");
+ registerArguments(ARG_ACTION);
+ }
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
- throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- String Act = ACTION.getValue(cmdLine);
-
- try {
- final PluginManager mgr = InitialNaming.lookup(PluginManager.NAME);
- final Plugin p =
- mgr.getRegistry().getPluginDescriptor("org.jnode.fs.jifs.def").getPlugin();
- if (new String("start").equals(Act)) {
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws NameNotFoundException, PluginException {
+ final PluginManager mgr = InitialNaming.lookup(PluginManager.NAME);
+ final Plugin p =
+ mgr.getRegistry().getPluginDescriptor("org.jnode.fs.jifs.def").getPlugin();
+ switch (ARG_ACTION.getValue()) {
+ case start:
p.start();
- }
- if (new String("stop").equals(Act)) {
+ break;
+ case stop:
p.stop();
- }
- if (new String("restart").equals(Act)) {
+ break;
+ case restart:
p.stop();
p.start();
- }
- } catch (NameNotFoundException N) {
- System.err.println(N);
+ break;
}
-
}
-
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <cr...@us...> - 2008-06-10 12:22:25
|
Revision: 4231
http://jnode.svn.sourceforge.net/jnode/?rev=4231&view=rev
Author: crawley
Date: 2008-06-10 05:22:23 -0700 (Tue, 10 Jun 2008)
Log Message:
-----------
Converted SMBMountCommand ... that's the last one I think.
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.smbfs.command.xml
trunk/fs/src/fs/org/jnode/fs/smbfs/command/SMBMountCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.smbfs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.smbfs.command.xml 2008-06-10 11:45:45 UTC (rev 4230)
+++ trunk/fs/descriptors/org.jnode.fs.smbfs.command.xml 2008-06-10 12:22:23 UTC (rev 4231)
@@ -7,9 +7,9 @@
license-name="lgpl"
provider-name="JNode.org">
-
<requires>
<import plugin="org.jnode.shell"/>
+ <import plugin="org.jnode.shell.syntax"/>
<import plugin="org.jnode.fs.smbfs"/>
</requires>
@@ -23,6 +23,20 @@
<alias name="smbmount" class="org.jnode.fs.smbfs.command.SMBMountCommand"/>
</extension>
+ <extension point="org.jnode.shell.syntaxes">
+ <syntax alias="smbmount">
+ <sequence description="mount an SMB file system">
+ <argument argLabel="directory"/>
+ <argument argLabel="host"/>
+ <argument argLabel="path"/>
+ <argument argLabel="username"/>
+ <optional>
+ <argument argLabel="password"/>
+ </optional>
+ </sequence>
+ </syntax>
+ </extension>
+
<extension point="org.jnode.security.permissions">
<permission class="java.net.SocketPermission" name="*:0-" actions="connect,resolve"/>
<permission class="java.util.PropertyPermission" name="*" actions="read,write"/>
Modified: trunk/fs/src/fs/org/jnode/fs/smbfs/command/SMBMountCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/smbfs/command/SMBMountCommand.java 2008-06-10 11:45:45 UTC (rev 4230)
+++ trunk/fs/src/fs/org/jnode/fs/smbfs/command/SMBMountCommand.java 2008-06-10 12:22:23 UTC (rev 4231)
@@ -21,10 +21,12 @@
package org.jnode.fs.smbfs.command;
+import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.DeviceUtils;
+import org.jnode.driver.DriverException;
import org.jnode.fs.service.FileSystemService;
import org.jnode.fs.smbfs.SMBFSDevice;
import org.jnode.fs.smbfs.SMBFSDriver;
@@ -33,49 +35,74 @@
import org.jnode.naming.InitialNaming;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Argument;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.ParsedArguments;
-import org.jnode.shell.help.argument.FileArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.HostNameArgument;
+import org.jnode.shell.syntax.StringArgument;
/**
* @author Levente S\u00e1ntha
+ * @author cr...@jn...
*/
public class SMBMountCommand extends AbstractCommand {
- private static final FileArgument MOUNTPOINT_ARG = new FileArgument("directory", "the mountpoint");
- private static final Argument HOST_ARG = new Argument("host", "Samba host");
- private static final Argument PATH_ARG = new Argument("path", "Samba path");
- private static final Argument USERNAME_ARG = new Argument("username", "Samba user");
- private static final Argument PASSWORD_ARG = new Argument("password", "Samba password");
- static Help.Info HELP_INFO = new Help.Info("mount", "Mount a Samba filesystem",
- new Parameter[]{new Parameter(MOUNTPOINT_ARG, Parameter.MANDATORY),
- new Parameter(HOST_ARG, Parameter.MANDATORY),
- new Parameter(PATH_ARG, Parameter.MANDATORY),
- new Parameter(USERNAME_ARG, Parameter.MANDATORY),
- new Parameter(PASSWORD_ARG, Parameter.OPTIONAL)});
+ private final FileArgument MOUNTPOINT_ARG =
+ new FileArgument("directory", Argument.MANDATORY, "the mountpoint");
+ private final HostNameArgument HOST_ARG =
+ new HostNameArgument("host", Argument.MANDATORY, "Samba host");
+ private final StringArgument PATH_ARG =
+ new StringArgument("path", Argument.MANDATORY, "Samba path");
+ private static final StringArgument USERNAME_ARG =
+ new StringArgument("username", Argument.MANDATORY, "Samba user");
+ private static final StringArgument PASSWORD_ARG =
+ new StringArgument("password", Argument.OPTIONAL, "Samba password");
+
+ public SMBMountCommand() {
+ super("Mount a Samba filesystem");
+ registerArguments(MOUNTPOINT_ARG, HOST_ARG, PATH_ARG, USERNAME_ARG, PASSWORD_ARG);
+ }
public static void main(String[] args) throws Exception {
new SMBMountCommand().execute(args);
}
- public void execute(CommandLine commandLine, InputStream in,
- PrintStream out, PrintStream err) throws Exception {
- ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
-
- final String mount_point = MOUNTPOINT_ARG.getValue(cmdLine);
- final String host = HOST_ARG.getValue(cmdLine);
- final String path = PATH_ARG.getValue(cmdLine);
- final String user = USERNAME_ARG.getValue(cmdLine);
- final String password = PASSWORD_ARG.getValue(cmdLine);
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws Exception {
+ final File mountPoint = MOUNTPOINT_ARG.getValue();
+ final String host = HOST_ARG.getValue();
+ final String path = PATH_ARG.getValue();
+ final String user = USERNAME_ARG.getValue();
+ final String password = PASSWORD_ARG.getValue();
+
+ final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
+ SMBFileSystemType type = fss.getFileSystemType(SMBFileSystemType.ID);
+
final SMBFSDevice dev = new SMBFSDevice(host, path, user, password);
dev.setDriver(new SMBFSDriver());
final DeviceManager dm = DeviceUtils.getDeviceManager();
dm.register(dev);
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
- SMBFileSystemType type = fss.getFileSystemType(SMBFileSystemType.ID);
- final SMBFileSystem fs = type.create(dev, true);
- fss.registerFileSystem(fs);
- fss.mount(mount_point, fs, null);
+
+ // This controls whether we attempt to undo the effects of the command
+ // e.g. when the 'mount' step fails.
+ boolean ok = false;
+ try {
+ final SMBFileSystem fs = type.create(dev, true);
+ fss.registerFileSystem(fs);
+ try {
+ fss.mount(mountPoint.toString(), fs, null);
+ ok = true;
+ } finally {
+ if (!ok) {
+ fss.unregisterFileSystem(dev);
+ }
+ }
+ } finally {
+ try {
+ if (!ok) {
+ dm.unregister(dev);
+ }
+ } catch (DriverException ex) {
+ // ignore
+ }
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|