1 /*
2 * SysDB - src/utils/os.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "utils/os.h"
33 #include "utils/error.h"
35 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
40 #include <dirent.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include <libgen.h>
48 #include <pwd.h>
50 /*
51 * public API
52 */
54 int
55 sdb_mkdir_all(const char *pathname, mode_t mode)
56 {
57 struct stat st;
58 char *pathname_copy;
59 char *base_dir;
61 int status = 0;
63 if ((! pathname) || (! *pathname)) {
64 errno = EINVAL;
65 return -1;
66 }
68 memset(&st, 0, sizeof(st));
69 if (! stat(pathname, &st)) {
70 if (! S_ISDIR(st.st_mode)) {
71 errno = ENOTDIR;
72 return -1;
73 }
74 return 0;
75 }
77 if (errno != ENOENT)
78 /* pathname exists but we cannot access it */
79 return -1;
81 pathname_copy = strdup(pathname);
82 if (! pathname_copy)
83 return -1;
84 base_dir = dirname(pathname_copy);
86 status = sdb_mkdir_all(base_dir, mode);
87 if (! status)
88 status = mkdir(pathname, mode);
90 free(pathname_copy);
91 return status;
92 } /* sdb_mkdir_all */
94 int
95 sdb_remove_all(const char *pathname)
96 {
97 struct stat st;
99 if ((! pathname) || (! *pathname)) {
100 errno = EINVAL;
101 return -1;
102 }
104 memset(&st, 0, sizeof(st));
105 if (stat(pathname, &st))
106 return -1;
108 if (S_ISDIR(st.st_mode)) {
109 DIR *d = opendir(pathname);
111 if (! d)
112 return -1;
114 while (42) {
115 struct dirent de;
116 struct dirent *res = NULL;
118 char filename[strlen(pathname) + sizeof(de.d_name) + 2];
120 memset(&de, 0, sizeof(de));
121 if (readdir_r(d, &de, &res)) {
122 closedir(d);
123 return -1;
124 }
126 if (! res)
127 break;
129 if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
130 || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
131 continue;
133 snprintf(filename, sizeof(filename),
134 "%s/%s", pathname, de.d_name);
135 if (sdb_remove_all(filename)) {
136 closedir(d);
137 return -1;
138 }
139 }
140 closedir(d);
141 }
142 return remove(pathname);
143 } /* sdb_remove_all */
145 char *
146 sdb_get_current_user(void)
147 {
148 struct passwd pw_entry;
149 struct passwd *result = NULL;
151 uid_t uid;
153 char buf[1024];
154 int status;
156 uid = geteuid();
157 memset(&pw_entry, 0, sizeof(pw_entry));
158 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
160 if (status || (! result)) {
161 char errbuf[1024];
162 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
163 sdb_strerror(errno, errbuf, sizeof(errbuf)));
164 return NULL;
165 }
166 return strdup(result->pw_name);
167 } /* sdb_get_current_user */
169 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */