Annotation of src/usr.bin/cvs/buf.c, Revision 1.11
1.11 ! xsa 1: /* $OpenBSD: buf.c,v 1.10 2005/05/26 21:46:36 jfb 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"
1.9 xsa 40: #include "cvs.h"
1.1 jfb 41: #include "log.h"
42:
43:
44: #define BUF_INCR 128
45:
46:
47: struct cvs_buf {
48: u_int cb_flags;
49:
50: /* buffer handle and size */
1.8 jfb 51: u_char *cb_buf;
1.1 jfb 52: size_t cb_size;
53:
54: /* start and length of valid data in buffer */
55: u_char *cb_cur;
56: size_t cb_len;
57: };
58:
59:
60:
1.8 jfb 61: #define SIZE_LEFT(b) ((size_t)(b->cb_buf - b->cb_cur) + b->cb_size)
1.1 jfb 62:
63:
64: static ssize_t cvs_buf_grow (BUF *, size_t);
65:
66:
67:
68: /*
69: * cvs_buf_alloc()
70: *
71: * Create a new buffer structure and return a pointer to it. This structure
72: * uses dynamically-allocated memory and must be freed with cvs_buf_free(),
73: * once the buffer is no longer needed.
74: */
75: BUF*
76: cvs_buf_alloc(size_t len, u_int flags)
77: {
78: BUF *b;
79:
80: b = (BUF *)malloc(sizeof(*b));
81: if (b == NULL) {
82: cvs_log(LP_ERRNO, "failed to allocate buffer");
83: return (NULL);
84: }
85:
86: b->cb_buf = malloc(len);
87: if (b->cb_buf == NULL) {
88: cvs_log(LP_ERRNO, "failed to allocate buffer");
89: free(b);
90: return (NULL);
91: }
1.3 joris 92: memset(b->cb_buf, 0, len);
1.1 jfb 93:
94: b->cb_flags = flags;
95: b->cb_size = len;
1.8 jfb 96: b->cb_cur = b->cb_buf;
1.1 jfb 97: b->cb_len = 0;
98:
99: return (b);
100: }
101:
102:
103: /*
104: * cvs_buf_load()
105: *
106: * Open the file specified by <path> and load all of its contents into a
107: * buffer.
108: * Returns the loaded buffer on success, or NULL on failure.
109: */
110: BUF*
111: cvs_buf_load(const char *path, u_int flags)
112: {
113: int fd;
114: ssize_t ret;
115: size_t len;
1.8 jfb 116: u_char *bp;
1.1 jfb 117: struct stat st;
118: BUF *buf;
119:
120: fd = open(path, O_RDONLY, 0600);
121: if (fd == -1) {
122: cvs_log(LP_ERRNO, "failed to open buffer source");
123: return (NULL);
124: }
125:
126: if (fstat(fd, &st) == -1) {
127: cvs_log(LP_ERRNO, "failed to stat buffer source");
128: (void)close(fd);
129: return (NULL);
130: }
131:
132: buf = cvs_buf_alloc((size_t)st.st_size, flags);
133: if (buf == NULL) {
134: (void)close(fd);
135: return (NULL);
136: }
137:
138: for (bp = buf->cb_cur; ; bp += (size_t)ret) {
139: len = MIN(SIZE_LEFT(buf), 4096);
140: ret = read(fd, bp, len);
141: if (ret == -1) {
142: cvs_log(LP_ERRNO, "read failed from buffer source");
143: (void)close(fd);
1.8 jfb 144: cvs_buf_free(buf);
1.1 jfb 145: return (NULL);
1.5 deraadt 146: } else if (ret == 0)
1.1 jfb 147: break;
148:
149: buf->cb_len += (size_t)ret;
150: }
151:
152: (void)close(fd);
153:
154: return (buf);
155: }
156:
157:
158: /*
159: * cvs_buf_free()
160: *
161: * Free the buffer <b> and all associated data.
162: */
163: void
164: cvs_buf_free(BUF *b)
165: {
166: free(b->cb_buf);
167: free(b);
168: }
169:
170:
171: /*
172: * cvs_buf_release()
173: *
174: * Free the buffer <b>'s structural information but do not free the contents
175: * of the buffer. Instead, they are returned and should be freed later using
176: * free().
177: */
178: void*
179: cvs_buf_release(BUF *b)
180: {
1.8 jfb 181: u_char *tmp;
1.6 tedu 182:
1.1 jfb 183: tmp = b->cb_buf;
184: free(b);
185: return (tmp);
186: }
187:
188:
189: /*
190: * cvs_buf_empty()
191: *
192: * Empty the contents of the buffer <b> and reset pointers.
193: */
194: void
195: cvs_buf_empty(BUF *b)
196: {
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));
218: memcpy(dst, b->cb_buf, rc);
219:
220: return (ssize_t)rc;
221: }
222:
223:
224: /*
225: * cvs_buf_set()
226: *
227: * Set the contents of the buffer <b> to the first <len> bytes of data found
228: * at <src>. If the buffer was not created with BUF_AUTOEXT, as many bytes
229: * as possible will be copied in the buffer.
230: */
231: int
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:
252: return (int)rlen;
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: /*
348: * cvs_buf_size()
349: *
350: * Returns the size of the buffer that is being used.
351: */
352: size_t
353: cvs_buf_size(BUF *b)
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: */
364: const void*
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 {
390: ret = write(fd, bp, MIN(len, 8192));
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:
1.9 xsa 416: ret = 0;
417:
1.7 djm 418: fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
419: if (fd == -1) {
1.10 jfb 420: cvs_log(LP_ERRNO, "failed to open file `%s'", path);
1.7 djm 421: return (-1);
422: }
423:
1.9 xsa 424: if (!cvs_noexec)
425: ret = cvs_buf_write_fd(b, fd);
426:
1.7 djm 427: if (ret == -1) {
1.10 jfb 428: cvs_log(LP_ERRNO, "failed to write to file `%s'", path);
1.7 djm 429: (void)unlink(path);
430: }
1.1 jfb 431: (void)close(fd);
432:
1.7 djm 433: return (ret);
1.1 jfb 434: }
435:
1.7 djm 436: /*
437: * cvs_buf_write_stmp()
438: *
439: * Write the contents of the buffer <b> to a temporary file whose path is
440: * specified using <template> (see mkstemp.3). NB. This function will modify
441: * <template>, as per mkstemp
442: */
443:
444: int
445: cvs_buf_write_stmp(BUF *b, char *template, mode_t mode)
446: {
447: int ret, fd;
448:
1.9 xsa 449: ret = 0;
450:
1.7 djm 451: fd = mkstemp(template);
452: if (fd == -1) {
453: cvs_log(LP_ERRNO, "failed to mkstemp file `%s': %s",
454: template, strerror(errno));
455: return (-1);
456: }
457:
1.9 xsa 458: if (!cvs_noexec)
459: ret = cvs_buf_write_fd(b, fd);
460:
1.7 djm 461: if (ret == -1) {
462: cvs_log(LP_ERRNO, "failed to write to temp file `%s': %s",
463: template, strerror(errno));
464: (void)unlink(template);
465: }
466: (void)close(fd);
467:
468: return (ret);
469: }
1.1 jfb 470:
471: /*
472: * cvs_buf_grow()
473: *
474: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
475: * operation regardless of the result.
476: * Returns the new size on success, or -1 on failure.
477: */
478: static ssize_t
479: cvs_buf_grow(BUF *b, size_t len)
480: {
481: void *tmp;
482: size_t diff;
483:
1.8 jfb 484: diff = b->cb_cur - b->cb_buf;
1.1 jfb 485: tmp = realloc(b->cb_buf, b->cb_size + len);
486: if (tmp == NULL) {
487: cvs_log(LP_ERRNO, "failed to grow buffer");
488: return (-1);
489: }
490: b->cb_buf = (u_char *)tmp;
491: b->cb_size += len;
492:
493: /* readjust pointers in case the buffer moved in memory */
494: b->cb_cur = b->cb_buf + diff;
495:
496: return (ssize_t)b->cb_size;
497: }