Annotation of src/usr.bin/cvs/buf.c, Revision 1.76
1.76 ! ray 1: /* $OpenBSD: buf.c,v 1.75 2010/07/23 08:31:19 ray 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.74 joris 41: #define BUF_GROW(bp, len) \
42: do { \
43: b->cb_buf = xrealloc(b->cb_buf, 1, b->cb_size + len); \
44: b->cb_size += len; \
45: } while (0);
1.1 jfb 46:
1.76 ! ray 47: struct buf {
1.15 xsa 48: u_char *cb_buf;
49: size_t cb_size;
50: size_t cb_len;
1.1 jfb 51: };
52:
1.15 xsa 53: BUF *
1.76 ! ray 54: buf_alloc(size_t len)
1.1 jfb 55: {
56: BUF *b;
57:
1.42 ray 58: b = xmalloc(sizeof(*b));
1.76 ! ray 59: /* Postpone creation of zero-sized buffers */
1.47 ray 60: if (len > 0)
61: b->cb_buf = xcalloc(1, len);
62: else
1.31 niallo 63: b->cb_buf = NULL;
1.1 jfb 64:
65: b->cb_size = len;
66: b->cb_len = 0;
67:
68: return (b);
69: }
70:
1.15 xsa 71: BUF *
1.76 ! ray 72: buf_load(const char *path)
1.1 jfb 73: {
74: int fd;
1.54 joris 75: BUF *bp;
76:
77: if ((fd = open(path, O_RDONLY, 0600)) == -1)
1.76 ! ray 78: fatal("buf_load: failed to load '%s' : %s", path,
1.54 joris 79: strerror(errno));
80:
1.76 ! ray 81: bp = buf_load_fd(fd);
1.54 joris 82: (void)close(fd);
83: return (bp);
84: }
85:
86: BUF *
1.76 ! ray 87: buf_load_fd(int fd)
1.54 joris 88: {
1.1 jfb 89: struct stat st;
90: BUF *buf;
91:
1.54 joris 92: if (fstat(fd, &st) == -1)
1.76 ! ray 93: fatal("buf_load_fd: fstat: %s", strerror(errno));
1.1 jfb 94:
1.54 joris 95: if (lseek(fd, 0, SEEK_SET) == -1)
1.76 ! ray 96: fatal("buf_load_fd: lseek: %s", strerror(errno));
1.1 jfb 97:
1.71 tobias 98: if (st.st_size > SIZE_MAX)
1.76 ! ray 99: fatal("buf_load_fd: file size too big");
! 100: buf = buf_alloc(st.st_size);
1.65 tobias 101: if (atomicio(read, fd, buf->cb_buf, buf->cb_size) != buf->cb_size)
1.76 ! ray 102: fatal("buf_load_fd: read: %s", strerror(errno));
1.65 tobias 103: buf->cb_len = buf->cb_size;
1.1 jfb 104:
105: return (buf);
106: }
107:
108: void
1.76 ! ray 109: buf_free(BUF *b)
1.1 jfb 110: {
1.31 niallo 111: if (b->cb_buf != NULL)
112: xfree(b->cb_buf);
1.19 joris 113: xfree(b);
1.1 jfb 114: }
115:
1.58 otto 116: u_char *
1.76 ! ray 117: buf_release(BUF *b)
1.1 jfb 118: {
1.8 jfb 119: u_char *tmp;
1.6 tedu 120:
1.1 jfb 121: tmp = b->cb_buf;
1.19 joris 122: xfree(b);
1.1 jfb 123: return (tmp);
124: }
125:
1.39 xsa 126: void
1.76 ! ray 127: buf_putc(BUF *b, int c)
1.1 jfb 128: {
129: u_char *bp;
130:
1.57 ray 131: bp = b->cb_buf + b->cb_len;
1.1 jfb 132: if (bp == (b->cb_buf + b->cb_size)) {
1.74 joris 133: BUF_GROW(b, BUF_INCR);
1.57 ray 134: bp = b->cb_buf + b->cb_len;
1.1 jfb 135: }
136: *bp = (u_char)c;
137: b->cb_len++;
1.72 joris 138: }
139:
140: void
1.76 ! ray 141: buf_puts(BUF *b, const char *str)
1.72 joris 142: {
1.76 ! ray 143: buf_append(b, str, strlen(str));
1.29 niallo 144: }
145:
1.68 tobias 146: void
1.76 ! ray 147: buf_append(BUF *b, const void *data, size_t len)
1.1 jfb 148: {
1.68 tobias 149: size_t left;
1.8 jfb 150: u_char *bp, *bep;
1.1 jfb 151:
1.57 ray 152: bp = b->cb_buf + b->cb_len;
1.1 jfb 153: bep = b->cb_buf + b->cb_size;
154: left = bep - bp;
155:
156: if (left < len) {
1.74 joris 157: BUF_GROW(b, len - left);
1.68 tobias 158: bp = b->cb_buf + b->cb_len;
1.1 jfb 159: }
160:
1.68 tobias 161: memcpy(bp, data, len);
162: b->cb_len += len;
1.1 jfb 163: }
164:
165: size_t
1.76 ! ray 166: buf_len(BUF *b)
1.1 jfb 167: {
168: return (b->cb_len);
169: }
170:
171: int
1.76 ! ray 172: buf_write_fd(BUF *b, int fd)
1.1 jfb 173: {
1.65 tobias 174: if (atomicio(vwrite, fd, b->cb_buf, b->cb_len) != b->cb_len)
175: return (-1);
1.7 djm 176: return (0);
177: }
178:
179: int
1.76 ! ray 180: buf_write(BUF *b, const char *path, mode_t mode)
1.7 djm 181: {
1.22 xsa 182: int fd;
1.74 joris 183: open:
1.32 niallo 184: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
1.49 deraadt 185: if (errno == EACCES && unlink(path) != -1)
1.32 niallo 186: goto open;
187: else
188: fatal("open: `%s': %s", path, strerror(errno));
189: }
1.7 djm 190:
1.76 ! ray 191: if (buf_write_fd(b, fd) == -1) {
1.7 djm 192: (void)unlink(path);
1.76 ! ray 193: fatal("buf_write: buf_write_fd: `%s'", path);
1.7 djm 194: }
1.44 niallo 195:
196: if (fchmod(fd, mode) < 0)
1.51 joris 197: cvs_log(LP_ERR, "permissions not set on file %s", path);
1.44 niallo 198:
1.1 jfb 199: (void)close(fd);
1.24 joris 200:
1.22 xsa 201: return (0);
1.1 jfb 202: }
203:
1.69 joris 204: int
1.76 ! ray 205: buf_write_stmp(BUF *b, char *template, struct timeval *tv)
1.7 djm 206: {
1.22 xsa 207: int fd;
1.7 djm 208:
1.20 xsa 209: if ((fd = mkstemp(template)) == -1)
210: fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7 djm 211:
1.76 ! ray 212: if (buf_write_fd(b, fd) == -1) {
1.7 djm 213: (void)unlink(template);
1.76 ! ray 214: fatal("buf_write_stmp: buf_write_fd: `%s'", template);
1.7 djm 215: }
1.44 niallo 216:
1.51 joris 217: if (tv != NULL) {
218: if (futimes(fd, tv) == -1)
1.76 ! ray 219: fatal("buf_write_stmp: futimes failed");
1.51 joris 220: }
221:
1.75 ray 222: worklist_add(template, &temp_files);
1.69 joris 223:
1.70 joris 224: if (lseek(fd, 0, SEEK_SET) < 0)
1.76 ! ray 225: fatal("buf_write_stmp: lseek: %s", strerror(errno));
1.51 joris 226:
1.69 joris 227: return (fd);
1.7 djm 228: }
1.1 jfb 229:
1.74 joris 230: u_char *
1.76 ! ray 231: buf_get(BUF *bp)
1.1 jfb 232: {
1.74 joris 233: return (bp->cb_buf);
1.52 joris 234: }
235:
236: int
1.76 ! ray 237: buf_differ(const BUF *b1, const BUF *b2)
1.52 joris 238: {
1.59 ray 239: if (b1->cb_len != b2->cb_len)
1.52 joris 240: return (1);
241:
1.59 ray 242: return (memcmp(b1->cb_buf, b2->cb_buf, b1->cb_len));
1.43 xsa 243: }