Blob


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