Annotation of src/usr.bin/cvs/file.c, Revision 1.31
1.1 jfb 1: /* $OpenBSD$ */
2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26 jfb 4: * All rights reserved.
1.1 jfb 5: *
1.26 jfb 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.26 jfb 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.26 jfb 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.26 jfb 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 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.26 jfb 129: cvs_file_ignore(cvs_ign_std[i]);
1.1 jfb 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.26 jfb 230: * If the type is DT_DIR, the CVS administrative repository and files will be
231: * created.
1.25 jfb 232: * Returns the created file on success, or NULL on failure.
1.1 jfb 233: */
234:
1.6 jfb 235: CVSFILE*
236: cvs_file_create(const char *path, u_int type, mode_t mode)
1.1 jfb 237: {
1.6 jfb 238: int fd;
239: CVSFILE *cfp;
1.1 jfb 240:
1.6 jfb 241: cfp = cvs_file_alloc(path, type);
242: if (cfp == NULL)
243: return (NULL);
1.26 jfb 244:
1.6 jfb 245: cfp->cf_type = type;
1.22 jfb 246: cfp->cf_mode = mode;
1.26 jfb 247: cfp->cf_ddat->cd_root = cvsroot_get(path);
1.28 jfb 248: cfp->cf_ddat->cd_repo = strdup(cfp->cf_path);
1.1 jfb 249:
1.6 jfb 250: if (type == DT_DIR) {
1.26 jfb 251: if ((mkdir(path, mode) == -1) || (cvs_mkadmin(cfp, mode) < 0)) {
1.6 jfb 252: cvs_file_free(cfp);
253: return (NULL);
254: }
1.26 jfb 255:
1.27 jfb 256: cfp->cf_ddat->cd_ent = cvs_ent_open(path, O_RDWR);
1.1 jfb 257: }
1.6 jfb 258: else {
259: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
260: if (fd == -1) {
261: cvs_file_free(cfp);
1.1 jfb 262: return (NULL);
263: }
1.6 jfb 264: (void)close(fd);
1.1 jfb 265: }
266:
1.6 jfb 267: return (cfp);
1.3 jfb 268: }
269:
270:
271: /*
272: * cvs_file_get()
273: *
274: * Load a cvs_file structure with all the information pertaining to the file
275: * <path>.
1.4 jfb 276: * The <flags> parameter specifies various flags that alter the behaviour of
1.21 jfb 277: * the function. The CF_RECURSE flag causes the function to recursively load
278: * subdirectories when <path> is a directory.
279: * The CF_SORT flag causes the files to be sorted in alphabetical order upon
280: * loading. The special case of "." as a path specification generates
281: * recursion for a single level and is equivalent to calling cvs_file_get() on
282: * all files of that directory.
1.3 jfb 283: * Returns a pointer to the cvs file structure, which must later be freed
284: * with cvs_file_free().
285: */
286:
1.14 jfb 287: CVSFILE*
1.3 jfb 288: cvs_file_get(const char *path, int flags)
289: {
1.14 jfb 290: return cvs_file_lget(path, flags, NULL);
1.9 jfb 291: }
292:
293:
294: /*
295: * cvs_file_getspec()
296: *
297: * Load a specific set of files whose paths are given in the vector <fspec>,
298: * whose size is given in <fsn>.
299: * Returns a pointer to the lowest common subdirectory to all specified
300: * files.
301: */
302:
303: CVSFILE*
304: cvs_file_getspec(char **fspec, int fsn, int flags)
305: {
1.26 jfb 306: int i;
307: char *sp, *np, pcopy[MAXPATHLEN];
308: CVSFILE *base, *cf, *nf;
309:
310: base = cvs_file_get(".", 0);
311: if (base == NULL)
312: return (NULL);
313:
314: for (i = 0; i < fsn; i++) {
315: strlcpy(pcopy, fspec[i], sizeof(pcopy));
316: cf = base;
317: sp = pcopy;
318:
319: do {
320: np = strchr(sp, '/');
321: if (np != NULL)
322: *np = '\0';
323: nf = cvs_file_find(cf, sp);
324: if (nf == NULL) {
325: nf = cvs_file_lget(pcopy, 0, cf);
326: if (nf == NULL) {
327: cvs_file_free(base);
328: return (NULL);
329: }
330:
331: cvs_file_attach(cf, nf);
332: }
333:
334: if (np != NULL) {
335: *np = '/';
336: sp = np + 1;
337: }
338:
339: cf = nf;
340: } while (np != NULL);
341: }
342:
343: return (base);
1.3 jfb 344: }
345:
346:
347: /*
1.13 jfb 348: * cvs_file_find()
349: *
350: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
351: * The file's pathname <path> must be relative to the base of <hier>.
352: * Returns the entry on success, or NULL on failure.
353: */
354:
355: CVSFILE*
356: cvs_file_find(CVSFILE *hier, const char *path)
357: {
358: char *pp, *sp, pbuf[MAXPATHLEN];
359: CVSFILE *sf, *cf;
360:
361: strlcpy(pbuf, path, sizeof(pbuf));
362:
363: cf = hier;
364: pp = pbuf;
365: do {
366: sp = strchr(pp, '/');
367: if (sp != NULL)
1.24 jfb 368: *(sp++) = '\0';
1.13 jfb 369:
370: /* special case */
371: if (*pp == '.') {
372: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
373: /* request to go back to parent */
374: if (cf->cf_parent == NULL) {
375: cvs_log(LP_NOTICE,
376: "path %s goes back too far", path);
377: return (NULL);
378: }
379: cf = cf->cf_parent;
380: continue;
381: }
382: else if (*(pp + 1) == '\0')
383: continue;
384: }
385:
386: TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
1.23 jfb 387: if (cvs_file_cmpname(pp, sf->cf_name) == 0)
1.13 jfb 388: break;
389: if (sf == NULL)
390: return (NULL);
391:
392: cf = sf;
393: pp = sp;
394: } while (sp != NULL);
395:
1.24 jfb 396: return (cf);
1.13 jfb 397: }
398:
399:
400: /*
1.22 jfb 401: * cvs_file_attach()
402: *
403: * Attach the file <file> as one of the children of parent <parent>, which
404: * has to be a file of type DT_DIR.
405: * Returns 0 on success, or -1 on failure.
406: */
407:
408: int
409: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
410: {
1.23 jfb 411: struct cvs_dir *dp;
1.22 jfb 412:
413: if (parent->cf_type != DT_DIR)
414: return (-1);
415:
1.23 jfb 416: dp = parent->cf_ddat;
417:
418: TAILQ_INSERT_TAIL(&(dp->cd_files), file, cf_list);
419: dp->cd_nfiles++;
1.22 jfb 420: file->cf_parent = parent;
421:
422: return (0);
423: }
424:
425:
426: /*
1.3 jfb 427: * cvs_file_getdir()
428: *
429: * Get a cvs directory structure for the directory whose path is <dir>.
1.30 jfb 430: * This function should not free the directory information on error, as this
431: * is performed by cvs_file_free().
1.3 jfb 432: */
433:
1.6 jfb 434: static int
1.14 jfb 435: cvs_file_getdir(CVSFILE *cf, int flags)
1.3 jfb 436: {
1.13 jfb 437: int ret, fd;
1.16 jfb 438: u_int ndirs;
1.3 jfb 439: long base;
1.13 jfb 440: void *dp, *ep;
1.11 jfb 441: char fbuf[2048], pbuf[MAXPATHLEN];
1.3 jfb 442: struct dirent *ent;
1.14 jfb 443: CVSFILE *cfp;
1.20 jfb 444: struct stat st;
1.3 jfb 445: struct cvs_dir *cdp;
1.10 jfb 446: struct cvs_flist dirs;
1.3 jfb 447:
1.18 jfb 448: ndirs = 0;
1.10 jfb 449: TAILQ_INIT(&dirs);
1.7 jfb 450: cdp = cf->cf_ddat;
451:
1.26 jfb 452: if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
453: cdp->cd_root = cvsroot_get(cf->cf_path);
1.30 jfb 454: if (cdp->cd_root == NULL)
1.26 jfb 455: return (-1);
1.3 jfb 456:
1.26 jfb 457: if (flags & CF_MKADMIN)
458: cvs_mkadmin(cf, 0755);
1.14 jfb 459:
1.26 jfb 460: /* if the CVS administrative directory exists, load the info */
461: snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR,
462: cf->cf_path);
463: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
464: if (cvs_readrepo(cf->cf_path, pbuf,
465: sizeof(pbuf)) == 0) {
466: cdp->cd_repo = strdup(pbuf);
467: if (cdp->cd_repo == NULL) {
1.28 jfb 468: cvs_log(LP_ERRNO,
469: "failed to dup repository string");
1.26 jfb 470: return (-1);
471: }
1.20 jfb 472: }
1.26 jfb 473:
474: cdp->cd_ent = cvs_ent_open(cf->cf_path, O_RDWR);
1.20 jfb 475: }
476: }
1.14 jfb 477:
1.26 jfb 478: if (!(flags & CF_RECURSE) || (cf->cf_cvstat == CVS_FST_UNKNOWN))
479: return (0);
480:
1.3 jfb 481: fd = open(cf->cf_path, O_RDONLY);
482: if (fd == -1) {
483: cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
1.6 jfb 484: return (-1);
1.3 jfb 485: }
486:
1.11 jfb 487: do {
488: ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
489: if (ret == -1) {
490: cvs_log(LP_ERRNO, "failed to get directory entries");
491: (void)close(fd);
492: return (-1);
493: }
1.10 jfb 494:
1.11 jfb 495: dp = fbuf;
496: ep = fbuf + (size_t)ret;
497: while (dp < ep) {
498: ent = (struct dirent *)dp;
1.31 ! jfb 499: if (ent->d_fileno == 0)
! 500: continue;
1.11 jfb 501: dp += ent->d_reclen;
502:
503: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.24 jfb 504: continue;
505:
506: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
1.11 jfb 507: continue;
508:
509: snprintf(pbuf, sizeof(pbuf), "%s/%s",
510: cf->cf_path, ent->d_name);
1.14 jfb 511: cfp = cvs_file_lget(pbuf, flags, cf);
1.11 jfb 512: if (cfp != NULL) {
1.26 jfb 513: if (cfp->cf_type == DT_DIR) {
514: TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.16 jfb 515: ndirs++;
516: }
517: else {
1.26 jfb 518: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
1.11 jfb 519: cf_list);
1.16 jfb 520: cdp->cd_nfiles++;
521: }
1.11 jfb 522: }
1.4 jfb 523: }
1.11 jfb 524: } while (ret > 0);
1.14 jfb 525:
1.10 jfb 526: if (flags & CF_SORT) {
1.16 jfb 527: cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
528: cvs_file_sort(&dirs, ndirs);
1.10 jfb 529: }
1.26 jfb 530:
531: while (!TAILQ_EMPTY(&dirs)) {
532: cfp = TAILQ_FIRST(&dirs);
533: TAILQ_REMOVE(&dirs, cfp, cf_list);
1.10 jfb 534: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.26 jfb 535: }
1.16 jfb 536: cdp->cd_nfiles += ndirs;
1.3 jfb 537:
538: (void)close(fd);
539:
1.6 jfb 540: return (0);
1.3 jfb 541: }
542:
543:
544: /*
545: * cvs_file_free()
546: *
547: * Free a cvs_file structure and its contents.
548: */
549:
550: void
1.14 jfb 551: cvs_file_free(CVSFILE *cf)
1.3 jfb 552: {
553: if (cf->cf_path != NULL)
554: free(cf->cf_path);
555: if (cf->cf_ddat != NULL)
556: cvs_file_freedir(cf->cf_ddat);
557: free(cf);
1.5 jfb 558: }
559:
560:
561: /*
562: * cvs_file_examine()
563: *
564: * Examine the contents of the CVS file structure <cf> with the function
565: * <exam>. The function is called for all subdirectories and files of the
566: * root file.
567: */
568:
569: int
570: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
571: {
572: int ret;
1.14 jfb 573: CVSFILE *fp;
1.5 jfb 574:
575: if (cf->cf_type == DT_DIR) {
576: ret = (*exam)(cf, arg);
1.10 jfb 577: TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5 jfb 578: ret = cvs_file_examine(fp, exam, arg);
579: if (ret == -1)
1.13 jfb 580: break;
1.5 jfb 581: }
582: }
583: else
1.13 jfb 584: ret = (*exam)(cf, arg);
585:
586: return (ret);
1.3 jfb 587: }
588:
589:
590: /*
591: * cvs_file_freedir()
592: *
593: * Free a cvs_dir structure and its contents.
594: */
595:
596: static void
597: cvs_file_freedir(struct cvs_dir *cd)
598: {
1.14 jfb 599: CVSFILE *cfp;
1.3 jfb 600:
601: if (cd->cd_root != NULL)
602: cvsroot_free(cd->cd_root);
603: if (cd->cd_repo != NULL)
604: free(cd->cd_repo);
605:
1.14 jfb 606: if (cd->cd_ent != NULL)
607: cvs_ent_close(cd->cd_ent);
608:
1.10 jfb 609: while (!TAILQ_EMPTY(&(cd->cd_files))) {
610: cfp = TAILQ_FIRST(&(cd->cd_files));
611: TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3 jfb 612: cvs_file_free(cfp);
613: }
614: }
615:
616:
617: /*
618: * cvs_file_sort()
619: *
1.16 jfb 620: * Sort a list of cvs file structures according to their filename. The list
621: * <flp> is modified according to the sorting algorithm. The number of files
622: * in the list must be given by <nfiles>.
623: * Returns 0 on success, or -1 on failure.
1.3 jfb 624: */
625:
626: static int
1.16 jfb 627: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 628: {
629: int i;
630: size_t nb;
1.16 jfb 631: CVSFILE *cf, **cfvec;
632:
633: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
634: if (cfvec == NULL) {
635: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
636: return (-1);
637: }
1.3 jfb 638:
639: i = 0;
1.10 jfb 640: TAILQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 641: if (i == (int)nfiles) {
1.3 jfb 642: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 643: /* rebuild the list and abort sorting */
644: while (--i >= 0)
645: TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
646: free(cfvec);
1.3 jfb 647: return (-1);
648: }
1.16 jfb 649: cfvec[i++] = cf;
1.3 jfb 650:
651: /* now unlink it from the list,
652: * we'll put it back in order later
653: */
1.10 jfb 654: TAILQ_REMOVE(flp, cf, cf_list);
1.3 jfb 655: }
656:
657: /* clear the list just in case */
1.10 jfb 658: TAILQ_INIT(flp);
1.3 jfb 659: nb = (size_t)i;
660:
661: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
662:
663: /* rebuild the list from the bottom up */
664: for (i = (int)nb - 1; i >= 0; i--)
1.10 jfb 665: TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 666:
1.16 jfb 667: free(cfvec);
1.3 jfb 668: return (0);
669: }
670:
671:
672: static int
673: cvs_file_cmp(const void *f1, const void *f2)
674: {
1.14 jfb 675: CVSFILE *cf1, *cf2;
676: cf1 = *(CVSFILE **)f1;
677: cf2 = *(CVSFILE **)f2;
1.23 jfb 678: return cvs_file_cmpname(cf1->cf_name, cf2->cf_name);
1.6 jfb 679: }
680:
681:
682: CVSFILE*
683: cvs_file_alloc(const char *path, u_int type)
684: {
685: size_t len;
686: char pbuf[MAXPATHLEN];
687: CVSFILE *cfp;
688: struct cvs_dir *ddat;
689:
1.14 jfb 690: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 691: if (cfp == NULL) {
692: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
693: return (NULL);
694: }
695: memset(cfp, 0, sizeof(*cfp));
696:
697: /* ditch trailing slashes */
698: strlcpy(pbuf, path, sizeof(pbuf));
699: len = strlen(pbuf);
700: while (pbuf[len - 1] == '/')
701: pbuf[--len] = '\0';
702:
703: cfp->cf_path = strdup(pbuf);
704: if (cfp->cf_path == NULL) {
705: free(cfp);
706: return (NULL);
707: }
708:
709: cfp->cf_name = strrchr(cfp->cf_path, '/');
710: if (cfp->cf_name == NULL)
711: cfp->cf_name = cfp->cf_path;
712: else
713: cfp->cf_name++;
714:
715: cfp->cf_type = type;
716: cfp->cf_cvstat = CVS_FST_UNKNOWN;
717:
718: if (type == DT_DIR) {
719: ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
720: if (ddat == NULL) {
721: cvs_file_free(cfp);
722: return (NULL);
723: }
724: memset(ddat, 0, sizeof(*ddat));
1.10 jfb 725: TAILQ_INIT(&(ddat->cd_files));
1.6 jfb 726: cfp->cf_ddat = ddat;
727: }
728: return (cfp);
1.1 jfb 729: }
1.14 jfb 730:
731:
732: /*
733: * cvs_file_lget()
734: *
735: * Get the file and link it with the parent right away.
1.22 jfb 736: * Returns a pointer to the created file structure on success, or NULL on
737: * failure.
1.14 jfb 738: */
739:
740: static CVSFILE*
741: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
742: {
743: int cwd;
744: struct stat st;
745: CVSFILE *cfp;
746: struct cvs_ent *ent;
747:
748: ent = NULL;
749:
750: if (strcmp(path, ".") == 0)
751: cwd = 1;
752: else
753: cwd = 0;
754:
755: if (stat(path, &st) == -1) {
756: cvs_log(LP_ERRNO, "failed to stat %s", path);
757: return (NULL);
758: }
759:
760: cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
761: if (cfp == NULL) {
762: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
763: return (NULL);
764: }
765: cfp->cf_parent = parent;
1.22 jfb 766: cfp->cf_mode = st.st_mode & ACCESSPERMS;
767: cfp->cf_mtime = st.st_mtime;
1.14 jfb 768:
769: if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
1.15 jfb 770: ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), cfp->cf_name);
1.14 jfb 771: }
772:
1.26 jfb 773: if (ent == NULL) {
1.14 jfb 774: cfp->cf_cvstat = (cwd == 1) ?
775: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.26 jfb 776: }
1.14 jfb 777: else {
778: /* always show directories as up-to-date */
779: if (ent->ce_type == CVS_ENT_DIR)
780: cfp->cf_cvstat = CVS_FST_UPTODATE;
781: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
782: cfp->cf_cvstat = CVS_FST_ADDED;
783: else {
784: /* check last modified time */
1.28 jfb 785: if (ent->ce_mtime == st.st_mtime)
1.14 jfb 786: cfp->cf_cvstat = CVS_FST_UPTODATE;
787: else
788: cfp->cf_cvstat = CVS_FST_MODIFIED;
789: }
790: }
791:
1.26 jfb 792: if ((cfp->cf_type == DT_DIR) && (cvs_file_getdir(cfp, flags) < 0)) {
793: cvs_file_free(cfp);
794: return (NULL);
1.14 jfb 795: }
796:
797: return (cfp);
1.23 jfb 798: }
799:
800:
801: static int
802: cvs_file_cmpname(const char *name1, const char *name2)
803: {
804: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
805: (strcasecmp(name1, name2));
1.14 jfb 806: }