git-fetch: be even quieter.
[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"
2a128d63 11#include "progress.h"
c38138cd
LT
12#include "csum-file.h"
13
78b713a1 14static void sha1flush(struct sha1file *f, unsigned int count)
c38138cd
LT
15{
16 void *buf = f->buffer;
17
18 for (;;) {
1c15afb9 19 int ret = xwrite(f->fd, buf, count);
c38138cd 20 if (ret > 0) {
218558af
NP
21 f->total += ret;
22 display_throughput(f->tp, f->total);
1d7f171c 23 buf = (char *) buf + ret;
c38138cd
LT
24 count -= ret;
25 if (count)
26 continue;
78b713a1 27 return;
c38138cd
LT
28 }
29 if (!ret)
e1808845 30 die("sha1 file '%s' write error. Out of diskspace", f->name);
e1808845 31 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
c38138cd
LT
32 }
33}
34
f0215369 35int sha1close(struct sha1file *f, unsigned char *result, int final)
c38138cd 36{
7ba502c4 37 int fd;
c38138cd
LT
38 unsigned offset = f->offset;
39 if (offset) {
40 SHA1_Update(&f->ctx, f->buffer, offset);
41 sha1flush(f, offset);
f0215369 42 f->offset = 0;
c38138cd 43 }
7ba502c4
NP
44 if (final) {
45 /* write checksum and close fd */
46 SHA1_Final(f->buffer, &f->ctx);
47 if (result)
48 hashcpy(result, f->buffer);
49 sha1flush(f, 20);
50 if (close(f->fd))
51 die("%s: sha1 file error on close (%s)",
52 f->name, strerror(errno));
53 fd = 0;
54 } else
55 fd = f->fd;
7bf058f0 56 free(f);
7ba502c4 57 return fd;
c38138cd
LT
58}
59
60int sha1write(struct sha1file *f, void *buf, unsigned int count)
61{
78d1e84f
NP
62 if (f->do_crc)
63 f->crc32 = crc32(f->crc32, buf, count);
c38138cd
LT
64 while (count) {
65 unsigned offset = f->offset;
66 unsigned left = sizeof(f->buffer) - offset;
67 unsigned nr = count > left ? left : count;
68
69 memcpy(f->buffer + offset, buf, nr);
70 count -= nr;
71 offset += nr;
1d7f171c 72 buf = (char *) buf + nr;
c38138cd
LT
73 left -= nr;
74 if (!left) {
75 SHA1_Update(&f->ctx, f->buffer, offset);
76 sha1flush(f, offset);
77 offset = 0;
78 }
79 f->offset = offset;
80 }
81 return 0;
82}
83
4397f014 84struct sha1file *sha1fd(int fd, const char *name)
2a128d63
NP
85{
86 return sha1fd_throughput(fd, name, NULL);
87}
88
89struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
4397f014
LT
90{
91 struct sha1file *f;
92 unsigned len;
93
94 f = xmalloc(sizeof(*f));
95
96 len = strlen(name);
97 if (len >= PATH_MAX)
98 die("you wascally wabbit, you");
99 f->namelen = len;
100 memcpy(f->name, name, len+1);
101
102 f->fd = fd;
103 f->error = 0;
104 f->offset = 0;
218558af 105 f->total = 0;
2a128d63 106 f->tp = tp;
78d1e84f 107 f->do_crc = 0;
4397f014
LT
108 SHA1_Init(&f->ctx);
109 return f;
110}
111
78d1e84f
NP
112void crc32_begin(struct sha1file *f)
113{
114 f->crc32 = crc32(0, Z_NULL, 0);
115 f->do_crc = 1;
116}
c38138cd 117
78d1e84f
NP
118uint32_t crc32_end(struct sha1file *f)
119{
120 f->do_crc = 0;
121 return f->crc32;
122}