Annotation of src/usr.bin/cvs/file.c, Revision 1.147
1.147 ! joris 1: /* $OpenBSD: file.c,v 1.146 2006/05/28 17:25:18 joris Exp $ */
1.1 jfb 2: /*
1.138 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26 jfb 5: * All rights reserved.
1.1 jfb 6: *
1.26 jfb 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
1.1 jfb 10: *
1.26 jfb 11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
1.1 jfb 13: * 2. The name of the author may not be used to endorse or promote products
1.26 jfb 14: * derived from this software without specific prior written permission.
1.1 jfb 15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26 jfb 25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 26: */
27:
1.134 xsa 28: #include "includes.h"
1.1 jfb 29:
30: #include "cvs.h"
1.83 xsa 31: #include "file.h"
1.1 jfb 32: #include "log.h"
33:
1.104 xsa 34: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
1.1 jfb 35:
1.104 xsa 36: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
1.1 jfb 37:
38: /*
39: * Standard patterns to ignore.
40: */
41: static const char *cvs_ign_std[] = {
42: ".",
43: "..",
1.142 joris 44: ".*",
1.1 jfb 45: "*.o",
1.45 xsa 46: "*.a",
1.1 jfb 47: "*.bak",
48: "*.orig",
49: "*.rej",
1.45 xsa 50: "*.old",
1.1 jfb 51: "*.exe",
52: "*.depend",
1.45 xsa 53: "*.obj",
54: "*.elc",
55: "*.ln",
56: "*.olb",
1.1 jfb 57: "CVS",
58: "core",
1.102 joris 59: "cvslog*",
1.49 jfb 60: "*.core",
1.8 jfb 61: ".#*",
1.45 xsa 62: "*~",
63: "_$*",
64: "*$",
1.1 jfb 65: };
66:
1.138 joris 67: struct ignore_head cvs_ign_pats;
68: struct ignore_head dir_ign_pats;
1.1 jfb 69:
1.138 joris 70: static int cvs_file_cmpname(const char *, const char *);
1.1 jfb 71:
1.138 joris 72: void
1.1 jfb 73: cvs_file_init(void)
74: {
1.61 xsa 75: int i, l;
1.138 joris 76: FILE *ifp;
1.1 jfb 77: size_t len;
1.138 joris 78: char *path, *buf;
79:
80: path = xmalloc(MAXPATHLEN);
81: buf = xmalloc(MAXNAMLEN);
1.1 jfb 82:
83: TAILQ_INIT(&cvs_ign_pats);
1.138 joris 84: TAILQ_INIT(&dir_ign_pats);
1.4 jfb 85:
1.1 jfb 86: /* standard patterns to ignore */
1.10 jfb 87: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138 joris 88: cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.1 jfb 89:
90: /* read the cvsignore file in the user's home directory, if any */
1.138 joris 91: l = snprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
92: if (l == -1 || l >= MAXPATHLEN)
93: fatal("overflow in cvs_file_init");
1.61 xsa 94:
1.111 xsa 95: ifp = fopen(path, "r");
96: if (ifp == NULL) {
97: if (errno != ENOENT)
98: cvs_log(LP_ERRNO,
99: "failed to open user's cvsignore file `%s'", path);
100: } else {
1.138 joris 101: while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.111 xsa 102: len = strlen(buf);
103: if (len == 0)
104: continue;
1.138 joris 105:
106: if (buf[len - 1] != '\n')
1.111 xsa 107: cvs_log(LP_ERR, "line too long in `%s'", path);
1.138 joris 108:
1.111 xsa 109: buf[--len] = '\0';
1.138 joris 110: cvs_file_ignore(buf, &cvs_ign_pats);
1.1 jfb 111: }
1.138 joris 112:
1.111 xsa 113: (void)fclose(ifp);
1.1 jfb 114: }
115:
1.138 joris 116: xfree(path);
117: xfree(buf);
1.1 jfb 118: }
119:
1.138 joris 120: void
121: cvs_file_ignore(const char *pat, struct ignore_head *list)
1.1 jfb 122: {
123: char *cp;
1.138 joris 124: size_t len;
1.1 jfb 125: struct cvs_ignpat *ip;
126:
1.136 ray 127: ip = xmalloc(sizeof(*ip));
1.138 joris 128: len = strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
129: if (len >= sizeof(ip->ip_pat))
130: fatal("cvs_file_ignore: truncation of pattern '%s'", pat);
1.1 jfb 131:
132: /* check if we will need globbing for that pattern */
133: ip->ip_flags = CVS_IGN_STATIC;
134: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
135: if (CVS_CHAR_ISMETA(*cp)) {
136: ip->ip_flags &= ~CVS_IGN_STATIC;
137: break;
138: }
139: }
140:
1.138 joris 141: TAILQ_INSERT_TAIL(list, ip, ip_list);
1.1 jfb 142: }
143:
144: int
1.5 jfb 145: cvs_file_chkign(const char *file)
1.1 jfb 146: {
1.23 jfb 147: int flags;
1.1 jfb 148: struct cvs_ignpat *ip;
149:
1.23 jfb 150: flags = FNM_PERIOD;
151: if (cvs_nocase)
152: flags |= FNM_CASEFOLD;
153:
1.1 jfb 154: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
155: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 156: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 157: return (1);
1.38 deraadt 158: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 159: return (1);
160: }
161:
1.138 joris 162: TAILQ_FOREACH(ip, &dir_ign_pats, ip_list) {
163: if (ip->ip_flags & CVS_IGN_STATIC) {
164: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
165: return (1);
166: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
167: return (1);
168: }
169:
1.1 jfb 170: return (0);
171: }
172:
1.138 joris 173: void
174: cvs_file_run(int argc, char **argv, struct cvs_recursion *cr)
1.1 jfb 175: {
1.138 joris 176: int i;
177: struct cvs_flisthead fl;
1.84 joris 178:
1.138 joris 179: TAILQ_INIT(&fl);
1.85 joris 180:
1.138 joris 181: for (i = 0; i < argc; i++)
182: cvs_file_get(argv[i], &fl);
1.1 jfb 183:
1.138 joris 184: cvs_file_walklist(&fl, cr);
185: cvs_file_freelist(&fl);
1.3 jfb 186: }
187:
1.138 joris 188: struct cvs_filelist *
189: cvs_file_get(const char *name, struct cvs_flisthead *fl)
190: {
191: const char *p;
192: struct cvs_filelist *l;
1.3 jfb 193:
1.138 joris 194: for (p = name; p[0] == '.' && p[1] == '/';)
195: p += 2;
1.35 jfb 196:
1.138 joris 197: TAILQ_FOREACH(l, fl, flist)
198: if (!strcmp(l->file_path, p))
199: return (l);
1.35 jfb 200:
1.138 joris 201: l = (struct cvs_filelist *)xmalloc(sizeof(*l));
202: l->file_path = xstrdup(p);
1.35 jfb 203:
1.138 joris 204: TAILQ_INSERT_TAIL(fl, l, flist);
205: return (l);
1.35 jfb 206: }
207:
1.138 joris 208: struct cvs_file *
209: cvs_file_get_cf(const char *d, const char *f, int fd, int type)
210: {
211: int l;
212: struct cvs_file *cf;
213: char *p, *rpath;
1.35 jfb 214:
1.138 joris 215: rpath = xmalloc(MAXPATHLEN);
1.3 jfb 216:
1.138 joris 217: l = snprintf(rpath, MAXPATHLEN, "%s/%s", d, f);
218: if (l == -1 || l >= MAXPATHLEN)
219: fatal("cvs_file_get_cf: overflow");
220:
221: for (p = rpath; p[0] == '.' && p[1] == '/';)
222: p += 2;
223:
224: cf = (struct cvs_file *)xmalloc(sizeof(*cf));
225: memset(cf, 0, sizeof(*cf));
226:
227: cf->file_name = xstrdup(f);
228: cf->file_wd = xstrdup(d);
229: cf->file_path = xstrdup(p);
230: cf->fd = fd;
231: cf->repo_fd = -1;
232: cf->file_type = type;
233: cf->file_status = cf->file_flags = 0;
234: cf->file_ent = NULL;
1.68 joris 235:
1.138 joris 236: xfree(rpath);
237: return (cf);
1.9 jfb 238: }
239:
1.138 joris 240: void
241: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9 jfb 242: {
1.138 joris 243: int len, fd, type;
244: struct stat st;
245: struct cvs_file *cf;
246: struct cvs_filelist *l, *nxt;
247: char *d, *f, *repo, *fpath;
248:
249: fpath = xmalloc(MAXPATHLEN);
250: repo = xmalloc(MAXPATHLEN);
251:
252: for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
253: if (cvs_quit)
254: fatal("received signal %d", sig_received);
255:
256: cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
257: l->file_path);
258:
259: if ((f = basename(l->file_path)) == NULL)
260: fatal("cvs_file_walklist: basename failed");
261: if ((d = dirname(l->file_path)) == NULL)
262: fatal("cvs_file_walklist: dirname failed");
263:
1.146 joris 264: type = CVS_FILE;
1.138 joris 265: if ((fd = open(l->file_path, O_RDONLY)) != -1) {
266: if (fstat(fd, &st) == -1) {
267: cvs_log(LP_ERRNO, "%s", l->file_path);
268: (void)close(fd);
269: goto next;
270: }
1.26 jfb 271:
1.138 joris 272: if (S_ISDIR(st.st_mode))
273: type = CVS_DIR;
274: else if (S_ISREG(st.st_mode))
275: type = CVS_FILE;
276: else {
277: cvs_log(LP_ERR,
278: "ignoring bad file type for %s",
279: l->file_path);
280: (void)close(fd);
281: goto next;
282: }
283: } else {
284: if (stat(d, &st) == -1) {
285: cvs_log(LP_ERRNO, "%s", d);
286: goto next;
287: }
1.87 joris 288:
1.146 joris 289: cvs_get_repository_path(d, repo, MAXPATHLEN);
1.138 joris 290: len = snprintf(fpath, MAXPATHLEN, "%s/%s",
291: repo, f);
292: if (len == -1 || len >= MAXPATHLEN)
293: fatal("cvs_file_walklist: overflow");
294:
295: if ((fd = open(fpath, O_RDONLY)) == -1) {
296: strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
297: fd = open(fpath, O_RDONLY);
298: }
1.26 jfb 299:
1.138 joris 300: if (fd != -1) {
301: if (fstat(fd, &st) == -1)
302: fatal("cvs_file_walklist: %s: %s",
303: fpath, strerror(errno));
304:
305: if (S_ISDIR(st.st_mode))
306: type = CVS_DIR;
307: else if (S_ISREG(st.st_mode))
308: type = CVS_FILE;
309: else {
310: cvs_log(LP_ERR,
311: "ignoring bad file type for %s",
312: l->file_path);
313: (void)close(fd);
314: goto next;
315: }
316:
317: /* this file is not in our working copy yet */
318: (void)close(fd);
319: fd = -1;
320: }
321: }
1.73 joris 322:
1.138 joris 323: cf = cvs_file_get_cf(d, f, fd, type);
324: if (cf->file_type == CVS_DIR) {
325: cvs_file_walkdir(cf, cr);
326: } else {
327: if (cr->local != NULL)
328: cr->local(cf);
1.128 joris 329: }
1.100 joris 330:
1.138 joris 331: cvs_file_free(cf);
1.123 joris 332:
1.138 joris 333: next:
334: nxt = TAILQ_NEXT(l, flist);
335: TAILQ_REMOVE(fl, l, flist);
1.68 joris 336:
1.138 joris 337: xfree(l->file_path);
338: xfree(l);
1.100 joris 339: }
340:
1.138 joris 341: xfree(fpath);
342: xfree(repo);
1.100 joris 343: }
344:
1.138 joris 345: void
346: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100 joris 347: {
1.138 joris 348: int l;
349: FILE *fp;
350: int nbytes;
351: size_t len;
352: long base;
353: size_t bufsize;
354: struct stat st;
355: struct dirent *dp;
1.100 joris 356: struct cvs_ent *ent;
1.138 joris 357: struct cvs_ignpat *ip;
358: struct cvs_ent_line *line;
359: struct cvs_flisthead fl, dl;
360: CVSENTRIES *entlist;
361: char *buf, *ebuf, *cp, *repo, *fpath;
1.100 joris 362:
1.138 joris 363: cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100 joris 364:
1.138 joris 365: if (cr->enterdir != NULL)
366: cr->enterdir(cf);
1.100 joris 367:
1.138 joris 368: if (cr->local != NULL)
369: cr->local(cf);
1.100 joris 370:
1.146 joris 371: if (cf->file_status == FILE_SKIP)
372: return;
373:
1.138 joris 374: fpath = xmalloc(MAXPATHLEN);
1.100 joris 375:
376: /*
1.138 joris 377: * If we do not have a admin directory inside here, dont bother.
1.100 joris 378: */
1.138 joris 379: l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
380: CVS_PATH_CVSDIR);
381: if (l == -1 || l >= MAXPATHLEN)
382: fatal("cvs_file_walkdir: overflow");
1.100 joris 383:
1.146 joris 384: l = stat(fpath, &st);
385: if (l == -1 || (l == 0 && !S_ISDIR(st.st_mode))) {
1.138 joris 386: xfree(fpath);
387: return;
1.100 joris 388: }
389:
390: /*
1.138 joris 391: * check for a local .cvsignore file
1.100 joris 392: */
1.138 joris 393: l = snprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
394: if (l == -1 || l >= MAXPATHLEN)
395: fatal("cvs_file_walkdir: overflow");
1.100 joris 396:
1.138 joris 397: if ((fp = fopen(fpath, "r")) != NULL) {
398: while (fgets(fpath, MAXPATHLEN, fp)) {
399: len = strlen(fpath);
400: if (fpath[len - 1] == '\n')
401: fpath[len - 1] = '\0';
1.26 jfb 402:
1.138 joris 403: cvs_file_ignore(fpath, &dir_ign_pats);
1.68 joris 404: }
1.26 jfb 405:
1.138 joris 406: (void)fclose(fp);
1.100 joris 407: }
408:
1.138 joris 409: if (fstat(cf->fd, &st) == -1)
410: fatal("cvs_file_walkdir: %s %s", cf->file_path,
411: strerror(errno));
1.100 joris 412:
1.138 joris 413: bufsize = st.st_size;
414: if (bufsize < st.st_blksize)
415: bufsize = st.st_blksize;
1.116 joris 416:
1.138 joris 417: buf = xmalloc(bufsize);
418: TAILQ_INIT(&fl);
419: TAILQ_INIT(&dl);
1.87 joris 420:
1.138 joris 421: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
422: ebuf = buf + nbytes;
423: cp = buf;
1.26 jfb 424:
1.138 joris 425: while (cp < ebuf) {
426: dp = (struct dirent *)cp;
427: if (!strcmp(dp->d_name, ".") ||
428: !strcmp(dp->d_name, "..") ||
429: !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
430: dp->d_reclen == 0) {
431: cp += dp->d_reclen;
432: continue;
433: }
1.13 jfb 434:
1.138 joris 435: if (cvs_file_chkign(dp->d_name)) {
436: cp += dp->d_reclen;
1.13 jfb 437: continue;
1.138 joris 438: }
1.13 jfb 439:
1.140 joris 440: if (!(cr->flags & CR_RECURSE_DIRS) &&
441: dp->d_type == DT_DIR) {
442: cp += dp->d_reclen;
443: continue;
444: }
445:
1.138 joris 446: l = snprintf(fpath, MAXPATHLEN, "%s/%s",
447: cf->file_path, dp->d_name);
448: if (l == -1 || l >= MAXPATHLEN)
449: fatal("cvs_file_walkdir: overflow");
1.13 jfb 450:
1.138 joris 451: /*
452: * Anticipate the file type to sort them,
453: * note that we do not determine the final
454: * type until we actually have the fd floating
455: * around.
456: */
457: if (dp->d_type == DT_DIR)
458: cvs_file_get(fpath, &dl);
459: else if (dp->d_type == DT_REG)
460: cvs_file_get(fpath, &fl);
1.13 jfb 461:
1.138 joris 462: cp += dp->d_reclen;
463: }
1.34 jfb 464: }
465:
1.138 joris 466: if (nbytes == -1)
467: fatal("cvs_file_walkdir: %s %s", cf->file_path,
468: strerror(errno));
1.22 jfb 469:
1.138 joris 470: xfree(buf);
1.22 jfb 471:
1.138 joris 472: while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
473: TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
474: xfree(ip);
1.91 joris 475: }
1.114 xsa 476:
1.138 joris 477: entlist = cvs_ent_open(cf->file_path);
478: TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
479: ent = cvs_ent_parse(line->buf);
1.61 xsa 480:
1.138 joris 481: l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
482: ent->ce_name);
483: if (l == -1 || l >= MAXPATHLEN)
484: fatal("cvs_file_walkdir: overflow");
1.100 joris 485:
1.140 joris 486: if (!(cr->flags & CR_RECURSE_DIRS) &&
487: ent->ce_type == CVS_ENT_DIR)
488: continue;
489:
1.138 joris 490: if (ent->ce_type == CVS_ENT_DIR)
491: cvs_file_get(fpath, &dl);
492: else if (ent->ce_type == CVS_ENT_FILE)
493: cvs_file_get(fpath, &fl);
1.68 joris 494:
1.138 joris 495: cvs_ent_free(ent);
1.128 joris 496: }
497:
1.138 joris 498: cvs_ent_close(entlist, ENT_NOSYNC);
1.91 joris 499:
1.140 joris 500: if (cr->flags & CR_REPO) {
501: repo = xmalloc(MAXPATHLEN);
1.146 joris 502: cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140 joris 503: cvs_repository_lock(repo);
1.68 joris 504:
1.140 joris 505: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
506: (cr->flags & CR_RECURSE_DIRS));
507: }
1.68 joris 508:
1.138 joris 509: cvs_file_walklist(&fl, cr);
510: cvs_file_freelist(&fl);
1.68 joris 511:
1.140 joris 512: if (cr->flags & CR_REPO) {
513: cvs_repository_unlock(repo);
514: xfree(repo);
515: }
1.68 joris 516:
1.138 joris 517: cvs_file_walklist(&dl, cr);
518: cvs_file_freelist(&dl);
1.34 jfb 519:
1.138 joris 520: xfree(fpath);
1.26 jfb 521:
1.138 joris 522: if (cr->leavedir != NULL)
523: cr->leavedir(cf);
1.3 jfb 524: }
525:
526: void
1.138 joris 527: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 528: {
1.138 joris 529: struct cvs_filelist *f;
1.62 jfb 530:
1.138 joris 531: while ((f = TAILQ_FIRST(fl)) != NULL) {
532: TAILQ_REMOVE(fl, f, flist);
533: xfree(f->file_path);
534: xfree(f);
1.62 jfb 535: }
1.5 jfb 536: }
537:
1.138 joris 538: void
1.143 joris 539: cvs_file_classify(struct cvs_file *cf, int loud)
1.3 jfb 540: {
1.138 joris 541: size_t len;
542: time_t mtime;
543: struct stat st;
1.143 joris 544: int rflags, l, ismodified, rcsdead, verbose;
1.138 joris 545: CVSENTRIES *entlist = NULL;
546: const char *state;
547: char *repo, *rcsfile, r1[16], r2[16];
548:
549: cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
550:
551: if (!strcmp(cf->file_path, ".")) {
552: cf->file_status = FILE_UPTODATE;
553: return;
554: }
555:
1.143 joris 556: verbose = (verbosity > 1 && loud == 1);
1.138 joris 557: entlist = cvs_ent_open(cf->file_wd);
558:
559: repo = xmalloc(MAXPATHLEN);
560: rcsfile = xmalloc(MAXPATHLEN);
561:
1.146 joris 562: cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.138 joris 563: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s",
564: repo, cf->file_name);
565: if (l == -1 || l >= MAXPATHLEN)
566: fatal("cvs_file_classify: overflow");
567:
568: if (cf->file_type == CVS_FILE) {
569: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
570: if (len >= MAXPATHLEN)
571: fatal("cvs_file_classify: truncation");
572: }
573:
574: cf->file_rpath = xstrdup(rcsfile);
575: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
576:
1.144 joris 577: if (cf->file_ent != NULL) {
578: if (cf->file_ent->ce_type == CVS_ENT_DIR &&
579: cf->file_type != CVS_DIR)
580: fatal("%s is suppose to be a directory, but it is not",
581: cf->file_path);
582: if (cf->file_ent->ce_type == CVS_ENT_FILE &&
583: cf->file_type != CVS_FILE)
584: fatal("%s is suppose to be a file, but it is not",
585: cf->file_path);
586: }
587:
1.138 joris 588: if (cf->file_type == CVS_DIR) {
589: if (cf->fd == -1 && stat(rcsfile, &st) != -1)
590: cf->file_status = DIR_CREATE;
591: else if (cf->file_ent != NULL)
592: cf->file_status = FILE_UPTODATE;
1.144 joris 593: else
594: cf->file_status = FILE_UNKNOWN;
595:
1.138 joris 596: xfree(repo);
597: xfree(rcsfile);
598: cvs_ent_close(entlist, ENT_NOSYNC);
599: return;
600: }
601:
1.147 ! joris 602: rflags = RCS_READ;
! 603: switch (cvs_cmdop) {
! 604: case CVS_OP_COMMIT:
! 605: rflags = RCS_WRITE;
! 606: break;
! 607: case CVS_OP_LOG:
! 608: rflags |= RCS_PARSE_FULLY;
! 609: break;
! 610: }
! 611:
1.138 joris 612: cf->repo_fd = open(cf->file_rpath, O_RDONLY);
613: if (cf->repo_fd != -1) {
614: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
615: if (cf->file_rcs == NULL)
616: fatal("cvs_file_classify: rcs_open failed while it "
617: "shouldn't");
618: } else {
619: cf->file_rcs = NULL;
1.3 jfb 620: }
621:
1.138 joris 622: if (cf->file_ent != NULL)
623: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
624: if (cf->file_rcs != NULL)
625: rcsnum_tostr(cf->file_rcs->rf_head, r2, sizeof(r2));
1.6 jfb 626:
1.138 joris 627: ismodified = rcsdead = 0;
628: if (cf->fd != -1 && cf->file_ent != NULL) {
629: if (fstat(cf->fd, &st) == -1)
630: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 631:
1.138 joris 632: mtime = cvs_hack_time(st.st_mtime, 1);
633: if (mtime == 0)
634: fatal("to gmt failed");
1.62 jfb 635:
1.138 joris 636: if (mtime != cf->file_ent->ce_mtime)
637: ismodified = 1;
1.62 jfb 638: }
639:
1.138 joris 640: if (cf->file_rcs != NULL) {
641: state = rcs_state_get(cf->file_rcs, cf->file_rcs->rf_head);
642: if (state == NULL)
643: fatal("failed to get state for HEAD for %s",
644: cf->file_path);
1.139 joris 645: if (!strcmp(state, RCS_STATE_DEAD))
1.138 joris 646: rcsdead = 1;
1.145 joris 647:
648: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 649: }
650:
1.138 joris 651: /*
652: * 10 Sin
653: * 20 Goto hell
654: * (I welcome you if-else hell)
655: */
656: if (cf->file_ent == NULL) {
657: if (cf->file_rcs == NULL) {
658: if (cf->fd == -1) {
1.143 joris 659: if (verbose)
1.138 joris 660: cvs_log(LP_NOTICE,
661: "nothing known about '%s'",
662: cf->file_path);
663: } else {
1.143 joris 664: if (verbose)
1.138 joris 665: cvs_log(LP_NOTICE,
666: "use add to create an entry for %s",
667: cf->file_path);
668: }
1.130 joris 669:
1.138 joris 670: cf->file_status = FILE_UNKNOWN;
671: } else if (rcsdead == 1) {
672: if (cf->fd == -1) {
673: cf->file_status = FILE_UPTODATE;
674: } else {
1.143 joris 675: if (verbose)
676: cvs_log(LP_NOTICE,
677: "use add to create an entry for %s",
678: cf->file_path);
1.138 joris 679: cf->file_status = FILE_UNKNOWN;
1.44 jfb 680: }
1.138 joris 681: } else {
682: cf->file_status = FILE_CHECKOUT;
1.14 jfb 683: }
1.138 joris 684: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
685: if (cf->fd == -1) {
1.143 joris 686: if (verbose)
1.138 joris 687: cvs_log(LP_NOTICE,
688: "warning: new-born %s has dissapeared",
689: cf->file_path);
690: cf->file_status = FILE_REMOVE_ENTRY;
691: } else if (cf->file_rcs == NULL || rcsdead == 1) {
692: cf->file_status = FILE_ADDED;
693: } else {
1.143 joris 694: if (verbose)
1.138 joris 695: cvs_log(LP_NOTICE,
696: "conflict: %s already created by others",
697: cf->file_path);
698: cf->file_status = FILE_CONFLICT;
699: }
700: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
701: if (cf->fd != -1) {
1.143 joris 702: if (verbose)
1.138 joris 703: cvs_log(LP_NOTICE,
704: "%s should be removed but is still there",
705: cf->file_path);
706: cf->file_status = FILE_REMOVED;
707: } else if (cf->file_rcs == NULL || rcsdead == 1) {
708: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 709: } else {
1.138 joris 710: if (strcmp(r1, r2)) {
1.143 joris 711: if (verbose)
1.138 joris 712: cvs_log(LP_NOTICE,
713: "conflict: removed %s was modified"
714: " by a second party",
715: cf->file_path);
716: cf->file_status = FILE_CONFLICT;
717: } else {
718: cf->file_status = FILE_REMOVED;
719: }
1.66 joris 720: }
1.138 joris 721: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
722: if (cf->file_rcs == NULL || rcsdead == 1) {
723: if (cf->fd == -1) {
1.143 joris 724: if (verbose)
1.138 joris 725: cvs_log(LP_NOTICE,
726: "warning: %s's entry exists but"
727: " there is no longer a file"
728: " in the repository,"
729: " removing entry",
730: cf->file_path);
731: cf->file_status = FILE_REMOVE_ENTRY;
732: } else {
733: if (ismodified) {
1.143 joris 734: if (verbose)
1.138 joris 735: cvs_log(LP_NOTICE,
736: "conflict: %s is no longer "
737: "in the repository but is "
738: "locally modified",
739: cf->file_path);
740: cf->file_status = FILE_CONFLICT;
741: } else {
1.143 joris 742: if (verbose)
1.138 joris 743: cvs_log(LP_NOTICE,
744: "%s is no longer in the "
745: "repository",
746: cf->file_path);
1.128 joris 747:
1.138 joris 748: cf->file_status = FILE_UNLINK;
749: }
750: }
1.128 joris 751: } else {
1.138 joris 752: if (cf->fd == -1) {
1.143 joris 753: if (verbose)
1.138 joris 754: cvs_log(LP_NOTICE,
755: "warning: %s was lost",
756: cf->file_path);
757: cf->file_status = FILE_LOST;
758: } else {
759: if (ismodified == 1)
760: cf->file_status = FILE_MODIFIED;
761: else
762: cf->file_status = FILE_UPTODATE;
763:
764: if (strcmp(r1, r2)) {
765: if (cf->file_status == FILE_MODIFIED)
766: cf->file_status = FILE_MERGE;
767: else
768: cf->file_status = FILE_PATCH;
769: }
770: }
1.128 joris 771: }
772: }
773:
1.138 joris 774: xfree(repo);
775: xfree(rcsfile);
776: cvs_ent_close(entlist, ENT_NOSYNC);
777: }
1.14 jfb 778:
1.138 joris 779: void
780: cvs_file_free(struct cvs_file *cf)
781: {
782: xfree(cf->file_name);
783: xfree(cf->file_wd);
784: xfree(cf->file_path);
785:
786: if (cf->file_rpath != NULL)
787: xfree(cf->file_rpath);
788: if (cf->file_ent != NULL)
789: cvs_ent_free(cf->file_ent);
790: if (cf->file_rcs != NULL)
791: rcs_close(cf->file_rcs);
792: if (cf->fd != -1)
793: (void)close(cf->fd);
794: if (cf->repo_fd != -1)
795: (void)close(cf->repo_fd);
796: xfree(cf);
1.23 jfb 797: }
798:
799: static int
800: cvs_file_cmpname(const char *name1, const char *name2)
801: {
802: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
803: (strcasecmp(name1, name2));
1.14 jfb 804: }