dir: Add l_dir_create
PATCH 1/2 dir: Add l_dir_create
--- ell/dir.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ell/dir.h | 2 ++ ell/ell.sym | 1 + 3 files changed, 55 insertions(+)
ell/dir.c +52 -0
@@ -15,8 +15,10 @@#include <limits.h> #include <sys/inotify.h> #include <errno.h> +#include <sys/stat.h> #include "private.h" +#include "useful.h" #include "queue.h" #include "io.h" #include "dir.h"
@@ -336,3 +338,53 @@ done:l_free (watch); } + +/** +* l_dir_create: +* @abspath: Absolute path of the directory to create +* +* Attempts to create a directory tree given by @abspath. @abspath must be +* an absolute path. +* +* Returns: 0 if successful, a negative errno otherwise +**/ +LIB_EXPORT int l_dir_create(const char *abspath) +{ +static const mode_t create_mode =S_IRUSR |S_IWUSR |S_IXUSR ; +struct stat st; + _auto_(l_free) char *dir = NULL; +const char *prev, *next; +int err; + +if (!abspath || abspath[0 ] !='/' ) +return -EINVAL ; + + err =stat (abspath, &st); +if (!err) { +/* File exists */ +if (S_ISDIR (st.st_mode)) +return 0 ; + +return -ENOTDIR ; + } + +if (errno !=ENOENT ) +return -errno; + + dir =l_malloc (strlen (abspath) +1 ); + dir[0 ] ='\0' ; + +for (prev = abspath; prev[0 ] && (next =strchrnul (prev +1 ,'/' )); + prev = next) { +/* Skip consecutive '/' characters */ +if (next - prev ==1 ) +continue ; + +strncat (dir, prev, next - prev); + +if (mkdir (dir, create_mode) ==-1 && errno !=EEXIST ) +return -errno; + } + +return 0 ; +}
ell/dir.h +2 -0
@@ -33,6 +33,8 @@ struct l_dir_watch *l_dir_watch_new(const char *pathname, l_dir_watch_destroy_func_t destroy);void l_dir_watch_destroy (struct l_dir_watch *watch); +int l_dir_create (const char *abspath); + #ifdef __cplusplus }#endif
ell/ell.sym +1 -0
@@ -307,6 +307,7 @@ global: l_dhcp6_lease_get_preferred_lifetime; l_dhcp6_lease_get_start_time; /* dir */ + l_dir_create; l_dir_watch_new; l_dir_watch_destroy; /* file */
Signature · 1 line
2.43.0
PATCH 2/2 file: Add l_file_set_contents
--- ell/ell.sym | 1 + ell/file.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ell/file.h | 1 + 3 files changed, 58 insertions(+)
ell/ell.sym +1 -0
@@ -312,6 +312,7 @@ global: l_dir_watch_destroy; /* file */ l_file_get_contents; + l_file_set_contents; /* genl */ l_genl_new; l_genl_ref;
ell/file.c +56 -0
68 lines not on this page · raw message
ell/file.h +1 -0
@@ -13,6 +13,7 @@ extern "C" {#endif void *l_file_get_contents (const char *filename,size_t *out_len); +int l_file_set_contents (const char *filename,const void *data,size_t len); #ifdef __cplusplus }
Signature · 1 line
2.43.0
Quoted 6 lines · Denis Kenzior
--- ell/dir.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ell/dir.h | 2 ++ ell/ell.sym | 1 + 3 files changed, 55 insertions(+)
All applied.
Regards,
-Denis