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