[BACK]Return to buf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/buf.c, Revision 1.2

1.2     ! xsa         1: /*     $OpenBSD: buf.c,v 1.1 2006/04/26 02:55:13 joris Exp $   */
1.1       joris       2: /*
                      3:  * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
                      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:
                     27: #include "includes.h"
                     28:
                     29: #include "buf.h"
                     30: #include "xmalloc.h"
                     31: #include "worklist.h"
                     32:
                     33: #define BUF_INCR       128
                     34:
                     35: struct rcs_buf {
                     36:        u_int   cb_flags;
                     37:
                     38:        /* buffer handle and size */
                     39:        u_char  *cb_buf;
                     40:        size_t   cb_size;
                     41:
                     42:        /* start and length of valid data in buffer */
                     43:        u_char  *cb_cur;
                     44:        size_t   cb_len;
                     45: };
                     46:
                     47: #define SIZE_LEFT(b)   (b->cb_size - (size_t)(b->cb_cur - b->cb_buf) \
                     48:                            - b->cb_len)
                     49:
                     50: static void    rcs_buf_grow(BUF *, size_t);
                     51:
                     52: /*
                     53:  * rcs_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 rcs_buf_free(),
                     57:  * once the buffer is no longer needed.
                     58:  */
                     59: BUF *
                     60: rcs_buf_alloc(size_t len, u_int flags)
                     61: {
                     62:        BUF *b;
                     63:
                     64:        b = xmalloc(sizeof(*b));
                     65:        /* Postpone creation of zero-sized buffers */
                     66:        if (len > 0)
                     67:                b->cb_buf = xcalloc(1, len);
                     68:        else
                     69:                b->cb_buf = NULL;
                     70:
                     71:        b->cb_flags = flags;
                     72:        b->cb_size = len;
                     73:        b->cb_cur = b->cb_buf;
                     74:        b->cb_len = 0;
                     75:
                     76:        return (b);
                     77: }
                     78:
                     79: /*
                     80:  * rcs_buf_load()
                     81:  *
                     82:  * Open the file specified by <path> and load all of its contents into a
                     83:  * buffer.
                     84:  * Returns the loaded buffer on success.
                     85:  */
                     86: BUF *
                     87: rcs_buf_load(const char *path, u_int flags)
                     88: {
                     89:        int fd;
                     90:        ssize_t ret;
                     91:        size_t len;
                     92:        u_char *bp;
                     93:        struct stat st;
                     94:        BUF *buf;
                     95:
                     96:        if ((fd = open(path, O_RDONLY, 0600)) == -1) {
                     97:                warn("%s", path);
                     98:                return (NULL);
                     99:        }
                    100:
                    101:        if (fstat(fd, &st) == -1)
                    102:                err(1, "rcs_buf_load: fstat: %s", path);
                    103:
                    104:        buf = rcs_buf_alloc((size_t)st.st_size, flags);
                    105:        for (bp = buf->cb_cur; ; bp += (size_t)ret) {
                    106:                len = SIZE_LEFT(buf);
                    107:                ret = read(fd, bp, len);
                    108:                if (ret == -1) {
                    109:                        rcs_buf_free(buf);
                    110:                        err(1, "rcs_buf_load: read: %s", strerror(errno));
                    111:                } else if (ret == 0)
                    112:                        break;
                    113:
                    114:                buf->cb_len += (size_t)ret;
                    115:        }
                    116:
                    117:        (void)close(fd);
                    118:
                    119:        return (buf);
                    120: }
                    121:
                    122: /*
                    123:  * rcs_buf_free()
                    124:  *
                    125:  * Free the buffer <b> and all associated data.
                    126:  */
                    127: void
                    128: rcs_buf_free(BUF *b)
                    129: {
                    130:        if (b->cb_buf != NULL)
                    131:                xfree(b->cb_buf);
                    132:        xfree(b);
                    133: }
                    134:
                    135: /*
                    136:  * rcs_buf_release()
                    137:  *
                    138:  * Free the buffer <b>'s structural information but do not free the contents
                    139:  * of the buffer.  Instead, they are returned and should be freed later using
                    140:  * free().
                    141:  */
                    142: void *
                    143: rcs_buf_release(BUF *b)
                    144: {
                    145:        u_char *tmp;
                    146:
                    147:        tmp = b->cb_buf;
                    148:        xfree(b);
                    149:        return (tmp);
                    150: }
                    151:
                    152: /*
                    153:  * rcs_buf_empty()
                    154:  *
                    155:  * Empty the contents of the buffer <b> and reset pointers.
                    156:  */
                    157: void
                    158: rcs_buf_empty(BUF *b)
                    159: {
                    160:        memset(b->cb_buf, 0, b->cb_size);
                    161:        b->cb_cur = b->cb_buf;
                    162:        b->cb_len = 0;
                    163: }
                    164:
                    165: /*
                    166:  * rcs_buf_set()
                    167:  *
                    168:  * Set the contents of the buffer <b> at offset <off> to the first <len>
                    169:  * bytes of data found at <src>.  If the buffer was not created with
                    170:  * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
                    171:  */
                    172: ssize_t
                    173: rcs_buf_set(BUF *b, const void *src, size_t len, size_t off)
                    174: {
                    175:        size_t rlen = 0;
                    176:
                    177:        if (b->cb_size < (len + off)) {
                    178:                if ((b->cb_flags & BUF_AUTOEXT)) {
                    179:                        rcs_buf_grow(b, len + off - b->cb_size);
                    180:                        rlen = len + off;
                    181:                } else {
                    182:                        rlen = b->cb_size - off;
                    183:                }
                    184:        } else {
                    185:                rlen = len;
                    186:        }
                    187:
                    188:        b->cb_len = rlen;
                    189:        memcpy((b->cb_buf + off), src, rlen);
                    190:
                    191:        if (b->cb_len == 0) {
                    192:                b->cb_cur = b->cb_buf + off;
                    193:                b->cb_len = rlen;
                    194:        }
                    195:
                    196:        return (rlen);
                    197: }
                    198:
                    199: /*
                    200:  * rcs_buf_putc()
                    201:  *
                    202:  * Append a single character <c> to the end of the buffer <b>.
                    203:  */
                    204: void
                    205: rcs_buf_putc(BUF *b, int c)
                    206: {
                    207:        u_char *bp;
                    208:
                    209:        bp = b->cb_cur + b->cb_len;
                    210:        if (bp == (b->cb_buf + b->cb_size)) {
                    211:                /* extend */
                    212:                if (b->cb_flags & BUF_AUTOEXT)
                    213:                        rcs_buf_grow(b, (size_t)BUF_INCR);
                    214:                else
                    215:                        errx(1, "rcs_buf_putc failed");
                    216:
                    217:                /* the buffer might have been moved */
                    218:                bp = b->cb_cur + b->cb_len;
                    219:        }
                    220:        *bp = (u_char)c;
                    221:        b->cb_len++;
                    222: }
                    223:
                    224: /*
                    225:  * rcs_buf_getc()
                    226:  *
                    227:  * Return u_char at buffer position <pos>.
                    228:  *
                    229:  */
                    230: u_char
                    231: rcs_buf_getc(BUF *b, size_t pos)
                    232: {
                    233:        return (b->cb_cur[pos]);
                    234: }
                    235:
                    236: /*
                    237:  * rcs_buf_append()
                    238:  *
                    239:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    240:  * buffer is too small to accept all data, it will attempt to append as much
                    241:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    242:  * will get resized to an appropriate size to accept all data.
                    243:  * Returns the number of bytes successfully appended to the buffer.
                    244:  */
                    245: ssize_t
                    246: rcs_buf_append(BUF *b, const void *data, size_t len)
                    247: {
                    248:        size_t left, rlen;
                    249:        u_char *bp, *bep;
                    250:
                    251:        bp = b->cb_cur + b->cb_len;
                    252:        bep = b->cb_buf + b->cb_size;
                    253:        left = bep - bp;
                    254:        rlen = len;
                    255:
                    256:        if (left < len) {
                    257:                if (b->cb_flags & BUF_AUTOEXT) {
                    258:                        rcs_buf_grow(b, len - left);
                    259:                        bp = b->cb_cur + b->cb_len;
                    260:                } else
                    261:                        rlen = bep - bp;
                    262:        }
                    263:
                    264:        memcpy(bp, data, rlen);
                    265:        b->cb_len += rlen;
                    266:
                    267:        return (rlen);
                    268: }
                    269:
                    270: /*
                    271:  * rcs_buf_fappend()
                    272:  *
                    273:  */
                    274: ssize_t
                    275: rcs_buf_fappend(BUF *b, const char *fmt, ...)
                    276: {
                    277:        ssize_t ret;
                    278:        char *str;
                    279:        va_list vap;
                    280:
                    281:        va_start(vap, fmt);
                    282:        ret = vasprintf(&str, fmt, vap);
                    283:        va_end(vap);
                    284:
                    285:        if (ret == -1)
                    286:                errx(1, "rcs_buf_fappend: failed to format data");
                    287:
                    288:        ret = rcs_buf_append(b, str, (size_t)ret);
                    289:        xfree(str);
                    290:        return (ret);
                    291: }
                    292:
                    293: /*
                    294:  * rcs_buf_len()
                    295:  *
                    296:  * Returns the size of the buffer that is being used.
                    297:  */
                    298: size_t
                    299: rcs_buf_len(BUF *b)
                    300: {
                    301:        return (b->cb_len);
                    302: }
                    303:
                    304: /*
                    305:  * rcs_buf_write_fd()
                    306:  *
                    307:  * Write the contents of the buffer <b> to the specified <fd>
                    308:  */
                    309: int
                    310: rcs_buf_write_fd(BUF *b, int fd)
                    311: {
                    312:        u_char *bp;
                    313:        size_t len;
                    314:        ssize_t ret;
                    315:
                    316:        len = b->cb_len;
                    317:        bp = b->cb_cur;
                    318:
                    319:        do {
                    320:                ret = write(fd, bp, len);
                    321:                if (ret == -1) {
                    322:                        if (errno == EINTR || errno == EAGAIN)
                    323:                                continue;
                    324:                        return (-1);
                    325:                }
                    326:
                    327:                len -= (size_t)ret;
                    328:                bp += (size_t)ret;
                    329:        } while (len > 0);
                    330:
                    331:        return (0);
                    332: }
                    333:
                    334: /*
                    335:  * rcs_buf_write()
                    336:  *
                    337:  * Write the contents of the buffer <b> to the file whose path is given in
                    338:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    339:  */
                    340: int
                    341: rcs_buf_write(BUF *b, const char *path, mode_t mode)
                    342: {
                    343:        int fd;
                    344:  open:
                    345:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
                    346:                if (errno == EACCES && unlink(path) != -1)
                    347:                        goto open;
                    348:                else
                    349:                        err(1, "open: `%s'", path);
                    350:        }
                    351:
                    352:        if (rcs_buf_write_fd(b, fd) == -1) {
                    353:                (void)unlink(path);
                    354:                errx(1, "rcs_buf_write: rcs_buf_write_fd: `%s'", path);
                    355:        }
                    356:
                    357:        if (fchmod(fd, mode) < 0)
                    358:                warn("permissions not set on file %s", path);
                    359:
                    360:        (void)close(fd);
                    361:
                    362:        return (0);
                    363: }
                    364:
                    365: /*
                    366:  * rcs_buf_write_stmp()
                    367:  *
                    368:  * Write the contents of the buffer <b> to a temporary file whose path is
                    369:  * specified using <template> (see mkstemp.3). NB. This function will modify
                    370:  * <template>, as per mkstemp
                    371:  */
                    372: void
                    373: rcs_buf_write_stmp(BUF *b, char *template, mode_t mode)
                    374: {
                    375:        int fd;
                    376:
                    377:        if ((fd = mkstemp(template)) == -1)
                    378:                err(1, "mkstemp: `%s'", template);
                    379:
                    380:        rcs_worklist_add(template, &rcs_temp_files);
                    381:
                    382:        if (rcs_buf_write_fd(b, fd) == -1) {
                    383:                (void)unlink(template);
                    384:                errx(1, "rcs_buf_write_stmp: rcs_buf_write_fd: `%s'", template);
                    385:        }
                    386:
                    387:        if (fchmod(fd, mode) < 0)
                    388:                warn("permissions not set on temporary file %s",
                    389:                    template);
                    390:
                    391:        (void)close(fd);
                    392: }
                    393:
                    394: /*
                    395:  * rcs_buf_grow()
                    396:  *
                    397:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    398:  * operation regardless of the result.
                    399:  */
                    400: static void
                    401: rcs_buf_grow(BUF *b, size_t len)
                    402: {
                    403:        void *tmp;
                    404:        size_t diff;
                    405:
                    406:        diff = b->cb_cur - b->cb_buf;
                    407:        tmp = xrealloc(b->cb_buf, 1, b->cb_size + len);
                    408:        b->cb_buf = tmp;
                    409:        b->cb_size += len;
                    410:
                    411:        /* readjust pointers in case the buffer moved in memory */
                    412:        b->cb_cur = b->cb_buf + diff;
                    413: }