]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Add new _cupsDirCreate private API and use it for config dirs.
authorMichael R Sweet <msweet@msweet.org>
Tue, 18 Jun 2024 22:48:10 +0000 (18:48 -0400)
committerMichael R Sweet <msweet@msweet.org>
Tue, 18 Jun 2024 22:48:10 +0000 (18:48 -0400)
cups/cups-private.h
cups/dest.c
cups/dir.c
cups/tls.c

index ab8f5bc1725c56d48ece456ad2ec39d9f21dfc94..af3843dc480d68798c358f7d4e29d03283bd073a 100644 (file)
@@ -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;
index 69708579e34a2d8f88eb8970423ba3698591721f..c9bea88cee2fc148286f9109325fb7f8f932765c 100644 (file)
@@ -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);
   }
index 4f0c2dc661e1b2b10a43a0c8b99d86f38a123457..ff5ea31620092610715703b758f2b99a88689e05 100644 (file)
  * 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...
  */
index 96ef1d8b50fedcb3ff51c36d17221007b0af71c1..b80d1cc6786da1a05e56dc9bb7c218df5f7f321e 100644 (file)
@@ -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);