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 BUF *
62 buf_alloc(size_t len)
63 {
64 BUF *b;
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 return NULL;
74 } else
75 b->cb_buf = NULL;
77 b->cb_size = len;
78 b->cb_len = 0;
80 return (b);
81 }
83 /*
84 * Open the file specified by <path> and load all of its contents into a
85 * buffer.
86 * Returns the loaded buffer on success or NULL on failure.
87 * Sets errno on error.
88 */
89 BUF *
90 buf_load(const char *path)
91 {
92 int fd;
93 ssize_t ret;
94 size_t len;
95 u_char *bp;
96 struct stat st;
97 BUF *buf;
99 buf = NULL;
101 if ((fd = open(path, O_RDONLY, 0600)) == -1)
102 goto out;
104 if (fstat(fd, &st) == -1)
105 goto out;
107 if ((uintmax_t)st.st_size > SIZE_MAX) {
108 errno = EFBIG;
109 goto out;
111 buf = buf_alloc(st.st_size);
112 for (bp = buf->cb_buf; ; bp += (size_t)ret) {
113 len = SIZE_LEFT(buf);
114 ret = read(fd, bp, len);
115 if (ret == -1) {
116 int saved_errno;
118 saved_errno = errno;
119 buf_free(buf);
120 buf = NULL;
121 errno = saved_errno;
122 goto out;
123 } else if (ret == 0)
124 break;
126 buf->cb_len += (size_t)ret;
129 out:
130 if (fd != -1) {
131 int saved_errno;
133 /* We may want to preserve errno here. */
134 saved_errno = errno;
135 (void)close(fd);
136 errno = saved_errno;
139 return (buf);
142 void
143 buf_free(BUF *b)
145 if (b == NULL)
146 return;
147 free(b->cb_buf);
148 free(b);
151 /*
152 * Free the buffer <b>'s structural information but do not free the contents
153 * of the buffer. Instead, they are returned and should be freed later using
154 * free().
155 */
156 void *
157 buf_release(BUF *b)
159 void *tmp;
161 tmp = b->cb_buf;
162 free(b);
163 return (tmp);
166 u_char *
167 buf_get(BUF *b)
169 return (b->cb_buf);
172 /*
173 * Empty the contents of the buffer <b> and reset pointers.
174 */
175 void
176 buf_empty(BUF *b)
178 memset(b->cb_buf, 0, b->cb_size);
179 b->cb_len = 0;
182 /*
183 * Append a single character <c> to the end of the buffer <b>.
184 */
185 const struct got_error *
186 buf_putc(BUF *b, int c)
188 const struct got_error *err = NULL;
189 u_char *bp;
191 if (SIZE_LEFT(b) == 0) {
192 err = buf_grow(b, BUF_INCR);
193 if (err)
194 return err;
196 bp = b->cb_buf + b->cb_len;
197 *bp = (u_char)c;
198 b->cb_len++;
199 return NULL;
202 /*
203 * Append a string <s> to the end of buffer <b>.
204 */
205 const struct got_error *
206 buf_puts(size_t *newlen, BUF *b, const char *str)
208 return buf_append(newlen, b, str, strlen(str));
211 /*
212 * Return u_char at buffer position <pos>.
213 */
214 u_char
215 buf_getc(BUF *b, size_t pos)
217 return (b->cb_buf[pos]);
220 /*
221 * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
222 * buffer is too small to accept all data, it will get resized to an
223 * appropriate size to accept all data.
224 * Returns the number of bytes successfully appended to the buffer.
225 */
226 const struct got_error *
227 buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
229 const struct got_error *err = NULL;
230 size_t left, rlen;
231 u_char *bp;
233 left = SIZE_LEFT(b);
234 rlen = len;
236 if (left < len) {
237 err = buf_grow(b, len - left);
238 if (err)
239 return err;
241 bp = b->cb_buf + b->cb_len;
242 memcpy(bp, data, rlen);
243 b->cb_len += rlen;
245 *newlen = rlen;
246 return NULL;
249 /*
250 * Returns the size of the buffer that is being used.
251 */
252 size_t
253 buf_len(BUF *b)
255 return (b->cb_len);
258 /*
259 * Write the contents of the buffer <b> to the specified <fd>
260 */
261 int
262 buf_write_fd(BUF *b, int fd)
264 u_char *bp;
265 size_t len;
266 ssize_t ret;
268 len = b->cb_len;
269 bp = b->cb_buf;
271 do {
272 ret = write(fd, bp, len);
273 if (ret == -1) {
274 if (errno == EINTR || errno == EAGAIN)
275 continue;
276 return (-1);
279 len -= (size_t)ret;
280 bp += (size_t)ret;
281 } while (len > 0);
283 return (0);
286 /*
287 * Write the contents of the buffer <b> to the file whose path is given in
288 * <path>. If the file does not exist, it is created with mode <mode>.
289 */
290 const struct got_error *
291 buf_write(BUF *b, const char *path, mode_t mode)
293 const struct got_error *err = NULL;
294 int fd;
295 open:
296 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
297 if (errno == EACCES && unlink(path) != -1)
298 goto open;
299 else
300 return got_error_from_errno2("open", path);
303 if (buf_write_fd(b, fd) == -1) {
304 err = got_error_from_errno("buf_write_fd");
305 (void)unlink(path);
306 return err;
309 if (fchmod(fd, mode) < 0)
310 err = got_error_from_errno2("fchmod", path);
312 if (close(fd) != 0 && err == NULL)
313 err = got_error_from_errno2("close", path);
315 return err;
318 /*
319 * Write the contents of the buffer <b> to a temporary file whose path is
320 * specified using <template> (see mkstemp.3).
321 * NB. This function will modify <template>, as per mkstemp
322 */
323 const struct got_error *
324 buf_write_stmp(BUF *b, char *template, struct wklhead *temp_files)
326 const struct got_error *err = NULL;
327 int fd;
329 if ((fd = mkstemp(template)) == -1)
330 return got_error_from_errno("mkstemp");
332 worklist_add(template, temp_files);
334 if (buf_write_fd(b, fd) == -1) {
335 err = got_error_from_errno("buf_write_fd");
336 (void)unlink(template);
339 if (close(fd) != 0 && err == NULL)
340 err = got_error_from_errno("close");
342 return err;
345 /*
346 * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
347 * operation regardless of the result.
348 */
349 static const struct got_error *
350 buf_grow(BUF *b, size_t len)
352 u_char *buf;
353 buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
354 if (buf == NULL)
355 return got_error_from_errno("reallocarray");
356 b->cb_buf = buf;
357 b->cb_size += len;
358 return NULL;