[PATCH] Plug memory leak in write_sha1_to_fd()
[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"
11#include "csum-file.h"
12
13static int sha1flush(struct sha1file *f, unsigned int count)
14{
15 void *buf = f->buffer;
16
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;
25 }
26 if (!ret)
e1808845 27 die("sha1 file '%s' write error. Out of diskspace", f->name);
c38138cd
LT
28 if (errno == EAGAIN || errno == EINTR)
29 continue;
e1808845 30 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
c38138cd
LT
31 }
32}
33
e1808845 34int sha1close(struct sha1file *f, unsigned char *result, int update)
c38138cd
LT
35{
36 unsigned offset = f->offset;
37 if (offset) {
38 SHA1_Update(&f->ctx, f->buffer, offset);
39 sha1flush(f, offset);
40 }
41 SHA1_Final(f->buffer, &f->ctx);
e1808845
LT
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));
c38138cd
LT
48 return 0;
49}
50
51int sha1write(struct sha1file *f, void *buf, unsigned int count)
52{
53 while (count) {
54 unsigned offset = f->offset;
55 unsigned left = sizeof(f->buffer) - offset;
56 unsigned nr = count > left ? left : count;
57
58 memcpy(f->buffer + offset, buf, nr);
59 count -= nr;
60 offset += nr;
2700628e 61 buf += nr;
c38138cd
LT
62 left -= nr;
63 if (!left) {
64 SHA1_Update(&f->ctx, f->buffer, offset);
65 sha1flush(f, offset);
66 offset = 0;
67 }
68 f->offset = offset;
69 }
70 return 0;
71}
72
73struct sha1file *sha1create(const char *fmt, ...)
74{
c38138cd
LT
75 struct sha1file *f;
76 unsigned len;
77 va_list arg;
78 int fd;
79
e1808845
LT
80 f = xmalloc(sizeof(*f));
81
c38138cd 82 va_start(arg, fmt);
e1808845 83 len = vsnprintf(f->name, sizeof(f->name), fmt, arg);
c38138cd 84 va_end(arg);
c38138cd
LT
85 if (len >= PATH_MAX)
86 die("you wascally wabbit, you");
e1808845
LT
87 f->namelen = len;
88
f312de01 89 fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0666);
c38138cd 90 if (fd < 0)
e1808845 91 die("unable to open %s (%s)", f->name, strerror(errno));
c38138cd
LT
92 f->fd = fd;
93 f->error = 0;
94 f->offset = 0;
95 SHA1_Init(&f->ctx);
96 return f;
97}
98
4397f014
LT
99struct sha1file *sha1fd(int fd, const char *name)
100{
101 struct sha1file *f;
102 unsigned len;
103
104 f = xmalloc(sizeof(*f));
105
106 len = strlen(name);
107 if (len >= PATH_MAX)
108 die("you wascally wabbit, you");
109 f->namelen = len;
110 memcpy(f->name, name, len+1);
111
112 f->fd = fd;
113 f->error = 0;
114 f->offset = 0;
115 SHA1_Init(&f->ctx);
116 return f;
117}
118
c38138cd
LT
119int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)
120{
121 z_stream stream;
122 unsigned long maxsize;
123 void *out;
124
125 memset(&stream, 0, sizeof(stream));
126 deflateInit(&stream, Z_DEFAULT_COMPRESSION);
127 maxsize = deflateBound(&stream, size);
128 out = xmalloc(maxsize);
129
130 /* Compress it */
131 stream.next_in = in;
132 stream.avail_in = size;
133
134 stream.next_out = out;
135 stream.avail_out = maxsize;
136
137 while (deflate(&stream, Z_FINISH) == Z_OK)
138 /* nothing */;
139 deflateEnd(&stream);
140
141 size = stream.total_out;
142 sha1write(f, out, size);
143 free(out);
144 return size;
145}
146
147