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