git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Consolidate SHA1 object file close
@ 2008-06-11  1:47 Linus Torvalds
  2008-06-11  7:42 ` Andreas Ericsson
  2008-06-11  7:43 ` Pierre Habouzit
  0 siblings, 2 replies; 11+ messages in thread
From: Linus Torvalds @ 2008-06-11  1:47 UTC (permalink / raw)
  To: Junio C Hamano, Git Mailing List; +Cc: Denis Bueno


This consolidates the common operations for closing the new temporary file 
that we have written, before we move it into place with the final name. 

There's some common code there (make it read-only and check for errors on 
close), but more importantly, this also gives a single place to add an 
fsync_or_die() call if we want to add a safe mode.

This was triggered due to Denis Bueno apparently twice being able to 
corrupt his git repository on OS X due to an unlucky combination of kernel 
crashes and a not-very-robust filesystem.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---

Junio, this is just the scaffolding, without the actual fsync_or_die call. 

I think it's a worthy place-holder regardless of whether we really want to 
do the fsync (whether conditionally with a config option or not, and 
whether there are more clever options like aio_fsync()).

 sha1_file.c |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/sha1_file.c b/sha1_file.c
index adcf37c..f311c79 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2105,6 +2105,15 @@ int hash_sha1_file(const void *buf, unsigned long len, const char *type,
 	return 0;
 }
 
+/* Finalize a file on disk, and close it. */
+static void close_sha1_file(int fd)
+{
+	/* For safe-mode, we could fsync_or_die(fd, "sha1 file") here */
+	fchmod(fd, 0444);
+	if (close(fd) != 0)
+		die("unable to write sha1 file");
+}
+
 static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
 			      void *buf, unsigned long len, time_t mtime)
 {
@@ -2170,9 +2179,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
 
 	if (write_buffer(fd, compressed, size) < 0)
 		die("unable to write sha1 file");
-	fchmod(fd, 0444);
-	if (close(fd))
-		die("unable to write sha1 file");
+	close_sha1_file(fd);
 	free(compressed);
 
 	if (mtime) {
@@ -2350,9 +2357,7 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
 	} while (1);
 	inflateEnd(&stream);
 
-	fchmod(local, 0444);
-	if (close(local) != 0)
-		die("unable to write sha1 file");
+	close_sha1_file(local);
 	SHA1_Final(real_sha1, &c);
 	if (ret != Z_STREAM_END) {
 		unlink(tmpfile);

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2008-06-12 16:01 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-11  1:47 Consolidate SHA1 object file close Linus Torvalds
2008-06-11  7:42 ` Andreas Ericsson
2008-06-11  7:43 ` Pierre Habouzit
2008-06-11 15:17   ` Linus Torvalds
2008-06-11 15:40     ` Pierre Habouzit
2008-06-11 17:25       ` Linus Torvalds
2008-06-11 17:46         ` Linus Torvalds
     [not found]         ` <alpine.LFD.1.10.0806111030580.3101@woody.linux-foundation.org>
2008-06-11 22:25           ` Pierre Habouzit
2008-06-11 23:03             ` Pierre Habouzit
2008-06-12 15:33             ` Linus Torvalds
2008-06-12 16:00               ` Pierre Habouzit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).