util: add mode, uid, gid parameters to UTI_CreateDirAndParents()
This commit is contained in:
parent
9a83cab2f8
commit
236576c124
4 changed files with 42 additions and 34 deletions
|
@ -306,9 +306,7 @@ LOG_CreateLogFileDir(void)
|
||||||
|
|
||||||
logdir = CNF_GetLogDir();
|
logdir = CNF_GetLogDir();
|
||||||
|
|
||||||
if (!UTI_CreateDirAndParents(logdir)) {
|
UTI_CreateDirAndParents(logdir, 0755, 0, 0);
|
||||||
LOG(LOGS_ERR, LOGF_Logging, "Could not create directory %s", logdir);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
|
@ -1092,7 +1092,7 @@ SRC_DumpSources(void)
|
||||||
direc_len = strlen(direc);
|
direc_len = strlen(direc);
|
||||||
file_len = direc_len + 24;
|
file_len = direc_len + 24;
|
||||||
filename = MallocArray(char, file_len); /* a bit of slack */
|
filename = MallocArray(char, file_len); /* a bit of slack */
|
||||||
if (UTI_CreateDirAndParents(direc)) {
|
if (UTI_CreateDirAndParents(direc, 0755, 0, 0)) {
|
||||||
for (i=0; i<n_sources; i++) {
|
for (i=0; i<n_sources; i++) {
|
||||||
a = (sources[i]->ref_id) >> 24;
|
a = (sources[i]->ref_id) >> 24;
|
||||||
b = ((sources[i]->ref_id) >> 16) & 0xff;
|
b = ((sources[i]->ref_id) >> 16) & 0xff;
|
||||||
|
@ -1108,8 +1108,6 @@ SRC_DumpSources(void)
|
||||||
fclose(out);
|
fclose(out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
LOG(LOGS_ERR, LOGF_Sources, "Could not create directory %s", direc);
|
|
||||||
}
|
}
|
||||||
Free(filename);
|
Free(filename);
|
||||||
}
|
}
|
||||||
|
|
69
util.c
69
util.c
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
#include "sysincl.h"
|
#include "sysincl.h"
|
||||||
|
|
||||||
|
#include "logging.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
@ -897,7 +898,7 @@ UTI_SetQuitSignalsHandler(void (*handler)(int))
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
create_dir(char *p)
|
create_dir(char *p, mode_t mode, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
|
@ -906,27 +907,39 @@ create_dir(char *p)
|
||||||
status = stat(p, &buf);
|
status = stat(p, &buf);
|
||||||
|
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
if (errno == ENOENT) {
|
if (errno != ENOENT) {
|
||||||
/* Try to create directory */
|
LOG(LOGS_ERR, LOGF_Util, "Could not access %s : %s", p, strerror(errno));
|
||||||
status = mkdir(p, 0755);
|
|
||||||
return status;
|
|
||||||
} else {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!S_ISDIR(buf.st_mode)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (S_ISDIR(buf.st_mode))
|
||||||
|
return 1;
|
||||||
|
LOG(LOGS_ERR, LOGF_Util, "%s is not directory", p);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create the directory */
|
||||||
|
if (mkdir(p, mode) < 0) {
|
||||||
|
LOG(LOGS_ERR, LOGF_Util, "Could not create directory %s : %s", p, strerror(errno));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Change its ownership if requested */
|
||||||
|
if ((uid || gid) && chown(p, uid, gid) < 0) {
|
||||||
|
LOG(LOGS_ERR, LOGF_Util, "Could not change ownership of %s : %s", p, strerror(errno));
|
||||||
|
/* Don't leave it there with incorrect ownership */
|
||||||
|
rmdir(p);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* Return 0 if the directory couldn't be created, 1 if it could (or
|
/* Return 0 if the directory couldn't be created, 1 if it could (or
|
||||||
already existed) */
|
already existed) */
|
||||||
int
|
int
|
||||||
UTI_CreateDirAndParents(const char *path)
|
UTI_CreateDirAndParents(const char *path, mode_t mode, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
char *p;
|
char *p;
|
||||||
int i, j, k, last;
|
int i, j, k, last;
|
||||||
|
@ -942,30 +955,26 @@ UTI_CreateDirAndParents(const char *path)
|
||||||
p[i++] = path[k++];
|
p[i++] = path[k++];
|
||||||
|
|
||||||
if (path[k] == '/' || !path[k]) {
|
if (path[k] == '/' || !path[k]) {
|
||||||
p[i] = 0;
|
/* Check whether its end of string, a trailing / or group of / */
|
||||||
|
|
||||||
if (create_dir(p) < 0) {
|
|
||||||
Free(p);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!path[k]) {
|
|
||||||
/* End of the string */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check whether its a trailing / or group of / */
|
|
||||||
last = 1;
|
last = 1;
|
||||||
j = k + 1;
|
j = k;
|
||||||
while (path[j]) {
|
while (path[j]) {
|
||||||
if (path[j] != '/') {
|
if (path[j] != '/') {
|
||||||
k = j - 1; /* Pick up a / into p[] thru the assignment at the top of the loop */
|
/* Pick up a / into p[] thru the assignment at the top of the loop */
|
||||||
|
k = j - 1;
|
||||||
last = 0;
|
last = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p[i] = 0;
|
||||||
|
|
||||||
|
if (!create_dir(p, last ? mode : 0755, last ? uid : 0, last ? gid : 0)) {
|
||||||
|
Free(p);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (last)
|
if (last)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
7
util.h
7
util.h
|
@ -130,7 +130,10 @@ extern int UTI_DecodePasswordFromText(char *key);
|
||||||
|
|
||||||
extern int UTI_SetQuitSignalsHandler(void (*handler)(int));
|
extern int UTI_SetQuitSignalsHandler(void (*handler)(int));
|
||||||
|
|
||||||
/* Create a directory and any parent directories that don't exist */
|
/* Create a directory with a specified mode (umasked) and set its uid/gid
|
||||||
extern int UTI_CreateDirAndParents(const char *path);
|
(if not 0). Create also any parent directories that don't exist with mode
|
||||||
|
755 and default uid/gid. Returns 1 if created or already exists (even with
|
||||||
|
different mode/uid/gid), 0 otherwise. */
|
||||||
|
extern int UTI_CreateDirAndParents(const char *path, mode_t mode, uid_t uid, gid_t gid);
|
||||||
|
|
||||||
#endif /* GOT_UTIL_H */
|
#endif /* GOT_UTIL_H */
|
||||||
|
|
Loading…
Reference in a new issue