Annotation of src/usr.bin/rcs/buf.c, Revision 1.25
1.25 ! nicm 1: /* $OpenBSD: buf.c,v 1.24 2015/02/05 12:59:58 millert 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:
97: if ((fd = open(path, O_RDONLY, 0600)) == -1)
98: goto out;
1.1 joris 99:
100: if (fstat(fd, &st) == -1)
1.8 ray 101: goto out;
1.1 joris 102:
1.8 ray 103: if (st.st_size > SIZE_MAX) {
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.25 ! nicm 141: free(b->cb_buf);
! 142: free(b);
1.1 joris 143: }
144:
145: /*
146: * Free the buffer <b>'s structural information but do not free the contents
147: * of the buffer. Instead, they are returned and should be freed later using
1.25 ! nicm 148: * free().
1.1 joris 149: */
150: void *
1.15 ray 151: buf_release(BUF *b)
1.1 joris 152: {
1.4 niallo 153: void *tmp;
1.1 joris 154:
155: tmp = b->cb_buf;
1.25 ! nicm 156: free(b);
1.1 joris 157: return (tmp);
1.5 niallo 158: }
159:
1.11 xsa 160: u_char *
1.15 ray 161: buf_get(BUF *b)
1.5 niallo 162: {
163: return (b->cb_buf);
1.1 joris 164: }
165:
166: /*
167: * Empty the contents of the buffer <b> and reset pointers.
168: */
169: void
1.15 ray 170: buf_empty(BUF *b)
1.1 joris 171: {
172: memset(b->cb_buf, 0, b->cb_size);
173: b->cb_len = 0;
174: }
175:
176: /*
177: * Append a single character <c> to the end of the buffer <b>.
178: */
179: void
1.15 ray 180: buf_putc(BUF *b, int c)
1.1 joris 181: {
182: u_char *bp;
183:
1.17 tobias 184: if (SIZE_LEFT(b) == 0)
185: buf_grow(b, BUF_INCR);
1.10 ray 186: bp = b->cb_buf + b->cb_len;
1.1 joris 187: *bp = (u_char)c;
188: b->cb_len++;
1.22 nicm 189: }
190:
191: /*
192: * Append a string <s> to the end of buffer <b>.
193: */
194: void
195: buf_puts(BUF *b, const char *str)
196: {
197: buf_append(b, str, strlen(str));
1.1 joris 198: }
199:
200: /*
201: * Return u_char at buffer position <pos>.
202: */
203: u_char
1.15 ray 204: buf_getc(BUF *b, size_t pos)
1.1 joris 205: {
1.10 ray 206: return (b->cb_buf[pos]);
1.1 joris 207: }
208:
209: /*
210: * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
1.18 nicm 211: * buffer is too small to accept all data, it will get resized to an
212: * appropriate size to accept all data.
1.1 joris 213: * Returns the number of bytes successfully appended to the buffer.
214: */
1.7 ray 215: size_t
1.15 ray 216: buf_append(BUF *b, const void *data, size_t len)
1.1 joris 217: {
218: size_t left, rlen;
1.17 tobias 219: u_char *bp;
1.1 joris 220:
1.17 tobias 221: left = SIZE_LEFT(b);
1.1 joris 222: rlen = len;
223:
1.17 tobias 224: if (left < len)
1.16 ray 225: buf_grow(b, len - left);
1.17 tobias 226: bp = b->cb_buf + b->cb_len;
1.1 joris 227: memcpy(bp, data, rlen);
228: b->cb_len += rlen;
229:
230: return (rlen);
231: }
232:
233: /*
234: * Returns the size of the buffer that is being used.
235: */
236: size_t
1.15 ray 237: buf_len(BUF *b)
1.1 joris 238: {
239: return (b->cb_len);
240: }
241:
242: /*
243: * Write the contents of the buffer <b> to the specified <fd>
244: */
245: int
1.15 ray 246: buf_write_fd(BUF *b, int fd)
1.1 joris 247: {
248: u_char *bp;
249: size_t len;
250: ssize_t ret;
251:
252: len = b->cb_len;
1.10 ray 253: bp = b->cb_buf;
1.1 joris 254:
255: do {
256: ret = write(fd, bp, len);
257: if (ret == -1) {
258: if (errno == EINTR || errno == EAGAIN)
259: continue;
260: return (-1);
261: }
262:
263: len -= (size_t)ret;
264: bp += (size_t)ret;
265: } while (len > 0);
266:
267: return (0);
268: }
269:
270: /*
271: * Write the contents of the buffer <b> to the file whose path is given in
272: * <path>. If the file does not exist, it is created with mode <mode>.
273: */
274: int
1.15 ray 275: buf_write(BUF *b, const char *path, mode_t mode)
1.1 joris 276: {
277: int fd;
278: open:
279: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
280: if (errno == EACCES && unlink(path) != -1)
281: goto open;
282: else
1.3 xsa 283: err(1, "%s", path);
1.1 joris 284: }
285:
1.15 ray 286: if (buf_write_fd(b, fd) == -1) {
1.1 joris 287: (void)unlink(path);
1.15 ray 288: errx(1, "buf_write: buf_write_fd: `%s'", path);
1.1 joris 289: }
290:
291: if (fchmod(fd, mode) < 0)
292: warn("permissions not set on file %s", path);
293:
294: (void)close(fd);
295:
296: return (0);
297: }
298:
299: /*
300: * Write the contents of the buffer <b> to a temporary file whose path is
1.18 nicm 301: * specified using <template> (see mkstemp.3).
302: * NB. This function will modify <template>, as per mkstemp
1.1 joris 303: */
304: void
1.15 ray 305: buf_write_stmp(BUF *b, char *template)
1.1 joris 306: {
307: int fd;
308:
309: if ((fd = mkstemp(template)) == -1)
1.3 xsa 310: err(1, "%s", template);
1.1 joris 311:
1.14 ray 312: worklist_add(template, &temp_files);
1.1 joris 313:
1.15 ray 314: if (buf_write_fd(b, fd) == -1) {
1.1 joris 315: (void)unlink(template);
1.15 ray 316: errx(1, "buf_write_stmp: buf_write_fd: `%s'", template);
1.1 joris 317: }
318:
319: (void)close(fd);
320: }
321:
322: /*
323: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
324: * operation regardless of the result.
325: */
326: static void
1.15 ray 327: buf_grow(BUF *b, size_t len)
1.1 joris 328: {
1.23 deraadt 329: b->cb_buf = xreallocarray(b->cb_buf, 1, b->cb_size + len);
1.1 joris 330: b->cb_size += len;
331: }