mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 19:25:10 +00:00
Userland: Simplify recursion in find(1)
Now walk_tree() itself checks whether it should descend into a file (if the file is a directory) or not.
This commit is contained in:
parent
ad15d0c880
commit
d4232d5ee2
1 changed files with 22 additions and 7 deletions
|
@ -461,9 +461,28 @@ static void walk_tree(const FileData& root_data, Command& command)
|
||||||
{
|
{
|
||||||
command.evaluate(root_data);
|
command.evaluate(root_data);
|
||||||
|
|
||||||
int dirfd = openat(root_data.dirfd, root_data.basename, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
// We should try to read directory entries if either:
|
||||||
if (dirfd < 0 && errno == ENOTDIR)
|
// * This is a directory.
|
||||||
|
// * This is a symlink (that could point to a directory),
|
||||||
|
// and we're following symlinks.
|
||||||
|
struct stat stat;
|
||||||
|
int rc = fstatat(root_data.dirfd, root_data.basename, &stat, AT_SYMLINK_NOFOLLOW);
|
||||||
|
if (rc < 0)
|
||||||
return;
|
return;
|
||||||
|
if (!S_ISDIR(stat.st_mode) && (!g_follow_symlinks || !S_ISLNK(stat.st_mode)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
int dirfd = openat(root_data.dirfd, root_data.basename, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||||
|
if (dirfd < 0) {
|
||||||
|
if (errno == ENOTDIR) {
|
||||||
|
// Above we decided to try to open this file because it could
|
||||||
|
// be a directory, but turns out it's not. This is fine though.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
perror(root_data.full_path.string().characters());
|
||||||
|
g_there_was_an_error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
DIR* dir = fdopendir(dirfd);
|
DIR* dir = fdopendir(dirfd);
|
||||||
|
|
||||||
|
@ -481,11 +500,7 @@ static void walk_tree(const FileData& root_data, Command& command)
|
||||||
dirfd,
|
dirfd,
|
||||||
dirent->d_name,
|
dirent->d_name,
|
||||||
};
|
};
|
||||||
struct stat stat;
|
walk_tree(file_data, command);
|
||||||
if (g_follow_symlinks || fstatat(dirfd, dirent->d_name, &stat, AT_SYMLINK_NOFOLLOW) < 0 || !S_ISLNK(stat.st_mode))
|
|
||||||
walk_tree(file_data, command);
|
|
||||||
else
|
|
||||||
command.evaluate(file_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errno != 0) {
|
if (errno != 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue