1 /* $OpenBSD: buf.c,v 1.27 2016/10/16 13:35:51 okan Exp $ */
3 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/queue.h>
41 #include "got_error.h"
46 /* buffer handle, buffer size, and data length */
52 #define SIZE_LEFT(b) ((b)->cb_size - (b)->cb_len)
54 static const struct got_error *buf_grow(BUF *, size_t);
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.
61 const struct got_error *
62 buf_alloc(BUF **b, size_t len)
64 const struct got_error *err = NULL;
66 *b = malloc(sizeof(**b));
69 /* Postpone creation of zero-sized buffers */
71 (*b)->cb_buf = calloc(1, len);
72 if ((*b)->cb_buf == NULL) {
73 err = got_error_from_errno("calloc");
88 * Open the file specified by <path> and load all of its contents into a
90 * Returns the loaded buffer on success or NULL on failure.
91 * Sets errno on error.
93 const struct got_error *
94 buf_load(BUF **buf, const char *path)
96 const struct got_error *err = NULL;
105 fd = open(path, O_RDONLY, 0600);
107 return got_error_from_errno2("open", path);
109 if (fstat(fd, &st) == -1) {
110 err = got_error_from_errno2("fstat", path);
114 if ((uintmax_t)st.st_size > SIZE_MAX) {
115 err = got_error_set_errno(EFBIG, path);
118 err = buf_alloc(buf, st.st_size);
121 for (bp = (*buf)->cb_buf; ; bp += (size_t)ret) {
122 len = SIZE_LEFT(*buf);
123 ret = read(fd, bp, len);
125 err = got_error_from_errno2("read", path);
130 (*buf)->cb_len += (size_t)ret;
134 if (close(fd) == -1 && err == NULL)
135 err = got_error_from_errno2("close", path);
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
174 * Empty the contents of the buffer <b> and reset pointers.
179 memset(b->cb_buf, 0, b->cb_size);
184 * Append a single character <c> to the end of the buffer <b>.
186 const struct got_error *
187 buf_putc(BUF *b, int c)
189 const struct got_error *err = NULL;
192 if (SIZE_LEFT(b) == 0) {
193 err = buf_grow(b, BUF_INCR);
197 bp = b->cb_buf + b->cb_len;
204 * Append a string <s> to the end of buffer <b>.
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));
213 * Return u_char at buffer position <pos>.
216 buf_getc(BUF *b, size_t pos)
218 return (b->cb_buf[pos]);
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.
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;
238 err = buf_grow(b, len - left);
242 bp = b->cb_buf + b->cb_len;
243 memcpy(bp, data, rlen);
251 * Returns the size of the buffer that is being used.
260 * Write the contents of the buffer <b> to the specified <fd>
263 buf_write_fd(BUF *b, int fd)
273 ret = write(fd, bp, len);
275 if (errno == EINTR || errno == EAGAIN)
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>.
291 const struct got_error *
292 buf_write(BUF *b, const char *path, mode_t mode)
294 const struct got_error *err = NULL;
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)
305 if (buf_write_fd(b, fd) == -1) {
306 err = got_error_from_errno("buf_write_fd");
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);
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
325 const struct got_error *
326 buf_write_stmp(BUF *b, char *template, struct wklhead *temp_files)
328 const struct got_error *err = NULL;
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");
348 * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
349 * operation regardless of the result.
351 static const struct got_error *
352 buf_grow(BUF *b, size_t len)
355 buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
357 return got_error_from_errno("reallocarray");