]> git.ipfire.org Git - thirdparty/git.git/blame - csum-file.c
remote.c: refactor creation of new dst ref
[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"
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
f0215369 32int sha1close(struct sha1file *f, unsigned char *result, int final)
c38138cd
LT
33{
34 unsigned offset = f->offset;
35 if (offset) {
36 SHA1_Update(&f->ctx, f->buffer, offset);
37 sha1flush(f, offset);
f0215369 38 f->offset = 0;
c38138cd 39 }
f0215369
DH
40 if (!final)
41 return 0; /* only want to flush (no checksum write, no close) */
c38138cd 42 SHA1_Final(f->buffer, &f->ctx);
e1808845 43 if (result)
e702496e 44 hashcpy(result, f->buffer);
f0215369 45 sha1flush(f, 20);
e1808845
LT
46 if (close(f->fd))
47 die("%s: sha1 file error on close (%s)", f->name, strerror(errno));
7bf058f0 48 free(f);
c38138cd
LT
49 return 0;
50}
51
52int sha1write(struct sha1file *f, void *buf, unsigned int count)
53{
78d1e84f
NP
54 if (f->do_crc)
55 f->crc32 = crc32(f->crc32, buf, count);
c38138cd
LT
56 while (count) {
57 unsigned offset = f->offset;
58 unsigned left = sizeof(f->buffer) - offset;
59 unsigned nr = count > left ? left : count;
60
61 memcpy(f->buffer + offset, buf, nr);
62 count -= nr;
63 offset += nr;
1d7f171c 64 buf = (char *) buf + nr;
c38138cd
LT
65 left -= nr;
66 if (!left) {
67 SHA1_Update(&f->ctx, f->buffer, offset);
68 sha1flush(f, offset);
69 offset = 0;
70 }
71 f->offset = offset;
72 }
73 return 0;
74}
75
76struct sha1file *sha1create(const char *fmt, ...)
77{
c38138cd
LT
78 struct sha1file *f;
79 unsigned len;
80 va_list arg;
81 int fd;
82
e1808845
LT
83 f = xmalloc(sizeof(*f));
84
c38138cd 85 va_start(arg, fmt);
e1808845 86 len = vsnprintf(f->name, sizeof(f->name), fmt, arg);
c38138cd 87 va_end(arg);
c38138cd
LT
88 if (len >= PATH_MAX)
89 die("you wascally wabbit, you");
e1808845
LT
90 f->namelen = len;
91
f312de01 92 fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0666);
c38138cd 93 if (fd < 0)
e1808845 94 die("unable to open %s (%s)", f->name, strerror(errno));
c38138cd
LT
95 f->fd = fd;
96 f->error = 0;
97 f->offset = 0;
78d1e84f 98 f->do_crc = 0;
c38138cd
LT
99 SHA1_Init(&f->ctx);
100 return f;
101}
102
4397f014
LT
103struct sha1file *sha1fd(int fd, const char *name)
104{
105 struct sha1file *f;
106 unsigned len;
107
108 f = xmalloc(sizeof(*f));
109
110 len = strlen(name);
111 if (len >= PATH_MAX)
112 die("you wascally wabbit, you");
113 f->namelen = len;
114 memcpy(f->name, name, len+1);
115
116 f->fd = fd;
117 f->error = 0;
118 f->offset = 0;
78d1e84f 119 f->do_crc = 0;
4397f014
LT
120 SHA1_Init(&f->ctx);
121 return f;
122}
123
960ccca6 124int sha1write_compressed(struct sha1file *f, void *in, unsigned int size, int level)
c38138cd
LT
125{
126 z_stream stream;
127 unsigned long maxsize;
128 void *out;
129
130 memset(&stream, 0, sizeof(stream));
960ccca6 131 deflateInit(&stream, level);
c38138cd
LT
132 maxsize = deflateBound(&stream, size);
133 out = xmalloc(maxsize);
134
135 /* Compress it */
136 stream.next_in = in;
137 stream.avail_in = size;
138
139 stream.next_out = out;
140 stream.avail_out = maxsize;
141
142 while (deflate(&stream, Z_FINISH) == Z_OK)
143 /* nothing */;
144 deflateEnd(&stream);
145
146 size = stream.total_out;
147 sha1write(f, out, size);
148 free(out);
149 return size;
150}
151
78d1e84f
NP
152void crc32_begin(struct sha1file *f)
153{
154 f->crc32 = crc32(0, Z_NULL, 0);
155 f->do_crc = 1;
156}
c38138cd 157
78d1e84f
NP
158uint32_t crc32_end(struct sha1file *f)
159{
160 f->do_crc = 0;
161 return f->crc32;
162}