Skip to contentSkip to conversation

dir: Add l_dir_create

PATCH In review Denis Kenzior 1 participant 3 messages Jan 29, 2024 – Jan 30, 2024

lore
Share & tools
lore
#0 lore raw

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
index f55382f82508..5f13f8afa0c5 100644
@@ -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
index 8b786f9f36b3..3da4f82bdbb4 100644
@@ -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
index e12fd0e825b8..17acd40683e8 100644
@@ -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
#1 lore raw

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
index 17acd40683e8..6ef885dd9d83 100644
@@ -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
index a93d8e9c27c2..f1ed8790fd02 100644
@@ -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
#2 lore raw
In reply to Denis Kenzior
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