From: Jonathan Nieder <jrnieder@gmail.com>
To: Stefan Beller <sbeller@google.com>
Cc: Junio C Hamano <gitster@pobox.com>,
git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>,
Jeff King <peff@peff.net>
Subject: [PATCH 04/14] hold_lock_file_for_append: pass error message back through a strbuf
Date: Tue, 2 Dec 2014 21:14:59 -0800 [thread overview]
Message-ID: <20141203051459.GN6527@google.com> (raw)
In-Reply-To: <20141203050217.GJ6527@google.com>
This way, the code does not need to carefully safeguard errno to allow
callers to print a reasonable error message when they choose to do
some cleanup before die()-ing.
Fixes a bug waiting to happen where copy_fd would clobber the errno
passed back via hold_lock_file_for_append from read() or write() when
flags did not contain LOCK_DIE_ON_ERROR. Luckily the only caller uses
flags == LOCK_DIE_ON_ERROR, avoiding that bug in practice.
Reported-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
The advertised bugfix.
lockfile.c | 29 ++++++++++-------------------
lockfile.h | 3 ++-
sha1_file.c | 7 ++++++-
3 files changed, 18 insertions(+), 21 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index b3020f3..8685c68 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -179,45 +179,36 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
return fd;
}
-int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
+int hold_lock_file_for_append(struct lock_file *lk, const char *path,
+ int flags, struct strbuf *err)
{
int fd, orig_fd;
- struct strbuf err = STRBUF_INIT;
+
+ assert(!(flags & LOCK_DIE_ON_ERROR));
+ assert(err && !err->len);
fd = lock_file(lk, path, flags);
if (fd < 0) {
- if (flags & LOCK_DIE_ON_ERROR)
- unable_to_lock_die(path, errno);
+ unable_to_lock_message(path, errno, err);
return fd;
}
orig_fd = open(path, O_RDONLY);
if (orig_fd < 0) {
if (errno != ENOENT) {
- int save_errno = errno;
-
- if (flags & LOCK_DIE_ON_ERROR)
- die("cannot open '%s' for copying", path);
+ strbuf_addf(err, "cannot open '%s' for copying: %s",
+ path, strerror(errno));
rollback_lock_file(lk);
- error("cannot open '%s' for copying", path);
- errno = save_errno;
return -1;
}
- } else if (copy_fd(orig_fd, fd, &err)) {
- int save_errno = errno;
-
- error("copy-fd: %s", err.buf);
- strbuf_release(&err);
- if (flags & LOCK_DIE_ON_ERROR)
- exit(128);
+ } else if (copy_fd(orig_fd, fd, err)) {
+ strbuf_prefixf(err, "cannot copy '%s': ", path);
close(orig_fd);
rollback_lock_file(lk);
- errno = save_errno;
return -1;
} else {
close(orig_fd);
}
- strbuf_release(&err);
return fd;
}
diff --git a/lockfile.h b/lockfile.h
index cd2ec95..ca36a1d 100644
--- a/lockfile.h
+++ b/lockfile.h
@@ -75,7 +75,8 @@ extern void unable_to_lock_message(const char *path, int err,
struct strbuf *buf);
extern NORETURN void unable_to_lock_die(const char *path, int err);
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
-extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
+extern int hold_lock_file_for_append(struct lock_file *, const char *path,
+ int, struct strbuf *err);
extern FILE *fdopen_lock_file(struct lock_file *, const char *mode);
extern char *get_locked_file_path(struct lock_file *);
extern int commit_lock_file_to(struct lock_file *, const char *path);
diff --git a/sha1_file.c b/sha1_file.c
index e1945e2..6c0ab3b 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -406,17 +406,22 @@ void add_to_alternates_file(const char *reference)
struct lock_file *lock;
int fd;
char *alt;
+ struct strbuf err = STRBUF_INIT;
lock = xcalloc(1, sizeof(*lock));
fd = hold_lock_file_for_append(lock, mkpath("%s/info/alternates",
get_object_directory()),
- LOCK_DIE_ON_ERROR);
+ 0, &err);
+ if (fd < 0)
+ die("%s", err.buf);
alt = mkpath("%s\n", reference);
write_or_die(fd, alt, strlen(alt));
if (commit_lock_file(lock))
die("could not close alternates file");
if (alt_odb_tail)
link_alt_odb_entries(alt, strlen(alt), '\n', NULL, 0);
+
+ strbuf_release(&err);
}
int foreach_alt_odb(alt_odb_fn fn, void *cb)
next prev parent reply other threads:[~2014-12-03 5:15 UTC|newest]
Thread overview: 83+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-17 22:14 [PATCH] copy.c: make copy_fd preserve meaningful errno Stefan Beller
2014-11-17 23:08 ` Junio C Hamano
2014-11-17 23:13 ` Stefan Beller
2014-11-17 23:35 ` Jonathan Nieder
2014-11-18 0:18 ` Stefan Beller
2014-11-18 0:48 ` Jonathan Nieder
2014-11-18 1:01 ` Stefan Beller
2014-12-03 5:02 ` [PATCH 0/14] " Jonathan Nieder
2014-12-03 5:10 ` [PATCH 01/14] strbuf: introduce strbuf_prefixf() Jonathan Nieder
2014-12-03 20:10 ` Eric Sunshine
2014-12-03 20:14 ` Jonathan Nieder
2014-12-03 21:45 ` Junio C Hamano
2014-12-03 21:59 ` Jonathan Nieder
2014-12-03 22:09 ` Jonathan Nieder
2014-12-03 22:40 ` Junio C Hamano
2014-12-03 5:12 ` [PATCH 02/14] add_to_alternates_file: respect GIT_OBJECT_DIRECTORY Jonathan Nieder
2014-12-03 18:53 ` Junio C Hamano
2014-12-03 5:13 ` [PATCH 03/14] copy_fd: pass error message back through a strbuf Jonathan Nieder
2014-12-03 19:01 ` Junio C Hamano
2014-12-03 20:28 ` Jonathan Nieder
2014-12-03 21:00 ` Jeff King
2014-12-03 21:38 ` Jonathan Nieder
2014-12-04 7:59 ` Jeff King
2014-12-04 8:36 ` Stefan Beller
2014-12-04 9:04 ` Jeff King
2014-12-10 17:02 ` Michael Haggerty
2014-12-10 19:00 ` Junio C Hamano
2014-12-10 19:14 ` Jeff King
2014-12-04 3:01 ` [PATCH/RFC] doc: document error handling functions and conventions (Re: [PATCH 03/14] copy_fd: pass error message back through a strbuf) Jonathan Nieder
2014-12-04 23:27 ` Junio C Hamano
2014-12-04 23:37 ` Jonathan Nieder
2014-12-04 23:41 ` Jonathan Nieder
2014-12-04 23:44 ` Jeff King
2014-12-04 23:52 ` Junio C Hamano
2014-12-05 0:01 ` Jeff King
2014-12-05 18:00 ` Junio C Hamano
2014-12-07 10:07 ` Jeff King
2014-12-09 18:43 ` Junio C Hamano
2014-12-09 18:49 ` Jeff King
2015-02-12 23:08 ` Junio C Hamano
2015-02-17 15:50 ` Michael Haggerty
2015-02-17 16:03 ` Junio C Hamano
2015-02-17 16:05 ` Jeff King
2015-02-17 22:46 ` Junio C Hamano
2014-12-03 20:02 ` [PATCH 03/14] copy_fd: pass error message back through a strbuf Junio C Hamano
2014-12-03 20:18 ` Jonathan Nieder
2014-12-03 21:43 ` Junio C Hamano
2014-12-03 21:51 ` Jonathan Nieder
2014-12-03 20:20 ` Stefan Beller
2014-12-03 5:14 ` Jonathan Nieder [this message]
2014-12-03 6:09 ` [PATCH 04/14] hold_lock_file_for_append: " Torsten Bögershausen
2014-12-03 7:04 ` Jonathan Nieder
2014-12-03 5:16 ` [PATCH 05/14] lock_packed_refs: " Jonathan Nieder
2014-12-03 5:19 ` [PATCH 06/14] lockfile: introduce flag for locks outside .git Jonathan Nieder
2014-12-03 23:13 ` Junio C Hamano
2014-12-03 23:24 ` Jonathan Nieder
2014-12-03 23:25 ` Junio C Hamano
2014-12-03 23:29 ` Jonathan Nieder
2014-12-03 23:38 ` Junio C Hamano
2014-12-03 23:41 ` Jonathan Nieder
2014-12-03 23:43 ` Junio C Hamano
2014-12-03 23:57 ` Jeff King
2014-12-04 5:51 ` Junio C Hamano
2014-12-04 17:56 ` Jonathan Nieder
2014-12-03 5:19 ` [PATCH 07/14] fast-import: use message from lockfile API when writing marks fails Jonathan Nieder
2014-12-03 5:20 ` [PATCH 08/14] credentials: use message from lockfile API when locking ~/.git-credentials fails Jonathan Nieder
2014-12-03 5:21 ` [PATCH 09/14] config: use message from lockfile API when locking config file fails Jonathan Nieder
2014-12-03 19:59 ` Junio C Hamano
2014-12-03 20:16 ` Jonathan Nieder
2014-12-03 5:22 ` [PATCH 10/14] rerere: error out on autoupdate failure Jonathan Nieder
2014-12-03 5:25 ` [PATCH 11/14] hold_locked_index: pass error message back through a strbuf Jonathan Nieder
2014-12-03 5:26 ` [PATCH 12/14] hold_lock_file_for_update: " Jonathan Nieder
2014-12-03 18:53 ` Jonathan Nieder
2014-12-03 5:26 ` [PATCH 13/14] lockfile: remove unused function 'unable_to_lock_die' Jonathan Nieder
2014-12-03 5:27 ` [PATCH 14/14] lockfile: make 'unable_to_lock_message' private Jonathan Nieder
2014-12-03 20:42 ` Stefan Beller
2014-11-18 16:32 ` [PATCH] copy.c: make copy_fd preserve meaningful errno Junio C Hamano
2014-11-18 17:08 ` Junio C Hamano
2014-11-21 9:14 ` Michael Haggerty
2014-11-21 9:17 ` Michael Haggerty
2014-11-21 17:48 ` Junio C Hamano
2014-11-21 17:54 ` Jeff King
2014-11-21 18:31 ` Junio C Hamano
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=20141203051459.GN6527@google.com \
--to=jrnieder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mhagger@alum.mit.edu \
--cc=peff@peff.net \
--cc=sbeller@google.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.