Add a du command to NSH that recursively summarizes the size of each
path argument in 1K-blocks, or human-readable form with -h.
Supports -s (summary only), -a (all files), -d N (max-depth), and -h,
matching GNU du semantics.
Testing:
Built and ran on sim:nsh with:
```bash
cmake -B out/nuttx_sim_nsh -S nuttx -DBOARD_CONFIG=sim:nsh -GNinja
ninja -C out/nuttx_sim_nsh
./out/nuttx_sim_nsh/nuttx
nsh> du
397449 /data/test/elf
71993 /data/test/coredump
5 /data/test/log2
3251 /data/test/log1
472700 /data/test
nsh> du -h
388.1M /data/test/elf
70.3M /data/test/coredump
4.1K /data/test/log2
3.1M /data/test/log1
461.6M /data/test
```
Host Ubuntu22.04 du on the same directory:
```bash
$ du
397456 ./elf
72000 ./coredump
8 ./log2
3252 ./log1
472720 .
$ du -h
389M ./elf
71M ./coredump
8.0K ./log2
3.2M ./log1
462M .
```
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
Fix two potential NULL pointer dereferences in nsh_fscmds.c:
1. fdinfo_callback: asprintf() failure left filepath potentially
NULL, which was then passed to nsh_catfile(). Add early return
on asprintf failure.
2. cmd_cat: malloc(BUFSIZ) for stdin reading was used without
checking the return value. Add NULL check with -ENOMEM return.
Also fix a typo in error message: 'nsh_catfaile' -> 'nsh_catfile'.
Signed-off-by: hanzhijian <hanzhijian@zepp.com>
Fixes#17063
Add modification time display to ls -l long format output.
The timestamp is shown as 'YYYY-MM-DD HH:MM' between the permission
string and the file size, following the ISO long-iso format.
Files with st_mtime == 0 (e.g., procfs, tmpfs pseudo-entries) skip
the timestamp display to avoid showing a meaningless epoch time.
Signed-off-by: hanzhijian <hanzhijian@zepp.com>
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>
Replace app-side includes of <debug.h> with <nuttx/debug.h> to use the
header from the NuttX tree explicitly after the header move.
Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
Replace floating-point arithmetic with fixed-point integer math to avoid
linking soft-float library (~2-3KB Flash) when displaying human-readable
file sizes (ls -lh command).
Changes:
- Use integer division and modulo to calculate size components
- Calculate decimal part: (remainder * 10) / unit for one decimal place
- Refactor duplicated code: consolidate GB/MB/KB logic into single path
- Remove CONFIG_HAVE_FLOAT dependency
This eliminates calls to __aeabi_f2d, __aeabi_fmul, __aeabi_i2f and other
ARM EABI floating-point helpers, reducing Flash footprint for systems
compiled with -mfloat-abi=soft.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This fixes heap corruption when deleting a folder containing other folders
or files. The issue appeared at commit 131d50ae9d, which removed the
stack-based temporary buffer.
unlink_recursive requires that the path is provided in PATH_MAX sized
buffer. It concatenates sub-folder or file names to the same buffer.
nsh_getfullpath just allocates a buffer using strdup, so there is no room
for concatenating more data to it.
To keep the stack usage smaller, instead of reverting the breaking commit,
allocate the temporary buffer with lib_get_pathbuffer instead.
Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.
Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
This PR will still allow basic shell operations such as cd/ls/pwd to be used even when the environment is disabled.
Signed-off-by: chao an <anchao@lixiang.com>
In below two cases "rm" command with "-f" option will return 0:
a. Bad arguments
b. File not exists
Following "rm" of GNU coreutils 8.32
GNU coreutils
$ rm --version
rm (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Rubin, David MacKenzie, Richard M. Stallman,
and Jim Meyering.
$ rm /FILE_NOT_EXISTS
rm: cannot remove '/FILE_NOT_EXISTS': No such file or directory
$ echo $?
1
$ rm -f
$ echo $?
0
$ rm -f /FILE_NOT_EXISTS
$ echo $?
0
Without this patch
nsh> rm
nsh: rm: missing required argument(s)
nsh> rm /FILE_NOT_EXISTS
nsh: rm: unlink failed: 2
nsh> echo $?
1
With this patch
nsh> rm -f
nsh> echo $?
0
nsh> rm -f /FILE_NOT_EXISTS
nsh> echo $?
0
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
Summary:
do
{
nbyteswritten = write(wrfd, iobuffer, nbytesread);
if (nbyteswritten >= 0)
The write return type is ssize_t, which is different in size from the originally declared type, so the unified type is ssize_t
Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
The behavior of mkdir is used during the cp -r process, and the mkdir behavior is skipped when mkdir is unavailable (this may cause unpredictable behavior of cp -r)
Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
Fix error: implicit declaration of function 'nsh_foreach_direntry' [-Werror=implicit-function-declaration] on file nsh_fscmds on some compiler versions.
Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
text data bss dec hex filename
398953 27088 4128 430169 69059 nuttx /* before */
389241 27072 4128 420441 66a59 nuttx /* after */
-9712 -16
Signed-off-by: chao an <anchao@xiaomi.com>
When rm -r is passed with no address specified, it will automatically recursively unlink all files under the root path ('/') until unlinking to the mounted folder causes the unlink to fail. In this change, rm -r without a specified path will prompt for missing arguments