From: <cr...@us...> - 2008-06-01 15:50:15
|
Revision: 4173 http://jnode.svn.sourceforge.net/jnode/?rev=4173&view=rev Author: crawley Date: 2008-06-01 08:50:10 -0700 (Sun, 01 Jun 2008) Log Message: ----------- Converted FdiskCommand, tidied up and fixed some minor bugs. Renamed Decimal/BinaryPrefix to Decimal/BinaryScaleFactor and added a supertype. Modified Paths: -------------- trunk/core/src/core/org/jnode/util/NumberUtils.java trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/DeviceView.java trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/PartitionView.java trunk/fs/descriptors/org.jnode.partitions.command.xml trunk/fs/src/fs/org/jnode/fs/ext2/BlockSize.java trunk/fs/src/fs/org/jnode/fs/ext2/Ext2FileSystemFormatter.java trunk/fs/src/fs/org/jnode/fs/jfat/ClusterSize.java trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java trunk/fs/src/fs/org/jnode/partitions/help/argument/IBMPartitionTypeArgument.java Added Paths: ----------- trunk/core/src/core/org/jnode/util/BinaryScaleFactor.java trunk/core/src/core/org/jnode/util/DecimalScaleFactor.java trunk/core/src/core/org/jnode/util/ScaleFactor.java trunk/shell/src/shell/org/jnode/shell/syntax/SizeArgument.java Removed Paths: ------------- trunk/core/src/core/org/jnode/util/BinaryPrefix.java trunk/core/src/core/org/jnode/util/DecimalPrefix.java Deleted: trunk/core/src/core/org/jnode/util/BinaryPrefix.java =================================================================== --- trunk/core/src/core/org/jnode/util/BinaryPrefix.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/core/src/core/org/jnode/util/BinaryPrefix.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -1,67 +0,0 @@ -/* - * $Id$ - */ -package org.jnode.util; - -import org.jnode.vm.annotation.SharedStatics; - -@SharedStatics -public enum BinaryPrefix { - B(1l, ""), - K(1024l, "K"), - M(1024l * 1024l, "M"), - G(1024l * 1024l * 1024l, "G"), - T(1024l * 1024l * 1024l * 1024l, "T"), - P(1024l * 1024l * 1024l * 1024l * 1024l, "P"), - E(1024l * 1024l * 1024l * 1024l * 1024l * 1024l, "E"); - //these units have too big multipliers to fit in a long - // (aka they are greater than 2^64) : - //Z(1024l*1024l*1024l*1024l*1024l*1024l*1024l, "Z"), - //Y(1024l*1024l*1024l*1024l*1024l*1024l*1024l*1024l, "Y"); - - public static final BinaryPrefix MIN = B; - public static final BinaryPrefix MAX = E; - - final private long multiplier; - final private String unit; - - private BinaryPrefix(long multiplier, String unit) { - this.multiplier = multiplier; - this.unit = unit; - } - - public long getMultiplier() { - return multiplier; - } - - public String getUnit() { - return unit; - } - - public String toString() { - return multiplier + ", " + unit; - } - - /** - * Convert the given value to a size string like 64K - * - * @param value the size to convert - * @param nbDecimals number of significant figures to display after dot. use Integer.MAX_VALUE for all. - * @return the text for the size - */ - public static String apply(final long value, final int nbDecimals) { - long v = value; - BinaryPrefix unit = null; - for (BinaryPrefix u : values()) { - if ((v < 1024) && (v >= 0)) { - unit = u; - break; - } - - v = v >>> 10; - } - unit = (unit == null) ? MAX : unit; - float dv = ((float) value) / unit.getMultiplier(); - return NumberUtils.toString(dv, nbDecimals) + " " + unit.getUnit(); - } -} Added: trunk/core/src/core/org/jnode/util/BinaryScaleFactor.java =================================================================== --- trunk/core/src/core/org/jnode/util/BinaryScaleFactor.java (rev 0) +++ trunk/core/src/core/org/jnode/util/BinaryScaleFactor.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -0,0 +1,67 @@ +/* + * $Id$ + */ +package org.jnode.util; + +import org.jnode.vm.annotation.SharedStatics; + +@SharedStatics +public enum BinaryScaleFactor implements ScaleFactor { + B(1l, ""), + K(1024l, "K"), + M(1024l * 1024l, "M"), + G(1024l * 1024l * 1024l, "G"), + T(1024l * 1024l * 1024l * 1024l, "T"), + P(1024l * 1024l * 1024l * 1024l * 1024l, "P"), + E(1024l * 1024l * 1024l * 1024l * 1024l * 1024l, "E"); + //these units have too big multipliers to fit in a long + // (aka they are greater than 2^64) : + //Z(1024l*1024l*1024l*1024l*1024l*1024l*1024l, "Z"), + //Y(1024l*1024l*1024l*1024l*1024l*1024l*1024l*1024l, "Y"); + + public static final BinaryScaleFactor MIN = B; + public static final BinaryScaleFactor MAX = E; + + final private long multiplier; + final private String unit; + + private BinaryScaleFactor(long multiplier, String unit) { + this.multiplier = multiplier; + this.unit = unit; + } + + public long getMultiplier() { + return multiplier; + } + + public String getUnit() { + return unit; + } + + public String toString() { + return multiplier + ", " + unit; + } + + /** + * Convert the given value to a size string like 64K + * + * @param value the size to convert + * @param nbDecimals number of significant figures to display after dot. use Integer.MAX_VALUE for all. + * @return the text for the size + */ + public static String apply(final long value, final int nbDecimals) { + long v = value; + BinaryScaleFactor unit = null; + for (BinaryScaleFactor u : values()) { + if ((v < 1024) && (v >= 0)) { + unit = u; + break; + } + + v = v >>> 10; + } + unit = (unit == null) ? MAX : unit; + float dv = ((float) value) / unit.getMultiplier(); + return NumberUtils.toString(dv, nbDecimals) + " " + unit.getUnit(); + } +} Deleted: trunk/core/src/core/org/jnode/util/DecimalPrefix.java =================================================================== --- trunk/core/src/core/org/jnode/util/DecimalPrefix.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/core/src/core/org/jnode/util/DecimalPrefix.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -1,67 +0,0 @@ -/* - * $Id$ - */ -package org.jnode.util; - -import org.jnode.vm.annotation.SharedStatics; - -@SharedStatics -public enum DecimalPrefix { - B(1l, ""), - K(1000l, "k"), - M(1000l * 1000l, "M"), - G(1000l * 1000l * 1000l, "G"), - T(1000l * 1000l * 1000l * 1000l, "T"), - P(1000l * 1000l * 1000l * 1000l * 1000l, "P"), - E(1000l * 1000l * 1000l * 1000l * 1000l * 1000l, "E"); - //these units have too big multipliers to fit in a long - // (aka they are greater than 2^64) : - //Z(1000l*1000l*1000l*1000l*1000l*1000l*1000l, "Z"), - //Y(1000l*1000l*1000l*1000l*1000l*1000l*1000l*1000l, "Y"); - - public static final DecimalPrefix MIN = B; - public static final DecimalPrefix MAX = E; - - final private long multiplier; - final private String unit; - - private DecimalPrefix(long multiplier, String unit) { - this.multiplier = multiplier; - this.unit = unit; - } - - public long getMultiplier() { - return multiplier; - } - - public String getUnit() { - return unit; - } - - public String toString() { - return multiplier + ", " + unit; - } - - /** - * Convert the given value to a size string like 64K - * - * @param v the size to convert - * @param nbDecimals number of significant figures to display after dot. use Integer.MAX_VALUE for all. - * @return the text for the size - */ - public static String apply(final long value, final int nbDecimals) { - long v = value; - DecimalPrefix unit = null; - for (DecimalPrefix u : values()) { - if ((v < 1000l) && (v >= 0l)) { - unit = u; - break; - } - - v = v / 1000l; - } - unit = (unit == null) ? MAX : unit; - float dv = ((float) value) / unit.getMultiplier(); - return NumberUtils.toString(dv, nbDecimals) + " " + unit.getUnit(); - } -} Added: trunk/core/src/core/org/jnode/util/DecimalScaleFactor.java =================================================================== --- trunk/core/src/core/org/jnode/util/DecimalScaleFactor.java (rev 0) +++ trunk/core/src/core/org/jnode/util/DecimalScaleFactor.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -0,0 +1,67 @@ +/* + * $Id$ + */ +package org.jnode.util; + +import org.jnode.vm.annotation.SharedStatics; + +@SharedStatics +public enum DecimalScaleFactor implements ScaleFactor { + B(1l, ""), + K(1000l, "k"), + M(1000l * 1000l, "M"), + G(1000l * 1000l * 1000l, "G"), + T(1000l * 1000l * 1000l * 1000l, "T"), + P(1000l * 1000l * 1000l * 1000l * 1000l, "P"), + E(1000l * 1000l * 1000l * 1000l * 1000l * 1000l, "E"); + //these units have too big multipliers to fit in a long + // (aka they are greater than 2^64) : + //Z(1000l*1000l*1000l*1000l*1000l*1000l*1000l, "Z"), + //Y(1000l*1000l*1000l*1000l*1000l*1000l*1000l*1000l, "Y"); + + public static final DecimalScaleFactor MIN = B; + public static final DecimalScaleFactor MAX = E; + + final private long multiplier; + final private String unit; + + private DecimalScaleFactor(long multiplier, String unit) { + this.multiplier = multiplier; + this.unit = unit; + } + + public long getMultiplier() { + return multiplier; + } + + public String getUnit() { + return unit; + } + + public String toString() { + return multiplier + ", " + unit; + } + + /** + * Convert the given value to a size string like 64K + * + * @param v the size to convert + * @param nbDecimals number of significant figures to display after dot. use Integer.MAX_VALUE for all. + * @return the text for the size + */ + public static String apply(final long value, final int nbDecimals) { + long v = value; + DecimalScaleFactor unit = null; + for (DecimalScaleFactor u : values()) { + if ((v < 1000l) && (v >= 0l)) { + unit = u; + break; + } + + v = v / 1000l; + } + unit = (unit == null) ? MAX : unit; + float dv = ((float) value) / unit.getMultiplier(); + return NumberUtils.toString(dv, nbDecimals) + " " + unit.getUnit(); + } +} Modified: trunk/core/src/core/org/jnode/util/NumberUtils.java =================================================================== --- trunk/core/src/core/org/jnode/util/NumberUtils.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/core/src/core/org/jnode/util/NumberUtils.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -255,7 +255,7 @@ * @return the text for of the size */ public static String toDecimalByte(long v, int nbDecimals) { - return DecimalPrefix.apply(v, nbDecimals) + "B"; + return DecimalScaleFactor.apply(v, nbDecimals) + "B"; } /** @@ -266,7 +266,7 @@ * @return the text for of the size */ public static String toBinaryByte(long v, int nbDecimals) { - return BinaryPrefix.apply(v, nbDecimals) + "B"; + return BinaryScaleFactor.apply(v, nbDecimals) + "B"; } /** Added: trunk/core/src/core/org/jnode/util/ScaleFactor.java =================================================================== --- trunk/core/src/core/org/jnode/util/ScaleFactor.java (rev 0) +++ trunk/core/src/core/org/jnode/util/ScaleFactor.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -0,0 +1,7 @@ +package org.jnode.util; + +public interface ScaleFactor { + public long getMultiplier(); + + public String getUnit(); +} Modified: trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/DeviceView.java =================================================================== --- trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/DeviceView.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/DeviceView.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -11,7 +11,7 @@ import org.jnode.apps.jpartition.model.Device; import org.jnode.apps.jpartition.model.Partition; import org.jnode.apps.jpartition.model.UserFacade; -import org.jnode.util.BinaryPrefix; +import org.jnode.util.BinaryScaleFactor; import org.jnode.util.NumberUtils; public class DeviceView extends DiskAreaView<Device> Modified: trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/PartitionView.java =================================================================== --- trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/PartitionView.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/distr/src/apps/org/jnode/apps/jpartition/swingview/PartitionView.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -14,7 +14,7 @@ import org.jnode.apps.jpartition.swingview.actions.AddPartitionAction; import org.jnode.apps.jpartition.swingview.actions.FormatPartitionAction; import org.jnode.apps.jpartition.swingview.actions.RemovePartitionAction; -import org.jnode.util.BinaryPrefix; +import org.jnode.util.BinaryScaleFactor; import org.jnode.util.NumberUtils; public class PartitionView extends DiskAreaView<Partition> Modified: trunk/fs/descriptors/org.jnode.partitions.command.xml =================================================================== --- trunk/fs/descriptors/org.jnode.partitions.command.xml 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/fs/descriptors/org.jnode.partitions.command.xml 2008-06-01 15:50:10 UTC (rev 4173) @@ -24,5 +24,37 @@ <extension point="org.jnode.shell.aliases"> <alias name="fdisk" class="org.jnode.partitions.command.FdiskCommand"/> </extension> + + <extension point="org.jnode.shell.syntaxes"> + <syntax alias="fdisk"> + <empty description="list all block devices"/> + <argument argLabel="deviceId" description="show a device's partition table"/> + <sequence description="initialize a device's master boot record"> + <option argLabel="initMBR" shortName="i" longName="initMBR"/> + <argument argLabel="deviceId"/> + </sequence> + <sequence description="modify (or create) a partition"> + <option argLabel="modify" shortName="m" longName="modify"/> + <argument argLabel="deviceId"/> + <argument argLabel="partition"/> + <argument argLabel="start"/> + <alternatives> + <argument argLabel="sectors"/> + <argument argLabel="bytes"/> + </alternatives> + <argument argLabel="type"/> + </sequence> + <sequence description="toggle a partition's bootable flag"> + <option argLabel="bootable" shortName="b" longName="bootable"/> + <argument argLabel="deviceId"/> + <argument argLabel="partition"/> + </sequence> + <sequence description="delete a partition"> + <option argLabel="delete" shortName="d" longName="delete"/> + <argument argLabel="deviceId"/> + <argument argLabel="partition"/> + </sequence> + </syntax> + </extension> </plugin> \ No newline at end of file Modified: trunk/fs/src/fs/org/jnode/fs/ext2/BlockSize.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/ext2/BlockSize.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/fs/src/fs/org/jnode/fs/ext2/BlockSize.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -1,6 +1,6 @@ package org.jnode.fs.ext2; -import org.jnode.util.BinaryPrefix; +import org.jnode.util.BinaryScaleFactor; public enum BlockSize { @@ -12,7 +12,7 @@ private BlockSize(int blockSizeKb) { - this.size = (int) (blockSizeKb * BinaryPrefix.K.getMultiplier()); //Converted into KB + this.size = (int) (blockSizeKb * BinaryScaleFactor.K.getMultiplier()); //Converted into KB } final public int getSize() { Modified: trunk/fs/src/fs/org/jnode/fs/ext2/Ext2FileSystemFormatter.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/ext2/Ext2FileSystemFormatter.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/fs/src/fs/org/jnode/fs/ext2/Ext2FileSystemFormatter.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -25,7 +25,7 @@ import org.jnode.fs.FileSystemException; import org.jnode.fs.Formatter; import org.jnode.fs.service.FileSystemService; -import org.jnode.util.BinaryPrefix; +import org.jnode.util.BinaryScaleFactor; import org.jnode.naming.InitialNaming; import javax.naming.NameNotFoundException; Modified: trunk/fs/src/fs/org/jnode/fs/jfat/ClusterSize.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/jfat/ClusterSize.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/fs/src/fs/org/jnode/fs/jfat/ClusterSize.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -1,6 +1,6 @@ package org.jnode.fs.jfat; -import org.jnode.util.BinaryPrefix; +import org.jnode.util.BinaryScaleFactor; public enum ClusterSize { @@ -16,7 +16,7 @@ private ClusterSize(int sizeInKb) { - size = (int) (sizeInKb * BinaryPrefix.K.getMultiplier()); //Converted into KB + size = (int) (sizeInKb * BinaryScaleFactor.K.getMultiplier()); //Converted into KB } final public int getSize() Modified: trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/fs/src/fs/org/jnode/partitions/command/FdiskCommand.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -22,6 +22,8 @@ package org.jnode.partitions.command; import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; import java.nio.ByteBuffer; import java.util.Collection; import java.util.List; @@ -43,278 +45,187 @@ import org.jnode.partitions.ibm.IBMPartitionTableEntry; import org.jnode.partitions.ibm.IBMPartitionTableType; import org.jnode.partitions.ibm.IBMPartitionTypes; -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.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.SizeArgument; +import org.jnode.shell.AbstractCommand; +import org.jnode.shell.CommandLine; +import org.jnode.shell.syntax.*; +import com.sun.org.apache.xml.internal.utils.ObjectPool; + /** * @author gbin * @author Trickkiste + * @author cr...@jn... */ -public class FdiskCommand { +public class FdiskCommand extends AbstractCommand { + // FIXME ... this is a dangerous command and it needs some extra checking to help + // avoid catastrophic errors. At the very least, it needs a mode that shows the + // user what would happen but does nothing. + private final FlagArgument FLAG_INIT_MBR = new FlagArgument( + "initMBR", Argument.OPTIONAL, "if set, init the device's Master Boot Record"); - static final OptionArgument INITMBR = - new OptionArgument( - "init. MBR", - "Type parameter", - new OptionArgument.Option[] { - new OptionArgument.Option("--initmbr", "initialize the Master Boot Record of the device")}); + private final FlagArgument FLAG_DELETE = new FlagArgument( + "delete", Argument.OPTIONAL, "if set, delete a partition"); + + private final FlagArgument FLAG_BOOTABLE = new FlagArgument( + "bootable", Argument.OPTIONAL, "if set, toggle the partition's bootable flag"); + + private final FlagArgument FLAG_MODIFY = new FlagArgument( + "modify", Argument.OPTIONAL, "if set, modify or create a partition"); - static final OptionArgument ACTION = - new OptionArgument( - "action", - "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")}); - - 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 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"); + private final IntegerArgument ARG_PARTITION = new IntegerArgument( + "partition", Argument.OPTIONAL, "Target partition number (0..3)"); - static final DeviceArgument ARG_DEVICE = - new DeviceArgument("device-id", "the device on which you want to change/create the partition"); + private final LongArgument ARG_START = new LongArgument( + "start", Argument.OPTIONAL, "Partition start sector"); + + private final LongArgument ARG_SECTORS = new LongArgument( + "sectors", Argument.OPTIONAL, "Partition size in sectors"); + + private final SizeArgument ARG_BYTES = new SizeArgument( + "bytes", Argument.OPTIONAL, "Partition size in bytes (300K, 45M, etc)"); + + private final IBMPartitionTypeArgument ARG_TYPE = new IBMPartitionTypeArgument( + "type", Argument.OPTIONAL, "IBM partition type code"); + + private final DeviceArgument ARG_DEVICE = new DeviceArgument( + "deviceId", Argument.OPTIONAL, "Target device", BlockDeviceAPI.class); + - 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_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 FdiskCommand() { + super("perform disk partition management tasks"); + registerArguments(FLAG_BOOTABLE, FLAG_DELETE, FLAG_INIT_MBR, FLAG_MODIFY, + ARG_DEVICE, ARG_PARTITION, ARG_START, ARG_SECTORS, ARG_BYTES, ARG_TYPE); + } - 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("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 void main(String[] args) throws SyntaxErrorException { - ParsedArguments cmdLine = HELP_INFO.parse(args); - - DeviceManager dm; - try { - 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); - - // no parameters - if (!isDevice) { - listAvailableDevice(dm); - return; - } - - // only device is set - 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); - - // initMBR - if (isInitMBR) { - helper.initMbr(); - helper.write(); - return; - } - - int partNumber = getPartitionNumber(helper, cmdLine); - - // modify a partition ? - if (ACTION_MODIFY.getValue(cmdLine).intern() == "-m") { - modifyPartition(helper, partNumber, cmdLine); - helper.write(); - return; - } - - // delete a partition ? - if (ACTION.getValue(cmdLine).intern() == "-d") { - helper.deletePartition(partNumber); - helper.write(); - return; - } - - // toggle boot flag for a partition ? - if (ACTION.getValue(cmdLine).intern() == "-b") { - helper.toggleBootable(partNumber); - helper.write(); - } - } catch (IOException e) { - e.printStackTrace(); - } catch (NameNotFoundException e) { - e.printStackTrace(); - } catch (ApiNotFoundException e) { - e.printStackTrace(); - } catch (DeviceNotFoundException e) { - e.printStackTrace(); - } + public static void main(String[] args) throws Exception { + new FdiskCommand().execute(args); } - - private static int getPartitionNumber(PartitionHelper helper, ParsedArguments cmdLine) - { - int partNumber = PARTITION.getInteger(cmdLine); + + public void execute(CommandLine commandLine, InputStream in, + PrintStream out, PrintStream err) throws Exception { + final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME); + + if (!ARG_DEVICE.isSet()) { + // Show all devices. + listAvailableDevices(dm, out); + return; + } + + Device dev = ARG_DEVICE.getValue(); + // FIXME PartitionHelper assumes that the device is an IDE device !?! + if (!(dev instanceof IDEDevice)) { + err.println(dev.getId() + " is not an IDE device"); + exit(1); + } + final PartitionHelper helper = new PartitionHelper(dev.getId()); - if ((partNumber >= helper.getNbPartitions()) || - (partNumber < 0) ) + if (FLAG_BOOTABLE.isSet()) { + helper.toggleBootable(getPartitionNumber(helper)); + helper.write(); + } + else if (FLAG_DELETE.isSet()) { + helper.deletePartition(getPartitionNumber(helper)); + helper.write(); + } + else if (FLAG_MODIFY.isSet()) { + modifyPartition(helper, getPartitionNumber(helper), out); + helper.write(); + } + else if (FLAG_INIT_MBR.isSet()) { + helper.initMbr(); + helper.write(); + } + else { + printPartitionTable(dev, out); + } + } + + private int getPartitionNumber(PartitionHelper helper) { + int partNumber = ARG_PARTITION.getValue(); + if (partNumber >= helper.getNbPartitions() || partNumber < 0) { throw new IllegalArgumentException("Partition number is invalid"); - + } return partNumber; } - 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); - -// 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 void modifyPartition(PartitionHelper helper, int id, PrintStream out) + throws IOException { + long start = ARG_START.getValue(); + long size = ARG_SECTORS.isSet() ? ARG_SECTORS.getValue() : ARG_BYTES.getValue(); + IBMPartitionTypes type = ARG_TYPE.getValue(); + + out.println("Init " + id + " with start = " + start + + ", size = " + size + ", fs = " + + Integer.toHexString(type.getCode())); + boolean sizeUnit = ARG_BYTES.isSet() ? + PartitionHelper.BYTES : PartitionHelper.SECTORS; + helper.modifyPartition(id, false, start, size, sizeUnit, type); } - private static void printTable(String deviceName, DeviceManager dm) - throws DeviceNotFoundException, ApiNotFoundException, IOException { - { - IDEDevice current = (IDEDevice)dm.getDevice(deviceName); - BlockDeviceAPI api = current.getAPI(BlockDeviceAPI.class); - IDEDriveDescriptor descriptor = current.getDescriptor(); - ByteBuffer MBR = ByteBuffer.allocate(IDEConstants.SECTOR_SIZE); - api.read(0, MBR); - if (IBMPartitionTable.containsPartitionTable(MBR.array())) { - IBMPartitionTable partitionTable = new IBMPartitionTable(new IBMPartitionTableType(), MBR.array(), current); + private void printPartitionTable(Device dev, PrintStream out) + throws DeviceNotFoundException, ApiNotFoundException, IOException { + IDEDevice ideDev = null; + // FIXME ... this needs to be generalized to other disc device types. + if (dev instanceof IDEDevice) { + ideDev = (IDEDevice) dev; + } + BlockDeviceAPI api = dev.getAPI(BlockDeviceAPI.class); + IDEDriveDescriptor descriptor = ideDev.getDescriptor(); + ByteBuffer MBR = ByteBuffer.allocate(IDEConstants.SECTOR_SIZE); + api.read(0, MBR); + if (IBMPartitionTable.containsPartitionTable(MBR.array())) { + IBMPartitionTable partitionTable = + new IBMPartitionTable(new IBMPartitionTableType(), MBR.array(), dev); + if (ideDev != null) { + out.println( "IDE Disk : " + dev.getId() + ": " + + descriptor.getSectorsIn28bitAddressing() * 512 + " bytes"); + } + out.println("Device Boot Start End Blocks System"); - System.out.println( - "Disk : " + current.getId() + ": " + descriptor.getSectorsIn28bitAddressing() * 512 + " bytes"); - System.out.println("Device Boot Start End Blocks System"); - - 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 - + " " - + (entry.getBootIndicator() ? "Boot" : "No") - + " " - + entry.getStartLba() - + " " - + (entry.getStartLba() + entry.getNrSectors()) - + " " - + entry.getNrSectors() - + " " - + si); - if(entry.isExtended()) { - final List<IBMPartitionTableEntry> exPartitions = partitionTable.getExtendedPartitions(); - int j = 0; - for (IBMPartitionTableEntry exEntry : exPartitions) { - si = exEntry.getSystemIndicator(); - System.out.println( - "ID " - + i - + " " - + (exEntry.getBootIndicator() ? "Boot" : "No") - + " " - + exEntry.getStartLba() - + " " - + "-----"//(exEntry.getStartLba() + entry.getNrSectors()) - + " " - + "-----"//exEntry.getNrSectors() - + " " - + si); - j++; - } - } - i++; - } - - } else { - System.out.println(" No valid MBR found on this device. Use --initmbr to initialize it."); - } - } + int i = 0; + for (IBMPartitionTableEntry entry : partitionTable) { + //IBMPartitionTableEntry entry = (IBMPartitionTableEntry)partitionTable.getEntry(i); + IBMPartitionTypes si = entry.getSystemIndicator(); + if (si != IBMPartitionTypes.PARTTYPE_EMPTY) { + out.println("ID " + i + " " + + (entry.getBootIndicator() ? "Boot" : "No") + " " + + entry.getStartLba() + " " + + (entry.getStartLba() + entry.getNrSectors()) + " " + + entry.getNrSectors() + " " + si); + } + if (entry.isExtended()) { + final List<IBMPartitionTableEntry> exPartitions = partitionTable.getExtendedPartitions(); + int j = 0; + for (IBMPartitionTableEntry exEntry : exPartitions) { + si = exEntry.getSystemIndicator(); + // FIXME ... this needs work + out.println("ID " + i + " " + + (exEntry.getBootIndicator() ? "Boot" : "No") + " " + + exEntry.getStartLba() + " " + + "-----" /* (exEntry.getStartLba() + entry.getNrSectors()) */ + " " + + "-----" /* exEntry.getNrSectors() */ + " " + si); + j++; + } + } + i++; + } + } + else { + out.println(" No valid MBR found on this device. Use --initMBR to initialize it."); + } } - private static void listAvailableDevice(DeviceManager dm) { + private void listAvailableDevices(DeviceManager dm, PrintStream out) { final Collection<Device> allDevices = dm.getDevicesByAPI(BlockDeviceAPI.class); - for (Device current : allDevices) { - System.out.println("Found device : " + current.getId() + "[" + current.getClass() + "]"); - - if (current instanceof IDEDevice) { - IDEDevice ideDevice = (IDEDevice)current; - IDEDriveDescriptor currentDescriptor = ideDevice.getDescriptor(); - if (currentDescriptor.isDisk()) { - System.out.println( - " IDE Disk : " - + ideDevice.getId() - + "(" - + currentDescriptor.getModel() - + " " - + currentDescriptor.getSectorsIn28bitAddressing() * IDEConstants.SECTOR_SIZE - + ")"); + for (Device dev : allDevices) { + out.println("Found device : " + dev.getId() + "[" + dev.getClass() + "]"); + if (dev instanceof IDEDevice) { + IDEDevice ideDevice = (IDEDevice) dev; + IDEDriveDescriptor desc = ideDevice.getDescriptor(); + if (desc.isDisk()) { + out.println(" IDE Disk : " + ideDevice.getId() + + "(" + desc.getModel() + " " + + desc.getSectorsIn28bitAddressing() * IDEConstants.SECTOR_SIZE + ")"); } } } Modified: trunk/fs/src/fs/org/jnode/partitions/help/argument/IBMPartitionTypeArgument.java =================================================================== --- trunk/fs/src/fs/org/jnode/partitions/help/argument/IBMPartitionTypeArgument.java 2008-06-01 15:45:59 UTC (rev 4172) +++ trunk/fs/src/fs/org/jnode/partitions/help/argument/IBMPartitionTypeArgument.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -1,43 +1,72 @@ +/* + * $Id$ + * + * JNode.org + * Copyright (C) 2008 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.partitions.help.argument; -import java.util.Arrays; -import java.util.Collection; - +import org.jnode.driver.console.CompletionInfo; import org.jnode.partitions.ibm.IBMPartitionTypes; -import org.jnode.shell.help.ParsedArguments; -import org.jnode.shell.help.argument.ListArgument; +import org.jnode.shell.CommandLine.Token; +import org.jnode.shell.syntax.Argument; +import org.jnode.shell.syntax.CommandSyntaxException; - -public class IBMPartitionTypeArgument extends ListArgument<IBMPartitionTypes> +/** + * Argument close for partition type codes. Input is in hexadecimal, and + * completion is supported. + * + * @author cr...@jn... + */ +public class IBMPartitionTypeArgument extends Argument<IBMPartitionTypes> { - public IBMPartitionTypeArgument(String name, String description) - { - super(name, description, false); - } - - public IBMPartitionTypes getArgValue(String value) - { - int fs = Integer.parseInt(value, 16); - return IBMPartitionTypes.valueOf(fs); - } + + public IBMPartitionTypeArgument(String label, int flags, String description) { + super(label, flags, new IBMPartitionTypes[0], description); + } - @Override - protected String toStringArgument(IBMPartitionTypes arg) { - return Integer.toHexString(arg.getCode()); - } + @Override + protected IBMPartitionTypes doAccept(Token value) throws CommandSyntaxException { + try { + int code = Integer.parseInt(value.token, 16); + return IBMPartitionTypes.valueOf(code); + } + catch (NumberFormatException ex) { + throw new CommandSyntaxException("Not a valid hexadecimal number"); + } + catch (IllegalArgumentException ex) { + throw new CommandSyntaxException(ex.getMessage()); + } + } + + @Override + public void complete(CompletionInfo completion, String partial) { + partial = partial.toLowerCase(); + for (IBMPartitionTypes pt : IBMPartitionTypes.values()) { + String code = Integer.toHexString(pt.getCode()); + if (code.startsWith(partial)) { + completion.addCompletion(code); + } + } + } + + @Override + protected String argumentKind() { + return "partition type"; + } - @Override - protected Collection<IBMPartitionTypes> getValues() { - return Arrays.asList(IBMPartitionTypes.values()); - } - - @Override - public int compare(IBMPartitionTypes choice1, IBMPartitionTypes choice2) { - return choice1.getCode() - choice2.getCode(); - } - - @Override - protected boolean isPartOfArgument(IBMPartitionTypes argument, String part) { - return toStringArgument(argument).startsWith(part); - } } Added: trunk/shell/src/shell/org/jnode/shell/syntax/SizeArgument.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/syntax/SizeArgument.java (rev 0) +++ trunk/shell/src/shell/org/jnode/shell/syntax/SizeArgument.java 2008-06-01 15:50:10 UTC (rev 4173) @@ -0,0 +1,90 @@ +/* + * $Id$ + * + * JNode.org + * Copyright (C) 2007-2008 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.shell.syntax; + +import org.jnode.driver.console.CompletionInfo; +import org.jnode.shell.CommandLine.Token; +import org.jnode.util.BinaryScaleFactor; +import org.jnode.util.DecimalScaleFactor; +import org.jnode.util.ScaleFactor; + +/** + * This Argument class accepts size values. These are integers with an optional decimal + * or binary scaling suffix; e.g. 1K means 1000 or 1024 + * + * @author cr...@jn... + */ +public class SizeArgument extends Argument<Long> { + private final boolean binaryScaling; + + public SizeArgument(String label, int flags, String description) { + this(label, flags, true, description); + } + + public SizeArgument(String label, int flags, boolean binaryScaling, String description) { + super(label, flags, new Long[0], description); + this.binaryScaling = binaryScaling; + } + + @Override + protected Long doAccept(Token token) throws CommandSyntaxException { + String str = token.token; + ScaleFactor factor = scaleFactor(str); + if (factor != null) { + str = str.substring(0, str.length() - factor.getUnit().length()); + } + try { + long tmp = Long.parseLong(str); + return new Long(tmp * factor.getMultiplier()); + } + catch (NumberFormatException ex) { + throw new CommandSyntaxException("invalid number '" + token.token + "'"); + } + } + + private ScaleFactor scaleFactor(String str) { + ScaleFactor[] prefixes = binaryScaling ? + BinaryScaleFactor.values() : DecimalScaleFactor.values(); + for (ScaleFactor unit : prefixes) { + String unitStr = unit.getUnit(); + if (str.endsWith(unitStr)) { + return unit; + } + } + return null; + } + + @Override + public void complete(CompletionInfo completion, String partial) { + // No completion for now + } + + @Override + protected String state() { + return super.state() + "binaryScaling=" + binaryScaling; + } + + @Override + protected String argumentKind() { + return "size"; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |