Annotation of src/usr.bin/rcs/buf.c, Revision 1.29
1.29 ! deraadt 1: /* $OpenBSD: buf.c,v 1.28 2019/06/28 13:35:03 deraadt Exp $ */
1.1 joris 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: */
26:
1.12 xsa 27: #include <sys/queue.h>
28: #include <sys/stat.h>
29:
30: #include <err.h>
31: #include <errno.h>
32: #include <fcntl.h>
1.24 millert 33: #include <stdint.h>
1.12 xsa 34: #include <stdio.h>
1.25 nicm 35: #include <stdlib.h>
1.12 xsa 36: #include <string.h>
37: #include <unistd.h>
1.1 joris 38:
39: #include "buf.h"
40: #include "xmalloc.h"
41: #include "worklist.h"
42:
43: #define BUF_INCR 128
44:
1.15 ray 45: struct buf {
1.10 ray 46: /* buffer handle, buffer size, and data length */
1.1 joris 47: u_char *cb_buf;
48: size_t cb_size;
49: size_t cb_len;
50: };
51:
1.10 ray 52: #define SIZE_LEFT(b) (b->cb_size - b->cb_len)
1.1 joris 53:
1.15 ray 54: static void buf_grow(BUF *, size_t);
1.1 joris 55:
56: /*
57: * Create a new buffer structure and return a pointer to it. This structure
1.18 nicm 58: * uses dynamically-allocated memory and must be freed with buf_free(), once
59: * the buffer is no longer needed.
1.1 joris 60: */
61: BUF *
1.16 ray 62: buf_alloc(size_t len)
1.1 joris 63: {
64: BUF *b;
65:
66: b = xmalloc(sizeof(*b));
67: /* Postpone creation of zero-sized buffers */
68: if (len > 0)
69: b->cb_buf = xcalloc(1, len);
70: else
71: b->cb_buf = NULL;
72:
73: b->cb_size = len;
74: b->cb_len = 0;
75:
76: return (b);
77: }
78:
79: /*
80: * Open the file specified by <path> and load all of its contents into a
81: * buffer.
1.8 ray 82: * Returns the loaded buffer on success or NULL on failure.
83: * Sets errno on error.
1.1 joris 84: */
85: BUF *
1.16 ray 86: buf_load(const char *path)
1.1 joris 87: {
88: int fd;
89: ssize_t ret;
90: size_t len;
91: u_char *bp;
92: struct stat st;
93: BUF *buf;
94:
1.8 ray 95: buf = NULL;
96:
1.29 ! deraadt 97: if ((fd = open(path, O_RDONLY)) == -1)
1.8 ray 98: goto out;
1.1 joris 99:
100: if (fstat(fd, &st) == -1)
1.8 ray 101: goto out;
1.1 joris 102:
1.27 okan 103: if ((uintmax_t)st.st_size > SIZE_MAX) {
1.8 ray 104: errno = EFBIG;
105: goto out;
106: }
1.16 ray 107: buf = buf_alloc(st.st_size);
1.10 ray 108: for (bp = buf->cb_buf; ; bp += (size_t)ret) {
1.1 joris 109: len = SIZE_LEFT(buf);
110: ret = read(fd, bp, len);
111: if (ret == -1) {
1.8 ray 112: int saved_errno;
113:
114: saved_errno = errno;
1.15 ray 115: buf_free(buf);
1.8 ray 116: buf = NULL;
117: errno = saved_errno;
118: goto out;
1.1 joris 119: } else if (ret == 0)
120: break;
121:
122: buf->cb_len += (size_t)ret;
123: }
124:
1.8 ray 125: out:
126: if (fd != -1) {
127: int saved_errno;
128:
129: /* We may want to preserve errno here. */
130: saved_errno = errno;
131: (void)close(fd);
132: errno = saved_errno;
133: }
1.1 joris 134:
135: return (buf);
136: }
137:
138: void
1.15 ray 139: buf_free(BUF *b)
1.1 joris 140: {
1.26 nicm 141: if (b == NULL)
142: return;
1.25 nicm 143: free(b->cb_buf);
144: free(b);
1.1 joris 145: }
146:
147: /*
148: * Free the buffer <b>'s structural information but do not free the contents
149: * of the buffer. Instead, they are returned and should be freed later using
1.25 nicm 150: * free().
1.1 joris 151: */
152: void *
1.15 ray 153: buf_release(BUF *b)
1.1 joris 154: {
1.4 niallo 155: void *tmp;
1.1 joris 156:
157: tmp = b->cb_buf;
1.25 nicm 158: free(b);
1.1 joris 159: return (tmp);
1.5 niallo 160: }
161:
1.11 xsa 162: u_char *
1.15 ray 163: buf_get(BUF *b)
1.5 niallo 164: {
165: return (b->cb_buf);
1.1 joris 166: }
167:
168: /*
169: * Empty the contents of the buffer <b> and reset pointers.
170: */
171: void
1.15 ray 172: buf_empty(BUF *b)
1.1 joris 173: {
174: memset(b->cb_buf, 0, b->cb_size);
175: b->cb_len = 0;
176: }
177:
178: /*
179: * Append a single character <c> to the end of the buffer <b>.
180: */
181: void
1.15 ray 182: buf_putc(BUF *b, int c)
1.1 joris 183: {
184: u_char *bp;
185:
1.17 tobias 186: if (SIZE_LEFT(b) == 0)
187: buf_grow(b, BUF_INCR);
1.10 ray 188: bp = b->cb_buf + b->cb_len;
1.1 joris 189: *bp = (u_char)c;
190: b->cb_len++;
1.22 nicm 191: }
192:
193: /*
194: * Append a string <s> to the end of buffer <b>.
195: */
196: void
197: buf_puts(BUF *b, const char *str)
198: {
199: buf_append(b, str, strlen(str));
1.1 joris 200: }
201:
202: /*
203: * Return u_char at buffer position <pos>.
204: */
205: u_char
1.15 ray 206: buf_getc(BUF *b, size_t pos)
1.1 joris 207: {
1.10 ray 208: return (b->cb_buf[pos]);
1.1 joris 209: }
210:
211: /*
212: * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
1.18 nicm 213: * buffer is too small to accept all data, it will get resized to an
214: * appropriate size to accept all data.
1.1 joris 215: * Returns the number of bytes successfully appended to the buffer.
216: */
1.7 ray 217: size_t
1.15 ray 218: buf_append(BUF *b, const void *data, size_t len)
1.1 joris 219: {
220: size_t left, rlen;
1.17 tobias 221: u_char *bp;
1.1 joris 222:
1.17 tobias 223: left = SIZE_LEFT(b);
1.1 joris 224: rlen = len;
225:
1.17 tobias 226: if (left < len)
1.16 ray 227: buf_grow(b, len - left);
1.17 tobias 228: bp = b->cb_buf + b->cb_len;
1.1 joris 229: memcpy(bp, data, rlen);
230: b->cb_len += rlen;
231:
232: return (rlen);
233: }
234:
235: /*
236: * Returns the size of the buffer that is being used.
237: */
238: size_t
1.15 ray 239: buf_len(BUF *b)
1.1 joris 240: {
241: return (b->cb_len);
242: }
243:
244: /*
245: * Write the contents of the buffer <b> to the specified <fd>
246: */
247: int
1.15 ray 248: buf_write_fd(BUF *b, int fd)
1.1 joris 249: {
250: u_char *bp;
251: size_t len;
252: ssize_t ret;
253:
254: len = b->cb_len;
1.10 ray 255: bp = b->cb_buf;
1.1 joris 256:
257: do {
258: ret = write(fd, bp, len);
259: if (ret == -1) {
260: if (errno == EINTR || errno == EAGAIN)
261: continue;
262: return (-1);
263: }
264:
265: len -= (size_t)ret;
266: bp += (size_t)ret;
267: } while (len > 0);
268:
269: return (0);
270: }
271:
272: /*
273: * Write the contents of the buffer <b> to the file whose path is given in
274: * <path>. If the file does not exist, it is created with mode <mode>.
275: */
276: int
1.15 ray 277: buf_write(BUF *b, const char *path, mode_t mode)
1.1 joris 278: {
279: int fd;
280: open:
281: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
282: if (errno == EACCES && unlink(path) != -1)
283: goto open;
284: else
1.3 xsa 285: err(1, "%s", path);
1.1 joris 286: }
287:
1.15 ray 288: if (buf_write_fd(b, fd) == -1) {
1.1 joris 289: (void)unlink(path);
1.15 ray 290: errx(1, "buf_write: buf_write_fd: `%s'", path);
1.1 joris 291: }
292:
1.28 deraadt 293: if (fchmod(fd, mode) == -1)
1.1 joris 294: warn("permissions not set on file %s", path);
295:
296: (void)close(fd);
297:
298: return (0);
299: }
300:
301: /*
302: * Write the contents of the buffer <b> to a temporary file whose path is
1.18 nicm 303: * specified using <template> (see mkstemp.3).
304: * NB. This function will modify <template>, as per mkstemp
1.1 joris 305: */
306: void
1.15 ray 307: buf_write_stmp(BUF *b, char *template)
1.1 joris 308: {
309: int fd;
310:
311: if ((fd = mkstemp(template)) == -1)
1.3 xsa 312: err(1, "%s", template);
1.1 joris 313:
1.14 ray 314: worklist_add(template, &temp_files);
1.1 joris 315:
1.15 ray 316: if (buf_write_fd(b, fd) == -1) {
1.1 joris 317: (void)unlink(template);
1.15 ray 318: errx(1, "buf_write_stmp: buf_write_fd: `%s'", template);
1.1 joris 319: }
320:
321: (void)close(fd);
322: }
323:
324: /*
325: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
326: * operation regardless of the result.
327: */
328: static void
1.15 ray 329: buf_grow(BUF *b, size_t len)
1.1 joris 330: {
1.23 deraadt 331: b->cb_buf = xreallocarray(b->cb_buf, 1, b->cb_size + len);
1.1 joris 332: b->cb_size += len;
333: }