New utility function mkdirx().

This commit is contained in:
default 2023-02-07 09:25:01 +01:00
parent 392c5147a3
commit 67f2d4318d
5 changed files with 28 additions and 17 deletions

4
data.c
View file

@ -88,7 +88,7 @@ int srv_open(char *basedir, int auto_upgrade)
/* create the queue/ subdir, just in case */ /* create the queue/ subdir, just in case */
xs *qdir = xs_fmt("%s/queue", srv_basedir); xs *qdir = xs_fmt("%s/queue", srv_basedir);
mkdir(qdir, DIR_PERM); mkdirx(qdir);
#ifdef __OpenBSD__ #ifdef __OpenBSD__
char *v = xs_dict_get(srv_config, "disable_openbsd_security"); char *v = xs_dict_get(srv_config, "disable_openbsd_security");
@ -434,7 +434,7 @@ d_char *_object_fn_by_md5(const char *md5)
{ {
xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]); xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
mkdir(bfn, DIR_PERM); mkdirx(bfn);
return xs_fmt("%s/%s.json", bfn, md5); return xs_fmt("%s/%s.json", bfn, md5);
} }

14
snac.c
View file

@ -30,6 +30,18 @@ int srv_running = 0;
int dbglevel = 0; int dbglevel = 0;
int mkdirx(const char *pathname)
/* creates a directory with special permissions */
{
int ret;
if ((ret = mkdir(pathname, DIR_PERM)) != -1)
ret = chmod(pathname, DIR_PERM);
return ret;
}
int valid_status(int status) int valid_status(int status)
/* is this HTTP status valid? */ /* is this HTTP status valid? */
{ {
@ -149,7 +161,7 @@ void srv_archive(const char *direction, xs_dict *req,
xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction); xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
FILE *f; FILE *f;
if (mkdir(dir, DIR_PERM) != -1) { if (mkdirx(dir) != -1) {
xs *meta_fn = xs_fmt("%s/_META", dir); xs *meta_fn = xs_fmt("%s/_META", dir);
if ((f = fopen(meta_fn, "w")) != NULL) { if ((f = fopen(meta_fn, "w")) != NULL) {

2
snac.h
View file

@ -17,6 +17,8 @@ extern int dbglevel;
#define L(s) (s) #define L(s) (s)
int mkdirx(const char *pathname);
int valid_status(int status); int valid_status(int status);
d_char *tid(int offset); d_char *tid(int offset);
double ftime(void); double ftime(void);

View file

@ -36,7 +36,7 @@ int snac_upgrade(xs_str **error)
else else
if (f < 2.1) { if (f < 2.1) {
xs *dir = xs_fmt("%s/object", srv_basedir); xs *dir = xs_fmt("%s/object", srv_basedir);
mkdir(dir, DIR_PERM); mkdirx(dir);
nf = 2.1; nf = 2.1;
} }
@ -61,7 +61,7 @@ int snac_upgrade(xs_str **error)
xs *dir = xs_fmt("%s/object/%c%c", srv_basedir, b[0], b[1]); xs *dir = xs_fmt("%s/object/%c%c", srv_basedir, b[0], b[1]);
xs *nfn = xs_fmt("%s/%s", dir, b); xs *nfn = xs_fmt("%s/%s", dir, b);
mkdir(dir, DIR_PERM); mkdirx(dir);
rename(fn, nfn); rename(fn, nfn);
} }
@ -88,7 +88,7 @@ int snac_upgrade(xs_str **error)
xs *dir = xs_fmt("%s/hidden", snac.basedir); xs *dir = xs_fmt("%s/hidden", snac.basedir);
/* create the hidden directory */ /* create the hidden directory */
mkdir(dir, DIR_PERM); mkdirx(dir);
/* rename all muted files incorrectly named .json */ /* rename all muted files incorrectly named .json */
xs *spec = xs_fmt("%s/muted/" "*.json", snac.basedir); xs *spec = xs_fmt("%s/muted/" "*.json", snac.basedir);
@ -117,10 +117,10 @@ int snac_upgrade(xs_str **error)
if (user_open(&snac, v)) { if (user_open(&snac, v)) {
xs *dir = xs_fmt("%s/public", snac.basedir); xs *dir = xs_fmt("%s/public", snac.basedir);
mkdir(dir, DIR_PERM); mkdirx(dir);
dir = xs_replace_i(dir, "public", "private"); dir = xs_replace_i(dir, "public", "private");
mkdir(dir, DIR_PERM); mkdirx(dir);
user_free(&snac); user_free(&snac);
} }

15
utils.c
View file

@ -143,22 +143,19 @@ int snac_init(const char *basedir)
} }
} }
if (mkdir(srv_basedir, DIR_PERM) == -1) { if (mkdirx(srv_basedir) == -1) {
printf("ERROR: cannot create directory '%s'\n", srv_basedir); printf("ERROR: cannot create directory '%s'\n", srv_basedir);
return 1; return 1;
} }
/* force permissions (mkdir also uses 777) */
chmod(srv_basedir, DIR_PERM);
xs *udir = xs_fmt("%s/user", srv_basedir); xs *udir = xs_fmt("%s/user", srv_basedir);
mkdir(udir, DIR_PERM); mkdirx(udir);
xs *odir = xs_fmt("%s/object", srv_basedir); xs *odir = xs_fmt("%s/object", srv_basedir);
mkdir(odir, DIR_PERM); mkdirx(odir);
xs *qdir = xs_fmt("%s/queue", srv_basedir); xs *qdir = xs_fmt("%s/queue", srv_basedir);
mkdir(qdir, DIR_PERM); mkdirx(qdir);
xs *gfn = xs_fmt("%s/greeting.html", srv_basedir); xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
if ((f = fopen(gfn, "w")) == NULL) { if ((f = fopen(gfn, "w")) == NULL) {
@ -246,7 +243,7 @@ int adduser(const char *uid)
xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid); xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
if (mkdir(basedir, DIR_PERM) == -1) { if (mkdirx(basedir) == -1) {
printf("ERROR: cannot create directory '%s'\n", basedir); printf("ERROR: cannot create directory '%s'\n", basedir);
return 0; return 0;
} }
@ -259,7 +256,7 @@ int adduser(const char *uid)
for (n = 0; dirs[n]; n++) { for (n = 0; dirs[n]; n++) {
xs *d = xs_fmt("%s/%s", basedir, dirs[n]); xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
mkdir(d, DIR_PERM); mkdirx(d);
} }
xs *scssfn = xs_fmt("%s/style.css", srv_basedir); xs *scssfn = xs_fmt("%s/style.css", srv_basedir);