1 /* -*- Mode: C ; c-basic-offset: 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"
30 #include <sys/types.h>
37 bool check_dir_exists(const char * dirname
)
41 ASSERT(*dirname
); /* empty string? */
43 if (stat(dirname
, &st
) != 0)
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
)
60 ASSERT(*dirname
); /* empty string? */
62 if (mkdir(dirname
, mode
) == 0)
74 /* dirname path exists, not necessarily as a directory.
75 This includes the case where pathname is a symbolic link,
78 if (stat(dirname
, &st
) != 0)
81 log_error("Failed to stat \"%s\": %d (%s)", dirname
, errno
, strerror(errno
));
84 else if (!S_ISDIR(st
.st_mode
))
87 log_error("\"%s\" exists but is not directory.", dirname
);
95 static bool rmkdir(char * buffer
, int mode
)
117 /* skip extra '/' chars */
129 if (!safe_mkdir(buffer
, mode
, &err
))
137 log_info("Directory \"%s\" created", buffer
);
142 log_error("Failed to create \"%s\" directory: %d (%s)", buffer
, errno
, strerror(errno
));
157 bool ensure_dir_exist(const char * dirname
, int mode
)
164 if (!safe_mkdir(dirname
, mode
, &err
))
169 if (err
== 0 || err
== EEXIST
)
176 log_error("Failed to create \"%s\" directory: %d (%s)", dirname
, errno
, strerror(errno
));
180 /* A directory component in dirname does not exist or is a dangling symbolic link */
182 len
= strlen(dirname
);
184 buffer
= malloc(len
+ 1);
187 log_error("malloc(%zu) failed.", len
);
191 memcpy(buffer
, dirname
, len
);
194 ret
= rmkdir(buffer
, mode
);
201 bool ensure_dir_exist_varg(int mode
, ...)
212 while ((str
= va_arg(ap
, const char *)) != NULL
)
220 buffer
= malloc(len
);
223 log_error("malloc(%zu) failed.", len
);
229 while ((str
= va_arg(ap
, const char *)) != NULL
)
239 ret
= rmkdir(buffer
, mode
);
246 bool ladish_rmdir_recursive(const char * dirpath
)
249 struct dirent
* dentry_ptr
;
250 char * entry_fullpath
;
256 dir
= opendir(dirpath
);
259 log_error("Cannot open directory '%s': %d (%s)", dirpath
, errno
, strerror(errno
));
263 while ((dentry_ptr
= readdir(dir
)) != NULL
)
265 if (strcmp(dentry_ptr
->d_name
, ".") == 0 ||
266 strcmp(dentry_ptr
->d_name
, "..") == 0)
271 entry_fullpath
= catdup3(dirpath
, "/", dentry_ptr
->d_name
);
272 if (entry_fullpath
== NULL
)
274 log_error("catdup() failed");
278 if (stat(entry_fullpath
, &st
) != 0)
280 log_error("failed to stat '%s': %d (%s)", entry_fullpath
, errno
, strerror(errno
));
284 if (S_ISDIR(st
.st_mode
))
286 if (!ladish_rmdir_recursive(entry_fullpath
))
293 if (unlink(entry_fullpath
) < 0)
295 log_error("unlink('%s') failed. errno = %d (%s)", dirpath
, errno
, strerror(errno
));
301 free(entry_fullpath
);
304 if (rmdir(dirpath
) < 0)
306 log_error("rmdir('%s') failed. errno = %d (%s)", dirpath
, errno
, strerror(errno
));
316 free(entry_fullpath
);
323 bool ladish_rotate(const char * src
, const char * dst
, unsigned int max_backups
)
325 size_t len
= strlen(dst
) + 100;
329 const char * older_path
;
333 ASSERT(max_backups
> 0);
335 oldest_found
= false;
338 backup
= max_backups
;
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
);
352 log_error("Failed to stat \"%s\": %d (%s)", path
, errno
, strerror(errno
));
356 if (!S_ISDIR(st
.st_mode
))
358 log_error("\"%s\" exists but is not directory.", path
);
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
));
376 /* try to remove dst.max_backups */
377 log_info("rmdir '%s'", path
);
378 if (!ladish_rmdir_recursive(path
))
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
));
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
));