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