]> git.ipfire.org Git - thirdparty/git.git/blame - csum-file.c
Documentation/git-archive: spell --worktree-attributes correctly
[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
e782e12f 14static void flush(struct sha1file *f, void * buf, unsigned int count)
c38138cd 15{
c38138cd 16 for (;;) {
1c15afb9 17 int ret = xwrite(f->fd, buf, count);
c38138cd 18 if (ret > 0) {
218558af
NP
19 f->total += ret;
20 display_throughput(f->tp, f->total);
1d7f171c 21 buf = (char *) buf + ret;
c38138cd
LT
22 count -= ret;
23 if (count)
24 continue;
78b713a1 25 return;
c38138cd
LT
26 }
27 if (!ret)
e1808845 28 die("sha1 file '%s' write error. Out of diskspace", f->name);
d824cbba 29 die_errno("sha1 file '%s' write error", f->name);
c38138cd
LT
30 }
31}
32
838cd346 33void sha1flush(struct sha1file *f)
c38138cd
LT
34{
35 unsigned offset = f->offset;
4c81b03e 36
c38138cd 37 if (offset) {
9126f009 38 git_SHA1_Update(&f->ctx, f->buffer, offset);
e782e12f 39 flush(f, f->buffer, offset);
f0215369 40 f->offset = 0;
c38138cd 41 }
838cd346
NP
42}
43
44int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
45{
46 int fd;
47
48 sha1flush(f);
9126f009 49 git_SHA1_Final(f->buffer, &f->ctx);
ac0463ed
NP
50 if (result)
51 hashcpy(result, f->buffer);
4c81b03e 52 if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
7ba502c4 53 /* write checksum and close fd */
e782e12f 54 flush(f, f->buffer, 20);
4c81b03e
LT
55 if (flags & CSUM_FSYNC)
56 fsync_or_die(f->fd, f->name);
7ba502c4 57 if (close(f->fd))
d824cbba 58 die_errno("%s: sha1 file error on close", f->name);
7ba502c4
NP
59 fd = 0;
60 } else
61 fd = f->fd;
7bf058f0 62 free(f);
7ba502c4 63 return fd;
c38138cd
LT
64}
65
66int sha1write(struct sha1file *f, void *buf, unsigned int count)
67{
68 while (count) {
69 unsigned offset = f->offset;
70 unsigned left = sizeof(f->buffer) - offset;
71 unsigned nr = count > left ? left : count;
a8032d12
NP
72 void *data;
73
74 if (f->do_crc)
75 f->crc32 = crc32(f->crc32, buf, nr);
76
77 if (nr == sizeof(f->buffer)) {
78 /* process full buffer directly without copy */
79 data = buf;
80 } else {
81 memcpy(f->buffer + offset, buf, nr);
82 data = f->buffer;
83 }
c38138cd 84
c38138cd
LT
85 count -= nr;
86 offset += nr;
1d7f171c 87 buf = (char *) buf + nr;
c38138cd
LT
88 left -= nr;
89 if (!left) {
9126f009 90 git_SHA1_Update(&f->ctx, data, offset);
e782e12f 91 flush(f, data, offset);
c38138cd
LT
92 offset = 0;
93 }
94 f->offset = offset;
95 }
96 return 0;
97}
98
4397f014 99struct sha1file *sha1fd(int fd, const char *name)
2a128d63
NP
100{
101 return sha1fd_throughput(fd, name, NULL);
102}
103
104struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
4397f014 105{
ec640ed1 106 struct sha1file *f = xmalloc(sizeof(*f));
4397f014 107 f->fd = fd;
4397f014 108 f->offset = 0;
218558af 109 f->total = 0;
2a128d63 110 f->tp = tp;
ec640ed1 111 f->name = name;
78d1e84f 112 f->do_crc = 0;
9126f009 113 git_SHA1_Init(&f->ctx);
4397f014
LT
114 return f;
115}
116
78d1e84f
NP
117void crc32_begin(struct sha1file *f)
118{
119 f->crc32 = crc32(0, Z_NULL, 0);
120 f->do_crc = 1;
121}
c38138cd 122
78d1e84f
NP
123uint32_t crc32_end(struct sha1file *f)
124{
125 f->do_crc = 0;
126 return f->crc32;
127}