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

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