[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.8

1.8     ! ray         1: /*     $OpenBSD: buf.c,v 1.7 2006/08/02 03:28:50 ray 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.
1.8     ! ray        84:  * Returns the loaded buffer on success or NULL on failure.
        !            85:  * Sets errno on error.
1.1       joris      86:  */
                     87: BUF *
                     88: rcs_buf_load(const char *path, u_int flags)
                     89: {
                     90:        int fd;
                     91:        ssize_t ret;
                     92:        size_t len;
                     93:        u_char *bp;
                     94:        struct stat st;
                     95:        BUF *buf;
                     96:
1.8     ! ray        97:        buf = NULL;
        !            98:
        !            99:        if ((fd = open(path, O_RDONLY, 0600)) == -1)
        !           100:                goto out;
1.1       joris     101:
                    102:        if (fstat(fd, &st) == -1)
1.8     ! ray       103:                goto out;
1.1       joris     104:
1.8     ! ray       105:        if (st.st_size > SIZE_MAX) {
        !           106:                errno = EFBIG;
        !           107:                goto out;
        !           108:        }
        !           109:        buf = rcs_buf_alloc(st.st_size, flags);
1.1       joris     110:        for (bp = buf->cb_cur; ; bp += (size_t)ret) {
                    111:                len = SIZE_LEFT(buf);
                    112:                ret = read(fd, bp, len);
                    113:                if (ret == -1) {
1.8     ! ray       114:                        int saved_errno;
        !           115:
        !           116:                        saved_errno = errno;
1.1       joris     117:                        rcs_buf_free(buf);
1.8     ! ray       118:                        buf = NULL;
        !           119:                        errno = saved_errno;
        !           120:                        goto out;
1.1       joris     121:                } else if (ret == 0)
                    122:                        break;
                    123:
                    124:                buf->cb_len += (size_t)ret;
                    125:        }
                    126:
1.8     ! ray       127: out:
        !           128:        if (fd != -1) {
        !           129:                int saved_errno;
        !           130:
        !           131:                /* We may want to preserve errno here. */
        !           132:                saved_errno = errno;
        !           133:                (void)close(fd);
        !           134:                errno = saved_errno;
        !           135:        }
1.1       joris     136:
                    137:        return (buf);
                    138: }
                    139:
                    140: /*
                    141:  * rcs_buf_free()
                    142:  *
                    143:  * Free the buffer <b> and all associated data.
                    144:  */
                    145: void
                    146: rcs_buf_free(BUF *b)
                    147: {
                    148:        if (b->cb_buf != NULL)
                    149:                xfree(b->cb_buf);
                    150:        xfree(b);
                    151: }
                    152:
                    153: /*
                    154:  * rcs_buf_release()
                    155:  *
                    156:  * Free the buffer <b>'s structural information but do not free the contents
                    157:  * of the buffer.  Instead, they are returned and should be freed later using
                    158:  * free().
                    159:  */
                    160: void *
                    161: rcs_buf_release(BUF *b)
                    162: {
1.4       niallo    163:        void *tmp;
1.1       joris     164:
                    165:        tmp = b->cb_buf;
                    166:        xfree(b);
                    167:        return (tmp);
1.5       niallo    168: }
                    169:
                    170: /*
                    171:  * rcs_buf_get()
                    172:  */
                    173: void *
                    174: rcs_buf_get(BUF *b)
                    175: {
                    176:        return (b->cb_buf);
1.1       joris     177: }
                    178:
                    179: /*
                    180:  * rcs_buf_empty()
                    181:  *
                    182:  * Empty the contents of the buffer <b> and reset pointers.
                    183:  */
                    184: void
                    185: rcs_buf_empty(BUF *b)
                    186: {
                    187:        memset(b->cb_buf, 0, b->cb_size);
                    188:        b->cb_cur = b->cb_buf;
                    189:        b->cb_len = 0;
                    190: }
                    191:
                    192: /*
                    193:  * rcs_buf_set()
                    194:  *
                    195:  * Set the contents of the buffer <b> at offset <off> to the first <len>
                    196:  * bytes of data found at <src>.  If the buffer was not created with
                    197:  * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
                    198:  */
1.7       ray       199: size_t
1.1       joris     200: rcs_buf_set(BUF *b, const void *src, size_t len, size_t off)
                    201: {
                    202:        size_t rlen = 0;
                    203:
                    204:        if (b->cb_size < (len + off)) {
                    205:                if ((b->cb_flags & BUF_AUTOEXT)) {
                    206:                        rcs_buf_grow(b, len + off - b->cb_size);
                    207:                        rlen = len + off;
                    208:                } else {
                    209:                        rlen = b->cb_size - off;
                    210:                }
                    211:        } else {
                    212:                rlen = len;
                    213:        }
                    214:
                    215:        b->cb_len = rlen;
                    216:        memcpy((b->cb_buf + off), src, rlen);
                    217:
                    218:        if (b->cb_len == 0) {
                    219:                b->cb_cur = b->cb_buf + off;
                    220:                b->cb_len = rlen;
                    221:        }
                    222:
                    223:        return (rlen);
                    224: }
                    225:
                    226: /*
                    227:  * rcs_buf_putc()
                    228:  *
                    229:  * Append a single character <c> to the end of the buffer <b>.
                    230:  */
                    231: void
                    232: rcs_buf_putc(BUF *b, int c)
                    233: {
                    234:        u_char *bp;
                    235:
                    236:        bp = b->cb_cur + b->cb_len;
                    237:        if (bp == (b->cb_buf + b->cb_size)) {
                    238:                /* extend */
                    239:                if (b->cb_flags & BUF_AUTOEXT)
                    240:                        rcs_buf_grow(b, (size_t)BUF_INCR);
                    241:                else
                    242:                        errx(1, "rcs_buf_putc failed");
                    243:
                    244:                /* the buffer might have been moved */
                    245:                bp = b->cb_cur + b->cb_len;
                    246:        }
                    247:        *bp = (u_char)c;
                    248:        b->cb_len++;
                    249: }
                    250:
                    251: /*
                    252:  * rcs_buf_getc()
                    253:  *
                    254:  * Return u_char at buffer position <pos>.
                    255:  *
                    256:  */
                    257: u_char
                    258: rcs_buf_getc(BUF *b, size_t pos)
                    259: {
                    260:        return (b->cb_cur[pos]);
                    261: }
                    262:
                    263: /*
                    264:  * rcs_buf_append()
                    265:  *
                    266:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    267:  * buffer is too small to accept all data, it will attempt to append as much
                    268:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    269:  * will get resized to an appropriate size to accept all data.
                    270:  * Returns the number of bytes successfully appended to the buffer.
                    271:  */
1.7       ray       272: size_t
1.1       joris     273: rcs_buf_append(BUF *b, const void *data, size_t len)
                    274: {
                    275:        size_t left, rlen;
                    276:        u_char *bp, *bep;
                    277:
                    278:        bp = b->cb_cur + b->cb_len;
                    279:        bep = b->cb_buf + b->cb_size;
                    280:        left = bep - bp;
                    281:        rlen = len;
                    282:
                    283:        if (left < len) {
                    284:                if (b->cb_flags & BUF_AUTOEXT) {
                    285:                        rcs_buf_grow(b, len - left);
                    286:                        bp = b->cb_cur + b->cb_len;
                    287:                } else
                    288:                        rlen = bep - bp;
                    289:        }
                    290:
                    291:        memcpy(bp, data, rlen);
                    292:        b->cb_len += rlen;
                    293:
                    294:        return (rlen);
                    295: }
                    296:
                    297: /*
                    298:  * rcs_buf_fappend()
                    299:  *
                    300:  */
1.7       ray       301: size_t
1.1       joris     302: rcs_buf_fappend(BUF *b, const char *fmt, ...)
                    303: {
1.7       ray       304:        size_t ret;
                    305:        int n;
1.1       joris     306:        char *str;
                    307:        va_list vap;
                    308:
                    309:        va_start(vap, fmt);
1.7       ray       310:        n = vasprintf(&str, fmt, vap);
1.1       joris     311:        va_end(vap);
                    312:
1.7       ray       313:        if (n == -1)
1.1       joris     314:                errx(1, "rcs_buf_fappend: failed to format data");
                    315:
1.7       ray       316:        ret = rcs_buf_append(b, str, n);
1.1       joris     317:        xfree(str);
                    318:        return (ret);
                    319: }
                    320:
                    321: /*
                    322:  * rcs_buf_len()
                    323:  *
                    324:  * Returns the size of the buffer that is being used.
                    325:  */
                    326: size_t
                    327: rcs_buf_len(BUF *b)
                    328: {
                    329:        return (b->cb_len);
                    330: }
                    331:
                    332: /*
                    333:  * rcs_buf_write_fd()
                    334:  *
                    335:  * Write the contents of the buffer <b> to the specified <fd>
                    336:  */
                    337: int
                    338: rcs_buf_write_fd(BUF *b, int fd)
                    339: {
                    340:        u_char *bp;
                    341:        size_t len;
                    342:        ssize_t ret;
                    343:
                    344:        len = b->cb_len;
                    345:        bp = b->cb_cur;
                    346:
                    347:        do {
                    348:                ret = write(fd, bp, len);
                    349:                if (ret == -1) {
                    350:                        if (errno == EINTR || errno == EAGAIN)
                    351:                                continue;
                    352:                        return (-1);
                    353:                }
                    354:
                    355:                len -= (size_t)ret;
                    356:                bp += (size_t)ret;
                    357:        } while (len > 0);
                    358:
                    359:        return (0);
                    360: }
                    361:
                    362: /*
                    363:  * rcs_buf_write()
                    364:  *
                    365:  * Write the contents of the buffer <b> to the file whose path is given in
                    366:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    367:  */
                    368: int
                    369: rcs_buf_write(BUF *b, const char *path, mode_t mode)
                    370: {
                    371:        int fd;
                    372:  open:
                    373:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
                    374:                if (errno == EACCES && unlink(path) != -1)
                    375:                        goto open;
                    376:                else
1.3       xsa       377:                        err(1, "%s", path);
1.1       joris     378:        }
                    379:
                    380:        if (rcs_buf_write_fd(b, fd) == -1) {
                    381:                (void)unlink(path);
                    382:                errx(1, "rcs_buf_write: rcs_buf_write_fd: `%s'", path);
                    383:        }
                    384:
                    385:        if (fchmod(fd, mode) < 0)
                    386:                warn("permissions not set on file %s", path);
                    387:
                    388:        (void)close(fd);
                    389:
                    390:        return (0);
                    391: }
                    392:
                    393: /*
                    394:  * rcs_buf_write_stmp()
                    395:  *
                    396:  * Write the contents of the buffer <b> to a temporary file whose path is
                    397:  * specified using <template> (see mkstemp.3). NB. This function will modify
                    398:  * <template>, as per mkstemp
                    399:  */
                    400: void
1.6       ray       401: rcs_buf_write_stmp(BUF *b, char *template)
1.1       joris     402: {
                    403:        int fd;
                    404:
                    405:        if ((fd = mkstemp(template)) == -1)
1.3       xsa       406:                err(1, "%s", template);
1.1       joris     407:
                    408:        rcs_worklist_add(template, &rcs_temp_files);
                    409:
                    410:        if (rcs_buf_write_fd(b, fd) == -1) {
                    411:                (void)unlink(template);
                    412:                errx(1, "rcs_buf_write_stmp: rcs_buf_write_fd: `%s'", template);
                    413:        }
                    414:
                    415:        (void)close(fd);
                    416: }
                    417:
                    418: /*
                    419:  * rcs_buf_grow()
                    420:  *
                    421:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    422:  * operation regardless of the result.
                    423:  */
                    424: static void
                    425: rcs_buf_grow(BUF *b, size_t len)
                    426: {
                    427:        void *tmp;
                    428:        size_t diff;
                    429:
                    430:        diff = b->cb_cur - b->cb_buf;
                    431:        tmp = xrealloc(b->cb_buf, 1, b->cb_size + len);
                    432:        b->cb_buf = tmp;
                    433:        b->cb_size += len;
                    434:
                    435:        /* readjust pointers in case the buffer moved in memory */
                    436:        b->cb_cur = b->cb_buf + diff;
                    437: }