blob: b7174c6c056c5a8f2a800ecbcb3cdf304c0bfc3f [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"
11#include "csum-file.h"
12
David Rientjes78b713a2006-08-14 13:32:01 -070013static void sha1flush(struct sha1file *f, unsigned int count)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070014{
15 void *buf = f->buffer;
16
17 for (;;) {
Junio C Hamano1c15afb2005-12-19 16:18:28 -080018 int ret = xwrite(f->fd, buf, count);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070019 if (ret > 0) {
Florian Forster1d7f1712006-06-18 17:18:09 +020020 buf = (char *) buf + ret;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070021 count -= ret;
22 if (count)
23 continue;
David Rientjes78b713a2006-08-14 13:32:01 -070024 return;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070025 }
26 if (!ret)
Linus Torvaldse1808842005-06-26 22:01:46 -070027 die("sha1 file '%s' write error. Out of diskspace", f->name);
Linus Torvaldse1808842005-06-26 22:01:46 -070028 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
Linus Torvaldsc38138c2005-06-26 20:27:56 -070029 }
30}
31
Linus Torvaldse1808842005-06-26 22:01:46 -070032int sha1close(struct sha1file *f, unsigned char *result, int update)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070033{
34 unsigned offset = f->offset;
35 if (offset) {
36 SHA1_Update(&f->ctx, f->buffer, offset);
37 sha1flush(f, offset);
38 }
39 SHA1_Final(f->buffer, &f->ctx);
Linus Torvaldse1808842005-06-26 22:01:46 -070040 if (result)
Shawn Pearcee7024962006-08-23 02:49:00 -040041 hashcpy(result, f->buffer);
Linus Torvaldse1808842005-06-26 22:01:46 -070042 if (update)
43 sha1flush(f, 20);
44 if (close(f->fd))
45 die("%s: sha1 file error on close (%s)", f->name, strerror(errno));
Sergey Vlasov7bf058f2005-08-08 22:46:13 +040046 free(f);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070047 return 0;
48}
49
50int sha1write(struct sha1file *f, void *buf, unsigned int count)
51{
52 while (count) {
53 unsigned offset = f->offset;
54 unsigned left = sizeof(f->buffer) - offset;
55 unsigned nr = count > left ? left : count;
56
57 memcpy(f->buffer + offset, buf, nr);
58 count -= nr;
59 offset += nr;
Florian Forster1d7f1712006-06-18 17:18:09 +020060 buf = (char *) buf + nr;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070061 left -= nr;
62 if (!left) {
63 SHA1_Update(&f->ctx, f->buffer, offset);
64 sha1flush(f, offset);
65 offset = 0;
66 }
67 f->offset = offset;
68 }
69 return 0;
70}
71
72struct sha1file *sha1create(const char *fmt, ...)
73{
Linus Torvaldsc38138c2005-06-26 20:27:56 -070074 struct sha1file *f;
75 unsigned len;
76 va_list arg;
77 int fd;
78
Linus Torvaldse1808842005-06-26 22:01:46 -070079 f = xmalloc(sizeof(*f));
Linus Torvaldsc38138c2005-06-26 20:27:56 -070080
Linus Torvaldse1808842005-06-26 22:01:46 -070081 va_start(arg, fmt);
82 len = vsnprintf(f->name, sizeof(f->name), fmt, arg);
83 va_end(arg);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070084 if (len >= PATH_MAX)
85 die("you wascally wabbit, you");
Linus Torvaldse1808842005-06-26 22:01:46 -070086 f->namelen = len;
87
Junio C Hamanof312de02005-07-06 01:21:46 -070088 fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0666);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070089 if (fd < 0)
Linus Torvaldse1808842005-06-26 22:01:46 -070090 die("unable to open %s (%s)", f->name, strerror(errno));
Linus Torvaldsc38138c2005-06-26 20:27:56 -070091 f->fd = fd;
92 f->error = 0;
93 f->offset = 0;
94 SHA1_Init(&f->ctx);
95 return f;
96}
97
Linus Torvalds4397f012005-06-28 11:10:06 -070098struct sha1file *sha1fd(int fd, const char *name)
99{
100 struct sha1file *f;
101 unsigned len;
102
103 f = xmalloc(sizeof(*f));
104
105 len = strlen(name);
106 if (len >= PATH_MAX)
107 die("you wascally wabbit, you");
108 f->namelen = len;
109 memcpy(f->name, name, len+1);
110
111 f->fd = fd;
112 f->error = 0;
113 f->offset = 0;
114 SHA1_Init(&f->ctx);
115 return f;
116}
117
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700118int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)
119{
120 z_stream stream;
121 unsigned long maxsize;
122 void *out;
123
124 memset(&stream, 0, sizeof(stream));
Joachim B Haga12f6c302006-07-03 22:11:47 +0200125 deflateInit(&stream, zlib_compression_level);
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700126 maxsize = deflateBound(&stream, size);
127 out = xmalloc(maxsize);
128
129 /* Compress it */
130 stream.next_in = in;
131 stream.avail_in = size;
132
133 stream.next_out = out;
134 stream.avail_out = maxsize;
135
136 while (deflate(&stream, Z_FINISH) == Z_OK)
137 /* nothing */;
138 deflateEnd(&stream);
139
140 size = stream.total_out;
141 sha1write(f, out, size);
142 free(out);
143 return size;
144}
145
146