tests: make scripts executable
[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
13static int sha1flush(struct sha1file *f, unsigned int count)
14{
15 void *buf = f->buffer;
16
17 for (;;) {
18 int ret = write(f->fd, buf, count);
19 if (ret > 0) {
20 buf += ret;
21 count -= ret;
22 if (count)
23 continue;
24 return 0;
25 }
26 if (!ret)
e1808845 27 die("sha1 file '%s' write error. Out of diskspace", f->name);
c38138cd
LT
28 if (errno == EAGAIN || errno == EINTR)
29 continue;
e1808845 30 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
c38138cd
LT
31 }
32}
33
e1808845 34int sha1close(struct sha1file *f, unsigned char *result, int update)
c38138cd
LT
35{
36 unsigned offset = f->offset;
37 if (offset) {
38 SHA1_Update(&f->ctx, f->buffer, offset);
39 sha1flush(f, offset);
40 }
41 SHA1_Final(f->buffer, &f->ctx);
e1808845
LT
42 if (result)
43 memcpy(result, f->buffer, 20);
44 if (update)
45 sha1flush(f, 20);
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{
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;
2700628e 62 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;
96 SHA1_Init(&f->ctx);
97 return f;
98}
99
4397f014
LT
100struct sha1file *sha1fd(int fd, const char *name)
101{
102 struct sha1file *f;
103 unsigned len;
104
105 f = xmalloc(sizeof(*f));
106
107 len = strlen(name);
108 if (len >= PATH_MAX)
109 die("you wascally wabbit, you");
110 f->namelen = len;
111 memcpy(f->name, name, len+1);
112
113 f->fd = fd;
114 f->error = 0;
115 f->offset = 0;
116 SHA1_Init(&f->ctx);
117 return f;
118}
119
c38138cd
LT
120int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)
121{
122 z_stream stream;
123 unsigned long maxsize;
124 void *out;
125
126 memset(&stream, 0, sizeof(stream));
127 deflateInit(&stream, Z_DEFAULT_COMPRESSION);
128 maxsize = deflateBound(&stream, size);
129 out = xmalloc(maxsize);
130
131 /* Compress it */
132 stream.next_in = in;
133 stream.avail_in = size;
134
135 stream.next_out = out;
136 stream.avail_out = maxsize;
137
138 while (deflate(&stream, Z_FINISH) == Z_OK)
139 /* nothing */;
140 deflateEnd(&stream);
141
142 size = stream.total_out;
143 sha1write(f, out, size);
144 free(out);
145 return size;
146}
147
148