]> git.ipfire.org Git - thirdparty/git.git/blame - csum-file.c
Merge branch 'tb/no-relative-file-url'
[thirdparty/git.git] / csum-file.c
CommitLineData
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
e74435a5 14static void flush(struct sha1file *f, const void *buf, unsigned int count)
c38138cd 15{
e337a04d
JH
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);
19
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);
26 }
27
c38138cd 28 for (;;) {
1c15afb9 29 int ret = xwrite(f->fd, buf, count);
c38138cd 30 if (ret > 0) {
218558af
NP
31 f->total += ret;
32 display_throughput(f->tp, f->total);
1d7f171c 33 buf = (char *) buf + ret;
c38138cd
LT
34 count -= ret;
35 if (count)
36 continue;
78b713a1 37 return;
c38138cd
LT
38 }
39 if (!ret)
e1808845 40 die("sha1 file '%s' write error. Out of diskspace", f->name);
d824cbba 41 die_errno("sha1 file '%s' write error", f->name);
c38138cd
LT
42 }
43}
44
838cd346 45void sha1flush(struct sha1file *f)
c38138cd
LT
46{
47 unsigned offset = f->offset;
4c81b03e 48
c38138cd 49 if (offset) {
9126f009 50 git_SHA1_Update(&f->ctx, f->buffer, offset);
e782e12f 51 flush(f, f->buffer, offset);
f0215369 52 f->offset = 0;
c38138cd 53 }
838cd346
NP
54}
55
56int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
57{
58 int fd;
59
60 sha1flush(f);
9126f009 61 git_SHA1_Final(f->buffer, &f->ctx);
ac0463ed
NP
62 if (result)
63 hashcpy(result, f->buffer);
4c81b03e 64 if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
7ba502c4 65 /* write checksum and close fd */
e782e12f 66 flush(f, f->buffer, 20);
4c81b03e
LT
67 if (flags & CSUM_FSYNC)
68 fsync_or_die(f->fd, f->name);
7ba502c4 69 if (close(f->fd))
d824cbba 70 die_errno("%s: sha1 file error on close", f->name);
7ba502c4
NP
71 fd = 0;
72 } else
73 fd = f->fd;
e337a04d
JH
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);
84 }
7bf058f0 85 free(f);
7ba502c4 86 return fd;
c38138cd
LT
87}
88
f06a5e60 89void sha1write(struct sha1file *f, const void *buf, unsigned int count)
c38138cd
LT
90{
91 while (count) {
92 unsigned offset = f->offset;
93 unsigned left = sizeof(f->buffer) - offset;
94 unsigned nr = count > left ? left : count;
e74435a5 95 const void *data;
a8032d12
NP
96
97 if (f->do_crc)
98 f->crc32 = crc32(f->crc32, buf, nr);
99
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;
106 }
c38138cd 107
c38138cd
LT
108 count -= nr;
109 offset += nr;
1d7f171c 110 buf = (char *) buf + nr;
c38138cd
LT
111 left -= nr;
112 if (!left) {
9126f009 113 git_SHA1_Update(&f->ctx, data, offset);
e782e12f 114 flush(f, data, offset);
c38138cd
LT
115 offset = 0;
116 }
117 f->offset = offset;
118 }
c38138cd
LT
119}
120
4397f014 121struct sha1file *sha1fd(int fd, const char *name)
2a128d63
NP
122{
123 return sha1fd_throughput(fd, name, NULL);
124}
125
e337a04d
JH
126struct sha1file *sha1fd_check(const char *name)
127{
128 int sink, check;
129 struct sha1file *f;
130
131 sink = open("/dev/null", O_WRONLY);
132 if (sink < 0)
133 return NULL;
134 check = open(name, O_RDONLY);
135 if (check < 0) {
136 int saved_errno = errno;
137 close(sink);
138 errno = saved_errno;
139 return NULL;
140 }
141 f = sha1fd(sink, name);
142 f->check_fd = check;
143 return f;
144}
145
2a128d63 146struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
4397f014 147{
ec640ed1 148 struct sha1file *f = xmalloc(sizeof(*f));
4397f014 149 f->fd = fd;
e337a04d 150 f->check_fd = -1;
4397f014 151 f->offset = 0;
218558af 152 f->total = 0;
2a128d63 153 f->tp = tp;
ec640ed1 154 f->name = name;
78d1e84f 155 f->do_crc = 0;
9126f009 156 git_SHA1_Init(&f->ctx);
4397f014
LT
157 return f;
158}
159
6c526148
JH
160void sha1file_checkpoint(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
161{
162 sha1flush(f);
163 checkpoint->offset = f->total;
164 checkpoint->ctx = f->ctx;
165}
166
167int sha1file_truncate(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
168{
169 off_t offset = checkpoint->offset;
170
171 if (ftruncate(f->fd, offset) ||
172 lseek(f->fd, offset, SEEK_SET) != offset)
173 return -1;
174 f->total = offset;
175 f->ctx = checkpoint->ctx;
176 f->offset = 0; /* sha1flush() was called in checkpoint */
177 return 0;
178}
179
78d1e84f
NP
180void crc32_begin(struct sha1file *f)
181{
1e4cd68c 182 f->crc32 = crc32(0, NULL, 0);
78d1e84f
NP
183 f->do_crc = 1;
184}
c38138cd 185
78d1e84f
NP
186uint32_t crc32_end(struct sha1file *f)
187{
188 f->do_crc = 0;
189 return f->crc32;
190}