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