Repositories / jai.git

jai.git

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

Branch

no infinite loops on dangling symlinks

Author
David Mazieres <dm@uun.org>
Date
2026-03-16 14:13:34 -0700
Commit
26230c5ef233ea5a5fdc4ca2248405716b7d9aef
fs.cc
index 2f044af..7a86eb0 100644
--- a/fs.cc
+++ b/fs.cc
@@ -206,6 +206,13 @@ ensure_dir(int dfd, const path &p, mode_t perm, FollowLinks follow,
     else if (errno != ENOENT)
       syserr(R"(ensure_dir("{}"): open("{}"))", p.string(),
              component->string());
+    else if (struct stat sb; fstatat(dfd, component->c_str(), &sb, 0))
+      syserr(R"(ensure_dir("{}"): stat("{}"))", p.string(),
+             component->string());
+    else if (!S_ISDIR(sb.st_mode)) {
+      syserr(R"(ensure_dir("{}"): "{}" is not a directory)", p.string(),
+             component->string());
+    }
     else if (mkdirat(dfd, component->c_str(), perm) && errno != EEXIST)
       syserr(R"(ensure_dir("{}"): mkdir("{}"))", p.string(),
              component->string());