]>
Commit | Line | Data |
---|---|---|
c38138cd LT |
1 | /* |
2 | * csum-file.c | |
3 | * | |
4 | * Copyright (C) 2005 Linus Torvalds | |
5 | * | |
6 | * Simple file write infrastructure for writing SHA1-summed | |
7 | * files. Useful when you write a file that you want to be | |
8 | * able to verify hasn't been messed with afterwards. | |
9 | */ | |
10 | #include "cache.h" | |
2a128d63 | 11 | #include "progress.h" |
c38138cd LT |
12 | #include "csum-file.h" |
13 | ||
e782e12f | 14 | static void flush(struct sha1file *f, void * buf, unsigned int count) |
c38138cd | 15 | { |
c38138cd | 16 | for (;;) { |
1c15afb9 | 17 | int ret = xwrite(f->fd, buf, count); |
c38138cd | 18 | if (ret > 0) { |
218558af NP |
19 | f->total += ret; |
20 | display_throughput(f->tp, f->total); | |
1d7f171c | 21 | buf = (char *) buf + ret; |
c38138cd LT |
22 | count -= ret; |
23 | if (count) | |
24 | continue; | |
78b713a1 | 25 | return; |
c38138cd LT |
26 | } |
27 | if (!ret) | |
e1808845 | 28 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
e1808845 | 29 | die("sha1 file '%s' write error (%s)", f->name, strerror(errno)); |
c38138cd LT |
30 | } |
31 | } | |
32 | ||
838cd346 | 33 | void sha1flush(struct sha1file *f) |
c38138cd LT |
34 | { |
35 | unsigned offset = f->offset; | |
4c81b03e | 36 | |
c38138cd | 37 | if (offset) { |
9126f009 | 38 | git_SHA1_Update(&f->ctx, f->buffer, offset); |
e782e12f | 39 | flush(f, f->buffer, offset); |
f0215369 | 40 | f->offset = 0; |
c38138cd | 41 | } |
838cd346 NP |
42 | } |
43 | ||
44 | int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags) | |
45 | { | |
46 | int fd; | |
47 | ||
48 | sha1flush(f); | |
9126f009 | 49 | git_SHA1_Final(f->buffer, &f->ctx); |
ac0463ed NP |
50 | if (result) |
51 | hashcpy(result, f->buffer); | |
4c81b03e | 52 | if (flags & (CSUM_CLOSE | CSUM_FSYNC)) { |
7ba502c4 | 53 | /* write checksum and close fd */ |
e782e12f | 54 | flush(f, f->buffer, 20); |
4c81b03e LT |
55 | if (flags & CSUM_FSYNC) |
56 | fsync_or_die(f->fd, f->name); | |
7ba502c4 NP |
57 | if (close(f->fd)) |
58 | die("%s: sha1 file error on close (%s)", | |
59 | f->name, strerror(errno)); | |
60 | fd = 0; | |
61 | } else | |
62 | fd = f->fd; | |
7bf058f0 | 63 | free(f); |
7ba502c4 | 64 | return fd; |
c38138cd LT |
65 | } |
66 | ||
67 | int sha1write(struct sha1file *f, void *buf, unsigned int count) | |
68 | { | |
69 | while (count) { | |
70 | unsigned offset = f->offset; | |
71 | unsigned left = sizeof(f->buffer) - offset; | |
72 | unsigned nr = count > left ? left : count; | |
a8032d12 NP |
73 | void *data; |
74 | ||
75 | if (f->do_crc) | |
76 | f->crc32 = crc32(f->crc32, buf, nr); | |
77 | ||
78 | if (nr == sizeof(f->buffer)) { | |
79 | /* process full buffer directly without copy */ | |
80 | data = buf; | |
81 | } else { | |
82 | memcpy(f->buffer + offset, buf, nr); | |
83 | data = f->buffer; | |
84 | } | |
c38138cd | 85 | |
c38138cd LT |
86 | count -= nr; |
87 | offset += nr; | |
1d7f171c | 88 | buf = (char *) buf + nr; |
c38138cd LT |
89 | left -= nr; |
90 | if (!left) { | |
9126f009 | 91 | git_SHA1_Update(&f->ctx, data, offset); |
e782e12f | 92 | flush(f, data, offset); |
c38138cd LT |
93 | offset = 0; |
94 | } | |
95 | f->offset = offset; | |
96 | } | |
97 | return 0; | |
98 | } | |
99 | ||
4397f014 | 100 | struct sha1file *sha1fd(int fd, const char *name) |
2a128d63 NP |
101 | { |
102 | return sha1fd_throughput(fd, name, NULL); | |
103 | } | |
104 | ||
105 | struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp) | |
4397f014 | 106 | { |
ec640ed1 | 107 | struct sha1file *f = xmalloc(sizeof(*f)); |
4397f014 | 108 | f->fd = fd; |
4397f014 | 109 | f->offset = 0; |
218558af | 110 | f->total = 0; |
2a128d63 | 111 | f->tp = tp; |
ec640ed1 | 112 | f->name = name; |
78d1e84f | 113 | f->do_crc = 0; |
9126f009 | 114 | git_SHA1_Init(&f->ctx); |
4397f014 LT |
115 | return f; |
116 | } | |
117 | ||
78d1e84f NP |
118 | void crc32_begin(struct sha1file *f) |
119 | { | |
120 | f->crc32 = crc32(0, Z_NULL, 0); | |
121 | f->do_crc = 1; | |
122 | } | |
c38138cd | 123 | |
78d1e84f NP |
124 | uint32_t crc32_end(struct sha1file *f) |
125 | { | |
126 | f->do_crc = 0; | |
127 | return f->crc32; | |
128 | } |