Skip to content

Commit e9039dd

Browse files
torvaldsgitster
authored andcommitted
Consolidate SHA1 object file close
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent cdf222f commit e9039dd

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

sha1_file.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,15 @@ int hash_sha1_file(const void *buf, unsigned long len, const char *type,
21052105
return 0;
21062106
}
21072107

2108+
/* Finalize a file on disk, and close it. */
2109+
static void close_sha1_file(int fd)
2110+
{
2111+
/* For safe-mode, we could fsync_or_die(fd, "sha1 file") here */
2112+
fchmod(fd, 0444);
2113+
if (close(fd) != 0)
2114+
die("unable to write sha1 file");
2115+
}
2116+
21082117
static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
21092118
void *buf, unsigned long len, time_t mtime)
21102119
{
@@ -2170,9 +2179,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
21702179

21712180
if (write_buffer(fd, compressed, size) < 0)
21722181
die("unable to write sha1 file");
2173-
fchmod(fd, 0444);
2174-
if (close(fd))
2175-
die("unable to write sha1 file");
2182+
close_sha1_file(fd);
21762183
free(compressed);
21772184

21782185
if (mtime) {
@@ -2350,9 +2357,7 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
23502357
} while (1);
23512358
inflateEnd(&stream);
23522359

2353-
fchmod(local, 0444);
2354-
if (close(local) != 0)
2355-
die("unable to write sha1 file");
2360+
close_sha1_file(local);
23562361
SHA1_Final(real_sha1, &c);
23572362
if (ret != Z_STREAM_END) {
23582363
unlink(tmpfile);

0 commit comments

Comments
 (0)