Blame


1 74b37681 2019-02-07 stsp /* $OpenBSD: buf.c,v 1.27 2016/10/16 13:35:51 okan Exp $ */
2 74b37681 2019-02-07 stsp /*
3 74b37681 2019-02-07 stsp * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
4 74b37681 2019-02-07 stsp * All rights reserved.
5 74b37681 2019-02-07 stsp *
6 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
7 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
8 74b37681 2019-02-07 stsp * are met:
9 74b37681 2019-02-07 stsp *
10 74b37681 2019-02-07 stsp * 1. Redistributions of source code must retain the above copyright
11 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer.
12 74b37681 2019-02-07 stsp * 2. The name of the author may not be used to endorse or promote products
13 74b37681 2019-02-07 stsp * derived from this software without specific prior written permission.
14 74b37681 2019-02-07 stsp *
15 74b37681 2019-02-07 stsp * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 74b37681 2019-02-07 stsp * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 74b37681 2019-02-07 stsp * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 74b37681 2019-02-07 stsp * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 74b37681 2019-02-07 stsp * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 74b37681 2019-02-07 stsp * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 74b37681 2019-02-07 stsp * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 74b37681 2019-02-07 stsp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 74b37681 2019-02-07 stsp * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 74b37681 2019-02-07 stsp * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 74b37681 2019-02-07 stsp */
26 74b37681 2019-02-07 stsp
27 74b37681 2019-02-07 stsp #include <sys/queue.h>
28 74b37681 2019-02-07 stsp #include <sys/stat.h>
29 74b37681 2019-02-07 stsp
30 74b37681 2019-02-07 stsp #include <errno.h>
31 74b37681 2019-02-07 stsp #include <fcntl.h>
32 74b37681 2019-02-07 stsp #include <stdint.h>
33 74b37681 2019-02-07 stsp #include <stdio.h>
34 74b37681 2019-02-07 stsp #include <stdlib.h>
35 74b37681 2019-02-07 stsp #include <string.h>
36 74b37681 2019-02-07 stsp #include <unistd.h>
37 74b37681 2019-02-07 stsp
38 74b37681 2019-02-07 stsp #include "buf.h"
39 74b37681 2019-02-07 stsp #include "worklist.h"
40 74b37681 2019-02-07 stsp
41 af45e626 2019-02-08 stsp #include "got_error.h"
42 af45e626 2019-02-08 stsp
43 74b37681 2019-02-07 stsp #define BUF_INCR 128
44 74b37681 2019-02-07 stsp
45 74b37681 2019-02-07 stsp struct buf {
46 74b37681 2019-02-07 stsp /* buffer handle, buffer size, and data length */
47 74b37681 2019-02-07 stsp u_char *cb_buf;
48 74b37681 2019-02-07 stsp size_t cb_size;
49 74b37681 2019-02-07 stsp size_t cb_len;
50 74b37681 2019-02-07 stsp };
51 74b37681 2019-02-07 stsp
52 575e8218 2019-10-07 stsp #define SIZE_LEFT(b) ((b)->cb_size - (b)->cb_len)
53 74b37681 2019-02-07 stsp
54 af45e626 2019-02-08 stsp static const struct got_error *buf_grow(BUF *, size_t);
55 74b37681 2019-02-07 stsp
56 74b37681 2019-02-07 stsp /*
57 74b37681 2019-02-07 stsp * Create a new buffer structure and return a pointer to it. This structure
58 74b37681 2019-02-07 stsp * uses dynamically-allocated memory and must be freed with buf_free(), once
59 74b37681 2019-02-07 stsp * the buffer is no longer needed.
60 74b37681 2019-02-07 stsp */
61 575e8218 2019-10-07 stsp const struct got_error *
62 575e8218 2019-10-07 stsp buf_alloc(BUF **b, size_t len)
63 74b37681 2019-02-07 stsp {
64 575e8218 2019-10-07 stsp const struct got_error *err = NULL;
65 74b37681 2019-02-07 stsp
66 575e8218 2019-10-07 stsp *b = malloc(sizeof(**b));
67 575e8218 2019-10-07 stsp if (*b == NULL)
68 af45e626 2019-02-08 stsp return NULL;
69 74b37681 2019-02-07 stsp /* Postpone creation of zero-sized buffers */
70 af45e626 2019-02-08 stsp if (len > 0) {
71 575e8218 2019-10-07 stsp (*b)->cb_buf = calloc(1, len);
72 575e8218 2019-10-07 stsp if ((*b)->cb_buf == NULL) {
73 575e8218 2019-10-07 stsp err = got_error_from_errno("calloc");
74 575e8218 2019-10-07 stsp free(*b);
75 575e8218 2019-10-07 stsp *b = NULL;
76 575e8218 2019-10-07 stsp return err;
77 57ebf19f 2019-08-28 hiltjo }
78 af45e626 2019-02-08 stsp } else
79 575e8218 2019-10-07 stsp (*b)->cb_buf = NULL;
80 74b37681 2019-02-07 stsp
81 575e8218 2019-10-07 stsp (*b)->cb_size = len;
82 575e8218 2019-10-07 stsp (*b)->cb_len = 0;
83 74b37681 2019-02-07 stsp
84 575e8218 2019-10-07 stsp return NULL;
85 74b37681 2019-02-07 stsp }
86 74b37681 2019-02-07 stsp
87 74b37681 2019-02-07 stsp /*
88 74b37681 2019-02-07 stsp * Open the file specified by <path> and load all of its contents into a
89 74b37681 2019-02-07 stsp * buffer.
90 74b37681 2019-02-07 stsp * Returns the loaded buffer on success or NULL on failure.
91 74b37681 2019-02-07 stsp * Sets errno on error.
92 74b37681 2019-02-07 stsp */
93 575e8218 2019-10-07 stsp const struct got_error *
94 575e8218 2019-10-07 stsp buf_load(BUF **buf, const char *path)
95 74b37681 2019-02-07 stsp {
96 575e8218 2019-10-07 stsp const struct got_error *err = NULL;
97 74b37681 2019-02-07 stsp int fd;
98 74b37681 2019-02-07 stsp ssize_t ret;
99 74b37681 2019-02-07 stsp size_t len;
100 74b37681 2019-02-07 stsp u_char *bp;
101 74b37681 2019-02-07 stsp struct stat st;
102 74b37681 2019-02-07 stsp
103 575e8218 2019-10-07 stsp *buf = NULL;
104 74b37681 2019-02-07 stsp
105 575e8218 2019-10-07 stsp fd = open(path, O_RDONLY, 0600);
106 575e8218 2019-10-07 stsp if (fd == -1)
107 575e8218 2019-10-07 stsp return got_error_from_errno2("open", path);
108 74b37681 2019-02-07 stsp
109 575e8218 2019-10-07 stsp if (fstat(fd, &st) == -1) {
110 575e8218 2019-10-07 stsp err = got_error_from_errno2("fstat", path);
111 74b37681 2019-02-07 stsp goto out;
112 575e8218 2019-10-07 stsp }
113 74b37681 2019-02-07 stsp
114 74b37681 2019-02-07 stsp if ((uintmax_t)st.st_size > SIZE_MAX) {
115 575e8218 2019-10-07 stsp err = got_error_set_errno(EFBIG, path);
116 74b37681 2019-02-07 stsp goto out;
117 74b37681 2019-02-07 stsp }
118 575e8218 2019-10-07 stsp err = buf_alloc(buf, st.st_size);
119 575e8218 2019-10-07 stsp if (err)
120 575e8218 2019-10-07 stsp goto out;
121 575e8218 2019-10-07 stsp for (bp = (*buf)->cb_buf; ; bp += (size_t)ret) {
122 575e8218 2019-10-07 stsp len = SIZE_LEFT(*buf);
123 74b37681 2019-02-07 stsp ret = read(fd, bp, len);
124 74b37681 2019-02-07 stsp if (ret == -1) {
125 575e8218 2019-10-07 stsp err = got_error_from_errno2("read", path);
126 74b37681 2019-02-07 stsp goto out;
127 74b37681 2019-02-07 stsp } else if (ret == 0)
128 74b37681 2019-02-07 stsp break;
129 74b37681 2019-02-07 stsp
130 575e8218 2019-10-07 stsp (*buf)->cb_len += (size_t)ret;
131 74b37681 2019-02-07 stsp }
132 74b37681 2019-02-07 stsp
133 74b37681 2019-02-07 stsp out:
134 575e8218 2019-10-07 stsp if (close(fd) == -1 && err == NULL)
135 575e8218 2019-10-07 stsp err = got_error_from_errno2("close", path);
136 575e8218 2019-10-07 stsp if (err) {
137 575e8218 2019-10-07 stsp buf_free(*buf);
138 575e8218 2019-10-07 stsp *buf = NULL;
139 74b37681 2019-02-07 stsp }
140 575e8218 2019-10-07 stsp return err;
141 74b37681 2019-02-07 stsp }
142 74b37681 2019-02-07 stsp
143 74b37681 2019-02-07 stsp void
144 74b37681 2019-02-07 stsp buf_free(BUF *b)
145 74b37681 2019-02-07 stsp {
146 74b37681 2019-02-07 stsp if (b == NULL)
147 74b37681 2019-02-07 stsp return;
148 74b37681 2019-02-07 stsp free(b->cb_buf);
149 74b37681 2019-02-07 stsp free(b);
150 74b37681 2019-02-07 stsp }
151 74b37681 2019-02-07 stsp
152 74b37681 2019-02-07 stsp /*
153 74b37681 2019-02-07 stsp * Free the buffer <b>'s structural information but do not free the contents
154 74b37681 2019-02-07 stsp * of the buffer. Instead, they are returned and should be freed later using
155 74b37681 2019-02-07 stsp * free().
156 74b37681 2019-02-07 stsp */
157 74b37681 2019-02-07 stsp void *
158 74b37681 2019-02-07 stsp buf_release(BUF *b)
159 74b37681 2019-02-07 stsp {
160 74b37681 2019-02-07 stsp void *tmp;
161 74b37681 2019-02-07 stsp
162 74b37681 2019-02-07 stsp tmp = b->cb_buf;
163 74b37681 2019-02-07 stsp free(b);
164 74b37681 2019-02-07 stsp return (tmp);
165 74b37681 2019-02-07 stsp }
166 74b37681 2019-02-07 stsp
167 74b37681 2019-02-07 stsp u_char *
168 74b37681 2019-02-07 stsp buf_get(BUF *b)
169 74b37681 2019-02-07 stsp {
170 74b37681 2019-02-07 stsp return (b->cb_buf);
171 74b37681 2019-02-07 stsp }
172 74b37681 2019-02-07 stsp
173 74b37681 2019-02-07 stsp /*
174 74b37681 2019-02-07 stsp * Empty the contents of the buffer <b> and reset pointers.
175 74b37681 2019-02-07 stsp */
176 74b37681 2019-02-07 stsp void
177 74b37681 2019-02-07 stsp buf_empty(BUF *b)
178 74b37681 2019-02-07 stsp {
179 74b37681 2019-02-07 stsp memset(b->cb_buf, 0, b->cb_size);
180 74b37681 2019-02-07 stsp b->cb_len = 0;
181 74b37681 2019-02-07 stsp }
182 74b37681 2019-02-07 stsp
183 74b37681 2019-02-07 stsp /*
184 74b37681 2019-02-07 stsp * Append a single character <c> to the end of the buffer <b>.
185 74b37681 2019-02-07 stsp */
186 af45e626 2019-02-08 stsp const struct got_error *
187 74b37681 2019-02-07 stsp buf_putc(BUF *b, int c)
188 74b37681 2019-02-07 stsp {
189 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
190 74b37681 2019-02-07 stsp u_char *bp;
191 74b37681 2019-02-07 stsp
192 af45e626 2019-02-08 stsp if (SIZE_LEFT(b) == 0) {
193 af45e626 2019-02-08 stsp err = buf_grow(b, BUF_INCR);
194 af45e626 2019-02-08 stsp if (err)
195 af45e626 2019-02-08 stsp return err;
196 af45e626 2019-02-08 stsp }
197 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
198 74b37681 2019-02-07 stsp *bp = (u_char)c;
199 74b37681 2019-02-07 stsp b->cb_len++;
200 af45e626 2019-02-08 stsp return NULL;
201 74b37681 2019-02-07 stsp }
202 74b37681 2019-02-07 stsp
203 74b37681 2019-02-07 stsp /*
204 74b37681 2019-02-07 stsp * Append a string <s> to the end of buffer <b>.
205 74b37681 2019-02-07 stsp */
206 af45e626 2019-02-08 stsp const struct got_error *
207 af45e626 2019-02-08 stsp buf_puts(size_t *newlen, BUF *b, const char *str)
208 74b37681 2019-02-07 stsp {
209 af45e626 2019-02-08 stsp return buf_append(newlen, b, str, strlen(str));
210 74b37681 2019-02-07 stsp }
211 74b37681 2019-02-07 stsp
212 74b37681 2019-02-07 stsp /*
213 74b37681 2019-02-07 stsp * Return u_char at buffer position <pos>.
214 74b37681 2019-02-07 stsp */
215 74b37681 2019-02-07 stsp u_char
216 74b37681 2019-02-07 stsp buf_getc(BUF *b, size_t pos)
217 74b37681 2019-02-07 stsp {
218 74b37681 2019-02-07 stsp return (b->cb_buf[pos]);
219 74b37681 2019-02-07 stsp }
220 74b37681 2019-02-07 stsp
221 74b37681 2019-02-07 stsp /*
222 74b37681 2019-02-07 stsp * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
223 74b37681 2019-02-07 stsp * buffer is too small to accept all data, it will get resized to an
224 74b37681 2019-02-07 stsp * appropriate size to accept all data.
225 74b37681 2019-02-07 stsp * Returns the number of bytes successfully appended to the buffer.
226 74b37681 2019-02-07 stsp */
227 af45e626 2019-02-08 stsp const struct got_error *
228 af45e626 2019-02-08 stsp buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
229 74b37681 2019-02-07 stsp {
230 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
231 74b37681 2019-02-07 stsp size_t left, rlen;
232 74b37681 2019-02-07 stsp u_char *bp;
233 74b37681 2019-02-07 stsp
234 74b37681 2019-02-07 stsp left = SIZE_LEFT(b);
235 74b37681 2019-02-07 stsp rlen = len;
236 74b37681 2019-02-07 stsp
237 af45e626 2019-02-08 stsp if (left < len) {
238 af45e626 2019-02-08 stsp err = buf_grow(b, len - left);
239 af45e626 2019-02-08 stsp if (err)
240 af45e626 2019-02-08 stsp return err;
241 af45e626 2019-02-08 stsp }
242 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
243 74b37681 2019-02-07 stsp memcpy(bp, data, rlen);
244 74b37681 2019-02-07 stsp b->cb_len += rlen;
245 74b37681 2019-02-07 stsp
246 af45e626 2019-02-08 stsp *newlen = rlen;
247 af45e626 2019-02-08 stsp return NULL;
248 74b37681 2019-02-07 stsp }
249 74b37681 2019-02-07 stsp
250 74b37681 2019-02-07 stsp /*
251 74b37681 2019-02-07 stsp * Returns the size of the buffer that is being used.
252 74b37681 2019-02-07 stsp */
253 74b37681 2019-02-07 stsp size_t
254 74b37681 2019-02-07 stsp buf_len(BUF *b)
255 74b37681 2019-02-07 stsp {
256 74b37681 2019-02-07 stsp return (b->cb_len);
257 74b37681 2019-02-07 stsp }
258 74b37681 2019-02-07 stsp
259 74b37681 2019-02-07 stsp /*
260 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the specified <fd>
261 74b37681 2019-02-07 stsp */
262 74b37681 2019-02-07 stsp int
263 74b37681 2019-02-07 stsp buf_write_fd(BUF *b, int fd)
264 74b37681 2019-02-07 stsp {
265 74b37681 2019-02-07 stsp u_char *bp;
266 74b37681 2019-02-07 stsp size_t len;
267 74b37681 2019-02-07 stsp ssize_t ret;
268 74b37681 2019-02-07 stsp
269 74b37681 2019-02-07 stsp len = b->cb_len;
270 74b37681 2019-02-07 stsp bp = b->cb_buf;
271 74b37681 2019-02-07 stsp
272 74b37681 2019-02-07 stsp do {
273 74b37681 2019-02-07 stsp ret = write(fd, bp, len);
274 74b37681 2019-02-07 stsp if (ret == -1) {
275 74b37681 2019-02-07 stsp if (errno == EINTR || errno == EAGAIN)
276 74b37681 2019-02-07 stsp continue;
277 74b37681 2019-02-07 stsp return (-1);
278 74b37681 2019-02-07 stsp }
279 74b37681 2019-02-07 stsp
280 74b37681 2019-02-07 stsp len -= (size_t)ret;
281 74b37681 2019-02-07 stsp bp += (size_t)ret;
282 74b37681 2019-02-07 stsp } while (len > 0);
283 74b37681 2019-02-07 stsp
284 74b37681 2019-02-07 stsp return (0);
285 74b37681 2019-02-07 stsp }
286 74b37681 2019-02-07 stsp
287 74b37681 2019-02-07 stsp /*
288 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the file whose path is given in
289 74b37681 2019-02-07 stsp * <path>. If the file does not exist, it is created with mode <mode>.
290 74b37681 2019-02-07 stsp */
291 af45e626 2019-02-08 stsp const struct got_error *
292 74b37681 2019-02-07 stsp buf_write(BUF *b, const char *path, mode_t mode)
293 74b37681 2019-02-07 stsp {
294 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
295 74b37681 2019-02-07 stsp int fd;
296 74b37681 2019-02-07 stsp open:
297 74b37681 2019-02-07 stsp if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
298 202329ae 2019-08-11 stsp err = got_error_from_errno2("open", path);
299 74b37681 2019-02-07 stsp if (errno == EACCES && unlink(path) != -1)
300 74b37681 2019-02-07 stsp goto open;
301 74b37681 2019-02-07 stsp else
302 202329ae 2019-08-11 stsp return err;
303 74b37681 2019-02-07 stsp }
304 74b37681 2019-02-07 stsp
305 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
307 74b37681 2019-02-07 stsp (void)unlink(path);
308 af45e626 2019-02-08 stsp return err;
309 74b37681 2019-02-07 stsp }
310 74b37681 2019-02-07 stsp
311 74b37681 2019-02-07 stsp if (fchmod(fd, mode) < 0)
312 638f9024 2019-05-13 stsp err = got_error_from_errno2("fchmod", path);
313 74b37681 2019-02-07 stsp
314 3a6ce05a 2019-02-11 stsp if (close(fd) != 0 && err == NULL)
315 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path);
316 74b37681 2019-02-07 stsp
317 af45e626 2019-02-08 stsp return err;
318 74b37681 2019-02-07 stsp }
319 74b37681 2019-02-07 stsp
320 74b37681 2019-02-07 stsp /*
321 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to a temporary file whose path is
322 74b37681 2019-02-07 stsp * specified using <template> (see mkstemp.3).
323 74b37681 2019-02-07 stsp * NB. This function will modify <template>, as per mkstemp
324 74b37681 2019-02-07 stsp */
325 af45e626 2019-02-08 stsp const struct got_error *
326 af45e626 2019-02-08 stsp buf_write_stmp(BUF *b, char *template, struct wklhead *temp_files)
327 74b37681 2019-02-07 stsp {
328 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
329 74b37681 2019-02-07 stsp int fd;
330 74b37681 2019-02-07 stsp
331 74b37681 2019-02-07 stsp if ((fd = mkstemp(template)) == -1)
332 638f9024 2019-05-13 stsp return got_error_from_errno("mkstemp");
333 74b37681 2019-02-07 stsp
334 af45e626 2019-02-08 stsp worklist_add(template, temp_files);
335 74b37681 2019-02-07 stsp
336 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
337 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
338 74b37681 2019-02-07 stsp (void)unlink(template);
339 74b37681 2019-02-07 stsp }
340 74b37681 2019-02-07 stsp
341 3a6ce05a 2019-02-11 stsp if (close(fd) != 0 && err == NULL)
342 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
343 3a6ce05a 2019-02-11 stsp
344 af45e626 2019-02-08 stsp return err;
345 74b37681 2019-02-07 stsp }
346 74b37681 2019-02-07 stsp
347 74b37681 2019-02-07 stsp /*
348 74b37681 2019-02-07 stsp * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
349 74b37681 2019-02-07 stsp * operation regardless of the result.
350 74b37681 2019-02-07 stsp */
351 af45e626 2019-02-08 stsp static const struct got_error *
352 74b37681 2019-02-07 stsp buf_grow(BUF *b, size_t len)
353 74b37681 2019-02-07 stsp {
354 af45e626 2019-02-08 stsp u_char *buf;
355 af45e626 2019-02-08 stsp buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
356 af45e626 2019-02-08 stsp if (buf == NULL)
357 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
358 af45e626 2019-02-08 stsp b->cb_buf = buf;
359 74b37681 2019-02-07 stsp b->cb_size += len;
360 af45e626 2019-02-08 stsp return NULL;
361 74b37681 2019-02-07 stsp }