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