blob: 53ce37f7ca42996dbfb4cf80e2127ea43496734d [file] [log] [blame]
Linus Torvaldsc38138c2005-06-26 20:27:56 -07001/*
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 17:06:21 -040011#include "progress.h"
Linus Torvaldsc38138c2005-06-26 20:27:56 -070012#include "csum-file.h"
13
brian m. carlson98a3bea2018-02-01 02:18:46 +000014static void flush(struct hashfile *f, const void *buf, unsigned int count)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070015{
Junio C Hamanoe337a042011-02-02 17:29:01 -080016 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);
Jeff King61d36332017-09-27 02:00:28 -040022 if (ret != count)
Junio C Hamanoe337a042011-02-02 17:29:01 -080023 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-26 20:27:56 -070028 for (;;) {
Junio C Hamano1c15afb2005-12-19 16:18:28 -080029 int ret = xwrite(f->fd, buf, count);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070030 if (ret > 0) {
Nicolas Pitre218558a2007-11-04 22:15:41 -050031 f->total += ret;
32 display_throughput(f->tp, f->total);
Florian Forster1d7f1712006-06-18 17:18:09 +020033 buf = (char *) buf + ret;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070034 count -= ret;
35 if (count)
36 continue;
David Rientjes78b713a2006-08-14 13:32:01 -070037 return;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070038 }
39 if (!ret)
Linus Torvaldse1808842005-06-26 22:01:46 -070040 die("sha1 file '%s' write error. Out of diskspace", f->name);
Thomas Rastd824cbb2009-06-27 17:58:46 +020041 die_errno("sha1 file '%s' write error", f->name);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070042 }
43}
44
brian m. carlson98a3bea2018-02-01 02:18:46 +000045void hashflush(struct hashfile *f)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070046{
47 unsigned offset = f->offset;
Linus Torvalds4c81b032008-05-30 08:42:16 -070048
Linus Torvaldsc38138c2005-06-26 20:27:56 -070049 if (offset) {
brian m. carlson4d273502018-02-01 02:18:47 +000050 the_hash_algo->update_fn(&f->ctx, f->buffer, offset);
Shawn O. Pearcee782e122008-10-10 08:39:20 -070051 flush(f, f->buffer, offset);
Dana L. Howf0215362007-05-13 11:28:19 -070052 f->offset = 0;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070053 }
Nicolas Pitre838cd342008-10-09 22:08:51 -040054}
55
Derrick Stoleef2af9f52018-04-02 16:34:14 -040056int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags)
Nicolas Pitre838cd342008-10-09 22:08:51 -040057{
58 int fd;
59
brian m. carlson98a3bea2018-02-01 02:18:46 +000060 hashflush(f);
brian m. carlson4d273502018-02-01 02:18:47 +000061 the_hash_algo->final_fn(f->buffer, &f->ctx);
Nicolas Pitreac0463e2008-08-29 16:08:00 -040062 if (result)
63 hashcpy(result, f->buffer);
Derrick Stoleecfe83212018-04-02 16:34:15 -040064 if (flags & CSUM_HASH_IN_STREAM)
brian m. carlson4d273502018-02-01 02:18:47 +000065 flush(f, f->buffer, the_hash_algo->rawsz);
Derrick Stoleecfe83212018-04-02 16:34:15 -040066 if (flags & CSUM_FSYNC)
67 fsync_or_die(f->fd, f->name);
68 if (flags & CSUM_CLOSE) {
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040069 if (close(f->fd))
Thomas Rastd824cbb2009-06-27 17:58:46 +020070 die_errno("%s: sha1 file error on close", f->name);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040071 fd = 0;
72 } else
73 fd = f->fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -080074 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 22:46:13 +040085 free(f);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040086 return fd;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070087}
88
brian m. carlson98a3bea2018-02-01 02:18:46 +000089void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070090{
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 13:59:49 -040095 const void *data;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070096
Nicolas Pitrea8032d12008-09-02 10:22:20 -040097 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-26 20:27:56 -0700108 count -= nr;
109 offset += nr;
Florian Forster1d7f1712006-06-18 17:18:09 +0200110 buf = (char *) buf + nr;
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700111 left -= nr;
112 if (!left) {
brian m. carlson4d273502018-02-01 02:18:47 +0000113 the_hash_algo->update_fn(&f->ctx, data, offset);
Shawn O. Pearcee782e122008-10-10 08:39:20 -0700114 flush(f, data, offset);
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700115 offset = 0;
116 }
117 f->offset = offset;
118 }
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700119}
120
brian m. carlson98a3bea2018-02-01 02:18:46 +0000121struct hashfile *hashfd(int fd, const char *name)
Linus Torvalds4397f012005-06-28 11:10:06 -0700122{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000123 return hashfd_throughput(fd, name, NULL);
Nicolas Pitre2a128d62007-10-30 17:06:21 -0400124}
125
brian m. carlson98a3bea2018-02-01 02:18:46 +0000126struct hashfile *hashfd_check(const char *name)
Junio C Hamanoe337a042011-02-02 17:29:01 -0800127{
128 int sink, check;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000129 struct hashfile *f;
Junio C Hamanoe337a042011-02-02 17:29:01 -0800130
131 sink = open("/dev/null", O_WRONLY);
132 if (sink < 0)
Jeff King599d2232015-03-18 02:30:12 -0400133 die_errno("unable to open /dev/null");
Junio C Hamanoe337a042011-02-02 17:29:01 -0800134 check = open(name, O_RDONLY);
Jeff King599d2232015-03-18 02:30:12 -0400135 if (check < 0)
136 die_errno("unable to open '%s'", name);
brian m. carlson98a3bea2018-02-01 02:18:46 +0000137 f = hashfd(sink, name);
Junio C Hamanoe337a042011-02-02 17:29:01 -0800138 f->check_fd = check;
139 return f;
140}
141
brian m. carlson98a3bea2018-02-01 02:18:46 +0000142struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
Nicolas Pitre2a128d62007-10-30 17:06:21 -0400143{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000144 struct hashfile *f = xmalloc(sizeof(*f));
Linus Torvalds4397f012005-06-28 11:10:06 -0700145 f->fd = fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -0800146 f->check_fd = -1;
Linus Torvalds4397f012005-06-28 11:10:06 -0700147 f->offset = 0;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500148 f->total = 0;
Nicolas Pitre2a128d62007-10-30 17:06:21 -0400149 f->tp = tp;
Nicolas Pitreec640ed2007-11-04 22:54:50 -0500150 f->name = name;
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400151 f->do_crc = 0;
brian m. carlson4d273502018-02-01 02:18:47 +0000152 the_hash_algo->init_fn(&f->ctx);
Linus Torvalds4397f012005-06-28 11:10:06 -0700153 return f;
154}
155
brian m. carlson98a3bea2018-02-01 02:18:46 +0000156void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Junio C Hamano6c526142011-11-17 16:26:54 -0800157{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000158 hashflush(f);
Junio C Hamano6c526142011-11-17 16:26:54 -0800159 checkpoint->offset = f->total;
160 checkpoint->ctx = f->ctx;
161}
162
brian m. carlson98a3bea2018-02-01 02:18:46 +0000163int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Junio C Hamano6c526142011-11-17 16:26:54 -0800164{
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;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000172 f->offset = 0; /* hashflush() was called in checkpoint */
Junio C Hamano6c526142011-11-17 16:26:54 -0800173 return 0;
174}
175
brian m. carlson98a3bea2018-02-01 02:18:46 +0000176void crc32_begin(struct hashfile *f)
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400177{
Stephen Boyd1e4cd682011-04-03 00:06:54 -0700178 f->crc32 = crc32(0, NULL, 0);
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400179 f->do_crc = 1;
180}
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700181
brian m. carlson98a3bea2018-02-01 02:18:46 +0000182uint32_t crc32_end(struct hashfile *f)
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400183{
184 f->do_crc = 0;
185 return f->crc32;
186}