bisect.c: use die_errno() and warning_errno()
[git/raj.git] / csum-file.c
bloba172199e44bf370cd66ee7073f5a2228fd9679ef
1 /*
2 * csum-file.c
4 * Copyright (C) 2005 Linus Torvalds
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"
14 static void flush(struct sha1file *f, const void *buf, unsigned int count)
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);
20 if (ret < 0)
21 die_errno("%s: sha1 file read error", f->name);
22 if (ret < count)
23 die("%s: sha1 file truncated", f->name);
24 if (memcmp(buf, check_buffer, count))
25 die("sha1 file '%s' validation error", f->name);
28 for (;;) {
29 int ret = xwrite(f->fd, buf, count);
30 if (ret > 0) {
31 f->total += ret;
32 display_throughput(f->tp, f->total);
33 buf = (char *) buf + ret;
34 count -= ret;
35 if (count)
36 continue;
37 return;
39 if (!ret)
40 die("sha1 file '%s' write error. Out of diskspace", f->name);
41 die_errno("sha1 file '%s' write error", f->name);
45 void sha1flush(struct sha1file *f)
47 unsigned offset = f->offset;
49 if (offset) {
50 git_SHA1_Update(&f->ctx, f->buffer, offset);
51 flush(f, f->buffer, offset);
52 f->offset = 0;
56 int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
58 int fd;
60 sha1flush(f);
61 git_SHA1_Final(f->buffer, &f->ctx);
62 if (result)
63 hashcpy(result, f->buffer);
64 if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
65 /* write checksum and close fd */
66 flush(f, f->buffer, 20);
67 if (flags & CSUM_FSYNC)
68 fsync_or_die(f->fd, f->name);
69 if (close(f->fd))
70 die_errno("%s: sha1 file error on close", f->name);
71 fd = 0;
72 } else
73 fd = f->fd;
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);
85 free(f);
86 return fd;
89 void sha1write(struct sha1file *f, const void *buf, unsigned int count)
91 while (count) {
92 unsigned offset = f->offset;
93 unsigned left = sizeof(f->buffer) - offset;
94 unsigned nr = count > left ? left : count;
95 const void *data;
97 if (f->do_crc)
98 f->crc32 = crc32(f->crc32, buf, nr);
100 if (nr == sizeof(f->buffer)) {
101 /* process full buffer directly without copy */
102 data = buf;
103 } else {
104 memcpy(f->buffer + offset, buf, nr);
105 data = f->buffer;
108 count -= nr;
109 offset += nr;
110 buf = (char *) buf + nr;
111 left -= nr;
112 if (!left) {
113 git_SHA1_Update(&f->ctx, data, offset);
114 flush(f, data, offset);
115 offset = 0;
117 f->offset = offset;
121 struct sha1file *sha1fd(int fd, const char *name)
123 return sha1fd_throughput(fd, name, NULL);
126 struct sha1file *sha1fd_check(const char *name)
128 int sink, check;
129 struct sha1file *f;
131 sink = open("/dev/null", O_WRONLY);
132 if (sink < 0)
133 die_errno("unable to open /dev/null");
134 check = open(name, O_RDONLY);
135 if (check < 0)
136 die_errno("unable to open '%s'", name);
137 f = sha1fd(sink, name);
138 f->check_fd = check;
139 return f;
142 struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
144 struct sha1file *f = xmalloc(sizeof(*f));
145 f->fd = fd;
146 f->check_fd = -1;
147 f->offset = 0;
148 f->total = 0;
149 f->tp = tp;
150 f->name = name;
151 f->do_crc = 0;
152 git_SHA1_Init(&f->ctx);
153 return f;
156 void sha1file_checkpoint(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
158 sha1flush(f);
159 checkpoint->offset = f->total;
160 checkpoint->ctx = f->ctx;
163 int sha1file_truncate(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
165 off_t offset = checkpoint->offset;
167 if (ftruncate(f->fd, offset) ||
168 lseek(f->fd, offset, SEEK_SET) != offset)
169 return -1;
170 f->total = offset;
171 f->ctx = checkpoint->ctx;
172 f->offset = 0; /* sha1flush() was called in checkpoint */
173 return 0;
176 void crc32_begin(struct sha1file *f)
178 f->crc32 = crc32(0, NULL, 0);
179 f->do_crc = 1;
182 uint32_t crc32_end(struct sha1file *f)
184 f->do_crc = 0;
185 return f->crc32;