Skip to content

Commit

Permalink
Add new _cupsDirCreate private API and use it for config dirs.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Jun 18, 2024
1 parent 5226e72 commit 33628dd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cups/cups-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cups/dest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
41 changes: 40 additions & 1 deletion cups/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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...
*/
Expand Down
16 changes: 2 additions & 14 deletions cups/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,31 +470,19 @@ 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);
}
}
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);
Expand Down

0 comments on commit 33628dd

Please sign in to comment.