🌐 AI搜索 & 代理 主页
blob: a172199e44bf370cd66ee7073f5a2228fd9679ef [file] [log] [blame]
Linus Torvaldsc38138c2005-06-27 03:27:561/*
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"
Nicolas Pitre2a128d62007-10-30 21:06:2111#include "progress.h"
Linus Torvaldsc38138c2005-06-27 03:27:5612#include "csum-file.h"
13
Jeff Kinge74435a2013-10-24 17:59:4914static void flush(struct sha1file *f, const void *buf, unsigned int count)
Linus Torvaldsc38138c2005-06-27 03:27:5615{
Junio C Hamanoe337a042011-02-03 01:29:0116 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
Linus Torvaldsc38138c2005-06-27 03:27:5628 for (;;) {
Junio C Hamano1c15afb2005-12-20 00:18:2829 int ret = xwrite(f->fd, buf, count);
Linus Torvaldsc38138c2005-06-27 03:27:5630 if (ret > 0) {
Nicolas Pitre218558a2007-11-05 03:15:4131 f->total += ret;
32 display_throughput(f->tp, f->total);
Florian Forster1d7f1712006-06-18 15:18:0933 buf = (char *) buf + ret;
Linus Torvaldsc38138c2005-06-27 03:27:5634 count -= ret;
35 if (count)
36 continue;
David Rientjes78b713a2006-08-14 20:32:0137 return;
Linus Torvaldsc38138c2005-06-27 03:27:5638 }
39 if (!ret)
Linus Torvaldse1808842005-06-27 05:01:4640 die("sha1 file '%s' write error. Out of diskspace", f->name);
Thomas Rastd824cbb2009-06-27 15:58:4641 die_errno("sha1 file '%s' write error", f->name);
Linus Torvaldsc38138c2005-06-27 03:27:5642 }
43}
44
Nicolas Pitre838cd342008-10-10 02:08:5145void sha1flush(struct sha1file *f)
Linus Torvaldsc38138c2005-06-27 03:27:5646{
47 unsigned offset = f->offset;
Linus Torvalds4c81b032008-05-30 15:42:1648
Linus Torvaldsc38138c2005-06-27 03:27:5649 if (offset) {
Nicolas Pitre9126f002008-10-01 18:05:2050 git_SHA1_Update(&f->ctx, f->buffer, offset);
Shawn O. Pearcee782e122008-10-10 15:39:2051 flush(f, f->buffer, offset);
Dana L. Howf0215362007-05-13 18:28:1952 f->offset = 0;
Linus Torvaldsc38138c2005-06-27 03:27:5653 }
Nicolas Pitre838cd342008-10-10 02:08:5154}
55
56int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
57{
58 int fd;
59
60 sha1flush(f);
Nicolas Pitre9126f002008-10-01 18:05:2061 git_SHA1_Final(f->buffer, &f->ctx);
Nicolas Pitreac0463e2008-08-29 20:08:0062 if (result)
63 hashcpy(result, f->buffer);
Linus Torvalds4c81b032008-05-30 15:42:1664 if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
Nicolas Pitre7ba502c2007-10-17 01:55:4865 /* write checksum and close fd */
Shawn O. Pearcee782e122008-10-10 15:39:2066 flush(f, f->buffer, 20);
Linus Torvalds4c81b032008-05-30 15:42:1667 if (flags & CSUM_FSYNC)
68 fsync_or_die(f->fd, f->name);
Nicolas Pitre7ba502c2007-10-17 01:55:4869 if (close(f->fd))
Thomas Rastd824cbb2009-06-27 15:58:4670 die_errno("%s: sha1 file error on close", f->name);
Nicolas Pitre7ba502c2007-10-17 01:55:4871 fd = 0;
72 } else
73 fd = f->fd;
Junio C Hamanoe337a042011-02-03 01:29:0174 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 }
Sergey Vlasov7bf058f2005-08-08 18:46:1385 free(f);
Nicolas Pitre7ba502c2007-10-17 01:55:4886 return fd;
Linus Torvaldsc38138c2005-06-27 03:27:5687}
88
Junio C Hamanof06a5e62014-01-10 18:33:0989void sha1write(struct sha1file *f, const void *buf, unsigned int count)
Linus Torvaldsc38138c2005-06-27 03:27:5690{
91 while (count) {
92 unsigned offset = f->offset;
93 unsigned left = sizeof(f->buffer) - offset;
94 unsigned nr = count > left ? left : count;
Jeff Kinge74435a2013-10-24 17:59:4995 const void *data;
Linus Torvaldsc38138c2005-06-27 03:27:5696
Nicolas Pitrea8032d12008-09-02 14:22:2097 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 }
107
Linus Torvaldsc38138c2005-06-27 03:27:56108 count -= nr;
109 offset += nr;
Florian Forster1d7f1712006-06-18 15:18:09110 buf = (char *) buf + nr;
Linus Torvaldsc38138c2005-06-27 03:27:56111 left -= nr;
112 if (!left) {
Nicolas Pitre9126f002008-10-01 18:05:20113 git_SHA1_Update(&f->ctx, data, offset);
Shawn O. Pearcee782e122008-10-10 15:39:20114 flush(f, data, offset);
Linus Torvaldsc38138c2005-06-27 03:27:56115 offset = 0;
116 }
117 f->offset = offset;
118 }
Linus Torvaldsc38138c2005-06-27 03:27:56119}
120
Linus Torvalds4397f012005-06-28 18:10:06121struct sha1file *sha1fd(int fd, const char *name)
122{
Nicolas Pitre2a128d62007-10-30 21:06:21123 return sha1fd_throughput(fd, name, NULL);
124}
125
Junio C Hamanoe337a042011-02-03 01:29:01126struct 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)
Jeff King599d2232015-03-18 06:30:12133 die_errno("unable to open /dev/null");
Junio C Hamanoe337a042011-02-03 01:29:01134 check = open(name, O_RDONLY);
Jeff King599d2232015-03-18 06:30:12135 if (check < 0)
136 die_errno("unable to open '%s'", name);
Junio C Hamanoe337a042011-02-03 01:29:01137 f = sha1fd(sink, name);
138 f->check_fd = check;
139 return f;
140}
141
Nicolas Pitre2a128d62007-10-30 21:06:21142struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
143{
Nicolas Pitreec640ed2007-11-05 03:54:50144 struct sha1file *f = xmalloc(sizeof(*f));
Linus Torvalds4397f012005-06-28 18:10:06145 f->fd = fd;
Junio C Hamanoe337a042011-02-03 01:29:01146 f->check_fd = -1;
Linus Torvalds4397f012005-06-28 18:10:06147 f->offset = 0;
Nicolas Pitre218558a2007-11-05 03:15:41148 f->total = 0;
Nicolas Pitre2a128d62007-10-30 21:06:21149 f->tp = tp;
Nicolas Pitreec640ed2007-11-05 03:54:50150 f->name = name;
Nicolas Pitre78d1e842007-04-09 05:06:31151 f->do_crc = 0;
Nicolas Pitre9126f002008-10-01 18:05:20152 git_SHA1_Init(&f->ctx);
Linus Torvalds4397f012005-06-28 18:10:06153 return f;
154}
155
Junio C Hamano6c526142011-11-18 00:26:54156void sha1file_checkpoint(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
157{
158 sha1flush(f);
159 checkpoint->offset = f->total;
160 checkpoint->ctx = f->ctx;
161}
162
163int sha1file_truncate(struct sha1file *f, struct sha1file_checkpoint *checkpoint)
164{
165 off_t offset = checkpoint->offset;
166
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;
174}
175
Nicolas Pitre78d1e842007-04-09 05:06:31176void crc32_begin(struct sha1file *f)
177{
Stephen Boyd1e4cd682011-04-03 07:06:54178 f->crc32 = crc32(0, NULL, 0);
Nicolas Pitre78d1e842007-04-09 05:06:31179 f->do_crc = 1;
180}
Linus Torvaldsc38138c2005-06-27 03:27:56181
Nicolas Pitre78d1e842007-04-09 05:06:31182uint32_t crc32_end(struct sha1file *f)
183{
184 f->do_crc = 0;
185 return f->crc32;
186}