[libc-commits] [libc] [libc][realpath][test] Create test files/directories (PR #209369)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Thu Jul 16 12:53:52 PDT 2026
================
@@ -12,23 +12,148 @@
//===----------------------------------------------------------------------===//
#include "hdr/errno_macros.h"
+#include "hdr/func/free.h"
#include "hdr/limits_macros.h"
#include "hdr/types/size_t.h"
#include "src/__support/CPP/string.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/CPP/utility.h"
#include "src/__support/OSUtil/path.h"
+#include "src/__support/fixedvector.h"
+#include "src/__support/libc_assert.h"
+#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
+#include "src/fcntl/openat.h"
#include "src/stdlib/realpath.h"
+#include "src/string/strdup.h"
+#include "src/sys/stat/mkdirat.h"
+#include "src/unistd/close.h"
+#include "src/unistd/getpid.h"
+#include "src/unistd/unlinkat.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
namespace cpp = LIBC_NAMESPACE::cpp;
namespace path = LIBC_NAMESPACE::path;
-using LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::FixedVector;
+using LIBC_NAMESPACE::testing::tlog;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
// This test assumes the following values, so fail early if they mismatch.
static_assert(path::SEPARATOR == '/');
static_assert(path::CURRENT_DIR_COMPONENT == ".");
static_assert(path::PARENT_DIR_COMPONENT == "..");
+// Size of a path separator.
+constexpr size_t PATH_SEP_SIZE = 1;
+
+// Creates the given directory if it does not exist. Returns zero on success.
+[[nodiscard]] int ensure_directory_exists(int dirfd, const char *path,
+ mode_t mode = 0755) {
+ if (LIBC_NAMESPACE::mkdirat(dirfd, path, mode) == 0)
+ return 0;
+ if (libc_errno == EEXIST)
+ libc_errno = 0;
+
+ if (libc_errno != 0) {
+ tlog << "Failed to create temp directory: " << path << "\n";
+ return -1;
+ }
+ return 0;
+}
+
+// A test directory that removes itself on destruction.
+class TestDir {
+ // The test directory's absolute path.
+ cpp::string path_;
----------------
kaladron wrote:
LLVM doesn't appear to use trailing `_` on private class variables.
https://github.com/llvm/llvm-project/pull/209369
More information about the libc-commits
mailing list