Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (103 sloc) 2.311 kb
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
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"
2a128d6 add throughput display to git-push
Nicolas Pitre authored
11 #include "progress.h"
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
12 #include "csum-file.h"
13
78b713a Make sha1flush void and remove conditional return.
David Rientjes authored
14 static void sha1flush(struct sha1file *f, unsigned int count)
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
15 {
16 void *buf = f->buffer;
17
18 for (;;) {
1c15afb xread/xwrite: do not worry about EINTR at calling sites.
Junio C Hamano authored
19 int ret = xwrite(f->fd, buf, count);
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
20 if (ret > 0) {
218558a make display of total transferred more accurate
Nicolas Pitre authored
21 f->total += ret;
22 display_throughput(f->tp, f->total);
1d7f171 Remove all void-pointer arithmetic.
Florian Forster authored
23 buf = (char *) buf + ret;
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
24 count -= ret;
25 if (count)
26 continue;
78b713a Make sha1flush void and remove conditional return.
David Rientjes authored
27 return;
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
28 }
29 if (!ret)
e180884 csum-file interface updates: return resulting SHA1
Linus Torvalds authored
30 die("sha1 file '%s' write error. Out of diskspace", f->name);
31 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
32 }
33 }
34
f021536 Alter sha1close() 3rd argument to request flush only
Dana L. How authored
35 int sha1close(struct sha1file *f, unsigned char *result, int final)
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
36 {
7ba502c pack-objects.c: fix some global variable abuse and memory leaks
Nicolas Pitre authored
37 int fd;
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
38 unsigned offset = f->offset;
39 if (offset) {
40 SHA1_Update(&f->ctx, f->buffer, offset);
41 sha1flush(f, offset);
f021536 Alter sha1close() 3rd argument to request flush only
Dana L. How authored
42 f->offset = 0;
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
43 }
7ba502c pack-objects.c: fix some global variable abuse and memory leaks
Nicolas Pitre authored
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;
7bf058f @sigprof [PATCH] Plug memory leak in sha1close()
sigprof authored
56 free(f);
7ba502c pack-objects.c: fix some global variable abuse and memory leaks
Nicolas Pitre authored
57 return fd;
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
58 }
59
60 int sha1write(struct sha1file *f, void *buf, unsigned int count)
61 {
78d1e84 compute a CRC32 for each object as stored in a pack
Nicolas Pitre authored
62 if (f->do_crc)
63 f->crc32 = crc32(f->crc32, buf, count);
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
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;
1d7f171 Remove all void-pointer arithmetic.
Florian Forster authored
72 buf = (char *) buf + nr;
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
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
4397f01 csum-file: add "sha1fd()" to create a SHA1 csum file from an existing fi...
Linus Torvalds authored
84 struct sha1file *sha1fd(int fd, const char *name)
85 {
2a128d6 add throughput display to git-push
Nicolas Pitre authored
86 return sha1fd_throughput(fd, name, NULL);
87 }
88
89 struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
90 {
ec640ed remove dead code from the csum-file interface
Nicolas Pitre authored
91 struct sha1file *f = xmalloc(sizeof(*f));
4397f01 csum-file: add "sha1fd()" to create a SHA1 csum file from an existing fi...
Linus Torvalds authored
92 f->fd = fd;
93 f->offset = 0;
218558a make display of total transferred more accurate
Nicolas Pitre authored
94 f->total = 0;
2a128d6 add throughput display to git-push
Nicolas Pitre authored
95 f->tp = tp;
ec640ed remove dead code from the csum-file interface
Nicolas Pitre authored
96 f->name = name;
78d1e84 compute a CRC32 for each object as stored in a pack
Nicolas Pitre authored
97 f->do_crc = 0;
4397f01 csum-file: add "sha1fd()" to create a SHA1 csum file from an existing fi...
Linus Torvalds authored
98 SHA1_Init(&f->ctx);
99 return f;
100 }
101
78d1e84 compute a CRC32 for each object as stored in a pack
Nicolas Pitre authored
102 void crc32_begin(struct sha1file *f)
103 {
104 f->crc32 = crc32(0, Z_NULL, 0);
105 f->do_crc = 1;
106 }
c38138c git-pack-objects: write the pack files with a SHA1 csum
Linus Torvalds authored
107
78d1e84 compute a CRC32 for each object as stored in a pack
Nicolas Pitre authored
108 uint32_t crc32_end(struct sha1file *f)
109 {
110 f->do_crc = 0;
111 return f->crc32;
112 }
Something went wrong with that request. Please try again.