@matheusbsbs / matheus-bagliano / commits / 838cd34664
Fork 0

fix pread()'s short read in index-pack

Since v1.6.0.2~13^2~ the completion of a thin pack uses sha1write() for its ability to compute a SHA1 on the written data. This also provides data buffering which, along with commit 92392b4a45, will confuse pread() whenever an appended object is 1) freed due to memory pressure because of the depth-first delta processing, and 2) needed again because it has many delta children, and 3) its data is still buffered by sha1write(). Let's fix the issue by simply forcing cached data out when such an object is written so it can be pread()'d at leisure. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Nicolas Pitre committed Oct 9, 2008 at 22:08 UTC 838cd34664422863096f1a089f779bed1f00edf6
3 files changed +14 -6
csum-file.c
+12 -6
@@ -11,7 +11,7 @@
11 #include "progress.h"
12 #include "csum-file.h"
13
14 -static void sha1flush(struct sha1file *f, unsigned int count)
14 +static void flush(struct sha1file *f, unsigned int count)
15 {
16 void *buf = f->buffer;
17
@@ -32,22 +32,28 @@ static void sha1flush(struct sha1file *f, unsigned int count)
32 }
33 }
34
35 -int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
35 +void sha1flush(struct sha1file *f)
36 {
37 - int fd;
37 unsigned offset = f->offset;
38
39 if (offset) {
40 SHA1_Update(&f->ctx, f->buffer, offset);
42 - sha1flush(f, offset);
41 + flush(f, offset);
42 f->offset = 0;
43 }
44 +}
45 +
46 +int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
47 +{
48 + int fd;
49 +
50 + sha1flush(f);
51 SHA1_Final(f->buffer, &f->ctx);
52 if (result)
53 hashcpy(result, f->buffer);
54 if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
55 /* write checksum and close fd */
50 - sha1flush(f, 20);
56 + flush(f, 20);
57 if (flags & CSUM_FSYNC)
58 fsync_or_die(f->fd, f->name);
59 if (close(f->fd))
@@ -76,7 +82,7 @@ int sha1write(struct sha1file *f, void *buf, unsigned int count)
82 left -= nr;
83 if (!left) {
84 SHA1_Update(&f->ctx, f->buffer, offset);
79 - sha1flush(f, offset);
85 + flush(f, offset);
86 offset = 0;
87 }
88 f->offset = offset;
csum-file.h
+1
@@ -24,6 +24,7 @@ extern struct sha1file *sha1fd(int fd, const char *name);
24 extern struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp);
25 extern int sha1close(struct sha1file *, unsigned char *, unsigned int);
26 extern int sha1write(struct sha1file *, void *, unsigned int);
27 +extern void sha1flush(struct sha1file *f);
28 extern void crc32_begin(struct sha1file *);
29 extern uint32_t crc32_end(struct sha1file *);
30
index-pack.c
+1
@@ -707,6 +707,7 @@ static struct object_entry *append_obj_to_pack(struct sha1file *f,
707 obj[1].idx.offset = obj[0].idx.offset + n;
708 obj[1].idx.offset += write_compressed(f, buf, size);
709 obj[0].idx.crc32 = crc32_end(f);
710 + sha1flush(f);
711 hashcpy(obj->idx.sha1, sha1);
712 return obj;
713 }