]>
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 | ||
98a3beab | 14 | static void flush(struct hashfile *f, const void *buf, unsigned int count) |
c38138cd | 15 | { |
e337a04d JH |
16 | if (0 <= f->check_fd && count) { |
17 | unsigned char check_buffer[8192]; | |
18 | ssize_t ret = read_in_full(f->check_fd, check_buffer, count); | |
19 | ||
20 | if (ret < 0) | |
21 | die_errno("%s: sha1 file read error", f->name); | |
61d36330 | 22 | if (ret != count) |
e337a04d JH |
23 | die("%s: sha1 file truncated", f->name); |
24 | if (memcmp(buf, check_buffer, count)) | |
25 | die("sha1 file '%s' validation error", f->name); | |
26 | } | |
27 | ||
c38138cd | 28 | for (;;) { |
1c15afb9 | 29 | int ret = xwrite(f->fd, buf, count); |
c38138cd | 30 | if (ret > 0) { |
218558af NP |
31 | f->total += ret; |
32 | display_throughput(f->tp, f->total); | |
1d7f171c | 33 | buf = (char *) buf + ret; |
c38138cd LT |
34 | count -= ret; |
35 | if (count) | |
36 | continue; | |
78b713a1 | 37 | return; |
c38138cd LT |
38 | } |
39 | if (!ret) | |
e1808845 | 40 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
d824cbba | 41 | die_errno("sha1 file '%s' write error", f->name); |
c38138cd LT |
42 | } |
43 | } | |
44 | ||
98a3beab | 45 | void hashflush(struct hashfile *f) |
c38138cd LT |
46 | { |
47 | unsigned offset = f->offset; | |
4c81b03e | 48 | |
c38138cd | 49 | if (offset) { |
4d273500 | 50 | the_hash_algo->update_fn(&f->ctx, f->buffer, offset); |
e782e12f | 51 | flush(f, f->buffer, offset); |
f0215369 | 52 | f->offset = 0; |
c38138cd | 53 | } |
838cd346 NP |
54 | } |
55 | ||
f2af9f5e | 56 | int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags) |
838cd346 NP |
57 | { |
58 | int fd; | |
59 | ||
98a3beab | 60 | hashflush(f); |
4d273500 | 61 | the_hash_algo->final_fn(f->buffer, &f->ctx); |
ac0463ed NP |
62 | if (result) |
63 | hashcpy(result, f->buffer); | |
cfe83216 | 64 | if (flags & CSUM_HASH_IN_STREAM) |
4d273500 | 65 | flush(f, f->buffer, the_hash_algo->rawsz); |
cfe83216 DS |
66 | if (flags & CSUM_FSYNC) |
67 | fsync_or_die(f->fd, f->name); | |
68 | if (flags & CSUM_CLOSE) { | |
7ba502c4 | 69 | if (close(f->fd)) |
d824cbba | 70 | die_errno("%s: sha1 file error on close", f->name); |
7ba502c4 NP |
71 | fd = 0; |
72 | } else | |
73 | fd = f->fd; | |
e337a04d JH |
74 | if (0 <= f->check_fd) { |
75 | char discard; | |
76 | int cnt = read_in_full(f->check_fd, &discard, 1); | |
77 | if (cnt < 0) | |
78 | die_errno("%s: error when reading the tail of sha1 file", | |
79 | f->name); | |
80 | if (cnt) | |
81 | die("%s: sha1 file has trailing garbage", f->name); | |
82 | if (close(f->check_fd)) | |
83 | die_errno("%s: sha1 file error on close", f->name); | |
84 | } | |
7bf058f0 | 85 | free(f); |
7ba502c4 | 86 | return fd; |
c38138cd LT |
87 | } |
88 | ||
98a3beab | 89 | void hashwrite(struct hashfile *f, const void *buf, unsigned int count) |
c38138cd LT |
90 | { |
91 | while (count) { | |
ddaf1f62 | 92 | unsigned left = sizeof(f->buffer) - f->offset; |
c38138cd | 93 | unsigned nr = count > left ? left : count; |
a8032d12 NP |
94 | |
95 | if (f->do_crc) | |
96 | f->crc32 = crc32(f->crc32, buf, nr); | |
97 | ||
98 | if (nr == sizeof(f->buffer)) { | |
ddaf1f62 DS |
99 | /* |
100 | * Flush a full batch worth of data directly | |
101 | * from the input, skipping the memcpy() to | |
102 | * the hashfile's buffer. In this block, | |
103 | * f->offset is necessarily zero. | |
104 | */ | |
105 | the_hash_algo->update_fn(&f->ctx, buf, nr); | |
106 | flush(f, buf, nr); | |
a8032d12 | 107 | } else { |
ddaf1f62 DS |
108 | /* |
109 | * Copy to the hashfile's buffer, flushing only | |
110 | * if it became full. | |
111 | */ | |
112 | memcpy(f->buffer + f->offset, buf, nr); | |
113 | f->offset += nr; | |
114 | left -= nr; | |
115 | if (!left) | |
116 | hashflush(f); | |
a8032d12 | 117 | } |
c38138cd | 118 | |
c38138cd | 119 | count -= nr; |
1d7f171c | 120 | buf = (char *) buf + nr; |
c38138cd | 121 | } |
c38138cd LT |
122 | } |
123 | ||
98a3beab | 124 | struct hashfile *hashfd(int fd, const char *name) |
2a128d63 | 125 | { |
98a3beab | 126 | return hashfd_throughput(fd, name, NULL); |
2a128d63 NP |
127 | } |
128 | ||
98a3beab | 129 | struct hashfile *hashfd_check(const char *name) |
e337a04d JH |
130 | { |
131 | int sink, check; | |
98a3beab | 132 | struct hashfile *f; |
e337a04d JH |
133 | |
134 | sink = open("/dev/null", O_WRONLY); | |
135 | if (sink < 0) | |
599d2231 | 136 | die_errno("unable to open /dev/null"); |
e337a04d | 137 | check = open(name, O_RDONLY); |
599d2231 JK |
138 | if (check < 0) |
139 | die_errno("unable to open '%s'", name); | |
98a3beab | 140 | f = hashfd(sink, name); |
e337a04d JH |
141 | f->check_fd = check; |
142 | return f; | |
143 | } | |
144 | ||
98a3beab | 145 | struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp) |
4397f014 | 146 | { |
98a3beab | 147 | struct hashfile *f = xmalloc(sizeof(*f)); |
4397f014 | 148 | f->fd = fd; |
e337a04d | 149 | f->check_fd = -1; |
4397f014 | 150 | f->offset = 0; |
218558af | 151 | f->total = 0; |
2a128d63 | 152 | f->tp = tp; |
ec640ed1 | 153 | f->name = name; |
78d1e84f | 154 | f->do_crc = 0; |
4d273500 | 155 | the_hash_algo->init_fn(&f->ctx); |
4397f014 LT |
156 | return f; |
157 | } | |
158 | ||
98a3beab | 159 | void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
6c526148 | 160 | { |
98a3beab | 161 | hashflush(f); |
6c526148 | 162 | checkpoint->offset = f->total; |
768e30ea | 163 | the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx); |
6c526148 JH |
164 | } |
165 | ||
98a3beab | 166 | int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
6c526148 JH |
167 | { |
168 | off_t offset = checkpoint->offset; | |
169 | ||
170 | if (ftruncate(f->fd, offset) || | |
171 | lseek(f->fd, offset, SEEK_SET) != offset) | |
172 | return -1; | |
173 | f->total = offset; | |
174 | f->ctx = checkpoint->ctx; | |
98a3beab | 175 | f->offset = 0; /* hashflush() was called in checkpoint */ |
6c526148 JH |
176 | return 0; |
177 | } | |
178 | ||
98a3beab | 179 | void crc32_begin(struct hashfile *f) |
78d1e84f | 180 | { |
1e4cd68c | 181 | f->crc32 = crc32(0, NULL, 0); |
78d1e84f NP |
182 | f->do_crc = 1; |
183 | } | |
c38138cd | 184 | |
98a3beab | 185 | uint32_t crc32_end(struct hashfile *f) |
78d1e84f NP |
186 | { |
187 | f->do_crc = 0; | |
188 | return f->crc32; | |
189 | } |