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