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"
34 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
39 #include <dirent.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
46 #include <libgen.h>
48 /*
49 * public API
50 */
52 int
53 sdb_mkdir_all(const char *pathname, mode_t mode)
54 {
55 struct stat st;
56 char *pathname_copy;
57 char *base_dir;
59 int status = 0;
61 if ((! pathname) || (! *pathname)) {
62 errno = EINVAL;
63 return -1;
64 }
66 memset(&st, 0, sizeof(st));
67 if (! stat(pathname, &st)) {
68 if (! S_ISDIR(st.st_mode)) {
69 errno = ENOTDIR;
70 return -1;
71 }
72 return 0;
73 }
75 if (errno != ENOENT)
76 /* pathname exists but we cannot access it */
77 return -1;
79 pathname_copy = strdup(pathname);
80 if (! pathname_copy)
81 return -1;
82 base_dir = dirname(pathname_copy);
84 status = sdb_mkdir_all(base_dir, mode);
85 if (! status)
86 status = mkdir(pathname, mode);
88 free(pathname_copy);
89 return status;
90 } /* sdb_mkdir_all */
92 int
93 sdb_remove_all(const char *pathname)
94 {
95 struct stat st;
97 if ((! pathname) || (! *pathname)) {
98 errno = EINVAL;
99 return -1;
100 }
102 memset(&st, 0, sizeof(st));
103 if (stat(pathname, &st))
104 return -1;
106 if (S_ISDIR(st.st_mode)) {
107 DIR *d = opendir(pathname);
109 if (! d)
110 return -1;
112 while (42) {
113 struct dirent de;
114 struct dirent *res = NULL;
116 char filename[strlen(pathname) + sizeof(de.d_name) + 2];
118 memset(&de, 0, sizeof(de));
119 if (readdir_r(d, &de, &res)) {
120 closedir(d);
121 return -1;
122 }
124 if (! res)
125 break;
127 if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
128 || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
129 continue;
131 snprintf(filename, sizeof(filename),
132 "%s/%s", pathname, de.d_name);
133 if (sdb_remove_all(filename)) {
134 closedir(d);
135 return -1;
136 }
137 }
138 closedir(d);
139 }
140 return remove(pathname);
141 } /* sdb_remove_all */
143 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */