From: Denis Kenzior <denkenz@gmail.com>
To: ofono@ofono.org
Subject: Re: [patch 02/20] write_file: make transaction-safe
Date: Fri, 23 Jul 2010 16:57:31 -0500 [thread overview]
Message-ID: <4C4A104B.2080406@gmail.com> (raw)
In-Reply-To: <21ceadbaf2b2fc2479a381c123140d27ed7c36ba.1279918330.git.inaky.perez-gonzalez@intel.com>
[-- Attachment #1: Type: text/plain, Size: 3112 bytes --]
Hi Inaky,
On 07/23/2010 03:59 PM, Inaky Perez-Gonzalez wrote:
> From: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
>
> write_file(), as written wasn't transaction-safe; a crash bewtween a
> file being open and the buffer being written before a safe close would
> leave the file with a set of undetermined contents.
>
> Modified to the file is written to a temporary file name; once
> completed, it is renamed to the final name. This way, a crash in the
> middle doesn't leave half-baked files.
> ---
> src/storage.c | 42 +++++++++++++++++++++++++++++++-----------
> 1 files changed, 31 insertions(+), 11 deletions(-)
>
> diff --git a/src/storage.c b/src/storage.c
> index cac5835..c88a8c8 100644
> --- a/src/storage.c
> +++ b/src/storage.c
> @@ -98,11 +98,21 @@ ssize_t read_file(unsigned char *buffer, size_t len,
> return r;
> }
>
> +/*
> + * Write a buffer to a file in a transactionally safe form
> + *
> + * Given a buffer, write it to a file named after
> + * @path_fmt+args. However, to make sure the file contents are
> + * consistent (ie: a crash right after opening or during write()
> + * doesn't leave a file half baked), the contents are written to a
> + * file with a temporary name and when closed, it is renamed to the
> + * specified name (@path_fmt+args).
> + */
> ssize_t write_file(const unsigned char *buffer, size_t len, mode_t mode,
> const char *path_fmt, ...)
> {
> va_list ap;
> - char *path;
> + char *tmp_path, *path;
> ssize_t r;
> int fd;
>
> @@ -110,26 +120,36 @@ ssize_t write_file(const unsigned char *buffer, size_t len, mode_t mode,
> path = g_strdup_vprintf(path_fmt, ap);
> va_end(ap);
>
> - if (create_dirs(path, mode | S_IXUSR) != 0) {
> - g_free(path);
> - return -1;
> - }
> + tmp_path = g_strdup_printf("%s.XXXXXX.tmp", path);
>
> - fd = TFR(open(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
> - if (fd == -1) {
> - g_free(path);
> - return -1;
> - }
> + r = -1;
> + if (create_dirs(path, mode | S_IXUSR) != 0)
> + goto error_create_dirs;
Please do me a favor and add an empty line here.
> + fd = TFR(g_mkstemp_full(tmp_path, O_WRONLY | O_CREAT | O_TRUNC, mode));
> + if (fd == -1)
> + goto error_mkstemp_full;
>
> r = TFR(write(fd, buffer, len));
>
> TFR(close(fd));
>
> if (r != (ssize_t) len) {
> - unlink(path);
> 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. */
Please follow comment conventions per doc/coding-style.txt Section M2.
> + unlink(path);
There should be an empty line here per doc/coding-style.txt Section M1.
> + /* conserve @r's value from 'write' */
> + if (link(tmp_path, path) == -1)
> + r = -1;
Another empty line here (before and after if/while/do/for blocks)
> +error_write:
> + unlink(tmp_path);
> +error_mkstemp_full:
> +error_create_dirs:
> + g_free(tmp_path);
> g_free(path);
> return r;
> }
Regards,
-Denis
next prev parent reply other threads:[~2010-07-23 21:57 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-23 20:59 [patch 00/20] SMS D-Bus support and misc small patches Inaky Perez-Gonzalez
2010-07-23 20:59 ` [patch 01/20] bug.h: Add BUILD_BUG_ON() and friends for compile-time assert checking Inaky Perez-Gonzalez
2010-07-23 21:41 ` Denis Kenzior
2010-07-23 21:57 ` Inaky Perez-Gonzalez
2010-07-23 21:59 ` Denis Kenzior
2010-07-23 20:59 ` [patch 02/20] write_file: make transaction-safe Inaky Perez-Gonzalez
2010-07-23 21:57 ` Denis Kenzior [this message]
2010-07-23 22:31 ` Inaky Perez-Gonzalez
2010-07-23 20:59 ` [patch 03/20] manpage: explain debugging options to -d Inaky Perez-Gonzalez
2010-07-23 22:05 ` Denis Kenzior
2010-07-23 20:59 ` [patch 04/20] SMS: introduce message ID API Inaky Perez-Gonzalez
2010-07-27 0:10 ` Denis Kenzior
2010-07-23 20:59 ` [patch 05/20] introduce DECLARE_SMS_ADDR_STR() Inaky Perez-Gonzalez
2010-07-23 22:30 ` Denis Kenzior
2010-07-23 20:59 ` [patch 06/20] _assembly_encode_address: export and rename Inaky Perez-Gonzalez
2010-07-23 22:31 ` Denis Kenzior
2010-07-23 20:59 ` [patch 07/20] SMS: implement SHA256-based message IDs [incomplete] Inaky Perez-Gonzalez
2010-07-27 17:03 ` Denis Kenzior
2010-07-29 21:26 ` Inaky Perez-Gonzalez
2010-07-29 21:37 ` Denis Kenzior
2010-07-31 0:22 ` Inaky Perez-Gonzalez
2010-07-23 20:59 ` [patch 08/20] sms: document the org.ofono.SMSMessage D-Bus interface Inaky Perez-Gonzalez
2010-07-23 23:11 ` Denis Kenzior
2010-07-26 17:19 ` Inaky Perez-Gonzalez
2010-07-26 18:05 ` Denis Kenzior
2010-07-26 20:41 ` Inaky Perez-Gonzalez
2010-07-23 20:59 ` [patch 09/20] SMS: document handle_sms_status_report() Inaky Perez-Gonzalez
2010-07-23 20:59 ` [patch 10/20] sms_text_prepare: document @use_delivery_reports Inaky Perez-Gonzalez
2010-07-23 23:01 ` Denis Kenzior
2010-07-23 20:59 ` [patch 11/20] SMS: rename create_tx_queue_entry() to tx_queue_entry_new() Inaky Perez-Gonzalez
2010-07-23 23:02 ` Denis Kenzior
2010-07-26 20:49 ` Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 12/20] struct tx_queue_entry: add a destructor Inaky Perez-Gonzalez
2010-07-23 23:06 ` Denis Kenzior
2010-07-23 23:11 ` Inaky Perez-Gonzalez
2010-07-23 23:14 ` Denis Kenzior
2010-07-26 18:48 ` Inaky Perez-Gonzalez
2010-07-26 20:49 ` Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 13/20] SMS: encapsulate D-Bus specific data in 'struct sms_msg_dbus_data' Inaky Perez-Gonzalez
2010-07-27 17:08 ` Denis Kenzior
2010-07-29 21:47 ` Inaky Perez-Gonzalez
2010-07-29 22:17 ` Denis Kenzior
2010-07-29 23:23 ` Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 14/20] SMS: introduce bare state machine and transitions Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 15/20] SMS: introduce Wait-for-Status-Report state and infrastructure Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 16/20] SMS: introduce a state change callback for TX messages Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 17/20] SMS: export outgoing messages over D-Bus Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 18/20] SMS: send D-Bus SMS-MSG::PropertyChanged signals when message changes status Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 19/20] SMS: introduce sms_msg_cancel and its D-Bus wrapper Inaky Perez-Gonzalez
2010-07-27 17:16 ` Denis Kenzior
2010-07-30 23:12 ` Inaky Perez-Gonzalez
2010-07-23 21:00 ` [patch 20/20] SMS: Implement D-Bus SMS-MSG::GetProperties Inaky Perez-Gonzalez
2010-07-27 17:18 ` Denis Kenzior
2010-08-02 19:14 ` Inaky Perez-Gonzalez
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=4C4A104B.2080406@gmail.com \
--to=denkenz@gmail.com \
--cc=ofono@ofono.org \
/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