Open Source Telephony
 help / color / mirror / Atom feed
From: Denis Kenzior <denkenz@gmail.com>
To: ofono@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH v2 14/18] storage: use l_file_set_contents
Date: Fri,  2 Feb 2024 16:53:46 -0600	[thread overview]
Message-ID: <20240202225405.993792-14-denkenz@gmail.com> (raw)
In-Reply-To: <20240202225405.993792-1-denkenz@gmail.com>

Use ell convenience function to set the file contents.
---
 src/storage.c | 41 ++++++-----------------------------------
 1 file changed, 6 insertions(+), 35 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 5b12c69ba6dd..00469f48510c 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
@@ -120,50 +121,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;
-	static const mode_t mode = 0600;
+	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;
 }
-- 
2.43.0


  parent reply	other threads:[~2024-02-02 22:55 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-02 22:53 [PATCH v2 01/18] umlrunner: Also mount /var/lib as tmpfs Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 02/18] build: Only enable backtrace(3) in maintainer mode Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 03/18] build: Bring in more ell classes Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 04/18] build: Enable _auto_ syntax Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 05/18] provisiondb: Remove some duplicate MCCMNC entries Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 06/18] storage: Introduce storage_get_file_path() Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 07/18] storage: Convert g_strdup_* use to l_strdup_* Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 08/18] common: Drop GLib use from gprs_auth_proto_to_string Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 09/18] common: Drop GLib use from gprs_proto_to_string Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 10/18] storage: Remove mode parameter Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 11/18] storage: Use l_malloc Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 12/18] storage: Remove mode argument Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 13/18] storage: Use void * instead of unsigned char * Denis Kenzior
2024-02-02 22:53 ` Denis Kenzior [this message]
2024-02-02 22:53 ` [PATCH v2 15/18] lte: Refactor lte settings management Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 16/18] lte: Add provisioning support Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 17/18] phonesim: Add lte atom Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 18/18] lte: Write provisioned info to disk Denis Kenzior
2024-02-02 23:30 ` [PATCH v2 01/18] umlrunner: Also mount /var/lib as tmpfs patchwork-bot+ofono

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=20240202225405.993792-14-denkenz@gmail.com \
    --to=denkenz@gmail.com \
    --cc=ofono@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