提交 4c81b03e 编写于 作者: L Linus Torvalds 提交者: Junio C Hamano

Make pack creation always fsync() the result

This means that we can depend on packs always being stable on disk,
simplifying a lot of the object serialization worries.  And unlike loose
objects, serializing pack creation IO isn't going to be a performance
killer.
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 d2b3691b
...@@ -515,10 +515,12 @@ static void write_pack_file(void) ...@@ -515,10 +515,12 @@ static void write_pack_file(void)
* If so, rewrite it like in fast-import * If so, rewrite it like in fast-import
*/ */
if (pack_to_stdout || nr_written == nr_remaining) { if (pack_to_stdout || nr_written == nr_remaining) {
sha1close(f, sha1, 1); unsigned flags = pack_to_stdout ? CSUM_CLOSE : CSUM_FSYNC;
sha1close(f, sha1, flags);
} else { } else {
int fd = sha1close(f, NULL, 0); int fd = sha1close(f, NULL, 0);
fixup_pack_header_footer(fd, sha1, pack_tmp_name, nr_written); fixup_pack_header_footer(fd, sha1, pack_tmp_name, nr_written);
fsync_or_die(fd, pack_tmp_name);
close(fd); close(fd);
} }
......
...@@ -761,6 +761,7 @@ extern ssize_t write_in_full(int fd, const void *buf, size_t count); ...@@ -761,6 +761,7 @@ extern ssize_t write_in_full(int fd, const void *buf, size_t count);
extern void write_or_die(int fd, const void *buf, size_t count); extern void write_or_die(int fd, const void *buf, size_t count);
extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg); extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg); extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg);
extern void fsync_or_die(int fd, const char *);
/* pager.c */ /* pager.c */
extern void setup_pager(void); extern void setup_pager(void);
......
...@@ -32,21 +32,24 @@ static void sha1flush(struct sha1file *f, unsigned int count) ...@@ -32,21 +32,24 @@ static void sha1flush(struct sha1file *f, unsigned int count)
} }
} }
int sha1close(struct sha1file *f, unsigned char *result, int final) int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
{ {
int fd; int fd;
unsigned offset = f->offset; unsigned offset = f->offset;
if (offset) { if (offset) {
SHA1_Update(&f->ctx, f->buffer, offset); SHA1_Update(&f->ctx, f->buffer, offset);
sha1flush(f, offset); sha1flush(f, offset);
f->offset = 0; f->offset = 0;
} }
if (final) { if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
/* write checksum and close fd */ /* write checksum and close fd */
SHA1_Final(f->buffer, &f->ctx); SHA1_Final(f->buffer, &f->ctx);
if (result) if (result)
hashcpy(result, f->buffer); hashcpy(result, f->buffer);
sha1flush(f, 20); sha1flush(f, 20);
if (flags & CSUM_FSYNC)
fsync_or_die(f->fd, f->name);
if (close(f->fd)) if (close(f->fd))
die("%s: sha1 file error on close (%s)", die("%s: sha1 file error on close (%s)",
f->name, strerror(errno)); f->name, strerror(errno));
......
...@@ -16,9 +16,13 @@ struct sha1file { ...@@ -16,9 +16,13 @@ struct sha1file {
unsigned char buffer[8192]; unsigned char buffer[8192];
}; };
/* sha1close flags */
#define CSUM_CLOSE 1
#define CSUM_FSYNC 2
extern struct sha1file *sha1fd(int fd, const char *name); extern struct sha1file *sha1fd(int fd, const char *name);
extern struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp); extern struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp);
extern int sha1close(struct sha1file *, unsigned char *, int); extern int sha1close(struct sha1file *, unsigned char *, unsigned int);
extern int sha1write(struct sha1file *, void *, unsigned int); extern int sha1write(struct sha1file *, void *, unsigned int);
extern void crc32_begin(struct sha1file *); extern void crc32_begin(struct sha1file *);
extern uint32_t crc32_end(struct sha1file *); extern uint32_t crc32_end(struct sha1file *);
......
...@@ -890,7 +890,7 @@ static char *create_index(void) ...@@ -890,7 +890,7 @@ static char *create_index(void)
SHA1_Update(&ctx, (*c)->sha1, 20); SHA1_Update(&ctx, (*c)->sha1, 20);
} }
sha1write(f, pack_data->sha1, sizeof(pack_data->sha1)); sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
sha1close(f, NULL, 1); sha1close(f, NULL, CSUM_FSYNC);
free(idx); free(idx);
SHA1_Final(pack_data->sha1, &ctx); SHA1_Final(pack_data->sha1, &ctx);
return tmpfile; return tmpfile;
......
...@@ -694,6 +694,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name, ...@@ -694,6 +694,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
if (!from_stdin) { if (!from_stdin) {
close(input_fd); close(input_fd);
} else { } else {
fsync_or_die(output_fd, curr_pack_name);
err = close(output_fd); err = close(output_fd);
if (err) if (err)
die("error while closing pack file: %s", strerror(errno)); die("error while closing pack file: %s", strerror(errno));
......
...@@ -139,7 +139,7 @@ char *write_idx_file(char *index_name, struct pack_idx_entry **objects, ...@@ -139,7 +139,7 @@ char *write_idx_file(char *index_name, struct pack_idx_entry **objects,
} }
sha1write(f, sha1, 20); sha1write(f, sha1, 20);
sha1close(f, NULL, 1); sha1close(f, NULL, CSUM_FSYNC);
SHA1_Final(sha1, &ctx); SHA1_Final(sha1, &ctx);
return index_name; return index_name;
} }
......
...@@ -78,6 +78,13 @@ ssize_t write_in_full(int fd, const void *buf, size_t count) ...@@ -78,6 +78,13 @@ ssize_t write_in_full(int fd, const void *buf, size_t count)
return total; return total;
} }
void fsync_or_die(int fd, const char *msg)
{
if (fsync(fd) < 0) {
die("%s: fsync error (%s)", msg, strerror(errno));
}
}
void write_or_die(int fd, const void *buf, size_t count) void write_or_die(int fd, const void *buf, size_t count)
{ {
if (write_in_full(fd, buf, count) < 0) { if (write_in_full(fd, buf, count) < 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册