Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cli/descriptors/org.jnode.command.file.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@
</sequence>
</syntax>
<syntax alias="mkdir">
<argument argLabel="directory" description="create a new directory"/>
<sequence description="create a new directory">
<optionSet>
<option argLabel="parents" shortName="p" longName="parents"/>
</optionSet>
<argument argLabel="directory" description="create a new directory"/>
</sequence>
</syntax>
<syntax alias="paste">
<empty />
Expand Down
29 changes: 20 additions & 9 deletions cli/src/commands/org/jnode/command/file/MkdirCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.syntax.Argument;
import org.jnode.shell.syntax.FileArgument;
import org.jnode.shell.syntax.FlagArgument;

/**
* @author Guillaume BINET (gbin@users.sourceforge.net)
Expand All @@ -34,16 +35,19 @@
public class MkdirCommand extends AbstractCommand {

private static final String help_dir = "the directory to create";
private static final String help_parents = "if set, create parent directories as needed";
private static final String help_super = "Create a new directory";
private static final String fmt_exists = "%s already exists%n";
private static final String err_cant_create = "Cannot create directory";

private final FileArgument argDir;
private final FlagArgument argParents;

public MkdirCommand() {
super(help_super);
argDir = new FileArgument("directory", Argument.MANDATORY | Argument.NONEXISTENT, help_dir);
registerArguments(argDir);
argDir = new FileArgument("directory", Argument.MANDATORY, help_dir);
argParents = new FlagArgument("parents", Argument.OPTIONAL, help_parents);
registerArguments(argDir, argParents);
}

public static void main(String[] args) throws Exception {
Expand All @@ -53,13 +57,20 @@ public static void main(String[] args) throws Exception {
public void execute() {
File dir = argDir.getValue();
PrintWriter err = getError().getPrintWriter();
if (dir.exists()) {
err.format(fmt_exists, dir);
exit(1);
}
if (!dir.mkdir()) {
err.println(err_cant_create);
exit(1);
if (argParents.isSet()) {
if (!dir.mkdirs()) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 mkdirs() ignores existing directory case

File.mkdirs() returns false when the directory already exists. Per POSIX mkdir -p, this should succeed silently. Current code treats any false return as a hard error:

if (!dir.mkdirs()) {
    err.println(err_cant_create);
    exit(1);
}

Fix: check !dir.exists() before treating failure as error:

if (argParents.isSet()) {
    if (!dir.exists() && !dir.mkdirs()) {
        err.println(err_cant_create);
        exit(1);
    }
}

err.println(err_cant_create);
exit(1);
}
} else {
if (dir.exists()) {
err.format(fmt_exists, dir);
exit(1);
}
if (!dir.mkdir()) {
err.println(err_cant_create);
exit(1);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<include setName="tail-command-tests.xml"/>
<include setName="tee-command-tests.xml"/>
<include setName="wc-command-tests.xml"/>
<include setName="mkdir-command-tests.xml"/>
</testSet>
22 changes: 22 additions & 0 deletions cli/src/test/org/jnode/test/command/file/mkdir-command-tests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<testSet title="mkdir command tests">
<plugin id="org.jnode.command.file"/>
<plugin id="org.jnode.shell.bjorne" class="org.jnode.test.shell.bjorne.BjornePseudoPlugin"/>
<testSpec title="single-directory" command="mkdir" runMode="AS_ALIAS" rc="0">
<arg>@TEMP_DIR@/newdir</arg>
</testSpec>
<testSpec title="nested-with-parents" command="mkdir" runMode="AS_ALIAS" rc="0">
<arg>-p</arg>
<arg>@TEMP_DIR@/a/b/c</arg>
</testSpec>
<testSpec title="nested-with-parents-long" command="mkdir" runMode="AS_ALIAS" rc="0">
<arg>--parents</arg>
<arg>@TEMP_DIR@/x/y/z</arg>
</testSpec>
<testSpec title="existing-with-parents" command="mkdir" runMode="AS_ALIAS" rc="0">

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Test expects rc=0 but will likely fail

This test does mkdir -p @TEMP_DIR@/newdir where newdir was already created by the single-directory test. Because java.io.File.mkdirs() returns false for existing directories, the code will call exit(1) instead of succeeding silently. This test will fail until the bug in MkdirCommand.java:61 is fixed.

If the JNode/Classpath File.mkdirs() happens to return true for existing directories, this comment is moot and the test is fine.

<arg>-p</arg>
<arg>@TEMP_DIR@/newdir</arg>
</testSpec>
<testSpec title="nested-without-parents-fails" command="mkdir" runMode="AS_ALIAS" rc="1">
<arg>@TEMP_DIR@/d/e/f</arg>
</testSpec>
</testSet>
Loading