]> git.ipfire.org Git - thirdparty/git.git/blame - csum-file.c
Teach "git diff" to honour --[no-]ext-diff
[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"
2a128d63 11#include "progress.h"
c38138cd
LT
12#include "csum-file.h"
13
838cd346 14static void flush(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
838cd346 35void sha1flush(struct sha1file *f)
c38138cd
LT
36{
37 unsigned offset = f->offset;
4c81b03e 38
c38138cd
LT
39 if (offset) {
40 SHA1_Update(&f->ctx, f->buffer, offset);
838cd346 41 flush(f, offset);
f0215369 42 f->offset = 0;
c38138cd 43 }
838cd346
NP
44}
45
46int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
47{
48 int fd;
49
50 sha1flush(f);
ac0463ed
NP
51 SHA1_Final(f->buffer, &f->ctx);
52 if (result)
53 hashcpy(result, f->buffer);
4c81b03e 54 if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
7ba502c4 55 /* write checksum and close fd */
838cd346 56 flush(f, 20);
4c81b03e
LT
57 if (flags & CSUM_FSYNC)
58 fsync_or_die(f->fd, f->name);
7ba502c4
NP
59 if (close(f->fd))
60 die("%s: sha1 file error on close (%s)",
61 f->name, strerror(errno));
62 fd = 0;
63 } else
64 fd = f->fd;
7bf058f0 65 free(f);
7ba502c4 66 return fd;
c38138cd
LT
67}
68
69int sha1write(struct sha1file *f, void *buf, unsigned int count)
70{
78d1e84f
NP
71 if (f->do_crc)
72 f->crc32 = crc32(f->crc32, buf, count);
c38138cd
LT
73 while (count) {
74 unsigned offset = f->offset;
75 unsigned left = sizeof(f->buffer) - offset;
76 unsigned nr = count > left ? left : count;
77
78 memcpy(f->buffer + offset, buf, nr);
79 count -= nr;
80 offset += nr;
1d7f171c 81 buf = (char *) buf + nr;
c38138cd
LT
82 left -= nr;
83 if (!left) {
84 SHA1_Update(&f->ctx, f->buffer, offset);
838cd346 85 flush(f, offset);
c38138cd
LT
86 offset = 0;
87 }
88 f->offset = offset;
89 }
90 return 0;
91}
92
4397f014 93struct sha1file *sha1fd(int fd, const char *name)
2a128d63
NP
94{
95 return sha1fd_throughput(fd, name, NULL);
96}
97
98struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
4397f014 99{
ec640ed1 100 struct sha1file *f = xmalloc(sizeof(*f));
4397f014 101 f->fd = fd;
4397f014 102 f->offset = 0;
218558af 103 f->total = 0;
2a128d63 104 f->tp = tp;
ec640ed1 105 f->name = name;
78d1e84f 106 f->do_crc = 0;
4397f014
LT
107 SHA1_Init(&f->ctx);
108 return f;
109}
110
78d1e84f
NP
111void crc32_begin(struct sha1file *f)
112{
113 f->crc32 = crc32(0, Z_NULL, 0);
114 f->do_crc = 1;
115}
c38138cd 116
78d1e84f
NP
117uint32_t crc32_end(struct sha1file *f)
118{
119 f->do_crc = 0;
120 return f->crc32;
121}