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 free(b);
74 return NULL;
75 }
76 } else
77 b->cb_buf = NULL;
79 b->cb_size = len;
80 b->cb_len = 0;
82 return (b);
83 }
85 /*
86 * Open the file specified by <path> and load all of its contents into a
87 * buffer.
88 * Returns the loaded buffer on success or NULL on failure.
89 * Sets errno on error.
90 */
91 BUF *
92 buf_load(const char *path)
93 {
94 int fd;
95 ssize_t ret;
96 size_t len;
97 u_char *bp;
98 struct stat st;
99 BUF *buf;
101 buf = NULL;
103 if ((fd = open(path, O_RDONLY, 0600)) == -1)
104 goto out;
106 if (fstat(fd, &st) == -1)
107 goto out;
109 if ((uintmax_t)st.st_size > SIZE_MAX) {
110 errno = EFBIG;
111 goto out;
113 buf = buf_alloc(st.st_size);
114 for (bp = buf->cb_buf; ; bp += (size_t)ret) {
115 len = SIZE_LEFT(buf);
116 ret = read(fd, bp, len);
117 if (ret == -1) {
118 int saved_errno;
120 saved_errno = errno;
121 buf_free(buf);
122 buf = NULL;
123 errno = saved_errno;
124 goto out;
125 } else if (ret == 0)
126 break;
128 buf->cb_len += (size_t)ret;
131 out:
132 if (fd != -1) {
133 int saved_errno;
135 /* We may want to preserve errno here. */
136 saved_errno = errno;
137 (void)close(fd);
138 errno = saved_errno;
141 return (buf);
144 void
145 buf_free(BUF *b)
147 if (b == NULL)
148 return;
149 free(b->cb_buf);
150 free(b);
153 /*
154 * Free the buffer <b>'s structural information but do not free the contents
155 * of the buffer. Instead, they are returned and should be freed later using
156 * free().
157 */
158 void *
159 buf_release(BUF *b)
161 void *tmp;
163 tmp = b->cb_buf;
164 free(b);
165 return (tmp);
168 u_char *
169 buf_get(BUF *b)
171 return (b->cb_buf);
174 /*
175 * Empty the contents of the buffer <b> and reset pointers.
176 */
177 void
178 buf_empty(BUF *b)
180 memset(b->cb_buf, 0, b->cb_size);
181 b->cb_len = 0;
184 /*
185 * Append a single character <c> to the end of the buffer <b>.
186 */
187 const struct got_error *
188 buf_putc(BUF *b, int c)
190 const struct got_error *err = NULL;
191 u_char *bp;
193 if (SIZE_LEFT(b) == 0) {
194 err = buf_grow(b, BUF_INCR);
195 if (err)
196 return err;
198 bp = b->cb_buf + b->cb_len;
199 *bp = (u_char)c;
200 b->cb_len++;
201 return NULL;
204 /*
205 * Append a string <s> to the end of buffer <b>.
206 */
207 const struct got_error *
208 buf_puts(size_t *newlen, BUF *b, const char *str)
210 return buf_append(newlen, b, str, strlen(str));
213 /*
214 * Return u_char at buffer position <pos>.
215 */
216 u_char
217 buf_getc(BUF *b, size_t pos)
219 return (b->cb_buf[pos]);
222 /*
223 * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
224 * buffer is too small to accept all data, it will get resized to an
225 * appropriate size to accept all data.
226 * Returns the number of bytes successfully appended to the buffer.
227 */
228 const struct got_error *
229 buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
231 const struct got_error *err = NULL;
232 size_t left, rlen;
233 u_char *bp;
235 left = SIZE_LEFT(b);
236 rlen = len;
238 if (left < len) {
239 err = buf_grow(b, len - left);
240 if (err)
241 return err;
243 bp = b->cb_buf + b->cb_len;
244 memcpy(bp, data, rlen);
245 b->cb_len += rlen;
247 *newlen = rlen;
248 return NULL;
251 /*
252 * Returns the size of the buffer that is being used.
253 */
254 size_t
255 buf_len(BUF *b)
257 return (b->cb_len);
260 /*
261 * Write the contents of the buffer <b> to the specified <fd>
262 */
263 int
264 buf_write_fd(BUF *b, int fd)
266 u_char *bp;
267 size_t len;
268 ssize_t ret;
270 len = b->cb_len;
271 bp = b->cb_buf;
273 do {
274 ret = write(fd, bp, len);
275 if (ret == -1) {
276 if (errno == EINTR || errno == EAGAIN)
277 continue;
278 return (-1);
281 len -= (size_t)ret;
282 bp += (size_t)ret;
283 } while (len > 0);
285 return (0);
288 /*
289 * Write the contents of the buffer <b> to the file whose path is given in
290 * <path>. If the file does not exist, it is created with mode <mode>.
291 */
292 const struct got_error *
293 buf_write(BUF *b, const char *path, mode_t mode)
295 const struct got_error *err = NULL;
296 int fd;
297 open:
298 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
299 err = got_error_from_errno2("open", path);
300 if (errno == EACCES && unlink(path) != -1)
301 goto open;
302 else
303 return err;
306 if (buf_write_fd(b, fd) == -1) {
307 err = got_error_from_errno("buf_write_fd");
308 (void)unlink(path);
309 return err;
312 if (fchmod(fd, mode) < 0)
313 err = got_error_from_errno2("fchmod", path);
315 if (close(fd) != 0 && err == NULL)
316 err = got_error_from_errno2("close", path);
318 return err;
321 /*
322 * Write the contents of the buffer <b> to a temporary file whose path is
323 * specified using <template> (see mkstemp.3).
324 * NB. This function will modify <template>, as per mkstemp
325 */
326 const struct got_error *
327 buf_write_stmp(BUF *b, char *template, struct wklhead *temp_files)
329 const struct got_error *err = NULL;
330 int fd;
332 if ((fd = mkstemp(template)) == -1)
333 return got_error_from_errno("mkstemp");
335 worklist_add(template, temp_files);
337 if (buf_write_fd(b, fd) == -1) {
338 err = got_error_from_errno("buf_write_fd");
339 (void)unlink(template);
342 if (close(fd) != 0 && err == NULL)
343 err = got_error_from_errno("close");
345 return err;
348 /*
349 * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
350 * operation regardless of the result.
351 */
352 static const struct got_error *
353 buf_grow(BUF *b, size_t len)
355 u_char *buf;
356 buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
357 if (buf == NULL)
358 return got_error_from_errno("reallocarray");
359 b->cb_buf = buf;
360 b->cb_size += len;
361 return NULL;