Fix mkpath() ignoring mode when umask is more restrictive

mkpath(dir, 0755) with umask 0027 silently creates a 0750 directory,
since mkdir(2) applies the umask to the requested mode.  Callers pass
an explicit mode and expect exactly that.

Apply the mode with chmod() after each successful mkdir().

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-09-13 17:17:37 +02:00
1 parent c1e050f93c
commit e874608b76
3 files changed
+49 -3

No files matched your search

+7 -1
View File
@@ -35,6 +35,9 @@
* @param dir Directory to created, relative or absolute
* @param mode A &mode_t mode to create @a dir with
*
* Directories are created with exactly @a mode, the process umask is
* not applied like it is for mkdir(2).
*
* @returns POSIX OK(0) on success, otherwise -1 with @a errno set.
*/
int mkpath(const char *dir, mode_t mode)
@@ -51,7 +54,10 @@ int mkpath(const char *dir, mode_t mode)
mkpath(dirname(strdupa(dir)), mode);
return mkdir(dir, mode);
if (mkdir(dir, mode))
return -1;
return chmod(dir, mode);
}
/**