Repositories / jai.git

jai.git

Clone (read-only): git clone http://git.guha-anderson.com/git/jai.git

Branch

Add createcb callback parameter to ensure_dir

Required by the /dev/shm cherry-pick (060ed28).
Author
Arjun Guha <a.guha@northeastern.edu>
Date
2026-04-02 21:27:29 -0400
Commit
5e6cbbf4fec4c9abd72a8cd6eb6192af971bdf26
fs.cc
index 2fa3aed..240a6df 100644
--- a/fs.cc
+++ b/fs.cc
@@ -187,11 +187,12 @@ is_dir_empty(int dirfd)
 
 Fd
 ensure_dir(int dfd, const path &p, mode_t perm, FollowLinks follow,
-           bool okay_if_other_owner)
+           bool okay_if_other_owner, std::function<void(int)> createcb)
 {
   assert(!p.empty());
 
   Fd fd;
+  bool created = false;
   int flag = follow == kFollow ? 0 : O_NOFOLLOW;
   if (p.is_absolute())
     dfd = *(fd = xopenat(-1, "/", O_RDONLY));
@@ -204,7 +205,8 @@ ensure_dir(int dfd, const path &p, mode_t perm, FollowLinks follow,
     else if (errno != ENOENT)
       syserr(R"(ensure_dir("{}"): open("{}"))", p.string(),
              fdpath(dfd, *component));
-    else if (mkdirat(dfd, component->c_str(), perm) && errno != EEXIST)
+    else if (created = !mkdirat(dfd, component->c_str(), perm),
+             !created && errno != EEXIST)
       syserr(R"(ensure_dir("{}"): mkdir("{}"))", p.string(),
              fdpath(dfd, *component));
     else if (struct stat sb; fstatat(dfd, component->c_str(), &sb, 0))
fs.h
index ef45de6..94f1d5e 100644
--- a/fs.h
+++ b/fs.h
@@ -10,6 +10,7 @@
 #include <filesystem>
 #include <ranges>
 #include <set>
+#include <functional>
 #include <string>
 #include <string_view>
 
@@ -160,7 +161,8 @@ bool is_fd_at_path(int targetfd, int dfd, const path &file,
 bool is_dir_empty(int dirfd);
 
 Fd ensure_dir(int dfd, const path &p, mode_t perm, FollowLinks follow,
-              bool okay_if_other_owner = false);
+              bool okay_if_other_owner = false,
+              std::function<void(int)> createcb = [](int) {});
 
 void make_whiteout(int dfd, const path &p);