Incorporate siginfo-1
[ladish.git] / common / dirhelpers.c
blob09e06369a156e5ff00bad88a2fd046b7d38345ef
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009,2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the directory helper functions
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "../common.h"
28 #include "catdup.h"
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <dirent.h>
37 bool check_dir_exists(const char * dirname)
39 struct stat st;
41 ASSERT(*dirname); /* empty string? */
43 if (stat(dirname, &st) != 0)
45 return false;
48 return S_ISDIR(st.st_mode);
51 /* ret=true, err=0 - directory was created successfully */
52 /* ret=true, err=EEXIST - directory already exists */
53 /* ret=true, err=ENOENT - A directory component in dirname does not exist or is a dangling symbolic link */
54 /* ret=true, err=error - directory creation failed in a bad way */
55 /* ret=false, err=error - dirname is not valid directory path */
56 static bool safe_mkdir(const char * dirname, int mode, int * err)
58 struct stat st;
60 ASSERT(*dirname); /* empty string? */
62 if (mkdir(dirname, mode) == 0)
64 *err = 0;
65 return true;
68 if (errno != EEXIST)
70 *err = errno;
71 return true;
74 /* dirname path exists, not necessarily as a directory.
75 This includes the case where pathname is a symbolic link,
76 dangling or not. */
78 if (stat(dirname, &st) != 0)
80 *err = errno;
81 log_error("Failed to stat \"%s\": %d (%s)", dirname, errno, strerror(errno));
82 return false;
84 else if (!S_ISDIR(st.st_mode))
86 *err = ENOTDIR;
87 log_error("\"%s\" exists but is not directory.", dirname);
88 return false;
91 *err = EEXIST;
92 return true;
95 static bool rmkdir(char * buffer, int mode)
97 int err;
98 char * p;
99 size_t len;
100 bool last;
102 len = 0;
103 p = buffer;
104 loop:
105 last = *p == 0;
106 if (!last)
108 if (*p != '/')
110 len++;
111 p++;
112 goto loop;
115 if (len == 0)
117 /* skip extra '/' chars */
118 p++;
119 goto loop;
122 *p = 0;
124 else if (len == 0)
126 return true;
129 if (!safe_mkdir(buffer, mode, &err))
131 return false;
134 switch (err)
136 case 0:
137 log_info("Directory \"%s\" created", buffer);
138 /* fall through */
139 case EEXIST:
140 break;
141 default:
142 log_error("Failed to create \"%s\" directory: %d (%s)", buffer, errno, strerror(errno));
143 return false;
146 if (!last)
148 *p = '/';
149 len = 0;
150 p++;
151 goto loop;
154 return true;
157 bool ensure_dir_exist(const char * dirname, int mode)
159 size_t len;
160 char * buffer;
161 int err;
162 bool ret;
164 if (!safe_mkdir(dirname, mode, &err))
166 return false;
169 if (err == 0 || err == EEXIST)
171 return true;
174 if (errno != ENOENT)
176 log_error("Failed to create \"%s\" directory: %d (%s)", dirname, errno, strerror(errno));
177 return false;
180 /* A directory component in dirname does not exist or is a dangling symbolic link */
182 len = strlen(dirname);
184 buffer = malloc(len + 1);
185 if (buffer == NULL)
187 log_error("malloc(%zu) failed.", len);
188 return false;
191 memcpy(buffer, dirname, len);
192 buffer[len] = 0;
194 ret = rmkdir(buffer, mode);
196 free(buffer);
198 return ret;
201 bool ensure_dir_exist_varg(int mode, ...)
203 va_list ap;
204 const char * str;
205 size_t len;
206 char * buffer;
207 char * p;
208 int ret;
210 len = 0;
211 va_start(ap, mode);
212 while ((str = va_arg(ap, const char *)) != NULL)
214 len += strlen(str);
216 va_end(ap);
217 ASSERT(len > 0);
218 len++;
220 buffer = malloc(len);
221 if (buffer == NULL)
223 log_error("malloc(%zu) failed.", len);
224 return false;
227 p = buffer;
228 va_start(ap, mode);
229 while ((str = va_arg(ap, const char *)) != NULL)
231 len = strlen(str);
232 memcpy(p, str, len);
233 p += len;
235 va_end(ap);
236 ASSERT(p != buffer);
237 *p = 0;
239 ret = rmkdir(buffer, mode);
241 free(buffer);
243 return ret;
246 bool ladish_rmdir_recursive(const char * dirpath)
248 DIR * dir;
249 struct dirent * dentry_ptr;
250 char * entry_fullpath;
251 struct stat st;
252 bool success;
254 success = false;
256 dir = opendir(dirpath);
257 if (dir == NULL)
259 log_error("Cannot open directory '%s': %d (%s)", dirpath, errno, strerror(errno));
260 goto exit;
263 while ((dentry_ptr = readdir(dir)) != NULL)
265 if (strcmp(dentry_ptr->d_name, ".") == 0 ||
266 strcmp(dentry_ptr->d_name, "..") == 0)
268 continue;
271 entry_fullpath = catdup3(dirpath, "/", dentry_ptr->d_name);
272 if (entry_fullpath == NULL)
274 log_error("catdup() failed");
275 goto close;
278 if (stat(entry_fullpath, &st) != 0)
280 log_error("failed to stat '%s': %d (%s)", entry_fullpath, errno, strerror(errno));
282 else
284 if (S_ISDIR(st.st_mode))
286 if (!ladish_rmdir_recursive(entry_fullpath))
288 goto free;
291 else
293 if (unlink(entry_fullpath) < 0)
295 log_error("unlink('%s') failed. errno = %d (%s)", dirpath, errno, strerror(errno));
296 goto free;
301 free(entry_fullpath);
304 if (rmdir(dirpath) < 0)
306 log_error("rmdir('%s') failed. errno = %d (%s)", dirpath, errno, strerror(errno));
308 else
310 success = true;
313 goto close;
315 free:
316 free(entry_fullpath);
317 close:
318 closedir(dir);
319 exit:
320 return success;
323 bool ladish_rotate(const char * src, const char * dst, unsigned int max_backups)
325 size_t len = strlen(dst) + 100;
326 char paths[2][len];
327 struct stat st;
328 char * path;
329 const char * older_path;
330 bool oldest_found;
331 unsigned int backup;
333 ASSERT(max_backups > 0);
335 oldest_found = false;
336 path = NULL;
337 older_path = NULL;
338 backup = max_backups;
339 while (backup > 0)
341 path = paths[backup % 2];
342 snprintf(path, len, "%s.%u", dst, backup);
344 if (stat(path, &st) != 0)
346 if (!oldest_found && errno == ENOENT)
348 log_info("\"%s\" does not exist", path);
349 goto next;
352 log_error("Failed to stat \"%s\": %d (%s)", path, errno, strerror(errno));
353 return false;
356 if (!S_ISDIR(st.st_mode))
358 log_error("\"%s\" exists but is not directory.", path);
359 return false;
362 oldest_found = true;
364 if (backup < max_backups)
366 ASSERT(older_path != NULL);
367 log_info("rename '%s' -> '%s'", path, older_path);
368 if (rename(path, older_path) != 0)
370 log_error("rename('%s' -> '%s') failed. errno = %d (%s)", path, older_path, errno, strerror(errno));
371 return false;
374 else
376 /* try to remove dst.max_backups */
377 log_info("rmdir '%s'", path);
378 if (!ladish_rmdir_recursive(path))
380 return false;
384 next:
385 older_path = path;
386 backup--;
389 ASSERT(path != NULL);
391 log_info("rename '%s' -> '%s'", dst, path);
392 if (rename(dst, path) != 0 && errno != ENOENT)
394 log_error("rename('%s' -> '%s') failed. errno = %d (%s)", dst, path, errno, strerror(errno));
395 return false;
398 log_info("rename '%s' -> '%s'", src, dst);
399 if (rename(src, dst) != 0)
401 log_error("rename('%s' -> '%s') failed. errno = %d (%s)", src, dst, errno, strerror(errno));
402 return false;
405 return true;