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