More doc
[alt-git.git] / csum-file.c
blob3ab65b7515d5a4cb9c8d9b6c4f8eb32121d3c628
1 /*
2 * csum-file.c
4 * Copyright (C) 2005 Linus Torvalds
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"
13 static int sha1flush(struct sha1file *f, unsigned int count)
15 void *buf = f->buffer;
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;
26 if (!ret)
27 die("sha1 file '%s' write error. Out of diskspace", f->name);
28 if (errno == EAGAIN || errno == EINTR)
29 continue;
30 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
34 int sha1close(struct sha1file *f, unsigned char *result, int update)
36 unsigned offset = f->offset;
37 if (offset) {
38 SHA1_Update(&f->ctx, f->buffer, offset);
39 sha1flush(f, offset);
41 SHA1_Final(f->buffer, &f->ctx);
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));
48 return 0;
51 int sha1write(struct sha1file *f, void *buf, unsigned int count)
53 while (count) {
54 unsigned offset = f->offset;
55 unsigned left = sizeof(f->buffer) - offset;
56 unsigned nr = count > left ? left : count;
58 memcpy(f->buffer + offset, buf, nr);
59 count -= nr;
60 offset += nr;
61 buf += nr;
62 left -= nr;
63 if (!left) {
64 SHA1_Update(&f->ctx, f->buffer, offset);
65 sha1flush(f, offset);
66 offset = 0;
68 f->offset = offset;
70 return 0;
73 struct sha1file *sha1create(const char *fmt, ...)
75 struct sha1file *f;
76 unsigned len;
77 va_list arg;
78 int fd;
80 f = xmalloc(sizeof(*f));
82 va_start(arg, fmt);
83 len = vsnprintf(f->name, sizeof(f->name), fmt, arg);
84 va_end(arg);
85 if (len >= PATH_MAX)
86 die("you wascally wabbit, you");
87 f->namelen = len;
89 fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0644);
90 if (fd < 0)
91 die("unable to open %s (%s)", f->name, strerror(errno));
92 f->fd = fd;
93 f->error = 0;
94 f->offset = 0;
95 SHA1_Init(&f->ctx);
96 return f;
99 int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)
101 z_stream stream;
102 unsigned long maxsize;
103 void *out;
105 memset(&stream, 0, sizeof(stream));
106 deflateInit(&stream, Z_DEFAULT_COMPRESSION);
107 maxsize = deflateBound(&stream, size);
108 out = xmalloc(maxsize);
110 /* Compress it */
111 stream.next_in = in;
112 stream.avail_in = size;
114 stream.next_out = out;
115 stream.avail_out = maxsize;
117 while (deflate(&stream, Z_FINISH) == Z_OK)
118 /* nothing */;
119 deflateEnd(&stream);
121 size = stream.total_out;
122 sha1write(f, out, size);
123 free(out);
124 return size;