Skip to content

nshlib: add chmod and chown builtins#3480

Open
Abhishekmishra2808 wants to merge 1 commit into
apache:masterfrom
Abhishekmishra2808:nsh-chmod-chown
Open

nshlib: add chmod and chown builtins#3480
Abhishekmishra2808 wants to merge 1 commit into
apache:masterfrom
Abhishekmishra2808:nsh-chmod-chown

Conversation

@Abhishekmishra2808
Copy link
Copy Markdown

Summary

This PR adds chmod and chown builtins to NSH by exposing the existing libc/VFS permission and ownership interfaces through the shell. The implementation supports numeric octal permission modes for chmod and numeric ownership forms for chown, including uid, uid:gid, uid:, and :gid, matching the semantics already handled by the underlying NuttX chown() implementation.
Documentation will be added shortly on Nuttx Repo

Impact

This makes filesystem permission and ownership management directly accessible from NSH and exposes the existing libc/VFS ownership semantics in NuttX, improving usability/debugging without requiring custom test applications.

Testing

image

Comment thread nshlib/nsh_fscmds.c Outdated

UNUSED(argc);

colon = strchr(spec, ':');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why need, let's check endptr after strtol

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, good optimization!!

@xiaoxiang781216
Copy link
Copy Markdown
Contributor

out of flash:

  Building NuttX...
lto-wrapper: warning: using serial compilation of 4 LTRANS jobs
lto-wrapper: note: see the '-flto' option documentation for more information
/tools/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/bin/ld: /github/workspace/sources/nuttx/nuttx section `.data' will not fit in region `flash'
/tools/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/bin/ld: region `flash' overflowed by 504 bytes
/tools/gcc-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/13.2.1/../../../../arm-none-eabi/bin/ld: warning: /github/workspace/sources/nuttx/nuttx has a LOAD segment with RWX permissions
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:226: nuttx] Error 1
make: *** [tools/Unix.mk:568: nuttx] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize lm3s6432-s2e/nsh
On branch master

Comment thread nshlib/Kconfig

config NSH_DISABLE_CHMOD
bool "Disable chmod"
default DEFAULT_SMALL
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

depends on FS_PERMISSION

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

Comment thread nshlib/Kconfig

config NSH_DISABLE_CHOWN
bool "Disable chown"
default DEFAULT_SMALL
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

depends on FS_PERMISSION

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

Comment thread nshlib/nsh_command.c Outdated
#endif

#ifndef CONFIG_NSH_DISABLE_CHOWN
CMD_MAP("chown", cmd_chown, 3, 3, "<uid>[:gid] <path>"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The implementation also accepts the []: form (e.g. chown :5 /path, where the uid is left unchanged), but the usage string only documents [:gid]. Users who run help chown will not learn that the uid side may be omitted.

Suggested change to keep the help text consistent with what the parser actually accepts:

-  CMD_MAP("chown", cmd_chown, 3, 3, "<uid>[:gid] <path>"),
+  CMD_MAP("chown", cmd_chown, 3, 3, "[<uid>][:<gid>] <path>"),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Nice catch!

Add minimal chmod/chown support to NSH using the
existing libc/VFS syscall interfaces.

Supported forms:

  - chmod <octal-mode> <path>
  - chown <uid>[:gid] <path>

The chown implementation supports the numeric
ownership forms already handled by the underlying
NuttX chown() implementation, including unchanged
uid/gid semantics via omitted fields.

Only numeric permission modes and numeric uid/gid
forms are supported in this initial implementation.

This adds interactive filesystem permission and
ownership management support to NSH and aligns the
shell more closely with standard POSIX environments.

Signed-off-by: Abhishek Mishra <mishra.abhishek2808@gmail.com>
@Abhishekmishra2808
Copy link
Copy Markdown
Author

@acassis @xiaoxiang781216 @JianyuWang0623, thanks for the review. Implemented your suggestions/fixes in the latest commit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants