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 2/2] file: Add l_file_set_contents
Date: Mon, 29 Jan 2024 13:41:37 -0600	[thread overview]
Message-ID: <20240129194139.714209-2-denkenz@gmail.com> (raw)
In-Reply-To: <20240129194139.714209-1-denkenz@gmail.com>

---
 ell/ell.sym |  1 +
 ell/file.c  | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/file.h  |  1 +
 3 files changed, 58 insertions(+)

diff --git a/ell/ell.sym b/ell/ell.sym
index 17acd40683e8..6ef885dd9d83 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -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;
diff --git a/ell/file.c b/ell/file.c
index fb510d17fb4a..6f0a909f88e5 100644
--- a/ell/file.c
+++ b/ell/file.c
@@ -15,9 +15,12 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
 
 #include "file.h"
 #include "private.h"
+#include "useful.h"
 
 /**
  * l_file_get_contents:
@@ -72,3 +75,56 @@ error:
 	close(fd);
 	return NULL;
 }
+
+/**
+ * l_file_set_contents:
+ * @filename: Destination filename
+ * @contents: Pointer to the contents
+ * @len: Length in bytes of the contents buffer
+ *
+ * Given a content buffer, write it to a file named @filename.  This function
+ * ensures that the contents are consistent (i.e. due to a crash right after
+ * opening or during write() by writing the contents to a tempoary which is then
+ * renamed to @filename.
+ *
+ * Returns: 0 if successful, a negative errno otherwise
+ **/
+LIB_EXPORT int l_file_set_contents(const char *filename,
+					const void *contents, size_t len)
+{
+	_auto_(l_free) char *tmp_path = NULL;
+	ssize_t r;
+	int fd;
+
+	if (!filename || !contents)
+		return -EINVAL;
+
+	tmp_path = l_strdup_printf("%s.XXXXXX.tmp", filename);
+
+	fd = L_TFR(mkostemps(tmp_path, 4, O_CLOEXEC));
+	if (fd == -1)
+		return -errno;
+
+	r = L_TFR(write(fd, contents, len));
+	L_TFR(close(fd));
+
+	if (r != (ssize_t) len) {
+		r = -EIO;
+		goto error_write;
+	}
+
+	/*
+	 * Now that the file contents are written, rename to the real
+	 * file name; this way we are uniquely sure that the whole
+	 * thing is there.
+	 * conserve @r's value from 'write'
+	 */
+	if (rename(tmp_path, filename) == -1)
+		r = -errno;
+
+error_write:
+	if (r < 0)
+		unlink(tmp_path);
+
+	return r < 0 ? r : 0;
+}
diff --git a/ell/file.h b/ell/file.h
index a93d8e9c27c2..f1ed8790fd02 100644
--- a/ell/file.h
+++ b/ell/file.h
@@ -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
 }
-- 
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 [PATCH 1/2] dir: Add l_dir_create Denis Kenzior
2024-01-29 19:41 ` Denis Kenzior [this message]
2024-01-30  2:39 ` 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-2-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