Skip to content
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

Merged
merged 1 commit into from Aug 11, 2020
Merged

Conversation

xiaoxiang781216
Copy link
Contributor

@xiaoxiang781216 xiaoxiang781216 commented Jul 30, 2020

Summary

see the reference here:
https://pubs.opengroup.org/onlinepubs/009695399/basedefs/ftw.h.html

Impact

New function

Testing

@xiaoxiang781216
Copy link
Contributor Author

@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);
}
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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] = '/';
}
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

should that be fixed?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok. it makes sense.


if (dir)
{
struct dirent *de;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
struct dirent *de;
FAR struct dirent *de;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

static int
do_nftw(FAR char *path, nftw_cb_t fn, int fdlimit, int flags, int level)
{
DIR *dir = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
DIR *dir = NULL;
FAR DIR *dir = NULL;

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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
@xiaoxiang781216
Copy link
Contributor Author

@yamt and @patacongo all issue is addressed, please take a look.

Copy link
Contributor

@yamt yamt left a comment

Choose a reason for hiding this comment

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

lgtm

@xiaoxiang781216
Copy link
Contributor Author

@yamt, please merge this patch if you don't have more concern.

@yamt yamt merged commit b13f321 into apache:master Aug 11, 2020
@btashton btashton added this to To-Add in Release Notes - 10.0.0 Oct 14, 2020
@btashton btashton moved this from To-Add to Added in Release Notes - 10.0.0 Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants