]>
Commit | Line | Data |
---|---|---|
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" | |
11 | #include "progress.h" | |
12 | #include "csum-file.h" | |
13 | ||
14 | static void flush(struct sha1file *f, void * buf, unsigned int count) | |
15 | { | |
16 | for (;;) { | |
17 | int ret = xwrite(f->fd, buf, count); | |
18 | if (ret > 0) { | |
19 | f->total += ret; | |
20 | display_throughput(f->tp, f->total); | |
21 | buf = (char *) buf + ret; | |
22 | count -= ret; | |
23 | if (count) | |
24 | continue; | |
25 | return; | |
26 | } | |
27 | if (!ret) | |
28 | die("sha1 file '%s' write error. Out of diskspace", f->name); | |
29 | die_errno("sha1 file '%s' write error", f->name); | |
30 | } | |
31 | } | |
32 | ||
33 | void sha1flush(struct sha1file *f) | |
34 | { | |
35 | unsigned offset = f->offset; | |
36 | ||
37 | if (offset) { | |
38 | git_SHA1_Update(&f->ctx, f->buffer, offset); | |
39 | flush(f, f->buffer, offset); | |
40 | f->offset = 0; | |
41 | } | |
42 | } | |
43 | ||
44 | int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags) | |
45 | { | |
46 | int fd; | |
47 | ||
48 | sha1flush(f); | |
49 | git_SHA1_Final(f->buffer, &f->ctx); | |
50 | if (result) | |
51 | hashcpy(result, f->buffer); | |
52 | if (flags & (CSUM_CLOSE | CSUM_FSYNC)) { | |
53 | /* write checksum and close fd */ | |
54 | flush(f, f->buffer, 20); | |
55 | if (flags & CSUM_FSYNC) | |
56 | fsync_or_die(f->fd, f->name); | |
57 | if (close(f->fd)) | |
58 | die_errno("%s: sha1 file error on close", f->name); | |
59 | fd = 0; | |
60 | } else | |
61 | fd = f->fd; | |
62 | free(f); | |
63 | return fd; | |
64 | } | |
65 | ||
66 | int sha1write(struct sha1file *f, void *buf, unsigned int count) | |
67 | { | |
68 | while (count) { | |
69 | unsigned offset = f->offset; | |
70 | unsigned left = sizeof(f->buffer) - offset; | |
71 | unsigned nr = count > left ? left : count; | |
72 | void *data; | |
73 | ||
74 | if (f->do_crc) | |
75 | f->crc32 = crc32(f->crc32, buf, nr); | |
76 | ||
77 | if (nr == sizeof(f->buffer)) { | |
78 | /* process full buffer directly without copy */ | |
79 | data = buf; | |
80 | } else { | |
81 | memcpy(f->buffer + offset, buf, nr); | |
82 | data = f->buffer; | |
83 | } | |
84 | ||
85 | count -= nr; | |
86 | offset += nr; | |
87 | buf = (char *) buf + nr; | |
88 | left -= nr; | |
89 | if (!left) { | |
90 | git_SHA1_Update(&f->ctx, data, offset); | |
91 | flush(f, data, offset); | |
92 | offset = 0; | |
93 | } | |
94 | f->offset = offset; | |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
99 | struct sha1file *sha1fd(int fd, const char *name) | |
100 | { | |
101 | return sha1fd_throughput(fd, name, NULL); | |
102 | } | |
103 | ||
104 | struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp) | |
105 | { | |
106 | struct sha1file *f = xmalloc(sizeof(*f)); | |
107 | f->fd = fd; | |
108 | f->offset = 0; | |
109 | f->total = 0; | |
110 | f->tp = tp; | |
111 | f->name = name; | |
112 | f->do_crc = 0; | |
113 | git_SHA1_Init(&f->ctx); | |
114 | return f; | |
115 | } | |
116 | ||
117 | void crc32_begin(struct sha1file *f) | |
118 | { | |
119 | f->crc32 = crc32(0, Z_NULL, 0); | |
120 | f->do_crc = 1; | |
121 | } | |
122 | ||
123 | uint32_t crc32_end(struct sha1file *f) | |
124 | { | |
125 | f->do_crc = 0; | |
126 | return f->crc32; | |
127 | } |