diff options
author | Pierre Ossman <ossman@cendio.se> | 2024-05-30 16:05:35 +0200 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2024-05-30 16:33:02 +0200 |
commit | a79c33d61eeba6c0745788b164d35832ef72f1ab (patch) | |
tree | 0ab297b8351fd5fde89c0b4ddb2cacfb6e41f768 /unix/vncserver/vncsession.c | |
parent | 68134f03af376d103f2ab4a3ec654199842f855d (diff) | |
download | tigervnc-a79c33d61eeba6c0745788b164d35832ef72f1ab.tar.gz tigervnc-a79c33d61eeba6c0745788b164d35832ef72f1ab.zip |
Create common recursive mkdir()
Avoid duplicating this complexity in too many places.
At the same time make the interface more identical to regular mkdir(),
for familiarity.
Diffstat (limited to 'unix/vncserver/vncsession.c')
-rw-r--r-- | unix/vncserver/vncsession.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/unix/vncserver/vncsession.c b/unix/vncserver/vncsession.c index 31dee57d..1ee096c7 100644 --- a/unix/vncserver/vncsession.c +++ b/unix/vncserver/vncsession.c @@ -358,33 +358,33 @@ switch_user(const char *username, uid_t uid, gid_t gid) } } -static void -mkvncdir(const char *dir) +static int +mkdir_p(const char *path_, mode_t mode) { - if (mkdir(dir, 0755) == -1) { - if (errno != EEXIST) { - syslog(LOG_CRIT, "Failure creating \"%s\": %s", dir, strerror(errno)); - _exit(EX_OSERR); - } - } -} + char *path = strdup(path_); + char *p; -static void -mkdirrecursive(const char *dir) -{ - char *path = strdup(dir); - char *p; - - for (p = path + 1; *p; p++) { - if (*p == '/') { - *p = '\0'; - mkvncdir(path); - *p = '/'; + for (p = path + 1; *p; p++) { + if (*p == '/') { + *p = '\0'; + if (mkdir(path, mode) == -1) { + if (errno != EEXIST) { + free(path); + return -1; } + } + *p = '/'; } + } - mkvncdir(path); + if (mkdir(path, mode) == -1) { free(path); + return -1; + } + + free(path); + + return 0; } static void @@ -431,7 +431,12 @@ redir_stdio(const char *homedir, const char *display, char **envp) #endif } - mkdirrecursive(logfile); + if (mkdir_p(logfile, 0755) == -1) { + if (errno != EEXIST) { + syslog(LOG_CRIT, "Failure creating \"%s\": %s", logfile, strerror(errno)); + _exit(EX_OSERR); + } + } hostlen = sysconf(_SC_HOST_NAME_MAX); if (hostlen < 0) { |