From 33628dd17726cc39e6a02c9c5fd7fe695a8ba1c5 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Tue, 18 Jun 2024 18:48:10 -0400 Subject: [PATCH] Add new _cupsDirCreate private API and use it for config dirs. --- cups/cups-private.h | 2 +- cups/dest.c | 2 +- cups/dir.c | 41 ++++++++++++++++++++++++++++++++++++++++- cups/tls.c | 16 ++-------------- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/cups/cups-private.h b/cups/cups-private.h index ab8f5bc172..af3843dc48 100644 --- a/cups/cups-private.h +++ b/cups/cups-private.h @@ -253,7 +253,7 @@ extern void _cupsBufferRelease(char *b) _CUPS_PRIVATE; extern http_t *_cupsConnect(void) _CUPS_PRIVATE; extern char *_cupsCreateDest(const char *name, const char *info, const char *device_id, const char *device_uri, char *uri, size_t urisize) _CUPS_PRIVATE; -extern ipp_attribute_t *_cupsEncodeOption(ipp_t *ipp, ipp_tag_t group_tag, _ipp_option_t *map, const char *name, const char *value) _CUPS_PRIVATE; +extern bool _cupsDirCreate(const char *path, mode_t mode) _CUPS_PRIVATE;extern ipp_attribute_t *_cupsEncodeOption(ipp_t *ipp, ipp_tag_t group_tag, _ipp_option_t *map, const char *name, const char *value) _CUPS_PRIVATE; extern int _cupsGet1284Values(const char *device_id, cups_option_t **values) _CUPS_PRIVATE; extern const char *_cupsGetDestResource(cups_dest_t *dest, unsigned flags, char *resource, size_t resourcesize) _CUPS_PRIVATE; extern int _cupsGetDests(http_t *http, ipp_op_t op, const char *name, cups_dest_t **dests, cups_ptype_t type, cups_ptype_t mask) _CUPS_PRIVATE; diff --git a/cups/dest.c b/cups/dest.c index 69708579e3..c9bea88cee 100644 --- a/cups/dest.c +++ b/cups/dest.c @@ -1988,7 +1988,7 @@ cupsSetDests2(http_t *http, // I - Connection to server or @code CUPS_HTTP_ * Create ~/.cups subdirectory... */ - mkdir(cg->userconfig, 0700); + _cupsDirCreate(cg->userconfig, 0700); snprintf(filename, sizeof(filename), "%s/lpoptions", cg->userconfig); } diff --git a/cups/dir.c b/cups/dir.c index 4f0c2dc661..ff5ea31620 100644 --- a/cups/dir.c +++ b/cups/dir.c @@ -15,12 +15,51 @@ * Include necessary headers... */ -#include "cups.h" +#include "cups-private.h" #include "string-private.h" #include "debug-internal.h" #include "dir.h" +// +// Common code... +// + +bool // O - `true` on success, `false` on failure +_cupsDirCreate(const char *path, // I - Directory path + mode_t mode) // I - Permissions of final directory +{ + bool ret = true; // Return value + char *copypath, // Copy of path + *ptr; // Pointer into path + + + // Copy the path + if ((copypath = strdup(path)) == NULL) + return (false); + + // Create any intermediate paths as needed... + for (ptr = strchr(copypath + 1, '/'); ptr; ptr = strchr(ptr + 1, '/')) + { + // Truncate path for the subdir and create it modulo the umask... + *ptr = '\0'; + if (mkdir(copypath, 0777) && errno != EEXIST) + { + ret = false; + break; + } + *ptr = '/'; + } + + // Free the copy of the path and then make the last component... + free(copypath); + if (ret && mkdir(path, mode) && errno != EEXIST) + ret = false; + + return (ret); +} + + /* * Windows implementation... */ diff --git a/cups/tls.c b/cups/tls.c index 96ef1d8b50..b80d1cc678 100644 --- a/cups/tls.c +++ b/cups/tls.c @@ -470,15 +470,9 @@ http_default_path( if (cg->userconfig) { - if (mkdir(cg->userconfig, 0755) && errno != EEXIST) - { - DEBUG_printf("1http_default_path: Failed to make directory '%s': %s", cg->userconfig, strerror(errno)); - return (NULL); - } - snprintf(buffer, bufsize, "%s/ssl", cg->userconfig); - if (mkdir(buffer, 0700) && errno != EEXIST) + if (!_cupsDirCreate(buffer, 0700)) { DEBUG_printf("1http_default_path: Failed to make directory '%s': %s", buffer, strerror(errno)); return (NULL); @@ -486,15 +480,9 @@ http_default_path( } else { - if (mkdir(cg->sysconfig, 0755) && errno != EEXIST) - { - DEBUG_printf("1http_default_path: Failed to make directory '%s': %s", cg->sysconfig, strerror(errno)); - return (NULL); - } - snprintf(buffer, bufsize, "%s/ssl", cg->sysconfig); - if (mkdir(buffer, 0700) && errno != EEXIST) + if (!_cupsDirCreate(buffer, 0700)) { DEBUG_printf("1http_default_path: Failed to make directory '%s': %s", buffer, strerror(errno)); return (NULL); -- 2.47.3