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 | |
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')
-rw-r--r-- | unix/vncpasswd/vncpasswd.cxx | 13 | ||||
-rw-r--r-- | unix/vncserver/vncsession.c | 49 |
2 files changed, 31 insertions, 31 deletions
diff --git a/unix/vncpasswd/vncpasswd.cxx b/unix/vncpasswd/vncpasswd.cxx index 68c44289..30091a3d 100644 --- a/unix/vncpasswd/vncpasswd.cxx +++ b/unix/vncpasswd/vncpasswd.cxx @@ -23,6 +23,7 @@ #include <config.h> #endif +#include <errno.h> #include <limits.h> #include <stdio.h> #include <string.h> @@ -161,16 +162,10 @@ int main(int argc, char** argv) fprintf(stderr, "Can't obtain VNC config directory\n"); exit(1); } - strcpy(fname, configDir); - char *p = NULL; - for (p = fname + 1; *p; p++) { - if (*p == '/') { - *p = '\0'; - mkdir(fname, 0777); - *p = '/'; - } + if (os::mkdir_p(configDir, 0777) == -1) { + fprintf(stderr, "Could not create VNC config directory: %s\n", strerror(errno)); + exit(1); } - mkdir(fname, 0777); snprintf(fname, sizeof(fname), "%s/passwd", configDir); } 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) { |