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

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