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