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