remove_any_whitespace_to_the_left_or_right_of_at_symbol() is awesomized and no longer...
[citadel.git] / citadel / citadel_dirs.c
1 /*
2  * citadel_dirs.c : calculate pathnames for various files used in the Citadel system
3  *
4  * Copyright (c) 1987-2021 by the citadel.org team
5  *
6  *  This program is open source software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 3.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  */
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <sys/stat.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <limits.h>
20 #include <time.h>
21 #include <errno.h>
22 #include <syslog.h>
23 #include <libcitadel.h>
24 #include "citadel.h"
25 #include "citadel_dirs.h"
26
27 /*
28  * Generate an associated file name for a room
29  */
30 size_t assoc_file_name(char *buf, size_t n, struct ctdlroom *qrbuf, const char *prefix) {
31         return snprintf(buf, n, "%s%ld", prefix, qrbuf->QRnumber);
32 }
33
34
35 int create_dir(char *which, long ACCESS, long UID, long GID) {
36         int rv;
37         rv = mkdir(which, ACCESS);
38         if ((rv == -1) && (errno != EEXIST)) {
39                 syslog(LOG_ERR,
40                        "failed to create directory %s: %s",
41                        which,
42                        strerror(errno));
43                 return rv;
44         }
45         rv = chmod(which, ACCESS);
46         if (rv == -1) {
47                 syslog(LOG_ERR,
48                        "failed to set permissions for directory %s: %s",
49                        which,
50                        strerror(errno));
51                 return rv;
52         }
53         rv = chown(which, UID, GID);
54         if (rv == -1) {
55                 syslog(LOG_ERR,
56                        "failed to set owner for directory %s: %s",
57                        which,
58                        strerror(errno));
59                 return rv;
60         }
61         return rv;
62 }
63
64
65 int create_run_directories(long UID, long GID) {
66         int rv = 0;
67         rv += create_dir(ctdl_message_dir   , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
68         rv += create_dir(ctdl_file_dir      , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
69         rv += create_dir(ctdl_key_dir       , S_IRUSR|S_IWUSR|S_IXUSR, UID, -1);
70         rv += create_dir(ctdl_run_dir       , S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, UID, GID);
71         return rv;
72 }