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