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

Annotation of src/usr.bin/cvs/hist.c, Revision 1.12

1.12    ! joris       1: /*     $OpenBSD: hist.c,v 1.11 2005/09/05 19:45:42 xsa Exp $   */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.3       tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.3       tedu        6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.3       tedu       10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.3       tedu       13:  *    derived from this software without specific prior written permission.
1.1       jfb        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
1.3       tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/stat.h>
                     28:
1.5       xsa        29: #include <fcntl.h>
1.1       jfb        30: #include <stdio.h>
                     31: #include <stdlib.h>
1.5       xsa        32: #include <string.h>
1.1       jfb        33: #include <unistd.h>
                     34:
1.5       xsa        35: #include "cvs.h"
1.1       jfb        36: #include "log.h"
                     37:
1.7       xsa        38: #define CVS_HIST_BUFSIZE       8192
1.1       jfb        39:
                     40:
                     41:
1.7       xsa        42: static int     cvs_hist_fillbuf(CVSHIST *);
                     43: static int     cvs_hist_fmt(const struct cvs_hent *, char *, size_t);
1.1       jfb        44:
                     45:
                     46: /*
                     47:  * cvs_hist_open()
                     48:  *
                     49:  * Open a CVS history file.
                     50:  * Returns the number of entries in the file on success, or -1 on error.
                     51:  */
1.8       xsa        52: CVSHIST *
1.1       jfb        53: cvs_hist_open(const char *path)
                     54: {
                     55:        CVSHIST *histp;
                     56:
1.12    ! joris      57:        histp = (CVSHIST *)xmalloc(sizeof(*histp));
1.1       jfb        58:        memset(histp, 0, sizeof(*histp));
                     59:
1.12    ! joris      60:        histp->chf_buf = (char *)xmalloc((size_t)CVS_HIST_BUFSIZE);
1.1       jfb        61:        histp->chf_blen = CVS_HIST_BUFSIZE;
                     62:        histp->chf_off = 0;
                     63:
                     64:        histp->chf_sindex = 0;
                     65:        histp->chf_cindex = 0;
                     66:        histp->chf_nbhent = 0;
1.11      xsa        67:
                     68:        cvs_log(LP_TRACE, "cvs_hist_open(%s)", path);
1.1       jfb        69:
                     70:        histp->chf_fd = open(path, O_RDONLY, 0);
                     71:        if (histp->chf_fd == -1) {
                     72:                cvs_log(LP_ERRNO,
                     73:                    "failed to open CVS history file `%s'", path);
1.9       xsa        74:                cvs_nolog = 1;
1.12    ! joris      75:                xfree(histp->chf_buf);
        !            76:                xfree(histp);
1.1       jfb        77:                return (NULL);
                     78:        }
                     79:
                     80:        cvs_hist_fillbuf(histp);
                     81:
                     82:        return (histp);
                     83: }
                     84:
                     85:
                     86: /*
                     87:  * cvs_hist_close()
                     88:  *
                     89:  * Close the CVS history file previously opened by a call to cvs_hist_open()
                     90:  */
                     91: void
                     92: cvs_hist_close(CVSHIST *histp)
                     93: {
                     94:        if (histp->chf_fd >= 0)
                     95:                (void)close(histp->chf_fd);
1.12    ! joris      96:        xfree(histp->chf_buf);
        !            97:        xfree(histp);
1.1       jfb        98: }
                     99:
                    100:
                    101: /*
                    102:  * cvs_hist_getnext()
                    103:  *
                    104:  * Get the next entry from the history file <histp>.  Whenever using this
                    105:  * function, it should be assumed that the return value of the previous call
                    106:  * to cvs_hist_getnext() is now invalid.
                    107:  * Returns the next entry from the file on success, or NULL on failure or if
                    108:  * no entries are left.
                    109:  */
1.8       xsa       110: struct cvs_hent *
1.1       jfb       111: cvs_hist_getnext(CVSHIST *histp)
                    112: {
                    113:        if (histp->chf_cindex == histp->chf_nbhent) {
                    114:                /* no more cached entries, refill buf and parse */
                    115:                cvs_hist_fillbuf(histp);
                    116:                cvs_hist_parse(histp);
                    117:        }
                    118:        return (&(histp->chf_hent[histp->chf_cindex++]));
                    119: }
                    120:
                    121:
                    122: /*
                    123:  * cvs_hist_append()
                    124:  *
                    125:  * Append a history entry to the history file <histp>.  The file offset is
                    126:  * first set to the end of the file.
                    127:  * Returns 0 on success, or -1 on failure.
                    128:  */
                    129: int
                    130: cvs_hist_append(CVSHIST *histp, struct cvs_hent *hentp)
                    131: {
                    132:        char hbuf[128];
1.9       xsa       133:
                    134:        if (cvs_nolog == 1)
                    135:                return (0);
1.1       jfb       136:
                    137:        if (cvs_hist_fmt(hentp, hbuf, sizeof(hbuf)) < 0) {
                    138:                cvs_log(LP_ERR, "failed to append CVS history entry");
                    139:                return (-1);
                    140:        }
                    141:
                    142:        /* position ourself at the end */
                    143:        if (lseek(histp->chf_fd, (off_t)0, SEEK_END) == -1) {
                    144:                cvs_log(LP_ERRNO, "failed to seek to end of CVS history file");
                    145:                return (-1);
                    146:        }
                    147:
                    148:        if (write(histp->chf_fd, hbuf, strlen(hbuf)) == -1) {
                    149:                cvs_log(LP_ERR, "failed to write CVS history entry to file");
                    150:                return (-1);
                    151:        }
                    152:
                    153:        return (0);
                    154: }
                    155:
                    156:
                    157:
                    158: /*
                    159:  * cvs_hist_fillbuf()
                    160:  *
                    161:  * Fill the history file's internal buffer for future parsing.
                    162:  */
                    163: static int
                    164: cvs_hist_fillbuf(CVSHIST *histp)
                    165: {
                    166:        ssize_t ret;
                    167:
                    168:        /* reposition ourself in case we're missing the start of a record */
                    169:        if (lseek(histp->chf_fd, histp->chf_off, SEEK_SET) == -1) {
                    170:                cvs_log(LP_ERRNO, "failed to seek in CVS history file");
                    171:                return (-1);
                    172:        }
                    173:        ret = read(histp->chf_fd, histp->chf_buf, histp->chf_blen);
                    174:        if (ret == -1) {
                    175:                cvs_log(LP_ERRNO, "failed to buffer CVS history file");
                    176:                return (-1);
1.2       deraadt   177:        } else {
1.1       jfb       178:                histp->chf_bused = (size_t)ret;
                    179:        }
                    180:
                    181:        return (ret);
                    182: }
                    183:
                    184:
                    185: /*
                    186:  * cvs_hist_parse()
                    187:  *
                    188:  * Parse the current contents of the internal buffer of <histp> and regenerate
                    189:  * the buffered history entries.
                    190:  * Returns the number of entries parsed on success, or -1 on failure.
                    191:  */
                    192: int
                    193: cvs_hist_parse(CVSHIST *histp)
                    194: {
                    195:        u_int i, fld;
                    196:        char *fields[CVS_HIST_NBFLD], *sp, *bep, *ep, *errp;
                    197:
                    198:        sp = histp->chf_buf;
                    199:        bep = histp->chf_buf + histp->chf_bused - 1;
                    200:
                    201:        for (i = 0; i < CVS_HIST_CACHE; i++) {
                    202:                ep = memchr(sp, '\n', bep - sp);
                    203:                if (ep == NULL) {
                    204:                        /*
                    205:                         * No record or incomplete record left to parse,
                    206:                         * so adjust the next read offset in consequence.
                    207:                         */
                    208:                        histp->chf_off += (off_t)(sp - histp->chf_buf);
                    209:                        break;
1.2       deraadt   210:                } else if (ep == bep) {
1.1       jfb       211:                        histp->chf_off += (off_t)histp->chf_bused;
                    212:                }
                    213:                *(ep++) = '\0';
                    214:
                    215:                printf("hist(%s)\n", sp);
                    216:
                    217:                histp->chf_hent[i].ch_event = *sp++;
                    218:
                    219:                /* split the record in fields */
                    220:                fields[0] = sp;
                    221:
                    222:                fld = 1;
                    223:                while (sp < ep) {
                    224:                        if (*sp == '|') {
                    225:                                *sp = '\0';
                    226:                                fields[fld++] = sp + 1;
                    227:                        }
                    228:                        if (fld == CVS_HIST_NBFLD)
                    229:                                break;
                    230:                        sp++;
                    231:                }
                    232: #if 0
                    233:                for (fld = 0; fld < CVS_HIST_NBFLD; fld++)
                    234:                        printf("fields[%u] = `%s'\n", fld, fields[fld]);
                    235: #endif
                    236:
                    237:                histp->chf_hent[i].ch_date = (time_t)strtol(fields[0],
                    238:                    &errp, 16);
                    239:                if (*errp != '\0') {
                    240:                        cvs_log(LP_ERR,
                    241:                            "parse error in date field of CVS history entry");
                    242:                        continue;
                    243:                }
                    244:
                    245:                histp->chf_hent[i].ch_user = fields[1];
                    246:                histp->chf_hent[i].ch_curdir = fields[2];
                    247:                histp->chf_hent[i].ch_repo = fields[3];
                    248:                histp->chf_hent[i].ch_rev = rcsnum_alloc();
                    249:                rcsnum_aton(fields[4], NULL, histp->chf_hent[i].ch_rev);
                    250:                histp->chf_hent[i].ch_arg = fields[5];
                    251:                sp = ep;
                    252:        }
                    253:
                    254:        /* update indexes */
                    255:        histp->chf_sindex += histp->chf_nbhent;
                    256:        histp->chf_nbhent = i;
                    257:        histp->chf_cindex = 0;
                    258:
                    259:
                    260:        return (i);
                    261: }
                    262:
                    263:
                    264: /*
                    265:  * cvs_hist_fmt()
                    266:  *
                    267:  * Format the contents of the CVS history entry <ent> into the format used in
                    268:  * the CVS `history' file, and store the resulting string in <buf>, which is
                    269:  * of size <blen>.
                    270:  */
                    271: static int
                    272: cvs_hist_fmt(const struct cvs_hent *ent, char *buf, size_t blen)
                    273: {
                    274:        char numbuf[64];
1.4       joris     275:        int len;
1.1       jfb       276:
1.3       tedu      277:        if (rcsnum_tostr(ent->ch_rev, numbuf, sizeof(numbuf)) == NULL)
1.1       jfb       278:                return (-1);
                    279:
1.4       joris     280:        len = snprintf(buf, blen, "%c%8x|%s|%s|%s|%s|%s",
1.1       jfb       281:            ent->ch_event, ent->ch_date, ent->ch_user, ent->ch_curdir,
1.4       joris     282:            ent->ch_repo, numbuf, ent->ch_arg);
                    283:        if (len >= (int)blen || len == -1)
                    284:                return (-1);
                    285:        return (len);
1.1       jfb       286: }