]>
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 | ||
a8032d12 | 14 | static void sha1flush(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 | ||
4c81b03e | 33 | int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags) |
c38138cd | 34 | { |
7ba502c4 | 35 | int fd; |
c38138cd | 36 | unsigned offset = f->offset; |
4c81b03e | 37 | |
c38138cd LT |
38 | if (offset) { |
39 | SHA1_Update(&f->ctx, f->buffer, offset); | |
a8032d12 | 40 | sha1flush(f, f->buffer, offset); |
f0215369 | 41 | f->offset = 0; |
c38138cd | 42 | } |
ac0463ed NP |
43 | SHA1_Final(f->buffer, &f->ctx); |
44 | if (result) | |
45 | hashcpy(result, f->buffer); | |
4c81b03e | 46 | if (flags & (CSUM_CLOSE | CSUM_FSYNC)) { |
7ba502c4 | 47 | /* write checksum and close fd */ |
a8032d12 | 48 | sha1flush(f, f->buffer, 20); |
4c81b03e LT |
49 | if (flags & CSUM_FSYNC) |
50 | fsync_or_die(f->fd, f->name); | |
7ba502c4 NP |
51 | if (close(f->fd)) |
52 | die("%s: sha1 file error on close (%s)", | |
53 | f->name, strerror(errno)); | |
54 | fd = 0; | |
55 | } else | |
56 | fd = f->fd; | |
7bf058f0 | 57 | free(f); |
7ba502c4 | 58 | return fd; |
c38138cd LT |
59 | } |
60 | ||
61 | int sha1write(struct sha1file *f, void *buf, unsigned int count) | |
62 | { | |
63 | while (count) { | |
64 | unsigned offset = f->offset; | |
65 | unsigned left = sizeof(f->buffer) - offset; | |
66 | unsigned nr = count > left ? left : count; | |
a8032d12 NP |
67 | void *data; |
68 | ||
69 | if (f->do_crc) | |
70 | f->crc32 = crc32(f->crc32, buf, nr); | |
71 | ||
72 | if (nr == sizeof(f->buffer)) { | |
73 | /* process full buffer directly without copy */ | |
74 | data = buf; | |
75 | } else { | |
76 | memcpy(f->buffer + offset, buf, nr); | |
77 | data = f->buffer; | |
78 | } | |
c38138cd | 79 | |
c38138cd LT |
80 | count -= nr; |
81 | offset += nr; | |
1d7f171c | 82 | buf = (char *) buf + nr; |
c38138cd LT |
83 | left -= nr; |
84 | if (!left) { | |
a8032d12 NP |
85 | SHA1_Update(&f->ctx, data, offset); |
86 | sha1flush(f, data, offset); | |
c38138cd LT |
87 | offset = 0; |
88 | } | |
89 | f->offset = offset; | |
90 | } | |
91 | return 0; | |
92 | } | |
93 | ||
4397f014 | 94 | struct sha1file *sha1fd(int fd, const char *name) |
2a128d63 NP |
95 | { |
96 | return sha1fd_throughput(fd, name, NULL); | |
97 | } | |
98 | ||
99 | struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp) | |
4397f014 | 100 | { |
ec640ed1 | 101 | struct sha1file *f = xmalloc(sizeof(*f)); |
4397f014 | 102 | f->fd = fd; |
4397f014 | 103 | f->offset = 0; |
218558af | 104 | f->total = 0; |
2a128d63 | 105 | f->tp = tp; |
ec640ed1 | 106 | f->name = name; |
78d1e84f | 107 | f->do_crc = 0; |
4397f014 LT |
108 | SHA1_Init(&f->ctx); |
109 | return f; | |
110 | } | |
111 | ||
78d1e84f NP |
112 | void crc32_begin(struct sha1file *f) |
113 | { | |
114 | f->crc32 = crc32(0, Z_NULL, 0); | |
115 | f->do_crc = 1; | |
116 | } | |
c38138cd | 117 | |
78d1e84f NP |
118 | uint32_t crc32_end(struct sha1file *f) |
119 | { | |
120 | f->do_crc = 0; | |
121 | return f->crc32; | |
122 | } |