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

Annotation of src/usr.bin/cvs/buf.c, Revision 1.6

1.6     ! tedu        1: /*     $OpenBSD: buf.c,v 1.5 2004/12/06 21:03:12 deraadt 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:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <stdio.h>
                     31: #include <ctype.h>
                     32: #include <fcntl.h>
                     33: #include <stdarg.h>
                     34: #include <unistd.h>
                     35: #include <string.h>
                     36: #include <stdlib.h>
                     37:
                     38: #include "buf.h"
                     39: #include "log.h"
                     40:
                     41:
                     42: #define BUF_INCR   128
                     43:
                     44:
                     45: struct cvs_buf {
                     46:        u_int    cb_flags;
                     47:
                     48:        /* buffer handle and size */
                     49:        void    *cb_buf;
                     50:        size_t   cb_size;
                     51:
                     52:        /* start and length of valid data in buffer */
                     53:        u_char  *cb_cur;
                     54:        size_t   cb_len;
                     55: };
                     56:
                     57:
                     58:
                     59: #define SIZE_LEFT(b)  ((size_t)((u_char *)b->cb_buf - b->cb_cur) + b->cb_size)
                     60:
                     61:
                     62: static ssize_t   cvs_buf_grow (BUF *, size_t);
                     63:
                     64:
                     65:
                     66: /*
                     67:  * cvs_buf_alloc()
                     68:  *
                     69:  * Create a new buffer structure and return a pointer to it.  This structure
                     70:  * uses dynamically-allocated memory and must be freed with cvs_buf_free(),
                     71:  * once the buffer is no longer needed.
                     72:  */
                     73: BUF*
                     74: cvs_buf_alloc(size_t len, u_int flags)
                     75: {
                     76:        BUF *b;
                     77:
                     78:        b = (BUF *)malloc(sizeof(*b));
                     79:        if (b == NULL) {
                     80:                cvs_log(LP_ERRNO, "failed to allocate buffer");
                     81:                return (NULL);
                     82:        }
                     83:
                     84:        b->cb_buf = malloc(len);
                     85:        if (b->cb_buf == NULL) {
                     86:                cvs_log(LP_ERRNO, "failed to allocate buffer");
                     87:                free(b);
                     88:                return (NULL);
                     89:        }
1.3       joris      90:        memset(b->cb_buf, 0, len);
1.1       jfb        91:
                     92:        b->cb_flags = flags;
                     93:        b->cb_size = len;
                     94:        b->cb_cur = (u_char *)b->cb_buf;
                     95:        b->cb_len = 0;
                     96:
                     97:        return (b);
                     98: }
                     99:
                    100:
                    101: /*
                    102:  * cvs_buf_load()
                    103:  *
                    104:  * Open the file specified by <path> and load all of its contents into a
                    105:  * buffer.
                    106:  * Returns the loaded buffer on success, or NULL on failure.
                    107:  */
                    108: BUF*
                    109: cvs_buf_load(const char *path, u_int flags)
                    110: {
                    111:        int fd;
                    112:        ssize_t ret;
                    113:        size_t len;
                    114:        void *bp;
                    115:        struct stat st;
                    116:        BUF *buf;
                    117:
                    118:        fd = open(path, O_RDONLY, 0600);
                    119:        if (fd == -1) {
                    120:                cvs_log(LP_ERRNO, "failed to open buffer source");
                    121:                return (NULL);
                    122:        }
                    123:
                    124:        if (fstat(fd, &st) == -1) {
                    125:                cvs_log(LP_ERRNO, "failed to stat buffer source");
                    126:                (void)close(fd);
                    127:                return (NULL);
                    128:        }
                    129:
                    130:        buf = cvs_buf_alloc((size_t)st.st_size, flags);
                    131:        if (buf == NULL) {
                    132:                (void)close(fd);
                    133:                return (NULL);
                    134:        }
                    135:
                    136:        for (bp = buf->cb_cur; ; bp += (size_t)ret) {
                    137:                len = MIN(SIZE_LEFT(buf), 4096);
                    138:                ret = read(fd, bp, len);
                    139:                if (ret == -1) {
                    140:                        cvs_log(LP_ERRNO, "read failed from buffer source");
                    141:                        (void)close(fd);
                    142:                        cvs_buf_free(bp);
                    143:                        return (NULL);
1.5       deraadt   144:                } else if (ret == 0)
1.1       jfb       145:                        break;
                    146:
                    147:                buf->cb_len += (size_t)ret;
                    148:        }
                    149:
                    150:        (void)close(fd);
                    151:
                    152:        return (buf);
                    153: }
                    154:
                    155:
                    156: /*
                    157:  * cvs_buf_free()
                    158:  *
                    159:  * Free the buffer <b> and all associated data.
                    160:  */
                    161: void
                    162: cvs_buf_free(BUF *b)
                    163: {
                    164:        free(b->cb_buf);
                    165:        free(b);
                    166: }
                    167:
                    168:
                    169: /*
                    170:  * cvs_buf_release()
                    171:  *
                    172:  * Free the buffer <b>'s structural information but do not free the contents
                    173:  * of the buffer.  Instead, they are returned and should be freed later using
                    174:  * free().
                    175:  */
                    176: void*
                    177: cvs_buf_release(BUF *b)
                    178: {
                    179:        void *tmp;
1.6     ! tedu      180:
1.1       jfb       181:        tmp = b->cb_buf;
                    182:        free(b);
                    183:        return (tmp);
                    184: }
                    185:
                    186:
                    187: /*
                    188:  * cvs_buf_empty()
                    189:  *
                    190:  * Empty the contents of the buffer <b> and reset pointers.
                    191:  */
                    192: void
                    193: cvs_buf_empty(BUF *b)
                    194: {
                    195:        b->cb_cur = (u_char *)b->cb_buf;
                    196:        b->cb_len = 0;
                    197: }
                    198:
                    199:
                    200: /*
                    201:  * cvs_buf_copy()
                    202:  *
                    203:  * Copy the first <len> bytes of data in the buffer <b> starting at offset
                    204:  * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
                    205:  * Returns the number of bytes successfully copied, or -1 on failure.
                    206:  */
                    207: ssize_t
                    208: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
                    209: {
                    210:        size_t rc;
                    211:
                    212:        if (off > b->cb_len)
                    213:                return (-1);
                    214:
                    215:        rc = MIN(len, (b->cb_len - off));
                    216:        memcpy(dst, b->cb_buf, rc);
                    217:
                    218:        return (ssize_t)rc;
                    219: }
                    220:
                    221:
                    222: /*
                    223:  * cvs_buf_set()
                    224:  *
                    225:  * Set the contents of the buffer <b> to the first <len> bytes of data found
                    226:  * at <src>.  If the buffer was not created with BUF_AUTOEXT, as many bytes
                    227:  * as possible will be copied in the buffer.
                    228:  */
                    229: int
                    230: cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
                    231: {
                    232:        size_t rlen;
                    233:
                    234:        if (b->cb_size < (len + off)) {
1.6     ! tedu      235:                if ((b->cb_flags & BUF_AUTOEXT) && (cvs_buf_grow(b,
1.1       jfb       236:                    len + off - b->cb_size) < 0))
                    237:                        return (-1);
                    238:                else
                    239:                        rlen = b->cb_size - off;
1.5       deraadt   240:        } else
1.1       jfb       241:                rlen = len;
                    242:
                    243:        memcpy((b->cb_buf + off), src, rlen);
                    244:
                    245:        if (b->cb_len == 0) {
                    246:                b->cb_cur = b->cb_buf + off;
                    247:                b->cb_len = rlen;
                    248:        }
                    249:
                    250:        return (int)rlen;
                    251: }
                    252:
                    253:
                    254: /*
                    255:  * cvs_buf_putc()
                    256:  *
                    257:  * Append a single character <c> to the end of the buffer <b>.
                    258:  * Returns 0 on success, or -1 on failure.
                    259:  */
                    260: int
                    261: cvs_buf_putc(BUF *b, int c)
                    262: {
                    263:        u_char *bp;
                    264:
                    265:        bp = b->cb_cur + b->cb_len;
                    266:        if (bp == (b->cb_buf + b->cb_size)) {
                    267:                /* extend */
                    268:                if (!(b->cb_flags & BUF_AUTOEXT) ||
                    269:                    (cvs_buf_grow(b, BUF_INCR) < 0))
                    270:                        return (-1);
                    271:
                    272:                /* the buffer might have been moved */
                    273:                bp = b->cb_cur + b->cb_len;
                    274:        }
                    275:        *bp = (u_char)c;
                    276:        b->cb_len++;
                    277:
                    278:        return (0);
                    279: }
                    280:
                    281:
                    282: /*
                    283:  * cvs_buf_append()
                    284:  *
                    285:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    286:  * buffer is too small to accept all data, it will attempt to append as much
                    287:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    288:  * will get resized to an appropriate size to accept all data.
                    289:  * Returns the number of bytes successfully appended to the buffer, or -1
                    290:  * on failure.
                    291:  */
                    292: ssize_t
                    293: cvs_buf_append(BUF *b, const void *data, size_t len)
                    294: {
                    295:        size_t left, rlen;
                    296:        void *bp, *bep;
                    297:
                    298:        bp = b->cb_cur + b->cb_len;
                    299:        bep = b->cb_buf + b->cb_size;
                    300:        left = bep - bp;
                    301:        rlen = len;
                    302:
                    303:        if (left < len) {
                    304:                if (b->cb_flags & BUF_AUTOEXT) {
                    305:                        if (cvs_buf_grow(b, len - left) < 0)
                    306:                                return (-1);
                    307:                        bp = b->cb_cur + b->cb_len;
1.5       deraadt   308:                } else
1.1       jfb       309:                        rlen = bep - bp;
                    310:        }
                    311:
                    312:        memcpy(bp, data, rlen);
                    313:        b->cb_len += rlen;
                    314:
                    315:        return (rlen);
                    316: }
                    317:
                    318:
                    319: /*
                    320:  * cvs_buf_fappend()
                    321:  *
                    322:  */
                    323: int
                    324: cvs_buf_fappend(BUF *b, const char *fmt, ...)
                    325: {
                    326:        int ret;
                    327:        char *str;
                    328:        va_list vap;
                    329:
                    330:        va_start(vap, fmt);
1.4       pat       331:        ret = vasprintf(&str, fmt, vap);
                    332:        va_end(vap);
1.1       jfb       333:
                    334:        if (ret == -1) {
                    335:                cvs_log(LP_ERRNO, "failed to format data");
                    336:                return (-1);
                    337:        }
                    338:
                    339:        ret = cvs_buf_append(b, str, ret);
                    340:        free(str);
                    341:        return (ret);
                    342: }
                    343:
                    344:
                    345: /*
                    346:  * cvs_buf_size()
                    347:  *
                    348:  * Returns the size of the buffer that is being used.
                    349:  */
                    350: size_t
                    351: cvs_buf_size(BUF *b)
                    352: {
                    353:        return (b->cb_len);
                    354: }
                    355:
                    356:
                    357: /*
                    358:  * cvs_buf_peek()
                    359:  *
                    360:  * Peek at the contents of the buffer <b> at offset <off>.
                    361:  */
                    362: const void*
                    363: cvs_buf_peek(BUF *b, size_t off)
                    364: {
                    365:        if (off >= b->cb_len)
                    366:                return (NULL);
                    367:
                    368:        return (b->cb_buf + off);
                    369: }
                    370:
                    371:
                    372: /*
                    373:  * cvs_buf_write()
                    374:  *
                    375:  * Write the contents of the buffer <b> to the file whose path is given in
                    376:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    377:  */
                    378: int
                    379: cvs_buf_write(BUF *b, const char *path, mode_t mode)
                    380: {
                    381:        int fd;
                    382:        u_char *bp;
                    383:        size_t len;
                    384:        ssize_t ret;
                    385:
                    386:        fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
                    387:        if (fd == -1) {
                    388:                cvs_log(LP_ERRNO, "failed to open file `%s'", path);
                    389:                return (-1);
                    390:        }
                    391:
                    392:        len = b->cb_len;
                    393:        bp = b->cb_cur;
                    394:
                    395:        do {
                    396:                ret = write(fd, bp, MIN(len, 8192));
                    397:                if (ret == -1) {
                    398:                        cvs_log(LP_ERRNO, "failed to write to file `%s'", path);
                    399:                        (void)close(fd);
                    400:                        (void)unlink(path);
                    401:                        return (-1);
                    402:                }
                    403:
                    404:                len -= (size_t)ret;
                    405:                bp += (size_t)ret;
                    406:        } while (len > 0);
                    407:
                    408:        (void)close(fd);
                    409:
                    410:        return (0);
                    411: }
                    412:
                    413:
                    414: /*
                    415:  * cvs_buf_grow()
                    416:  *
                    417:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    418:  * operation regardless of the result.
                    419:  * Returns the new size on success, or -1 on failure.
                    420:  */
                    421: static ssize_t
                    422: cvs_buf_grow(BUF *b, size_t len)
                    423: {
                    424:        void *tmp;
                    425:        size_t diff;
                    426:
                    427:        diff = b->cb_cur - (u_char *)b->cb_buf;
                    428:        tmp = realloc(b->cb_buf, b->cb_size + len);
                    429:        if (tmp == NULL) {
                    430:                cvs_log(LP_ERRNO, "failed to grow buffer");
                    431:                return (-1);
                    432:        }
                    433:        b->cb_buf = (u_char *)tmp;
                    434:        b->cb_size += len;
                    435:
                    436:        /* readjust pointers in case the buffer moved in memory */
                    437:        b->cb_cur = b->cb_buf + diff;
                    438:
                    439:        return (ssize_t)b->cb_size;
                    440: }