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

Annotation of src/usr.bin/cvs/file.c, Revision 1.18

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      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/types.h>
                     28: #include <sys/queue.h>
                     29: #include <sys/stat.h>
                     30:
                     31: #include <pwd.h>
                     32: #include <errno.h>
                     33: #include <stdio.h>
                     34: #include <fcntl.h>
                     35: #include <dirent.h>
                     36: #include <stdlib.h>
                     37: #include <unistd.h>
                     38: #include <string.h>
                     39: #include <fnmatch.h>
                     40:
                     41: #include "cvs.h"
                     42: #include "log.h"
1.14      jfb        43: #include "file.h"
1.1       jfb        44:
                     45:
                     46: #define CVS_IGN_STATIC    0x01     /* pattern is static, no need to glob */
                     47:
                     48: #define CVS_CHAR_ISMETA(c)  ((c == '*') || (c == '?') || (c == '['))
                     49:
                     50:
                     51: /* ignore pattern */
                     52: struct cvs_ignpat {
                     53:        char  ip_pat[MAXNAMLEN];
                     54:        int   ip_flags;
                     55:        TAILQ_ENTRY (cvs_ignpat) ip_list;
                     56: };
                     57:
                     58:
                     59: /*
                     60:  * Standard patterns to ignore.
                     61:  */
                     62:
                     63: static const char *cvs_ign_std[] = {
                     64:        ".",
                     65:        "..",
                     66:        "*.o",
                     67:        "*.so",
                     68:        "*.bak",
                     69:        "*.orig",
                     70:        "*.rej",
                     71:        "*.exe",
                     72:        "*.depend",
                     73:        "CVS",
                     74:        "core",
1.8       jfb        75:        ".#*",
1.1       jfb        76: #ifdef OLD_SMELLY_CRUFT
                     77:        "RCSLOG",
                     78:        "tags",
                     79:        "TAGS",
                     80:        "RCS",
                     81:        "SCCS",
                     82:        "#*",
                     83:        ",*",
                     84: #endif
                     85: };
                     86:
                     87:
1.11      jfb        88: /*
                     89:  * Entries in the CVS/Entries file with a revision of '0' have only been
                     90:  * added.  Compare against this revision to see if this is the case
                     91:  */
1.4       jfb        92: static RCSNUM *cvs_addedrev;
                     93:
                     94:
1.1       jfb        95: TAILQ_HEAD(, cvs_ignpat)  cvs_ign_pats;
                     96:
                     97:
1.14      jfb        98: static int        cvs_file_getdir  (CVSFILE *, int);
1.6       jfb        99: static void       cvs_file_freedir (struct cvs_dir *);
1.16      jfb       100: static int        cvs_file_sort    (struct cvs_flist *, u_int);
1.6       jfb       101: static int        cvs_file_cmp     (const void *, const void *);
                    102: static CVSFILE*   cvs_file_alloc   (const char *, u_int);
1.14      jfb       103: static CVSFILE*   cvs_file_lget    (const char *, int, CVSFILE *);
1.3       jfb       104:
                    105:
                    106:
1.1       jfb       107: /*
                    108:  * cvs_file_init()
                    109:  *
                    110:  */
                    111:
                    112: int
                    113: cvs_file_init(void)
                    114: {
                    115:        int i;
                    116:        size_t len;
                    117:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    118:        FILE *ifp;
                    119:        struct passwd *pwd;
                    120:
                    121:        TAILQ_INIT(&cvs_ign_pats);
                    122:
1.4       jfb       123:        cvs_addedrev = rcsnum_alloc();
                    124:        rcsnum_aton("0", NULL, cvs_addedrev);
                    125:
1.1       jfb       126:        /* standard patterns to ignore */
1.10      jfb       127:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.1       jfb       128:                cvs_file_ignore(cvs_ign_std[i]);
                    129:
                    130:        /* read the cvsignore file in the user's home directory, if any */
                    131:        pwd = getpwuid(getuid());
                    132:        if (pwd != NULL) {
                    133:                snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
                    134:                ifp = fopen(path, "r");
                    135:                if (ifp == NULL) {
                    136:                        if (errno != ENOENT)
                    137:                                cvs_log(LP_ERRNO, "failed to open `%s'", path);
                    138:                }
                    139:                else {
                    140:                        while (fgets(buf, sizeof(buf), ifp) != NULL) {
                    141:                                len = strlen(buf);
                    142:                                if (len == 0)
                    143:                                        continue;
                    144:                                if (buf[len - 1] != '\n') {
                    145:                                        cvs_log(LP_ERR, "line too long in `%s'",
                    146:                                            path);
                    147:                                }
                    148:                                buf[--len] = '\0';
                    149:                                cvs_file_ignore(buf);
                    150:                        }
                    151:                        (void)fclose(ifp);
                    152:                }
                    153:        }
                    154:
                    155:        return (0);
                    156: }
                    157:
                    158:
                    159: /*
                    160:  * cvs_file_ignore()
                    161:  *
                    162:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    163:  * Returns 0 on success, or -1 on failure.
                    164:  */
                    165:
                    166: int
                    167: cvs_file_ignore(const char *pat)
                    168: {
                    169:        char *cp;
                    170:        struct cvs_ignpat *ip;
                    171:
                    172:        ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
                    173:        if (ip == NULL) {
                    174:                cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
                    175:                return (-1);
                    176:        }
                    177:
                    178:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    179:
                    180:        /* check if we will need globbing for that pattern */
                    181:        ip->ip_flags = CVS_IGN_STATIC;
                    182:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    183:                if (CVS_CHAR_ISMETA(*cp)) {
                    184:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    185:                        break;
                    186:                }
                    187:        }
                    188:
                    189:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    190:
                    191:        return (0);
                    192: }
                    193:
                    194:
                    195: /*
1.5       jfb       196:  * cvs_file_chkign()
1.1       jfb       197:  *
                    198:  * Returns 1 if the filename <file> is matched by one of the ignore
                    199:  * patterns, or 0 otherwise.
                    200:  */
                    201:
                    202: int
1.5       jfb       203: cvs_file_chkign(const char *file)
1.1       jfb       204: {
                    205:        struct cvs_ignpat *ip;
                    206:
                    207:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    208:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    209:                        if (strcmp(file, ip->ip_pat) == 0)
                    210:                                return (1);
                    211:                }
                    212:                else if (fnmatch(ip->ip_pat, file, FNM_PERIOD) == 0)
                    213:                        return (1);
                    214:        }
                    215:
                    216:        return (0);
                    217: }
                    218:
                    219:
                    220: /*
1.6       jfb       221:  * cvs_file_create()
1.1       jfb       222:  *
1.6       jfb       223:  * Create a new file whose path is specified in <path> and of type <type>.
1.1       jfb       224:  */
                    225:
1.6       jfb       226: CVSFILE*
                    227: cvs_file_create(const char *path, u_int type, mode_t mode)
1.1       jfb       228: {
1.6       jfb       229:        int fd;
                    230:        CVSFILE *cfp;
1.1       jfb       231:
1.6       jfb       232:        cfp = cvs_file_alloc(path, type);
                    233:        if (cfp == NULL)
                    234:                return (NULL);
                    235:        cfp->cf_type = type;
1.1       jfb       236:
1.6       jfb       237:        if (type == DT_DIR) {
                    238:                if (mkdir(path, mode) == -1) {
                    239:                        cvs_file_free(cfp);
                    240:                        return (NULL);
                    241:                }
1.1       jfb       242:        }
1.6       jfb       243:        else {
                    244:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    245:                if (fd == -1) {
                    246:                        cvs_file_free(cfp);
1.1       jfb       247:                        return (NULL);
                    248:                }
1.6       jfb       249:                (void)close(fd);
1.1       jfb       250:        }
                    251:
1.6       jfb       252:        return (cfp);
1.3       jfb       253: }
                    254:
                    255:
                    256: /*
                    257:  * cvs_file_get()
                    258:  *
                    259:  * Load a cvs_file structure with all the information pertaining to the file
                    260:  * <path>.
1.4       jfb       261:  * The <flags> parameter specifies various flags that alter the behaviour of
                    262:  * the function.  The CF_STAT flag is used to keep stat information of the
                    263:  * file in the structure after it is used (it is lost otherwise).  The
                    264:  * CF_RECURSE flag causes the function to recursively load subdirectories
                    265:  * when <path> is a directory.  The CF_SORT flag causes the files to be
                    266:  * sorted in alphabetical order upon loading.
                    267:  * The special case of "." as a path specification generates recursion for
                    268:  * a single level and is equivalent to calling cvs_file_get() on all files
                    269:  * of that directory.
1.3       jfb       270:  * Returns a pointer to the cvs file structure, which must later be freed
                    271:  * with cvs_file_free().
                    272:  */
                    273:
1.14      jfb       274: CVSFILE*
1.3       jfb       275: cvs_file_get(const char *path, int flags)
                    276: {
1.14      jfb       277:        return cvs_file_lget(path, flags, NULL);
1.9       jfb       278: }
                    279:
                    280:
                    281: /*
                    282:  * cvs_file_getspec()
                    283:  *
                    284:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    285:  * whose size is given in <fsn>.
                    286:  * Returns a pointer to the lowest common subdirectory to all specified
                    287:  * files.
                    288:  */
                    289:
                    290: CVSFILE*
                    291: cvs_file_getspec(char **fspec, int fsn, int flags)
                    292: {
                    293:        int i, c;
1.15      jfb       294:        char common[MAXPATHLEN], *cp;
1.14      jfb       295:        CVSFILE *cfp;
1.9       jfb       296:
1.3       jfb       297:        return (cfp);
                    298: }
                    299:
                    300:
                    301: /*
1.13      jfb       302:  * cvs_file_find()
                    303:  *
                    304:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    305:  * The file's pathname <path> must be relative to the base of <hier>.
                    306:  * Returns the entry on success, or NULL on failure.
                    307:  */
                    308:
                    309: CVSFILE*
                    310: cvs_file_find(CVSFILE *hier, const char *path)
                    311: {
                    312:        char *pp, *sp, pbuf[MAXPATHLEN];
                    313:        CVSFILE *sf, *cf;
                    314:
                    315:        strlcpy(pbuf, path, sizeof(pbuf));
                    316:
                    317:        cf = hier;
                    318:        pp = pbuf;
                    319:        do {
                    320:                sp = strchr(pp, '/');
                    321:                if (sp != NULL)
                    322:                        *sp = '\0';
                    323:
                    324:                /* special case */
                    325:                if (*pp == '.') {
                    326:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    327:                                /* request to go back to parent */
                    328:                                if (cf->cf_parent == NULL) {
                    329:                                        cvs_log(LP_NOTICE,
                    330:                                            "path %s goes back too far", path);
                    331:                                        return (NULL);
                    332:                                }
                    333:                                cf = cf->cf_parent;
                    334:                                continue;
                    335:                        }
                    336:                        else if (*(pp + 1) == '\0')
                    337:                                continue;
                    338:                }
                    339:
                    340:                TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
                    341:                        if (strcmp(pp, sf->cf_name) == 0)
                    342:                                break;
                    343:                if (sf == NULL)
                    344:                        return (NULL);
                    345:
                    346:                cf = sf;
                    347:                pp = sp;
                    348:        } while (sp != NULL);
                    349:
                    350:        return (NULL);
                    351: }
                    352:
                    353:
                    354: /*
1.3       jfb       355:  * cvs_file_getdir()
                    356:  *
                    357:  * Get a cvs directory structure for the directory whose path is <dir>.
                    358:  */
                    359:
1.6       jfb       360: static int
1.14      jfb       361: cvs_file_getdir(CVSFILE *cf, int flags)
1.3       jfb       362: {
1.13      jfb       363:        int ret, fd;
1.16      jfb       364:        u_int ndirs;
1.3       jfb       365:        long base;
1.13      jfb       366:        void *dp, *ep;
1.11      jfb       367:        char fbuf[2048], pbuf[MAXPATHLEN];
1.3       jfb       368:        struct dirent *ent;
1.14      jfb       369:        CVSFILE *cfp;
1.3       jfb       370:        struct cvs_dir *cdp;
1.10      jfb       371:        struct cvs_flist dirs;
1.3       jfb       372:
1.18    ! jfb       373:        ndirs = 0;
1.10      jfb       374:        TAILQ_INIT(&dirs);
1.7       jfb       375:        cdp = cf->cf_ddat;
                    376:
1.4       jfb       377:        if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
                    378:                cdp->cd_repo = strdup(pbuf);
                    379:                if (cdp->cd_repo == NULL) {
                    380:                        free(cdp);
1.6       jfb       381:                        return (-1);
1.4       jfb       382:                }
1.3       jfb       383:        }
                    384:
                    385:        cdp->cd_root = cvsroot_get(cf->cf_path);
                    386:        if (cdp->cd_root == NULL) {
                    387:                cvs_file_freedir(cdp);
1.6       jfb       388:                return (-1);
1.3       jfb       389:        }
                    390:
1.14      jfb       391:        if (flags & CF_MKADMIN)
                    392:                cvs_mkadmin(cf, 0755);
                    393:
                    394:        cdp->cd_ent = cvs_ent_open(cf->cf_path, O_RDONLY);
                    395:
1.3       jfb       396:        fd = open(cf->cf_path, O_RDONLY);
                    397:        if (fd == -1) {
                    398:                cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
                    399:                cvs_file_freedir(cdp);
1.6       jfb       400:                return (-1);
1.3       jfb       401:        }
                    402:
1.11      jfb       403:        do {
                    404:                ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    405:                if (ret == -1) {
                    406:                        cvs_log(LP_ERRNO, "failed to get directory entries");
                    407:                        (void)close(fd);
                    408:                        cvs_file_freedir(cdp);
                    409:                        return (-1);
                    410:                }
1.10      jfb       411:
1.11      jfb       412:                dp = fbuf;
                    413:                ep = fbuf + (size_t)ret;
                    414:                while (dp < ep) {
                    415:                        ent = (struct dirent *)dp;
                    416:                        dp += ent->d_reclen;
                    417:
                    418:                        if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
                    419:                                continue;
                    420:
                    421:                        snprintf(pbuf, sizeof(pbuf), "%s/%s",
                    422:                            cf->cf_path, ent->d_name);
1.14      jfb       423:                        cfp = cvs_file_lget(pbuf, flags, cf);
1.11      jfb       424:                        if (cfp != NULL) {
1.16      jfb       425:                                if (cfp->cf_type == DT_DIR) {
1.11      jfb       426:                                        TAILQ_INSERT_HEAD(&dirs, cfp, cf_list);
1.16      jfb       427:                                        ndirs++;
                    428:                                }
                    429:                                else {
1.11      jfb       430:                                        TAILQ_INSERT_HEAD(&(cdp->cd_files), cfp,
                    431:                                            cf_list);
1.16      jfb       432:                                        cdp->cd_nfiles++;
                    433:                                }
1.11      jfb       434:                        }
1.4       jfb       435:                }
1.11      jfb       436:        } while (ret > 0);
1.3       jfb       437:
1.14      jfb       438:        /* we can now close our Entries file */
                    439:        if (cdp->cd_ent != NULL) {
                    440:                cvs_ent_close(cdp->cd_ent);
                    441:                cdp->cd_ent = NULL;
                    442:        }
                    443:
1.10      jfb       444:        if (flags & CF_SORT) {
1.16      jfb       445:                cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
                    446:                cvs_file_sort(&dirs, ndirs);
1.10      jfb       447:        }
                    448:        TAILQ_FOREACH(cfp, &dirs, cf_list)
                    449:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.16      jfb       450:        cdp->cd_nfiles += ndirs;
1.3       jfb       451:
                    452:        (void)close(fd);
1.6       jfb       453:        cf->cf_ddat = cdp;
1.3       jfb       454:
1.6       jfb       455:        return (0);
1.3       jfb       456: }
                    457:
                    458:
                    459: /*
                    460:  * cvs_file_free()
                    461:  *
                    462:  * Free a cvs_file structure and its contents.
                    463:  */
                    464:
                    465: void
1.14      jfb       466: cvs_file_free(CVSFILE *cf)
1.3       jfb       467: {
                    468:        if (cf->cf_path != NULL)
                    469:                free(cf->cf_path);
                    470:        if (cf->cf_stat != NULL)
                    471:                free(cf->cf_stat);
                    472:        if (cf->cf_ddat != NULL)
                    473:                cvs_file_freedir(cf->cf_ddat);
                    474:        free(cf);
1.5       jfb       475: }
                    476:
                    477:
                    478: /*
                    479:  * cvs_file_examine()
                    480:  *
                    481:  * Examine the contents of the CVS file structure <cf> with the function
                    482:  * <exam>.  The function is called for all subdirectories and files of the
                    483:  * root file.
                    484:  */
                    485:
                    486: int
                    487: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    488: {
                    489:        int ret;
1.14      jfb       490:        CVSFILE *fp;
1.5       jfb       491:
                    492:        if (cf->cf_type == DT_DIR) {
                    493:                ret = (*exam)(cf, arg);
1.10      jfb       494:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       495:                        ret = cvs_file_examine(fp, exam, arg);
                    496:                        if (ret == -1)
1.13      jfb       497:                                break;
1.5       jfb       498:                }
                    499:        }
                    500:        else
1.13      jfb       501:                ret = (*exam)(cf, arg);
                    502:
                    503:        return (ret);
1.3       jfb       504: }
                    505:
                    506:
                    507: /*
                    508:  * cvs_file_freedir()
                    509:  *
                    510:  * Free a cvs_dir structure and its contents.
                    511:  */
                    512:
                    513: static void
                    514: cvs_file_freedir(struct cvs_dir *cd)
                    515: {
1.14      jfb       516:        CVSFILE *cfp;
1.3       jfb       517:
                    518:        if (cd->cd_root != NULL)
                    519:                cvsroot_free(cd->cd_root);
                    520:        if (cd->cd_repo != NULL)
                    521:                free(cd->cd_repo);
                    522:
1.14      jfb       523:        if (cd->cd_ent != NULL)
                    524:                cvs_ent_close(cd->cd_ent);
                    525:
1.10      jfb       526:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
                    527:                cfp = TAILQ_FIRST(&(cd->cd_files));
                    528:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       529:                cvs_file_free(cfp);
                    530:        }
                    531: }
                    532:
                    533:
                    534: /*
                    535:  * cvs_file_sort()
                    536:  *
1.16      jfb       537:  * Sort a list of cvs file structures according to their filename.  The list
                    538:  * <flp> is modified according to the sorting algorithm.  The number of files
                    539:  * in the list must be given by <nfiles>.
                    540:  * Returns 0 on success, or -1 on failure.
1.3       jfb       541:  */
                    542:
                    543: static int
1.16      jfb       544: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       545: {
                    546:        int i;
                    547:        size_t nb;
1.16      jfb       548:        CVSFILE *cf, **cfvec;
                    549:
                    550:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
                    551:        if (cfvec == NULL) {
                    552:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                    553:                return (-1);
                    554:        }
1.3       jfb       555:
                    556:        i = 0;
1.10      jfb       557:        TAILQ_FOREACH(cf, flp, cf_list) {
1.16      jfb       558:                if (i == (int)nfiles) {
1.3       jfb       559:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb       560:                        /* rebuild the list and abort sorting */
                    561:                        while (--i >= 0)
                    562:                                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
                    563:                        free(cfvec);
1.3       jfb       564:                        return (-1);
                    565:                }
1.16      jfb       566:                cfvec[i++] = cf;
1.3       jfb       567:
                    568:                /* now unlink it from the list,
                    569:                 * we'll put it back in order later
                    570:                 */
1.10      jfb       571:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       572:        }
                    573:
                    574:        /* clear the list just in case */
1.10      jfb       575:        TAILQ_INIT(flp);
1.3       jfb       576:        nb = (size_t)i;
                    577:
                    578:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    579:
                    580:        /* rebuild the list from the bottom up */
                    581:        for (i = (int)nb - 1; i >= 0; i--)
1.10      jfb       582:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       583:
1.16      jfb       584:        free(cfvec);
1.3       jfb       585:        return (0);
                    586: }
                    587:
                    588:
                    589: static int
                    590: cvs_file_cmp(const void *f1, const void *f2)
                    591: {
1.14      jfb       592:        CVSFILE *cf1, *cf2;
                    593:        cf1 = *(CVSFILE **)f1;
                    594:        cf2 = *(CVSFILE **)f2;
1.3       jfb       595:        return strcmp(cf1->cf_name, cf2->cf_name);
1.6       jfb       596: }
                    597:
                    598:
                    599: CVSFILE*
                    600: cvs_file_alloc(const char *path, u_int type)
                    601: {
                    602:        size_t len;
                    603:        char pbuf[MAXPATHLEN];
                    604:        CVSFILE *cfp;
                    605:        struct cvs_dir *ddat;
                    606:
1.14      jfb       607:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       608:        if (cfp == NULL) {
                    609:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    610:                return (NULL);
                    611:        }
                    612:        memset(cfp, 0, sizeof(*cfp));
                    613:
                    614:        /* ditch trailing slashes */
                    615:        strlcpy(pbuf, path, sizeof(pbuf));
                    616:        len = strlen(pbuf);
                    617:        while (pbuf[len - 1] == '/')
                    618:                pbuf[--len] = '\0';
                    619:
                    620:        cfp->cf_path = strdup(pbuf);
                    621:        if (cfp->cf_path == NULL) {
                    622:                free(cfp);
                    623:                return (NULL);
                    624:        }
                    625:
                    626:        cfp->cf_name = strrchr(cfp->cf_path, '/');
                    627:        if (cfp->cf_name == NULL)
                    628:                cfp->cf_name = cfp->cf_path;
                    629:        else
                    630:                cfp->cf_name++;
                    631:
                    632:        cfp->cf_type = type;
                    633:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    634:
                    635:        if (type == DT_DIR) {
                    636:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    637:                if (ddat == NULL) {
                    638:                        cvs_file_free(cfp);
                    639:                        return (NULL);
                    640:                }
                    641:                memset(ddat, 0, sizeof(*ddat));
1.10      jfb       642:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       643:                cfp->cf_ddat = ddat;
                    644:        }
                    645:        return (cfp);
1.1       jfb       646: }
1.14      jfb       647:
                    648:
                    649: /*
                    650:  * cvs_file_lget()
                    651:  *
                    652:  * Get the file and link it with the parent right away.
                    653:  */
                    654:
                    655: static CVSFILE*
                    656: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
                    657: {
                    658:        int cwd;
                    659:        size_t len;
                    660:        char buf[32];
                    661:        struct stat st;
                    662:        struct tm lmtm;
                    663:        CVSFILE *cfp;
                    664:        struct cvs_ent *ent;
                    665:
                    666:        ent = NULL;
                    667:
                    668:        if (strcmp(path, ".") == 0)
                    669:                cwd = 1;
                    670:        else
                    671:                cwd = 0;
                    672:
                    673:        if (stat(path, &st) == -1) {
                    674:                cvs_log(LP_ERRNO, "failed to stat %s", path);
                    675:                return (NULL);
                    676:        }
                    677:
                    678:        cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
                    679:        if (cfp == NULL) {
                    680:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    681:                return (NULL);
                    682:        }
                    683:        cfp->cf_parent = parent;
                    684:
                    685:        if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
1.15      jfb       686:                ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), cfp->cf_name);
1.14      jfb       687:        }
                    688:
                    689:        if (ent == NULL)
                    690:                cfp->cf_cvstat = (cwd == 1) ?
                    691:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
                    692:        else {
                    693:                /* always show directories as up-to-date */
                    694:                if (ent->ce_type == CVS_ENT_DIR)
                    695:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                    696:                else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                    697:                        cfp->cf_cvstat = CVS_FST_ADDED;
                    698:                else {
                    699:                        /* check last modified time */
                    700:                        if ((gmtime_r((time_t *)&(st.st_mtime),
                    701:                            &lmtm) == NULL) ||
                    702:                            (asctime_r(&lmtm, buf) == NULL)) {
                    703:                                cvs_log(LP_ERR,
                    704:                                    "failed to generate file timestamp");
                    705:                                /* fake an up to date file */
                    706:                                strlcpy(buf, ent->ce_timestamp, sizeof(buf));
                    707:                        }
                    708:                        len = strlen(buf);
                    709:                        if ((len > 0) && (buf[len - 1] == '\n'))
                    710:                                buf[--len] = '\0';
                    711:
                    712:                        if (strcmp(buf, ent->ce_timestamp) == 0)
                    713:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                    714:                        else
                    715:                                cfp->cf_cvstat = CVS_FST_MODIFIED;
                    716:                }
                    717:        }
                    718:
                    719:        if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
                    720:                if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN)) {
                    721:                        free(cfp->cf_ddat);
                    722:                        cfp->cf_ddat = NULL;
                    723:                }
                    724:                else if (cvs_file_getdir(cfp, flags) < 0) {
                    725:                        cvs_file_free(cfp);
                    726:                        return (NULL);
                    727:                }
                    728:        }
                    729:
                    730:        if (flags & CF_STAT) {
                    731:                cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
                    732:                if (cfp->cf_stat == NULL) {
                    733:                        cvs_log(LP_ERRNO, "failed to allocate stat structure");
                    734:                        cvs_file_free(cfp);
                    735:                        return (NULL);
                    736:                }
                    737:
                    738:                memcpy(cfp->cf_stat, &st, sizeof(struct stat));
                    739:        }
                    740:
                    741:        return (cfp);
                    742: }
                    743:
                    744: