[3047] in BarnOwl Developers
Re: [PATCH] owl_util_file_deleteline should not error on non-existent
daemon@ATHENA.MIT.EDU (David A Benjamin)
Tue May 17 12:04:48 2011
Date: Tue, 17 May 2011 12:04:45 -0400 (EDT)
From: David A Benjamin <davidben@MIT.EDU>
To: Jason Gross <jgross@mit.edu>
cc: barnowl-dev@mit.edu
In-Reply-To: <1305647179-31082-1-git-send-email-jgross@mit.edu>
I think this is the wrong place to add the check. If I ask you to delete a
line from a file, and you find that the file doesn't exist, that is
definitely grounds for a failure.
Better would be to either make owl_util_file_deleteline report what kind
of error (errno should work here, but you have to be careful to re-set it
because owl_function_error may set errno) so the caller can interpret it
correctly or to have startup check before calling
owl_util_file_deleteline.
David
On Tue, 17 May 2011, Jason Gross wrote:
> This fixes trac #175: '"startup" command produces error when run for the
> first time'. The startup command deletes duplicate lines, and thus
> errored when no file existed. The purpose of owl_util_file_deleteline
> is to make sure that the given line is no longer in the file, so there
> seems to be no harm in returning 0 (meaning no lines were deleted) on
> the file not existing.
> ---
> util.c | 2 ++
> 1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/util.c b/util.c
> index 82f36ef..44899f0 100644
> --- a/util.c
> +++ b/util.c
> @@ -423,6 +423,7 @@ gchar *owl_util_recursive_resolve_link(const char *filename)
> * case-insensitive.
> *
> * Returns the number of lines removed on success. Returns -1 on failure.
> + * Returns 0 if the file does not exist; the line wasn't present.
> */
> int owl_util_file_deleteline(const char *filename, const char *line, int backup)
> {
> @@ -433,6 +434,7 @@ int owl_util_file_deleteline(const char *filename, const char *line, int backup)
> int numremoved = 0;
>
> if ((old = fopen(filename, "r")) == NULL) {
> + if (errno == ENOENT) return 0;
> owl_function_error("Cannot open %s (for reading): %s",
> filename, strerror(errno));
> return -1;
> --
> 1.7.2.3
>
>