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