Skip to content

Commit

Permalink
Refactor mkpath() to drop all uses of strdupa()
Browse files Browse the repository at this point in the history
Fixes #488

Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Aug 3, 2024
1 parent e1c196a commit b9edfbf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 74 deletions.
2 changes: 1 addition & 1 deletion include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ noinst_HEADERS = base64.h md5.h sha1.h \
jsmn.h json.h log.h \
md5.h os.h plugin.h \
queue.h sha1.h ssl.h \
strdupa.h tcp.h
tcp.h
4 changes: 2 additions & 2 deletions include/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
#include <unistd.h>
#include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
#include <sys/types.h>
#include "strdupa.h"

/* From The Practice of Programming, by Kernighan and Pike */
#ifndef NELEMS
#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
#endif

int mkpath (char *dir, mode_t mode);
int mkpath (const char *dir, mode_t mode);
int makepath (const char *dir);

#ifndef pidfile
int pidfile (const char *basename);
Expand Down
52 changes: 0 additions & 52 deletions include/strdupa.h

This file was deleted.

56 changes: 38 additions & 18 deletions src/makepath.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* mkpath() -- Create all components leading up to a given directory
*
* Copyright (c) 2013-2021 Joachim Wiberg <[email protected]>
* Copyright (c) 2013-2024 Joachim Wiberg <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -16,11 +16,35 @@
*/

#include <errno.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h> /* strdup(), strrchr() */
#include <stdlib.h> /* free() */
#include <sys/stat.h> /* mkdir() */

#include "compat.h"

/* Recursively create directories */
static int _mkpath(char *dir, mode_t mode)
{
char *slash;

if (!mkdir(dir, mode) || errno == EEXIST)
return 0;

if (errno != ENOENT)
return -1;

slash = strrchr(dir, '/');
if (!slash)
return -1;

*slash = 0;
if (_mkpath(dir, mode) == -1)
return -1;

*slash = '/';
return mkdir(dir, mode);
}

/**
* mkpath - Like makepath() but takes a mode_t argument
* @dir: Directory to created, relative or absolute
Expand All @@ -29,21 +53,24 @@
* Returns:
* POSIX OK(0) on success, otherwise -1 with @errno set.
*/
int mkpath(char *dir, mode_t mode)
int mkpath(const char *dir, mode_t mode)
{
struct stat sb;
char *_dir;
int rc;

if (!dir) {
errno = EINVAL;
return 1;
}

if (!stat(dir, &sb))
return 0;
_dir = strdup(dir);
if (!_dir)
return -1;

mkpath(dirname(strdupa(dir)), mode);
rc = _mkpath(_dir, mode);
free(_dir);

return mkdir(dir, mode);
return rc;
}

/**
Expand All @@ -56,14 +83,7 @@ int mkpath(char *dir, mode_t mode)
* fails allocating temporary memory. For other error codes see the
* mkdir() syscall description.
*/
int makepath(char *dir)
int makepath(const char *dir)
{
return mkpath(dir, 0777);
}

/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
2 changes: 1 addition & 1 deletion src/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ int os_check_perms(void)
}
}

pidfile_dir = dirname(strdupa(pidfn));
pidfile_dir = dirname(pidfn);
if (access(pidfile_dir, F_OK)) {
if (mkpath(pidfile_dir, 0755) && errno != EEXIST)
logit(LOG_ERR, "No write permission to %s, aborting.", pidfile_dir);
Expand Down

0 comments on commit b9edfbf

Please sign in to comment.