Code

Merge branch 'lt/pack-sync'
authorJunio C Hamano <gitster@pobox.com>
Mon, 2 Jun 2008 06:43:21 +0000 (23:43 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 2 Jun 2008 06:43:21 +0000 (23:43 -0700)
* lt/pack-sync:
  Remove now unnecessary 'sync()' calls
  Make pack creation always fsync() the result

builtin-pack-objects.c
builtin-prune-packed.c
builtin-prune.c
cache.h
csum-file.c
csum-file.h
fast-import.c
git-repack.sh
index-pack.c
pack-write.c
write_or_die.c

index 70d2f5d4161a0bb62f4734be97d0a8dd2a5098dc..447d492dbbfb578b8903293fa1d2a988eecfefac 100644 (file)
@@ -514,11 +514,14 @@ static void write_pack_file(void)
                 * Did we write the wrong # entries in the header?
                 * If so, rewrite it like in fast-import
                 */
-               if (pack_to_stdout || nr_written == nr_remaining) {
-                       sha1close(f, sha1, 1);
+               if (pack_to_stdout) {
+                       sha1close(f, sha1, CSUM_CLOSE);
+               } else if (nr_written == nr_remaining) {
+                       sha1close(f, sha1, CSUM_FSYNC);
                } else {
                        int fd = sha1close(f, NULL, 0);
                        fixup_pack_header_footer(fd, sha1, pack_tmp_name, nr_written);
+                       fsync_or_die(fd, pack_tmp_name);
                        close(fd);
                }
 
index 23faf3129fb65ec5592c5021d661498143d4f608..241afbbab54cef58361b0a8284fe1cefcd5dc2a8 100644 (file)
@@ -85,7 +85,6 @@ int cmd_prune_packed(int argc, const char **argv, const char *prefix)
                /* Handle arguments here .. */
                usage(prune_packed_usage);
        }
-       sync();
        prune_packed_objects(opts);
        return 0;
 }
index 25f9304b829c9074e8a034eba5e34c38794058a4..bd3d2f67f3eb54cedb1a6c18582c7e10aa3f7bc6 100644 (file)
@@ -156,7 +156,6 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
        mark_reachable_objects(&revs, 1);
        prune_object_dir(get_object_directory());
 
-       sync();
        prune_packed_objects(show_only);
        remove_temporary_files();
        return 0;
diff --git a/cache.h b/cache.h
index eab1a172fe4b2a9d735dc480879fd1034da7942a..092a997b073090e14feaa4825e821af040101eb6 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -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 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 void fsync_or_die(int fd, const char *);
 
 /* pager.c */
 extern void setup_pager(void);
index 9728a9954129246b96713d2f3b8dbd52541c416b..ace64f165e4a01fb99892e9b89e7df791a7f4ca1 100644 (file)
@@ -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;
        unsigned offset = f->offset;
+
        if (offset) {
                SHA1_Update(&f->ctx, f->buffer, offset);
                sha1flush(f, offset);
                f->offset = 0;
        }
-       if (final) {
+       if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
                /* write checksum and close fd */
                SHA1_Final(f->buffer, &f->ctx);
                if (result)
                        hashcpy(result, f->buffer);
                sha1flush(f, 20);
+               if (flags & CSUM_FSYNC)
+                       fsync_or_die(f->fd, f->name);
                if (close(f->fd))
                        die("%s: sha1 file error on close (%s)",
                            f->name, strerror(errno));
index 1af76562f31da89e4cd2592079edb9c6a45736e3..72c9487f4fd9fcab5e02fc2dc6afd3cb7f9c036a 100644 (file)
@@ -16,9 +16,13 @@ struct sha1file {
        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_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 void crc32_begin(struct sha1file *);
 extern uint32_t crc32_end(struct sha1file *);
index 93119bbd9484504d19f069a5c0038fb1e2f40a26..e72b28679410155cb43968d315f02588e89fd5f1 100644 (file)
@@ -890,7 +890,7 @@ static char *create_index(void)
                SHA1_Update(&ctx, (*c)->sha1, 20);
        }
        sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
-       sha1close(f, NULL, 1);
+       sha1close(f, NULL, CSUM_FSYNC);
        free(idx);
        SHA1_Final(pack_data->sha1, &ctx);
        return tmpfile;
index 10f735cff561c6606f5c2bd3f9feec1d6f6ae52e..072d1b40f7d71aa731c90e9af158a0fe5cf89fde 100755 (executable)
@@ -125,7 +125,6 @@ then
        # We know $existing are all redundant.
        if [ -n "$existing" ]
        then
-               sync
                ( cd "$PACKDIR" &&
                  for e in $existing
                  do
index aaba9443ccbab1315e67c0e6bedaa82afe5464fc..5ac91baf98615d7e494550155c1c4d7665ed2dcd 100644 (file)
@@ -694,6 +694,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
        if (!from_stdin) {
                close(input_fd);
        } else {
+               fsync_or_die(output_fd, curr_pack_name);
                err = close(output_fd);
                if (err)
                        die("error while closing pack file: %s", strerror(errno));
index c66c8af725f6d37b4edaeab75eebac7a387f4922..f52cabe83829289dee7e44673b59a02db38918a5 100644 (file)
@@ -139,7 +139,7 @@ char *write_idx_file(char *index_name, struct pack_idx_entry **objects,
        }
 
        sha1write(f, sha1, 20);
-       sha1close(f, NULL, 1);
+       sha1close(f, NULL, CSUM_FSYNC);
        SHA1_Final(sha1, &ctx);
        return index_name;
 }
index 32f99140205f8969d8a884b3cf9448eec3f1dd16..630be4cb9414b1686a5ad86a4d5166f2828096f1 100644 (file)
@@ -78,6 +78,13 @@ ssize_t write_in_full(int fd, const void *buf, size_t count)
        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)
 {
        if (write_in_full(fd, buf, count) < 0) {