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