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