New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
libc: Implement ftw and nftw function #1486
Conversation
@yamt and @patacongo since my team membrer also need ftw/nftw function, I provide an implementation, please review it. |
pathbuf[PATH_MAX] = '\0'; | ||
|
||
return do_nftw(pathbuf, fn, fdlimit, flags, 0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i guess it's supposed to restore the original cwd when finished.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here(https://pubs.opengroup.org/onlinepubs/009695399/functions/nftw.html) is what the spec said:
FTW_CHDIR
If set, nftw() shall change the current working directory to each directory as it reports files in that directory. If clear, nftw() shall not change the current working directory.
so the spec permit the current work directory change if the caller pass FTW_CHDIR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That feature would depend on !defined(CONFIG_DISABLE_ENVIRON),
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other implementation i'm aware of restores the cwd. (netbsd)
glibc seems to do the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will save and restore cwd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
path[base - 1] = '\0'; | ||
r = chdir(path); | ||
path[base - 1] = '/'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this work when the user-given path is relative?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, the caller must give the absolute path, otherwise lstat or stat at line 92 will fail first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should that be fixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, because the spec don't see how to handle this? But if ftw/nftw need consider the current work directory, should we modify stat/lstat to support it too? Here is the musl implementation:
https://github.com/bminor/musl/blob/master/src/misc/nftw.c
which don't do any special thing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as the musl impl doesn't seem to do chdir at all, i guess relative path can just work there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
musl can work, just because stat/lstat/chdir support the cwd concept, here is musl' lstat implementation:
https://github.com/bminor/musl/blob/master/src/stat/lstat.c#L6
you can see lstat use AT_FDCWD flag there.
So the correct fix is enhance the low level file system related functions in nuttx with the cwd support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. it makes sense.
libs/libc/dirent/lib_nftw.c
Outdated
|
||
if (dir) | ||
{ | ||
struct dirent *de; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct dirent *de; | |
FAR struct dirent *de; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
libs/libc/dirent/lib_nftw.c
Outdated
static int | ||
do_nftw(FAR char *path, nftw_cb_t fn, int fdlimit, int flags, int level) | ||
{ | ||
DIR *dir = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DIR *dir = NULL; | |
FAR DIR *dir = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
base, level | ||
}; | ||
|
||
#ifndef CONFIG_DISABLE_ENVIRON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i guess it's more user-friendly to make this compile-time (not define FTW_CHDIR) or runtime error (return EINVAL or something explicitly)
i prefer the former.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
see the reference here: https://pubs.opengroup.org/onlinepubs/009695399/basedefs/ftw.h.html Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Change-Id: I3b368106a56b0e9d4c653f3ae16304b0a9d55fbc
@yamt and @patacongo all issue is addressed, please take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@yamt, please merge this patch if you don't have more concern. |
Summary
see the reference here:
https://pubs.opengroup.org/onlinepubs/009695399/basedefs/ftw.h.html
Impact
New function
Testing