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

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