-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor mkpath() to drop all uses of strdupa()
Fixes #488 Signed-off-by: Joachim Wiberg <[email protected]>
- Loading branch information
Showing
5 changed files
with
42 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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: | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters