Annotation of src/usr.bin/cvs/buf.c, Revision 1.21
1.21 ! xsa 1: /* $OpenBSD: buf.c,v 1.20 2005/12/20 16:55:21 xsa Exp $ */
1.1 jfb 2: /*
1.2 jfb 3: * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
1.1 jfb 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:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <ctype.h>
1.11 xsa 31: #include <errno.h>
1.1 jfb 32: #include <fcntl.h>
33: #include <stdarg.h>
1.11 xsa 34: #include <stdio.h>
35: #include <stdlib.h>
36: #include <string.h>
1.1 jfb 37: #include <unistd.h>
38:
39: #include "buf.h"
40: #include "log.h"
1.19 joris 41: #include "xmalloc.h"
1.1 jfb 42:
1.15 xsa 43: #define BUF_INCR 128
1.1 jfb 44:
45:
46: struct cvs_buf {
1.15 xsa 47: u_int cb_flags;
1.1 jfb 48:
49: /* buffer handle and size */
1.15 xsa 50: u_char *cb_buf;
51: size_t cb_size;
1.1 jfb 52:
53: /* start and length of valid data in buffer */
1.15 xsa 54: u_char *cb_cur;
55: size_t cb_len;
1.1 jfb 56: };
57:
58:
1.16 moritz 59: #define SIZE_LEFT(b) (b->cb_size - (size_t)(b->cb_cur - b->cb_buf) \
60: - b->cb_len)
1.1 jfb 61:
62:
1.15 xsa 63: static ssize_t cvs_buf_grow(BUF *, size_t);
1.1 jfb 64:
65:
66:
67: /*
68: * cvs_buf_alloc()
69: *
70: * Create a new buffer structure and return a pointer to it. This structure
71: * uses dynamically-allocated memory and must be freed with cvs_buf_free(),
72: * once the buffer is no longer needed.
73: */
1.15 xsa 74: BUF *
1.1 jfb 75: cvs_buf_alloc(size_t len, u_int flags)
76: {
77: BUF *b;
78:
1.19 joris 79: b = (BUF *)xmalloc(sizeof(*b));
80: b->cb_buf = xmalloc(len);
1.3 joris 81: memset(b->cb_buf, 0, len);
1.1 jfb 82:
83: b->cb_flags = flags;
84: b->cb_size = len;
1.8 jfb 85: b->cb_cur = b->cb_buf;
1.1 jfb 86: b->cb_len = 0;
87:
88: return (b);
89: }
90:
91:
92: /*
93: * cvs_buf_load()
94: *
95: * Open the file specified by <path> and load all of its contents into a
96: * buffer.
1.20 xsa 97: * Returns the loaded buffer on success.
1.1 jfb 98: */
1.15 xsa 99: BUF *
1.1 jfb 100: cvs_buf_load(const char *path, u_int flags)
101: {
102: int fd;
103: ssize_t ret;
104: size_t len;
1.8 jfb 105: u_char *bp;
1.1 jfb 106: struct stat st;
107: BUF *buf;
108:
1.20 xsa 109: if ((fd = open(path, O_RDONLY, 0600)) == -1)
110: fatal("cvs_buf_load: open: `%s': %s", path, strerror(errno));
1.1 jfb 111:
1.20 xsa 112: if (fstat(fd, &st) == -1)
113: fatal("cvs_buf_load: fstat: %s", strerror(errno));
1.1 jfb 114:
1.20 xsa 115: if ((buf = cvs_buf_alloc((size_t)st.st_size, flags)) == NULL)
116: fatal("cvs_buf_load: cvs_buf_alloc failed");
1.1 jfb 117:
118: for (bp = buf->cb_cur; ; bp += (size_t)ret) {
1.16 moritz 119: len = SIZE_LEFT(buf);
1.1 jfb 120: ret = read(fd, bp, len);
121: if (ret == -1) {
1.8 jfb 122: cvs_buf_free(buf);
1.20 xsa 123: fatal("cvs_buf_load: read: %s", strerror(errno));
1.5 deraadt 124: } else if (ret == 0)
1.1 jfb 125: break;
126:
127: buf->cb_len += (size_t)ret;
128: }
129:
130: (void)close(fd);
131:
132: return (buf);
133: }
134:
135:
136: /*
137: * cvs_buf_free()
138: *
139: * Free the buffer <b> and all associated data.
140: */
141: void
142: cvs_buf_free(BUF *b)
143: {
1.19 joris 144: xfree(b->cb_buf);
145: xfree(b);
1.1 jfb 146: }
147:
148:
149: /*
150: * cvs_buf_release()
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: */
1.17 xsa 156: void *
1.1 jfb 157: cvs_buf_release(BUF *b)
158: {
1.8 jfb 159: u_char *tmp;
1.6 tedu 160:
1.1 jfb 161: tmp = b->cb_buf;
1.19 joris 162: xfree(b);
1.1 jfb 163: return (tmp);
164: }
165:
166:
167: /*
168: * cvs_buf_empty()
169: *
170: * Empty the contents of the buffer <b> and reset pointers.
171: */
172: void
173: cvs_buf_empty(BUF *b)
174: {
1.16 moritz 175: memset(b->cb_buf, 0, b->cb_size);
1.8 jfb 176: b->cb_cur = b->cb_buf;
1.1 jfb 177: b->cb_len = 0;
178: }
179:
180:
181: /*
182: * cvs_buf_copy()
183: *
184: * Copy the first <len> bytes of data in the buffer <b> starting at offset
185: * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
186: * Returns the number of bytes successfully copied, or -1 on failure.
187: */
188: ssize_t
189: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
190: {
191: size_t rc;
192:
193: if (off > b->cb_len)
1.21 ! xsa 194: fatal("cvs_buf_copy failed");
1.1 jfb 195:
196: rc = MIN(len, (b->cb_len - off));
1.16 moritz 197: memcpy(dst, b->cb_buf + off, rc);
1.1 jfb 198:
199: return (ssize_t)rc;
200: }
201:
202:
203: /*
204: * cvs_buf_set()
205: *
1.16 moritz 206: * Set the contents of the buffer <b> at offset <off> to the first <len>
207: * bytes of data found at <src>. If the buffer was not created with
208: * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
1.1 jfb 209: */
1.16 moritz 210: ssize_t
1.1 jfb 211: cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
212: {
213: size_t rlen;
214:
215: if (b->cb_size < (len + off)) {
1.20 xsa 216: if ((b->cb_flags & BUF_AUTOEXT) &&
217: (cvs_buf_grow(b, len + off - b->cb_size) < 0))
218: fatal("cvs_buf_set failed");
1.1 jfb 219: else
220: rlen = b->cb_size - off;
1.5 deraadt 221: } else
1.1 jfb 222: rlen = len;
223:
224: memcpy((b->cb_buf + off), src, rlen);
225:
226: if (b->cb_len == 0) {
227: b->cb_cur = b->cb_buf + off;
228: b->cb_len = rlen;
229: }
230:
1.16 moritz 231: return (rlen);
1.1 jfb 232: }
233:
234:
235: /*
236: * cvs_buf_putc()
237: *
238: * Append a single character <c> to the end of the buffer <b>.
1.21 ! xsa 239: * Returns 0 on success.
1.1 jfb 240: */
241: int
242: cvs_buf_putc(BUF *b, int c)
243: {
244: u_char *bp;
245:
246: bp = b->cb_cur + b->cb_len;
247: if (bp == (b->cb_buf + b->cb_size)) {
248: /* extend */
249: if (!(b->cb_flags & BUF_AUTOEXT) ||
1.18 xsa 250: (cvs_buf_grow(b, (size_t)BUF_INCR) < 0))
1.21 ! xsa 251: fatal("cvs_buf_putc failed");
1.1 jfb 252:
253: /* the buffer might have been moved */
254: bp = b->cb_cur + b->cb_len;
255: }
256: *bp = (u_char)c;
257: b->cb_len++;
258:
259: return (0);
260: }
261:
262:
263: /*
264: * cvs_buf_append()
265: *
266: * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
267: * buffer is too small to accept all data, it will attempt to append as much
268: * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
269: * will get resized to an appropriate size to accept all data.
270: * Returns the number of bytes successfully appended to the buffer, or -1
271: * on failure.
272: */
273: ssize_t
274: cvs_buf_append(BUF *b, const void *data, size_t len)
275: {
276: size_t left, rlen;
1.8 jfb 277: u_char *bp, *bep;
1.1 jfb 278:
279: bp = b->cb_cur + b->cb_len;
280: bep = b->cb_buf + b->cb_size;
281: left = bep - bp;
282: rlen = len;
283:
284: if (left < len) {
285: if (b->cb_flags & BUF_AUTOEXT) {
286: if (cvs_buf_grow(b, len - left) < 0)
1.20 xsa 287: fatal("cvs_buf_append failed");
1.1 jfb 288: bp = b->cb_cur + b->cb_len;
1.5 deraadt 289: } else
1.1 jfb 290: rlen = bep - bp;
291: }
292:
293: memcpy(bp, data, rlen);
294: b->cb_len += rlen;
295:
296: return (rlen);
297: }
298:
299:
300: /*
301: * cvs_buf_fappend()
302: *
303: */
304: int
305: cvs_buf_fappend(BUF *b, const char *fmt, ...)
306: {
307: int ret;
308: char *str;
309: va_list vap;
310:
311: va_start(vap, fmt);
1.4 pat 312: ret = vasprintf(&str, fmt, vap);
313: va_end(vap);
1.1 jfb 314:
1.20 xsa 315: if (ret == -1)
316: fatal("cvs_buf_fappend: failed to format data");
1.1 jfb 317:
1.18 xsa 318: ret = cvs_buf_append(b, str, (size_t)ret);
1.19 joris 319: xfree(str);
1.1 jfb 320: return (ret);
321: }
322:
323:
324: /*
1.16 moritz 325: * cvs_buf_len()
1.1 jfb 326: *
327: * Returns the size of the buffer that is being used.
328: */
329: size_t
1.16 moritz 330: cvs_buf_len(BUF *b)
1.1 jfb 331: {
332: return (b->cb_len);
333: }
334:
335:
336: /*
337: * cvs_buf_peek()
338: *
339: * Peek at the contents of the buffer <b> at offset <off>.
340: */
1.17 xsa 341: const void *
1.1 jfb 342: cvs_buf_peek(BUF *b, size_t off)
343: {
344: if (off >= b->cb_len)
345: return (NULL);
346:
347: return (b->cb_buf + off);
348: }
349:
350:
351: /*
1.7 djm 352: * cvs_buf_write_fd()
1.1 jfb 353: *
1.7 djm 354: * Write the contents of the buffer <b> to the specified <fd>
1.1 jfb 355: */
356: int
1.7 djm 357: cvs_buf_write_fd(BUF *b, int fd)
1.1 jfb 358: {
359: u_char *bp;
360: size_t len;
361: ssize_t ret;
362:
363: len = b->cb_len;
364: bp = b->cb_cur;
365:
366: do {
1.16 moritz 367: ret = write(fd, bp, len);
1.1 jfb 368: if (ret == -1) {
1.7 djm 369: if (errno == EINTR || errno == EAGAIN)
370: continue;
1.20 xsa 371: fatal("cvs_buf_write_fd failed");
1.1 jfb 372: }
373:
374: len -= (size_t)ret;
375: bp += (size_t)ret;
376: } while (len > 0);
377:
1.7 djm 378: return (0);
379: }
380:
381: /*
382: * cvs_buf_write()
383: *
384: * Write the contents of the buffer <b> to the file whose path is given in
385: * <path>. If the file does not exist, it is created with mode <mode>.
386: */
387:
388: int
389: cvs_buf_write(BUF *b, const char *path, mode_t mode)
390: {
391: int ret, fd;
392:
1.20 xsa 393: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1)
394: fatal("open: `%s': %s", path, strerror(errno));
1.7 djm 395:
1.12 xsa 396: ret = cvs_buf_write_fd(b, fd);
1.7 djm 397: if (ret == -1) {
1.10 jfb 398: cvs_log(LP_ERRNO, "failed to write to file `%s'", path);
1.7 djm 399: (void)unlink(path);
400: }
1.1 jfb 401: (void)close(fd);
402:
1.7 djm 403: return (ret);
1.1 jfb 404: }
405:
1.7 djm 406: /*
407: * cvs_buf_write_stmp()
408: *
1.13 joris 409: * Write the contents of the buffer <b> to a temporary file whose path is
1.7 djm 410: * specified using <template> (see mkstemp.3). NB. This function will modify
411: * <template>, as per mkstemp
412: */
413:
414: int
415: cvs_buf_write_stmp(BUF *b, char *template, mode_t mode)
416: {
417: int ret, fd;
418:
1.20 xsa 419: if ((fd = mkstemp(template)) == -1)
420: fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7 djm 421:
1.12 xsa 422: ret = cvs_buf_write_fd(b, fd);
1.7 djm 423: if (ret == -1) {
1.14 xsa 424: cvs_log(LP_ERRNO, "failed to write to temp file `%s'",
425: template);
1.7 djm 426: (void)unlink(template);
427: }
428: (void)close(fd);
429:
430: return (ret);
431: }
1.1 jfb 432:
433: /*
434: * cvs_buf_grow()
435: *
436: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
437: * operation regardless of the result.
438: * Returns the new size on success, or -1 on failure.
439: */
440: static ssize_t
441: cvs_buf_grow(BUF *b, size_t len)
442: {
443: void *tmp;
444: size_t diff;
445:
1.8 jfb 446: diff = b->cb_cur - b->cb_buf;
1.19 joris 447: tmp = xrealloc(b->cb_buf, b->cb_size + len);
1.1 jfb 448: b->cb_buf = (u_char *)tmp;
449: b->cb_size += len;
450:
451: /* readjust pointers in case the buffer moved in memory */
452: b->cb_cur = b->cb_buf + diff;
453:
454: return (ssize_t)b->cb_size;
455: }