public inbox for ell@lists.linux.dev
 help / color / mirror / Atom feed
From: Denis Kenzior <denkenz@gmail.com>
To: ell@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH 1/2] dir: Add l_dir_create
Date: Mon, 29 Jan 2024 13:41:36 -0600	[thread overview]
Message-ID: <20240129194139.714209-1-denkenz@gmail.com> (raw)

---
 ell/dir.c   | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/dir.h   |  2 ++
 ell/ell.sym |  1 +
 3 files changed, 55 insertions(+)

diff --git a/ell/dir.c b/ell/dir.c
index f55382f82508..5f13f8afa0c5 100644
--- a/ell/dir.c
+++ b/ell/dir.c
@@ -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;
+}
diff --git a/ell/dir.h b/ell/dir.h
index 8b786f9f36b3..3da4f82bdbb4 100644
--- a/ell/dir.h
+++ b/ell/dir.h
@@ -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
diff --git a/ell/ell.sym b/ell/ell.sym
index e12fd0e825b8..17acd40683e8 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -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 */
-- 
2.43.0


             reply	other threads:[~2024-01-29 19:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-29 19:41 Denis Kenzior [this message]
2024-01-29 19:41 ` [PATCH 2/2] file: Add l_file_set_contents Denis Kenzior
2024-01-30  2:39 ` [PATCH 1/2] dir: Add l_dir_create Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240129194139.714209-1-denkenz@gmail.com \
    --to=denkenz@gmail.com \
    --cc=ell@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox