Annotation of src/usr.bin/cvs/buf.c, Revision 1.52
1.52 ! joris 1: /* $OpenBSD: buf.c,v 1.51 2006/05/27 03:30:30 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:
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:
81: /*
82: * cvs_buf_load()
83: *
84: * Open the file specified by <path> and load all of its contents into a
85: * buffer.
1.20 xsa 86: * Returns the loaded buffer on success.
1.1 jfb 87: */
1.15 xsa 88: BUF *
1.1 jfb 89: cvs_buf_load(const char *path, u_int flags)
90: {
91: int fd;
92: ssize_t ret;
93: size_t len;
1.8 jfb 94: u_char *bp;
1.1 jfb 95: struct stat st;
96: BUF *buf;
97:
1.34 niallo 98: if ((fd = open(path, O_RDONLY, 0600)) == -1) {
1.51 joris 99: cvs_log(LP_ERR, "%s", path);
1.34 niallo 100: return (NULL);
101: }
1.1 jfb 102:
1.20 xsa 103: if (fstat(fd, &st) == -1)
104: fatal("cvs_buf_load: fstat: %s", strerror(errno));
1.1 jfb 105:
1.51 joris 106: buf = cvs_buf_alloc(st.st_size, flags);
1.1 jfb 107: for (bp = buf->cb_cur; ; bp += (size_t)ret) {
1.16 moritz 108: len = SIZE_LEFT(buf);
1.1 jfb 109: ret = read(fd, bp, len);
1.51 joris 110: if (ret == -1)
1.20 xsa 111: fatal("cvs_buf_load: read: %s", strerror(errno));
1.51 joris 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_set()
168: *
1.16 moritz 169: * Set the contents of the buffer <b> at offset <off> to the first <len>
170: * bytes of data found at <src>. If the buffer was not created with
171: * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
1.1 jfb 172: */
1.16 moritz 173: ssize_t
1.1 jfb 174: cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
175: {
1.23 xsa 176: size_t rlen = 0;
1.1 jfb 177:
178: if (b->cb_size < (len + off)) {
1.48 joris 179: if ((b->cb_flags & BUF_AUTOEXT)) {
1.46 ray 180: cvs_buf_grow(b, len + off - b->cb_size);
1.48 joris 181: rlen = len + off;
182: } else {
1.1 jfb 183: rlen = b->cb_size - off;
1.48 joris 184: }
185: } else {
1.1 jfb 186: rlen = len;
1.48 joris 187: }
1.1 jfb 188:
1.48 joris 189: b->cb_len = rlen;
1.1 jfb 190: memcpy((b->cb_buf + off), src, rlen);
191:
192: if (b->cb_len == 0) {
193: b->cb_cur = b->cb_buf + off;
194: b->cb_len = rlen;
195: }
196:
1.16 moritz 197: return (rlen);
1.1 jfb 198: }
199:
200: /*
201: * cvs_buf_putc()
202: *
203: * Append a single character <c> to the end of the buffer <b>.
204: */
1.39 xsa 205: void
1.1 jfb 206: cvs_buf_putc(BUF *b, int c)
207: {
208: u_char *bp;
209:
210: bp = b->cb_cur + b->cb_len;
211: if (bp == (b->cb_buf + b->cb_size)) {
212: /* extend */
1.46 ray 213: if (b->cb_flags & BUF_AUTOEXT)
214: cvs_buf_grow(b, (size_t)BUF_INCR);
215: else
1.21 xsa 216: fatal("cvs_buf_putc failed");
1.1 jfb 217:
218: /* the buffer might have been moved */
219: bp = b->cb_cur + b->cb_len;
220: }
221: *bp = (u_char)c;
222: b->cb_len++;
1.29 niallo 223: }
224:
225: /*
226: * cvs_buf_getc()
227: *
228: * Return u_char at buffer position <pos>.
229: *
230: */
231: u_char
1.40 ray 232: cvs_buf_getc(BUF *b, size_t pos)
1.29 niallo 233: {
234: return (b->cb_cur[pos]);
1.1 jfb 235: }
236:
237: /*
238: * cvs_buf_append()
239: *
240: * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
241: * buffer is too small to accept all data, it will attempt to append as much
242: * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
243: * will get resized to an appropriate size to accept all data.
1.27 joris 244: * Returns the number of bytes successfully appended to the buffer.
1.1 jfb 245: */
246: ssize_t
247: cvs_buf_append(BUF *b, const void *data, size_t len)
248: {
249: size_t left, rlen;
1.8 jfb 250: u_char *bp, *bep;
1.1 jfb 251:
252: bp = b->cb_cur + b->cb_len;
253: bep = b->cb_buf + b->cb_size;
254: left = bep - bp;
255: rlen = len;
256:
257: if (left < len) {
258: if (b->cb_flags & BUF_AUTOEXT) {
1.46 ray 259: cvs_buf_grow(b, len - left);
1.1 jfb 260: bp = b->cb_cur + b->cb_len;
1.5 deraadt 261: } else
1.1 jfb 262: rlen = bep - bp;
263: }
264:
265: memcpy(bp, data, rlen);
266: b->cb_len += rlen;
267:
268: return (rlen);
269: }
270:
271: /*
272: * cvs_buf_fappend()
273: *
274: */
1.37 ray 275: ssize_t
1.1 jfb 276: cvs_buf_fappend(BUF *b, const char *fmt, ...)
277: {
1.37 ray 278: ssize_t ret;
1.1 jfb 279: char *str;
280: va_list vap;
281:
282: va_start(vap, fmt);
1.4 pat 283: ret = vasprintf(&str, fmt, vap);
284: va_end(vap);
1.1 jfb 285:
1.20 xsa 286: if (ret == -1)
287: fatal("cvs_buf_fappend: failed to format data");
1.1 jfb 288:
1.18 xsa 289: ret = cvs_buf_append(b, str, (size_t)ret);
1.19 joris 290: xfree(str);
1.1 jfb 291: return (ret);
292: }
293:
294: /*
1.16 moritz 295: * cvs_buf_len()
1.1 jfb 296: *
297: * Returns the size of the buffer that is being used.
298: */
299: size_t
1.16 moritz 300: cvs_buf_len(BUF *b)
1.1 jfb 301: {
302: return (b->cb_len);
303: }
304:
305: /*
1.7 djm 306: * cvs_buf_write_fd()
1.1 jfb 307: *
1.7 djm 308: * Write the contents of the buffer <b> to the specified <fd>
1.1 jfb 309: */
310: int
1.7 djm 311: cvs_buf_write_fd(BUF *b, int fd)
1.1 jfb 312: {
313: u_char *bp;
314: size_t len;
315: ssize_t ret;
316:
317: len = b->cb_len;
318: bp = b->cb_cur;
319:
320: do {
1.16 moritz 321: ret = write(fd, bp, len);
1.1 jfb 322: if (ret == -1) {
1.7 djm 323: if (errno == EINTR || errno == EAGAIN)
324: continue;
1.22 xsa 325: return (-1);
1.1 jfb 326: }
327:
328: len -= (size_t)ret;
329: bp += (size_t)ret;
330: } while (len > 0);
331:
1.7 djm 332: return (0);
333: }
334:
335: /*
336: * cvs_buf_write()
337: *
338: * Write the contents of the buffer <b> to the file whose path is given in
339: * <path>. If the file does not exist, it is created with mode <mode>.
340: */
341: int
342: cvs_buf_write(BUF *b, const char *path, mode_t mode)
343: {
1.22 xsa 344: int fd;
1.32 niallo 345: open:
346: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
1.49 deraadt 347: if (errno == EACCES && unlink(path) != -1)
1.32 niallo 348: goto open;
349: else
350: fatal("open: `%s': %s", path, strerror(errno));
351: }
1.7 djm 352:
1.22 xsa 353: if (cvs_buf_write_fd(b, fd) == -1) {
1.7 djm 354: (void)unlink(path);
1.22 xsa 355: fatal("cvs_buf_write: cvs_buf_write_fd: `%s'", path);
1.7 djm 356: }
1.44 niallo 357:
358: if (fchmod(fd, mode) < 0)
1.51 joris 359: cvs_log(LP_ERR, "permissions not set on file %s", path);
1.44 niallo 360:
1.1 jfb 361: (void)close(fd);
1.24 joris 362:
1.22 xsa 363: return (0);
1.1 jfb 364: }
365:
1.7 djm 366: /*
367: * cvs_buf_write_stmp()
368: *
1.13 joris 369: * Write the contents of the buffer <b> to a temporary file whose path is
1.7 djm 370: * specified using <template> (see mkstemp.3). NB. This function will modify
371: * <template>, as per mkstemp
372: */
1.38 niallo 373: void
1.51 joris 374: cvs_buf_write_stmp(BUF *b, char *template, mode_t mode, struct timeval *tv)
1.7 djm 375: {
1.22 xsa 376: int fd;
1.7 djm 377:
1.20 xsa 378: if ((fd = mkstemp(template)) == -1)
379: fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7 djm 380:
1.22 xsa 381: if (cvs_buf_write_fd(b, fd) == -1) {
1.7 djm 382: (void)unlink(template);
1.22 xsa 383: fatal("cvs_buf_write_stmp: cvs_buf_write_fd: `%s'", template);
1.7 djm 384: }
1.45 joris 385:
1.44 niallo 386: if (fchmod(fd, mode) < 0)
1.51 joris 387: cvs_log(LP_ERR, "permissions not set on temporary file %s",
1.44 niallo 388: template);
389:
1.51 joris 390: if (tv != NULL) {
391: if (futimes(fd, tv) == -1)
392: fatal("cvs_buf_write_stmp: futimes failed");
393: }
394:
1.7 djm 395: (void)close(fd);
1.51 joris 396:
397: cvs_worklist_add(template, &temp_files);
1.7 djm 398: }
1.1 jfb 399:
400: /*
401: * cvs_buf_grow()
402: *
403: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
404: * operation regardless of the result.
405: */
1.46 ray 406: static void
1.1 jfb 407: cvs_buf_grow(BUF *b, size_t len)
408: {
409: void *tmp;
410: size_t diff;
411:
1.8 jfb 412: diff = b->cb_cur - b->cb_buf;
1.46 ray 413: tmp = xrealloc(b->cb_buf, 1, b->cb_size + len);
414: b->cb_buf = tmp;
1.1 jfb 415: b->cb_size += len;
416:
417: /* readjust pointers in case the buffer moved in memory */
418: b->cb_cur = b->cb_buf + diff;
419: }
1.43 xsa 420:
421: /*
422: * cvs_buf_copy()
423: *
424: * Copy the first <len> bytes of data in the buffer <b> starting at offset
425: * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
426: * Returns the number of bytes successfully copied, or -1 on failure.
427: */
428: ssize_t
429: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
430: {
431: size_t rc;
432:
433: if (off > b->cb_len)
434: fatal("cvs_buf_copy failed");
435:
436: rc = MIN(len, (b->cb_len - off));
437: memcpy(dst, b->cb_buf + off, rc);
438:
439: return (ssize_t)rc;
440: }
441:
442: /*
443: * cvs_buf_peek()
444: *
445: * Peek at the contents of the buffer <b> at offset <off>.
446: */
447: const void *
448: cvs_buf_peek(BUF *b, size_t off)
449: {
450: if (off >= b->cb_len)
451: return (NULL);
452:
453: return (b->cb_buf + off);
1.52 ! joris 454: }
! 455:
! 456: int
! 457: cvs_buf_differ(BUF *b1, BUF *b2)
! 458: {
! 459: char *c1, *c2;
! 460: int i, l1, l2, len, ret;
! 461:
! 462: l1 = cvs_buf_len(b1);
! 463: l2 = cvs_buf_len(b2);
! 464: len = MIN(l1, l2);
! 465:
! 466: if (l1 != l2)
! 467: return (1);
! 468:
! 469: c1 = cvs_buf_release(b1);
! 470: c2 = cvs_buf_release(b2);
! 471:
! 472: ret = memcmp(c1, c2, len);
! 473:
! 474: if (c1 != NULL)
! 475: xfree(c1);
! 476: if (c2 != NULL)
! 477: xfree(c2);
! 478:
! 479: return (ret);
1.43 xsa 480: }