Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Patrick Palka <patrick@parcs.ath.cx>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Append to input history file instead of overwriting it
Date: Mon, 12 Jan 2015 19:05:00 -0000	[thread overview]
Message-ID: <54B41B03.5030306@redhat.com> (raw)
In-Reply-To: <1420915570-5605-1-git-send-email-patrick@parcs.ath.cx>

On 01/10/2015 06:46 PM, Patrick Palka wrote:

> +/* Safely append new history entries to the history file in a corruption-free
> +   way using an intermediate local history file.  */
> +
> +static void
> +gdb_safe_append_history (void)
> +{
> +  int ret, saved_errno;
> +  char *local_history_filename;
> +
> +  local_history_filename = xstrprintf ("%s.%d", history_filename, getpid ());

IMO just appending a number is not sufficiently distinct
from what a user might reasonably want to name alternate history
files.  How about picking a more obscure name, like what
I had originally suggested:

  local_history_filename = xstrprintf ("%s-gdb%d~", history_filename, getpid ());

?

> +
> +  ret = rename (history_filename, local_history_filename);
> +  saved_errno = errno;
> +  if (ret < 0 && saved_errno == ENOENT)
> +    {
> +      /* If the rename failed with ENOENT then either the global history file
> +	 never existed in the first place or another GDB process is currently
> +	 appending to it (and has thus temporarily renamed it).  Since we can't
> +	 distinguish between these two cases, we have to conservatively assume
> +	 the first case and therefore must write out (not append) our known
> +	 history to our local history file and try to move it back anyway.
> +	 Otherwise a global history file would never get created!  */
> +      write_history (local_history_filename);
> +    }
> +  else if (ret < 0)
> +    {
> +      warning (_("Could not rename %s to %s: %s"),
> +	       history_filename, local_history_filename, strerror (saved_errno));

use safe_strerror.  Watch out for line too long.


> +      goto out;

Let's avoid gotos when simple enough.  In this case, seems to
me that to skip the second rename call, we only need to move
the "else if" and "else" within a parent "else" branch.

Also, please use a cleanup instead of the xfree at the end:

  local_history_filename = xstrprintf ("%s-gdb%d~", history_filename, getpid ());
  old_chain = make_cleanup (xfree, local_history_filename);
  ...
  do_cleanups (old_chain);

> +    }
> +  else
> +    {
> +      append_history (command_count, local_history_filename);
> +      history_truncate_file (local_history_filename, history_max_entries);
> +    }
> +
> +  ret = rename (local_history_filename, history_filename);
> +  saved_errno = errno;
> +  if (ret < 0 && saved_errno != EEXIST)
> +    warning (_("Could not rename %s to %s: %s"),
> +	     local_history_filename, history_filename, strerror (saved_errno));

safe_strerror.

> +
> +out:
> +  xfree (local_history_filename);
> +}
> +

This is OK with these changes.

Thanks!

-- 
Pedro Alves


  reply	other threads:[~2015-01-12 19:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-29  2:01 Patrick Palka
2014-12-01 20:50 ` Sergio Durigan Junior
2014-12-04 16:18 ` Pedro Alves
2014-12-05  0:19   ` Patrick Palka
2014-12-05 10:45     ` Pedro Alves
2014-12-05 14:11       ` Patrick Palka
2014-12-10 16:54         ` Pedro Alves
2014-12-10 17:17           ` Eli Zaretskii
2014-12-10 17:23             ` Pedro Alves
2015-01-10 14:10           ` Patrick Palka
2015-01-10 15:16             ` Patrick Palka
2015-01-10 15:18               ` Patrick Palka
2015-01-10 15:39                 ` Eli Zaretskii
2015-01-10 15:48                   ` Patrick Palka
2015-01-10 16:03                     ` Eli Zaretskii
2015-01-10 16:18                       ` Patrick Palka
2015-01-10 16:41                         ` Eli Zaretskii
2015-01-10 18:17                           ` Patrick Palka
2015-01-10 18:46                             ` Patrick Palka
2015-01-12 19:05                               ` Pedro Alves [this message]
2015-01-12 22:56                                 ` Patrick Palka

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=54B41B03.5030306@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=patrick@parcs.ath.cx \
    /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