[libc-commits] [libc] [libc] Add POSIX.1-2024 <ftw.h> and Linux nftw implementation (PR #234996)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 14:48:30 PST 2026
================
@@ -0,0 +1,583 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of the POSIX nftw function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/ftw/nftw.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/ftw_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/types/dev_t.h"
+#include "hdr/types/ino_t.h"
+#include "hdr/types/struct_FTW.h"
+#include "hdr/types/struct_dirent.h"
+#include "hdr/types/struct_stat.h"
+#include "src/__support/CPP/limits.h"
+#include "src/__support/CPP/new.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/File/dir.h"
+#include "src/__support/OSUtil/linux/stat/stat_via_statx.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/chdir.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/close.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/fchdir.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/open.h"
+#include "src/__support/alloc-checker.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/macros/optimization.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace {
+
+using NftwCallback = int (*)(const char *, const struct stat *, int,
+ struct FTW *);
+
+constexpr size_t INITIAL_PATH_CAPACITY = 256;
+constexpr size_t INITIAL_NAMES_CAPACITY = 256;
+constexpr size_t INITIAL_VISITED_CAPACITY = 32;
+constexpr size_t MAX_PATH_BUF_CAPACITY =
+ static_cast<size_t>(cpp::numeric_limits<int>::max());
+constexpr int VALID_FLAGS =
+ FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL;
+
+struct Node {
+ ino_t ino;
+ dev_t dev;
+ const Node *parent;
+};
+
+struct ScopedDir {
+ Dir *dir = nullptr;
+ explicit ScopedDir(Dir *d) : dir(d) {}
+ ScopedDir(const ScopedDir &) = delete;
+ ScopedDir &operator=(const ScopedDir &) = delete;
+ LIBC_INLINE ~ScopedDir() noexcept {
+ if (dir != nullptr)
+ static_cast<void>(dir->close());
+ }
+ Dir *operator->() const { return dir; }
+ int close() {
+ if (dir == nullptr)
+ return 0;
+ Dir *d = dir;
+ dir = nullptr;
+ return d->close();
+ }
+};
+
+struct VisitedDirs {
+ struct Entry {
+ dev_t dev;
+ ino_t ino;
+ };
+ Entry *data = nullptr;
+ size_t count = 0;
+ size_t capacity = 0;
+
+ VisitedDirs() = default;
+ VisitedDirs(const VisitedDirs &) = delete;
+ VisitedDirs &operator=(const VisitedDirs &) = delete;
+ LIBC_INLINE ~VisitedDirs() noexcept { delete[] data; }
+
+ bool contains(dev_t dev, ino_t ino) const {
+ for (size_t i = 0; i < count; ++i)
+ if (data[i].dev == dev && data[i].ino == ino)
+ return true;
+ return false;
+ }
+
+ bool insert(dev_t dev, ino_t ino) {
+ if (count == capacity) {
+ constexpr size_t MAX_CAP =
+ cpp::numeric_limits<size_t>::max() / sizeof(Entry);
+ if (LIBC_UNLIKELY(capacity > MAX_CAP / 2))
+ return false;
+ size_t new_cap = capacity == 0 ? INITIAL_VISITED_CAPACITY : capacity * 2;
+ AllocChecker ac;
+ Entry *new_data = new (ac) Entry[new_cap];
+ if (LIBC_UNLIKELY(!ac))
+ return false;
+ if (count > 0)
+ inline_memcpy(new_data, data, count * sizeof(Entry));
+ delete[] data;
+ data = new_data;
+ capacity = new_cap;
+ }
+ data[count++] = {dev, ino};
+ return true;
+ }
+};
+
+struct PathBuffer {
+ char *data = nullptr;
+ size_t len = 0;
+ size_t capacity = 0;
+
+ PathBuffer() = default;
+ PathBuffer(const PathBuffer &) = delete;
+ PathBuffer &operator=(const PathBuffer &) = delete;
+ LIBC_INLINE ~PathBuffer() noexcept { delete[] data; }
+
+ bool ensure_capacity(size_t required) {
+ if (required <= capacity)
+ return true;
+ if (LIBC_UNLIKELY(required > MAX_PATH_BUF_CAPACITY))
+ return false;
+ size_t new_cap = capacity == 0 ? INITIAL_PATH_CAPACITY
+ : (capacity > MAX_PATH_BUF_CAPACITY / 2
+ ? MAX_PATH_BUF_CAPACITY
+ : capacity * 2);
+ if (new_cap < required)
+ new_cap = required;
+ AllocChecker ac;
+ char *new_data = new (ac) char[new_cap];
+ if (LIBC_UNLIKELY(!ac))
+ return false;
+ if (len > 0)
+ inline_memcpy(new_data, data, len);
+ new_data[len] = '\0';
+ delete[] data;
+ data = new_data;
+ capacity = new_cap;
+ return true;
+ }
+
+ bool init(cpp::string_view sv) {
+ while (sv.size() > 1 && sv.back() == '/')
+ sv.remove_suffix(1);
+ if (LIBC_UNLIKELY(sv.size() >= MAX_PATH_BUF_CAPACITY ||
+ !ensure_capacity(sv.size() + 1)))
+ return false;
+ inline_memcpy(data, sv.data(), sv.size());
+ len = sv.size();
+ data[len] = '\0';
+ return true;
+ }
+
+ bool push_child(cpp::string_view child_name) {
+ bool need_slash = (len > 0 && data[len - 1] != '/');
+ size_t slash_len = need_slash ? 1 : 0;
+ if (LIBC_UNLIKELY(child_name.size() >=
+ MAX_PATH_BUF_CAPACITY - len - slash_len))
+ return false;
+ size_t add_len = slash_len + child_name.size();
+ if (LIBC_UNLIKELY(!ensure_capacity(len + add_len + 1)))
+ return false;
+ if (need_slash)
+ data[len++] = '/';
+ inline_memcpy(data + len, child_name.data(), child_name.size());
+ len += child_name.size();
+ data[len] = '\0';
+ return true;
+ }
+
+ void pop_to(size_t prev_len) {
+ len = prev_len;
+ data[len] = '\0';
+ }
+};
+
+// Contiguous buffer of null-terminated entry names used when available_fds == 1
+// requires closing the current directory before recursing into children.
+struct DirEntryNames {
+ char *buf = nullptr;
+ size_t used = 0;
+ size_t cap = 0;
+
+ DirEntryNames() = default;
+ DirEntryNames(const DirEntryNames &) = delete;
+ DirEntryNames &operator=(const DirEntryNames &) = delete;
+ LIBC_INLINE ~DirEntryNames() noexcept { delete[] buf; }
+
+ bool append(cpp::string_view name) {
+ constexpr size_t MAX_SIZE = cpp::numeric_limits<size_t>::max();
+ if (LIBC_UNLIKELY(name.size() >= MAX_SIZE - used))
+ return false;
+ size_t need = used + name.size() + 1;
+ if (LIBC_UNLIKELY(need > cap)) {
+ size_t new_cap = cap == 0 ? INITIAL_NAMES_CAPACITY
+ : (cap > MAX_SIZE / 2 ? MAX_SIZE : cap * 2);
+ if (new_cap < need)
+ new_cap = need;
+ AllocChecker ac;
+ char *new_buf = new (ac) char[new_cap];
+ if (LIBC_UNLIKELY(!ac))
+ return false;
+ if (used > 0)
+ inline_memcpy(new_buf, buf, used);
+ delete[] buf;
+ buf = new_buf;
+ cap = new_cap;
+ }
+ inline_memcpy(buf + used, name.data(), name.size());
+ used += name.size();
+ buf[used++] = '\0';
+ return true;
+ }
+};
+
+struct OriginCwd {
+ int fd = -1;
+ bool dirty = false;
+
+ OriginCwd() = default;
+ OriginCwd(const OriginCwd &) = delete;
+ OriginCwd &operator=(const OriginCwd &) = delete;
+
+ LIBC_INLINE ~OriginCwd() noexcept {
+ if (dirty && fd >= 0)
+ linux_syscalls::fchdir(fd);
+ if (fd >= 0)
+ linux_syscalls::close(fd);
+ }
+
+ ErrorOr<int> capture(int fd_limit, int &available_fds) {
+ ErrorOr<int> fd_or_err =
+ linux_syscalls::open(".", O_PATH | O_DIRECTORY | O_CLOEXEC, 0);
+ if (!fd_or_err.has_value())
+ return Error(fd_or_err.error());
+ fd = fd_or_err.value();
+ available_fds = (fd_limit > 1) ? (fd_limit - 1) : 1;
+ return 0;
+ }
+
+ ErrorOr<int> restore() {
+ if (fd < 0)
+ return 0;
+ ErrorOr<int> res = linux_syscalls::fchdir(fd);
+ if (res.has_value())
+ dirty = false;
+ return res;
+ }
+
+ ErrorOr<int> chdir_to_walk_path(const char *walk_path) {
+ if (walk_path[0] != '/') {
+ ErrorOr<int> res = restore();
+ if (!res.has_value())
+ return res;
+ }
+ ErrorOr<int> res = linux_syscalls::chdir(walk_path);
+ if (res.has_value())
+ dirty = true;
+ return res;
+ }
+};
+
+struct WalkContext {
+ const char *raw_root_path;
+ PathBuffer path_buf;
+ NftwCallback fn;
+ int flags;
+ dev_t start_device = 0;
+ OriginCwd origin_cwd;
+ VisitedDirs visited_dirs;
+
+ ErrorOr<int> chdir_to_parent_of(int base) {
+ if (base == 0)
+ return origin_cwd.restore();
+ if (base == 1) {
+ ErrorOr<int> res = linux_syscalls::chdir("/");
+ if (res.has_value())
+ origin_cwd.dirty = true;
+ return res;
+ }
+ char saved = path_buf.data[base - 1];
+ path_buf.data[base - 1] = '\0';
+ ErrorOr<int> res = origin_cwd.chdir_to_walk_path(path_buf.data);
+ path_buf.data[base - 1] = saved;
+ return res;
+ }
+};
+
+ErrorOr<int> walk_tree(WalkContext &ctx, int available_fds, int base, int level,
----------------
kaladron wrote:
Done, thanks! =)
https://github.com/llvm/llvm-project/pull/234996
More information about the libc-commits
mailing list