]>
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 | ||
78b713a1 | 14 | static void sha1flush(struct sha1file *f, unsigned int count) |
c38138cd LT |
15 | { |
16 | void *buf = f->buffer; | |
17 | ||
18 | for (;;) { | |
1c15afb9 | 19 | int ret = xwrite(f->fd, buf, count); |
c38138cd | 20 | if (ret > 0) { |
2a128d63 | 21 | display_throughput(f->tp, ret); |
1d7f171c | 22 | buf = (char *) buf + ret; |
c38138cd LT |
23 | count -= ret; |
24 | if (count) | |
25 | continue; | |
78b713a1 | 26 | return; |
c38138cd LT |
27 | } |
28 | if (!ret) | |
e1808845 | 29 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
e1808845 | 30 | die("sha1 file '%s' write error (%s)", f->name, strerror(errno)); |
c38138cd LT |
31 | } |
32 | } | |
33 | ||
f0215369 | 34 | int sha1close(struct sha1file *f, unsigned char *result, int final) |
c38138cd | 35 | { |
7ba502c4 | 36 | int fd; |
c38138cd LT |
37 | unsigned offset = f->offset; |
38 | if (offset) { | |
39 | SHA1_Update(&f->ctx, f->buffer, offset); | |
40 | sha1flush(f, offset); | |
f0215369 | 41 | f->offset = 0; |
c38138cd | 42 | } |
7ba502c4 NP |
43 | if (final) { |
44 | /* write checksum and close fd */ | |
45 | SHA1_Final(f->buffer, &f->ctx); | |
46 | if (result) | |
47 | hashcpy(result, f->buffer); | |
48 | sha1flush(f, 20); | |
49 | if (close(f->fd)) | |
50 | die("%s: sha1 file error on close (%s)", | |
51 | f->name, strerror(errno)); | |
52 | fd = 0; | |
53 | } else | |
54 | fd = f->fd; | |
7bf058f0 | 55 | free(f); |
7ba502c4 | 56 | return fd; |
c38138cd LT |
57 | } |
58 | ||
59 | int sha1write(struct sha1file *f, void *buf, unsigned int count) | |
60 | { | |
78d1e84f NP |
61 | if (f->do_crc) |
62 | f->crc32 = crc32(f->crc32, buf, count); | |
c38138cd LT |
63 | while (count) { |
64 | unsigned offset = f->offset; | |
65 | unsigned left = sizeof(f->buffer) - offset; | |
66 | unsigned nr = count > left ? left : count; | |
67 | ||
68 | memcpy(f->buffer + offset, buf, nr); | |
69 | count -= nr; | |
70 | offset += nr; | |
1d7f171c | 71 | buf = (char *) buf + nr; |
c38138cd LT |
72 | left -= nr; |
73 | if (!left) { | |
74 | SHA1_Update(&f->ctx, f->buffer, offset); | |
75 | sha1flush(f, offset); | |
76 | offset = 0; | |
77 | } | |
78 | f->offset = offset; | |
79 | } | |
80 | return 0; | |
81 | } | |
82 | ||
4397f014 | 83 | struct sha1file *sha1fd(int fd, const char *name) |
2a128d63 NP |
84 | { |
85 | return sha1fd_throughput(fd, name, NULL); | |
86 | } | |
87 | ||
88 | struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp) | |
4397f014 LT |
89 | { |
90 | struct sha1file *f; | |
91 | unsigned len; | |
92 | ||
93 | f = xmalloc(sizeof(*f)); | |
94 | ||
95 | len = strlen(name); | |
96 | if (len >= PATH_MAX) | |
97 | die("you wascally wabbit, you"); | |
98 | f->namelen = len; | |
99 | memcpy(f->name, name, len+1); | |
100 | ||
101 | f->fd = fd; | |
102 | f->error = 0; | |
103 | f->offset = 0; | |
2a128d63 | 104 | f->tp = tp; |
78d1e84f | 105 | f->do_crc = 0; |
4397f014 LT |
106 | SHA1_Init(&f->ctx); |
107 | return f; | |
108 | } | |
109 | ||
78d1e84f NP |
110 | void crc32_begin(struct sha1file *f) |
111 | { | |
112 | f->crc32 = crc32(0, Z_NULL, 0); | |
113 | f->do_crc = 1; | |
114 | } | |
c38138cd | 115 | |
78d1e84f NP |
116 | uint32_t crc32_end(struct sha1file *f) |
117 | { | |
118 | f->do_crc = 0; | |
119 | return f->crc32; | |
120 | } |