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