git-rev-list: Add regexp tuning options
[git/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"
11#include "csum-file.h"
12
78b713a1 13static void sha1flush(struct sha1file *f, unsigned int count)
c38138cd
LT
14{
15 void *buf = f->buffer;
16
17 for (;;) {
1c15afb9 18 int ret = xwrite(f->fd, buf, count);
c38138cd 19 if (ret > 0) {
1d7f171c 20 buf = (char *) buf + ret;
c38138cd
LT
21 count -= ret;
22 if (count)
23 continue;
78b713a1 24 return;
c38138cd
LT
25 }
26 if (!ret)
e1808845 27 die("sha1 file '%s' write error. Out of diskspace", f->name);
e1808845 28 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
c38138cd
LT
29 }
30}
31
e1808845 32int sha1close(struct sha1file *f, unsigned char *result, int update)
c38138cd
LT
33{
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);
e1808845 40 if (result)
e702496e 41 hashcpy(result, f->buffer);
e1808845
LT
42 if (update)
43 sha1flush(f, 20);
44 if (close(f->fd))
45 die("%s: sha1 file error on close (%s)", f->name, strerror(errno));
7bf058f0 46 free(f);
c38138cd
LT
47 return 0;
48}
49
50int sha1write(struct sha1file *f, void *buf, unsigned int count)
51{
78d1e84f
NP
52 if (f->do_crc)
53 f->crc32 = crc32(f->crc32, buf, count);
c38138cd
LT
54 while (count) {
55 unsigned offset = f->offset;
56 unsigned left = sizeof(f->buffer) - offset;
57 unsigned nr = count > left ? left : count;
58
59 memcpy(f->buffer + offset, buf, nr);
60 count -= nr;
61 offset += nr;
1d7f171c 62 buf = (char *) buf + nr;
c38138cd
LT
63 left -= nr;
64 if (!left) {
65 SHA1_Update(&f->ctx, f->buffer, offset);
66 sha1flush(f, offset);
67 offset = 0;
68 }
69 f->offset = offset;
70 }
71 return 0;
72}
73
74struct sha1file *sha1create(const char *fmt, ...)
75{
c38138cd
LT
76 struct sha1file *f;
77 unsigned len;
78 va_list arg;
79 int fd;
80
e1808845
LT
81 f = xmalloc(sizeof(*f));
82
c38138cd 83 va_start(arg, fmt);
e1808845 84 len = vsnprintf(f->name, sizeof(f->name), fmt, arg);
c38138cd 85 va_end(arg);
c38138cd
LT
86 if (len >= PATH_MAX)
87 die("you wascally wabbit, you");
e1808845
LT
88 f->namelen = len;
89
f312de01 90 fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0666);
c38138cd 91 if (fd < 0)
e1808845 92 die("unable to open %s (%s)", f->name, strerror(errno));
c38138cd
LT
93 f->fd = fd;
94 f->error = 0;
95 f->offset = 0;
78d1e84f 96 f->do_crc = 0;
c38138cd
LT
97 SHA1_Init(&f->ctx);
98 return f;
99}
100
4397f014
LT
101struct sha1file *sha1fd(int fd, const char *name)
102{
103 struct sha1file *f;
104 unsigned len;
105
106 f = xmalloc(sizeof(*f));
107
108 len = strlen(name);
109 if (len >= PATH_MAX)
110 die("you wascally wabbit, you");
111 f->namelen = len;
112 memcpy(f->name, name, len+1);
113
114 f->fd = fd;
115 f->error = 0;
116 f->offset = 0;
78d1e84f 117 f->do_crc = 0;
4397f014
LT
118 SHA1_Init(&f->ctx);
119 return f;
120}
121
960ccca6 122int sha1write_compressed(struct sha1file *f, void *in, unsigned int size, int level)
c38138cd
LT
123{
124 z_stream stream;
125 unsigned long maxsize;
126 void *out;
127
128 memset(&stream, 0, sizeof(stream));
960ccca6 129 deflateInit(&stream, level);
c38138cd
LT
130 maxsize = deflateBound(&stream, size);
131 out = xmalloc(maxsize);
132
133 /* Compress it */
134 stream.next_in = in;
135 stream.avail_in = size;
136
137 stream.next_out = out;
138 stream.avail_out = maxsize;
139
140 while (deflate(&stream, Z_FINISH) == Z_OK)
141 /* nothing */;
142 deflateEnd(&stream);
143
144 size = stream.total_out;
145 sha1write(f, out, size);
146 free(out);
147 return size;
148}
149
78d1e84f
NP
150void crc32_begin(struct sha1file *f)
151{
152 f->crc32 = crc32(0, Z_NULL, 0);
153 f->do_crc = 1;
154}
c38138cd 155
78d1e84f
NP
156uint32_t crc32_end(struct sha1file *f)
157{
158 f->do_crc = 0;
159 return f->crc32;
160}