diff mbox series

[13/17] storage: use l_file_set_contents

Message ID 20240130212137.814082-13-denkenz@gmail.com (mailing list archive)
State Superseded
Headers show
Series [01/17] umlrunner: Also mount /var/lib as tmpfs | expand

Commit Message

Denis Kenzior Jan. 30, 2024, 9:21 p.m. UTC
Use ell convenience function to set the file contents.
---
 src/storage.c | 41 ++++++-----------------------------------
 1 file changed, 6 insertions(+), 35 deletions(-)
diff mbox series

Patch

diff --git a/src/storage.c b/src/storage.c
index 30e004e75d91..130fcc9ce4a7 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -3,6 +3,7 @@ 
  *  oFono - Open Source Telephony
  *
  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2024  Cruise, LLC
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
@@ -121,50 +122,20 @@  ssize_t read_file(void *buffer, size_t len, const char *path_fmt, ...)
 ssize_t write_file(const void *buffer, size_t len, const char *path_fmt, ...)
 {
 	va_list ap;
-	char *tmp_path, *path;
-	ssize_t r;
-	int fd;
-	mode_t mode = S_IRUSR | S_IWUSR;
+	char *path;
+	int r;
 
 	va_start(ap, path_fmt);
 	path = l_strdup_vprintf(path_fmt, ap);
 	va_end(ap);
 
-	tmp_path = l_strdup_printf("%s.XXXXXX.tmp", path);
-
-	r = -1;
-	if (create_dirs(path) != 0)
+	r = create_dirs(path);
+	if (r < 0)
 		goto error_create_dirs;
 
-	fd = L_TFR(g_mkstemp_full(tmp_path, O_WRONLY | O_CREAT | O_TRUNC, mode));
-	if (fd == -1)
-		goto error_mkstemp_full;
-
-	r = L_TFR(write(fd, buffer, len));
-
-	L_TFR(close(fd));
-
-	if (r != (ssize_t) len) {
-		r = -1;
-		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.
-	 */
-	unlink(path);
-
-	/* conserve @r's value from 'write' */
-	if (link(tmp_path, path) == -1)
-		r = -1;
+	r = l_file_set_contents(path, buffer, len);
 
-error_write:
-	unlink(tmp_path);
-error_mkstemp_full:
 error_create_dirs:
-	l_free(tmp_path);
 	l_free(path);
 	return r;
 }