Annotation of src/usr.bin/cvs/file.c, Revision 1.17
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.10 jfb 373: TAILQ_INIT(&dirs);
1.7 jfb 374: cdp = cf->cf_ddat;
375:
1.4 jfb 376: if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
377: cdp->cd_repo = strdup(pbuf);
378: if (cdp->cd_repo == NULL) {
379: free(cdp);
1.6 jfb 380: return (-1);
1.4 jfb 381: }
1.3 jfb 382: }
383:
384: cdp->cd_root = cvsroot_get(cf->cf_path);
385: if (cdp->cd_root == NULL) {
386: cvs_file_freedir(cdp);
1.6 jfb 387: return (-1);
1.3 jfb 388: }
389:
1.14 jfb 390: if (flags & CF_MKADMIN)
391: cvs_mkadmin(cf, 0755);
392:
393: cdp->cd_ent = cvs_ent_open(cf->cf_path, O_RDONLY);
394:
1.3 jfb 395: fd = open(cf->cf_path, O_RDONLY);
396: if (fd == -1) {
397: cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
398: cvs_file_freedir(cdp);
1.6 jfb 399: return (-1);
1.3 jfb 400: }
401:
1.11 jfb 402: do {
403: ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
404: if (ret == -1) {
405: cvs_log(LP_ERRNO, "failed to get directory entries");
406: (void)close(fd);
407: cvs_file_freedir(cdp);
408: return (-1);
409: }
1.10 jfb 410:
1.11 jfb 411: dp = fbuf;
412: ep = fbuf + (size_t)ret;
413: while (dp < ep) {
414: ent = (struct dirent *)dp;
415: dp += ent->d_reclen;
416:
417: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
418: continue;
419:
420: snprintf(pbuf, sizeof(pbuf), "%s/%s",
421: cf->cf_path, ent->d_name);
1.14 jfb 422: cfp = cvs_file_lget(pbuf, flags, cf);
1.11 jfb 423: if (cfp != NULL) {
1.16 jfb 424: if (cfp->cf_type == DT_DIR) {
1.11 jfb 425: TAILQ_INSERT_HEAD(&dirs, cfp, cf_list);
1.16 jfb 426: ndirs++;
427: }
428: else {
1.11 jfb 429: TAILQ_INSERT_HEAD(&(cdp->cd_files), cfp,
430: cf_list);
1.16 jfb 431: cdp->cd_nfiles++;
432: }
1.11 jfb 433: }
1.4 jfb 434: }
1.11 jfb 435: } while (ret > 0);
1.3 jfb 436:
1.14 jfb 437: /* we can now close our Entries file */
438: if (cdp->cd_ent != NULL) {
439: cvs_ent_close(cdp->cd_ent);
440: cdp->cd_ent = NULL;
441: }
442:
1.10 jfb 443: if (flags & CF_SORT) {
1.16 jfb 444: cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
445: cvs_file_sort(&dirs, ndirs);
1.10 jfb 446: }
447: TAILQ_FOREACH(cfp, &dirs, cf_list)
448: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.16 jfb 449: cdp->cd_nfiles += ndirs;
1.3 jfb 450:
451: (void)close(fd);
1.6 jfb 452: cf->cf_ddat = cdp;
1.3 jfb 453:
1.6 jfb 454: return (0);
1.3 jfb 455: }
456:
457:
458: /*
459: * cvs_file_free()
460: *
461: * Free a cvs_file structure and its contents.
462: */
463:
464: void
1.14 jfb 465: cvs_file_free(CVSFILE *cf)
1.3 jfb 466: {
467: if (cf->cf_path != NULL)
468: free(cf->cf_path);
469: if (cf->cf_stat != NULL)
470: free(cf->cf_stat);
471: if (cf->cf_ddat != NULL)
472: cvs_file_freedir(cf->cf_ddat);
473: free(cf);
1.5 jfb 474: }
475:
476:
477: /*
478: * cvs_file_examine()
479: *
480: * Examine the contents of the CVS file structure <cf> with the function
481: * <exam>. The function is called for all subdirectories and files of the
482: * root file.
483: */
484:
485: int
486: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
487: {
488: int ret;
1.14 jfb 489: CVSFILE *fp;
1.5 jfb 490:
491: if (cf->cf_type == DT_DIR) {
492: ret = (*exam)(cf, arg);
1.10 jfb 493: TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5 jfb 494: ret = cvs_file_examine(fp, exam, arg);
495: if (ret == -1)
1.13 jfb 496: break;
1.5 jfb 497: }
498: }
499: else
1.13 jfb 500: ret = (*exam)(cf, arg);
501:
502: return (ret);
1.3 jfb 503: }
504:
505:
506: /*
507: * cvs_file_freedir()
508: *
509: * Free a cvs_dir structure and its contents.
510: */
511:
512: static void
513: cvs_file_freedir(struct cvs_dir *cd)
514: {
1.14 jfb 515: CVSFILE *cfp;
1.3 jfb 516:
517: if (cd->cd_root != NULL)
518: cvsroot_free(cd->cd_root);
519: if (cd->cd_repo != NULL)
520: free(cd->cd_repo);
521:
1.14 jfb 522: if (cd->cd_ent != NULL)
523: cvs_ent_close(cd->cd_ent);
524:
1.10 jfb 525: while (!TAILQ_EMPTY(&(cd->cd_files))) {
526: cfp = TAILQ_FIRST(&(cd->cd_files));
527: TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3 jfb 528: cvs_file_free(cfp);
529: }
530: }
531:
532:
533: /*
534: * cvs_file_sort()
535: *
1.16 jfb 536: * Sort a list of cvs file structures according to their filename. The list
537: * <flp> is modified according to the sorting algorithm. The number of files
538: * in the list must be given by <nfiles>.
539: * Returns 0 on success, or -1 on failure.
1.3 jfb 540: */
541:
542: static int
1.16 jfb 543: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 544: {
545: int i;
546: size_t nb;
1.16 jfb 547: CVSFILE *cf, **cfvec;
548:
549: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
550: if (cfvec == NULL) {
551: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
552: return (-1);
553: }
1.3 jfb 554:
555: i = 0;
1.10 jfb 556: TAILQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 557: if (i == (int)nfiles) {
1.3 jfb 558: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 559: /* rebuild the list and abort sorting */
560: while (--i >= 0)
561: TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
562: free(cfvec);
1.3 jfb 563: return (-1);
564: }
1.16 jfb 565: cfvec[i++] = cf;
1.3 jfb 566:
567: /* now unlink it from the list,
568: * we'll put it back in order later
569: */
1.10 jfb 570: TAILQ_REMOVE(flp, cf, cf_list);
1.3 jfb 571: }
572:
573: /* clear the list just in case */
1.10 jfb 574: TAILQ_INIT(flp);
1.3 jfb 575: nb = (size_t)i;
576:
577: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
578:
579: /* rebuild the list from the bottom up */
580: for (i = (int)nb - 1; i >= 0; i--)
1.10 jfb 581: TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 582:
1.16 jfb 583: free(cfvec);
1.3 jfb 584: return (0);
585: }
586:
587:
588: static int
589: cvs_file_cmp(const void *f1, const void *f2)
590: {
1.14 jfb 591: CVSFILE *cf1, *cf2;
592: cf1 = *(CVSFILE **)f1;
593: cf2 = *(CVSFILE **)f2;
1.3 jfb 594: return strcmp(cf1->cf_name, cf2->cf_name);
1.6 jfb 595: }
596:
597:
598: CVSFILE*
599: cvs_file_alloc(const char *path, u_int type)
600: {
601: size_t len;
602: char pbuf[MAXPATHLEN];
603: CVSFILE *cfp;
604: struct cvs_dir *ddat;
605:
1.14 jfb 606: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 607: if (cfp == NULL) {
608: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
609: return (NULL);
610: }
611: memset(cfp, 0, sizeof(*cfp));
612:
613: /* ditch trailing slashes */
614: strlcpy(pbuf, path, sizeof(pbuf));
615: len = strlen(pbuf);
616: while (pbuf[len - 1] == '/')
617: pbuf[--len] = '\0';
618:
619: cfp->cf_path = strdup(pbuf);
620: if (cfp->cf_path == NULL) {
621: free(cfp);
622: return (NULL);
623: }
624:
625: cfp->cf_name = strrchr(cfp->cf_path, '/');
626: if (cfp->cf_name == NULL)
627: cfp->cf_name = cfp->cf_path;
628: else
629: cfp->cf_name++;
630:
631: cfp->cf_type = type;
632: cfp->cf_cvstat = CVS_FST_UNKNOWN;
633:
634: if (type == DT_DIR) {
635: ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
636: if (ddat == NULL) {
637: cvs_file_free(cfp);
638: return (NULL);
639: }
640: memset(ddat, 0, sizeof(*ddat));
1.10 jfb 641: TAILQ_INIT(&(ddat->cd_files));
1.6 jfb 642: cfp->cf_ddat = ddat;
643: }
644: return (cfp);
1.1 jfb 645: }
1.14 jfb 646:
647:
648: /*
649: * cvs_file_lget()
650: *
651: * Get the file and link it with the parent right away.
652: */
653:
654: static CVSFILE*
655: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
656: {
657: int cwd;
658: size_t len;
659: char buf[32];
660: struct stat st;
661: struct tm lmtm;
662: CVSFILE *cfp;
663: struct cvs_ent *ent;
664:
665: ent = NULL;
666:
667: if (strcmp(path, ".") == 0)
668: cwd = 1;
669: else
670: cwd = 0;
671:
672: if (stat(path, &st) == -1) {
673: cvs_log(LP_ERRNO, "failed to stat %s", path);
674: return (NULL);
675: }
676:
677: cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
678: if (cfp == NULL) {
679: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
680: return (NULL);
681: }
682: cfp->cf_parent = parent;
683:
684: if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
1.15 jfb 685: ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), cfp->cf_name);
1.14 jfb 686: }
687:
688: if (ent == NULL)
689: cfp->cf_cvstat = (cwd == 1) ?
690: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
691: else {
692: /* always show directories as up-to-date */
693: if (ent->ce_type == CVS_ENT_DIR)
694: cfp->cf_cvstat = CVS_FST_UPTODATE;
695: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
696: cfp->cf_cvstat = CVS_FST_ADDED;
697: else {
698: /* check last modified time */
699: if ((gmtime_r((time_t *)&(st.st_mtime),
700: &lmtm) == NULL) ||
701: (asctime_r(&lmtm, buf) == NULL)) {
702: cvs_log(LP_ERR,
703: "failed to generate file timestamp");
704: /* fake an up to date file */
705: strlcpy(buf, ent->ce_timestamp, sizeof(buf));
706: }
707: len = strlen(buf);
708: if ((len > 0) && (buf[len - 1] == '\n'))
709: buf[--len] = '\0';
710:
711: if (strcmp(buf, ent->ce_timestamp) == 0)
712: cfp->cf_cvstat = CVS_FST_UPTODATE;
713: else
714: cfp->cf_cvstat = CVS_FST_MODIFIED;
715: }
716: }
717:
718: if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
719: if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN)) {
720: free(cfp->cf_ddat);
721: cfp->cf_ddat = NULL;
722: }
723: else if (cvs_file_getdir(cfp, flags) < 0) {
724: cvs_file_free(cfp);
725: return (NULL);
726: }
727: }
728:
729: if (flags & CF_STAT) {
730: cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
731: if (cfp->cf_stat == NULL) {
732: cvs_log(LP_ERRNO, "failed to allocate stat structure");
733: cvs_file_free(cfp);
734: return (NULL);
735: }
736:
737: memcpy(cfp->cf_stat, &st, sizeof(struct stat));
738: }
739:
740: return (cfp);
741: }
742:
743: