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

1.38    ! niallo      1: /*     $OpenBSD: buf.c,v 1.37 2006/03/17 07:37:08 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.1       jfb        32:
1.15      xsa        33: #define BUF_INCR       128
1.1       jfb        34:
                     35: struct cvs_buf {
1.15      xsa        36:        u_int   cb_flags;
1.1       jfb        37:
                     38:        /* buffer handle and size */
1.15      xsa        39:        u_char  *cb_buf;
                     40:        size_t   cb_size;
1.1       jfb        41:
                     42:        /* start and length of valid data in buffer */
1.15      xsa        43:        u_char  *cb_cur;
                     44:        size_t   cb_len;
1.1       jfb        45: };
                     46:
1.16      moritz     47: #define SIZE_LEFT(b)   (b->cb_size - (size_t)(b->cb_cur - b->cb_buf) \
                     48:                            - b->cb_len)
1.1       jfb        49:
1.15      xsa        50: static ssize_t cvs_buf_grow(BUF *, size_t);
1.1       jfb        51:
                     52: /*
                     53:  * cvs_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 cvs_buf_free(),
                     57:  * once the buffer is no longer needed.
                     58:  */
1.15      xsa        59: BUF *
1.1       jfb        60: cvs_buf_alloc(size_t len, u_int flags)
                     61: {
                     62:        BUF *b;
                     63:
1.19      joris      64:        b = (BUF *)xmalloc(sizeof(*b));
1.30      niallo     65:        /* Postpone creation of zero-sized buffers */
                     66:        if (len > 0) {
                     67:                b->cb_buf = xmalloc(len);
                     68:                memset(b->cb_buf, 0, len);
1.31      niallo     69:        } else
                     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_copy()
                    168:  *
                    169:  * Copy the first <len> bytes of data in the buffer <b> starting at offset
                    170:  * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
                    171:  * Returns the number of bytes successfully copied, or -1 on failure.
                    172:  */
                    173: ssize_t
                    174: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
                    175: {
                    176:        size_t rc;
                    177:
                    178:        if (off > b->cb_len)
1.21      xsa       179:                fatal("cvs_buf_copy failed");
1.1       jfb       180:
                    181:        rc = MIN(len, (b->cb_len - off));
1.16      moritz    182:        memcpy(dst, b->cb_buf + off, rc);
1.1       jfb       183:
                    184:        return (ssize_t)rc;
                    185: }
                    186:
                    187: /*
                    188:  * cvs_buf_set()
                    189:  *
1.16      moritz    190:  * Set the contents of the buffer <b> at offset <off> to the first <len>
                    191:  * bytes of data found at <src>.  If the buffer was not created with
                    192:  * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
1.1       jfb       193:  */
1.16      moritz    194: ssize_t
1.1       jfb       195: cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
                    196: {
1.23      xsa       197:        size_t rlen = 0;
1.1       jfb       198:
                    199:        if (b->cb_size < (len + off)) {
1.20      xsa       200:                if ((b->cb_flags & BUF_AUTOEXT) &&
                    201:                    (cvs_buf_grow(b, len + off - b->cb_size) < 0))
                    202:                        fatal("cvs_buf_set failed");
1.1       jfb       203:                else
                    204:                        rlen = b->cb_size - off;
1.5       deraadt   205:        } else
1.1       jfb       206:                rlen = len;
                    207:
                    208:        memcpy((b->cb_buf + off), src, rlen);
                    209:
                    210:        if (b->cb_len == 0) {
                    211:                b->cb_cur = b->cb_buf + off;
                    212:                b->cb_len = rlen;
                    213:        }
                    214:
1.16      moritz    215:        return (rlen);
1.1       jfb       216: }
                    217:
                    218: /*
                    219:  * cvs_buf_putc()
                    220:  *
                    221:  * Append a single character <c> to the end of the buffer <b>.
1.21      xsa       222:  * Returns 0 on success.
1.1       jfb       223:  */
                    224: int
                    225: cvs_buf_putc(BUF *b, int c)
                    226: {
                    227:        u_char *bp;
                    228:
                    229:        bp = b->cb_cur + b->cb_len;
                    230:        if (bp == (b->cb_buf + b->cb_size)) {
                    231:                /* extend */
                    232:                if (!(b->cb_flags & BUF_AUTOEXT) ||
1.18      xsa       233:                    (cvs_buf_grow(b, (size_t)BUF_INCR) < 0))
1.21      xsa       234:                        fatal("cvs_buf_putc failed");
1.1       jfb       235:
                    236:                /* the buffer might have been moved */
                    237:                bp = b->cb_cur + b->cb_len;
                    238:        }
                    239:        *bp = (u_char)c;
                    240:        b->cb_len++;
                    241:
                    242:        return (0);
1.29      niallo    243: }
                    244:
                    245: /*
                    246:  * cvs_buf_getc()
                    247:  *
                    248:  * Return u_char at buffer position <pos>.
                    249:  *
                    250:  */
                    251: u_char
                    252: cvs_buf_getc(BUF *b, u_int pos)
                    253: {
                    254:        return (b->cb_cur[pos]);
1.1       jfb       255: }
                    256:
                    257: /*
                    258:  * cvs_buf_append()
                    259:  *
                    260:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    261:  * buffer is too small to accept all data, it will attempt to append as much
                    262:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    263:  * will get resized to an appropriate size to accept all data.
1.27      joris     264:  * Returns the number of bytes successfully appended to the buffer.
1.1       jfb       265:  */
                    266: ssize_t
                    267: cvs_buf_append(BUF *b, const void *data, size_t len)
                    268: {
                    269:        size_t left, rlen;
1.8       jfb       270:        u_char *bp, *bep;
1.1       jfb       271:
                    272:        bp = b->cb_cur + b->cb_len;
                    273:        bep = b->cb_buf + b->cb_size;
                    274:        left = bep - bp;
                    275:        rlen = len;
                    276:
                    277:        if (left < len) {
                    278:                if (b->cb_flags & BUF_AUTOEXT) {
                    279:                        if (cvs_buf_grow(b, len - left) < 0)
1.20      xsa       280:                                fatal("cvs_buf_append failed");
1.1       jfb       281:                        bp = b->cb_cur + b->cb_len;
1.5       deraadt   282:                } else
1.1       jfb       283:                        rlen = bep - bp;
                    284:        }
                    285:
                    286:        memcpy(bp, data, rlen);
                    287:        b->cb_len += rlen;
                    288:
                    289:        return (rlen);
                    290: }
                    291:
                    292: /*
                    293:  * cvs_buf_fappend()
                    294:  *
                    295:  */
1.37      ray       296: ssize_t
1.1       jfb       297: cvs_buf_fappend(BUF *b, const char *fmt, ...)
                    298: {
1.37      ray       299:        ssize_t ret;
1.1       jfb       300:        char *str;
                    301:        va_list vap;
                    302:
                    303:        va_start(vap, fmt);
1.4       pat       304:        ret = vasprintf(&str, fmt, vap);
                    305:        va_end(vap);
1.1       jfb       306:
1.20      xsa       307:        if (ret == -1)
                    308:                fatal("cvs_buf_fappend: failed to format data");
1.1       jfb       309:
1.18      xsa       310:        ret = cvs_buf_append(b, str, (size_t)ret);
1.19      joris     311:        xfree(str);
1.1       jfb       312:        return (ret);
                    313: }
                    314:
                    315: /*
1.16      moritz    316:  * cvs_buf_len()
1.1       jfb       317:  *
                    318:  * Returns the size of the buffer that is being used.
                    319:  */
                    320: size_t
1.16      moritz    321: cvs_buf_len(BUF *b)
1.1       jfb       322: {
                    323:        return (b->cb_len);
                    324: }
                    325:
                    326: /*
                    327:  * cvs_buf_peek()
                    328:  *
                    329:  * Peek at the contents of the buffer <b> at offset <off>.
                    330:  */
1.17      xsa       331: const void *
1.1       jfb       332: cvs_buf_peek(BUF *b, size_t off)
                    333: {
                    334:        if (off >= b->cb_len)
                    335:                return (NULL);
                    336:
                    337:        return (b->cb_buf + off);
                    338: }
                    339:
                    340: /*
1.7       djm       341:  * cvs_buf_write_fd()
1.1       jfb       342:  *
1.7       djm       343:  * Write the contents of the buffer <b> to the specified <fd>
1.1       jfb       344:  */
                    345: int
1.7       djm       346: cvs_buf_write_fd(BUF *b, int fd)
1.1       jfb       347: {
                    348:        u_char *bp;
                    349:        size_t len;
                    350:        ssize_t ret;
                    351:
                    352:        len = b->cb_len;
                    353:        bp = b->cb_cur;
                    354:
                    355:        do {
1.16      moritz    356:                ret = write(fd, bp, len);
1.1       jfb       357:                if (ret == -1) {
1.7       djm       358:                        if (errno == EINTR || errno == EAGAIN)
                    359:                                continue;
1.22      xsa       360:                        return (-1);
1.1       jfb       361:                }
                    362:
                    363:                len -= (size_t)ret;
                    364:                bp += (size_t)ret;
                    365:        } while (len > 0);
                    366:
1.7       djm       367:        return (0);
                    368: }
                    369:
                    370: /*
                    371:  * cvs_buf_write()
                    372:  *
                    373:  * Write the contents of the buffer <b> to the file whose path is given in
                    374:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    375:  */
                    376: int
                    377: cvs_buf_write(BUF *b, const char *path, mode_t mode)
                    378: {
1.22      xsa       379:        int fd;
1.32      niallo    380:  open:
                    381:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
                    382:                if ((errno == EACCES) && (unlink(path) != -1))
                    383:                        goto open;
                    384:                else
                    385:                        fatal("open: `%s': %s", path, strerror(errno));
                    386:        }
1.7       djm       387:
1.22      xsa       388:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       389:                (void)unlink(path);
1.22      xsa       390:                fatal("cvs_buf_write: cvs_buf_write_fd: `%s'", path);
1.7       djm       391:        }
1.1       jfb       392:        (void)close(fd);
1.24      joris     393:
                    394:        if (chmod(path, mode) < 0)
                    395:                fatal("cvs_buf_write: chmod failed: %s", strerror(errno));
1.1       jfb       396:
1.22      xsa       397:        return (0);
1.1       jfb       398: }
                    399:
1.7       djm       400: /*
                    401:  * cvs_buf_write_stmp()
                    402:  *
1.13      joris     403:  * Write the contents of the buffer <b> to a temporary file whose path is
1.7       djm       404:  * specified using <template> (see mkstemp.3). NB. This function will modify
                    405:  * <template>, as per mkstemp
                    406:  */
1.38    ! niallo    407: void
1.7       djm       408: cvs_buf_write_stmp(BUF *b, char *template, mode_t mode)
                    409: {
1.22      xsa       410:        int fd;
1.7       djm       411:
1.20      xsa       412:        if ((fd = mkstemp(template)) == -1)
                    413:                fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7       djm       414:
1.22      xsa       415:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       416:                (void)unlink(template);
1.22      xsa       417:                fatal("cvs_buf_write_stmp: cvs_buf_write_fd: `%s'", template);
1.7       djm       418:        }
1.36      niallo    419:        (void)fchmod(fd, mode);
1.7       djm       420:        (void)close(fd);
                    421: }
1.1       jfb       422:
                    423: /*
                    424:  * cvs_buf_grow()
                    425:  *
                    426:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    427:  * operation regardless of the result.
                    428:  * Returns the new size on success, or -1 on failure.
                    429:  */
                    430: static ssize_t
                    431: cvs_buf_grow(BUF *b, size_t len)
                    432: {
                    433:        void *tmp;
                    434:        size_t diff;
                    435:
1.8       jfb       436:        diff = b->cb_cur - b->cb_buf;
1.30      niallo    437:        /* Buffer not allocated yet */
                    438:        if (b->cb_size == 0)
                    439:                tmp = xmalloc(len);
                    440:        else
                    441:                tmp = xrealloc(b->cb_buf, b->cb_size + len);
1.1       jfb       442:        b->cb_buf = (u_char *)tmp;
                    443:        b->cb_size += len;
                    444:
                    445:        /* readjust pointers in case the buffer moved in memory */
                    446:        b->cb_cur = b->cb_buf + diff;
                    447:
                    448:        return (ssize_t)b->cb_size;
                    449: }