Annotation of src/usr.bin/cvs/file.c, Revision 1.145
1.145 ! joris 1: /* $OpenBSD: file.c,v 1.144 2006/05/28 08:49:01 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:
264: if ((fd = open(l->file_path, O_RDONLY)) != -1) {
265: if (fstat(fd, &st) == -1) {
266: cvs_log(LP_ERRNO, "%s", l->file_path);
267: (void)close(fd);
268: goto next;
269: }
1.26 jfb 270:
1.138 joris 271: if (S_ISDIR(st.st_mode))
272: type = CVS_DIR;
273: else if (S_ISREG(st.st_mode))
274: type = CVS_FILE;
275: else {
276: cvs_log(LP_ERR,
277: "ignoring bad file type for %s",
278: l->file_path);
279: (void)close(fd);
280: goto next;
281: }
282: } else {
283: if (stat(d, &st) == -1) {
284: cvs_log(LP_ERRNO, "%s", d);
285: goto next;
286: }
1.87 joris 287:
1.138 joris 288: cvs_get_repo(d, repo, MAXPATHLEN);
289: len = snprintf(fpath, MAXPATHLEN, "%s/%s",
290: repo, f);
291: if (len == -1 || len >= MAXPATHLEN)
292: fatal("cvs_file_walklist: overflow");
293:
294: if ((fd = open(fpath, O_RDONLY)) == -1) {
295: strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
296: fd = open(fpath, O_RDONLY);
297: }
1.26 jfb 298:
1.138 joris 299: if (fd != -1) {
300: if (fstat(fd, &st) == -1)
301: fatal("cvs_file_walklist: %s: %s",
302: fpath, strerror(errno));
303:
304: if (S_ISDIR(st.st_mode))
305: type = CVS_DIR;
306: else if (S_ISREG(st.st_mode))
307: type = CVS_FILE;
308: else {
309: cvs_log(LP_ERR,
310: "ignoring bad file type for %s",
311: l->file_path);
312: (void)close(fd);
313: goto next;
314: }
315:
316: /* this file is not in our working copy yet */
317: (void)close(fd);
318: fd = -1;
319: }
320: }
1.73 joris 321:
1.138 joris 322: cf = cvs_file_get_cf(d, f, fd, type);
323: if (cf->file_type == CVS_DIR) {
324: cvs_file_walkdir(cf, cr);
325: } else {
326: if (cr->local != NULL)
327: cr->local(cf);
1.128 joris 328: }
1.100 joris 329:
1.138 joris 330: cvs_file_free(cf);
1.123 joris 331:
1.138 joris 332: next:
333: nxt = TAILQ_NEXT(l, flist);
334: TAILQ_REMOVE(fl, l, flist);
1.68 joris 335:
1.138 joris 336: xfree(l->file_path);
337: xfree(l);
1.100 joris 338: }
339:
1.138 joris 340: xfree(fpath);
341: xfree(repo);
1.100 joris 342: }
343:
1.138 joris 344: void
345: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100 joris 346: {
1.138 joris 347: int l;
348: FILE *fp;
349: int nbytes;
350: size_t len;
351: long base;
352: size_t bufsize;
353: struct stat st;
354: struct dirent *dp;
1.100 joris 355: struct cvs_ent *ent;
1.138 joris 356: struct cvs_ignpat *ip;
357: struct cvs_ent_line *line;
358: struct cvs_flisthead fl, dl;
359: CVSENTRIES *entlist;
360: char *buf, *ebuf, *cp, *repo, *fpath;
1.100 joris 361:
1.138 joris 362: cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100 joris 363:
1.138 joris 364: if (cr->enterdir != NULL)
365: cr->enterdir(cf);
1.100 joris 366:
1.138 joris 367: if (cr->local != NULL)
368: cr->local(cf);
1.100 joris 369:
1.138 joris 370: fpath = xmalloc(MAXPATHLEN);
1.100 joris 371:
372: /*
1.138 joris 373: * If we do not have a admin directory inside here, dont bother.
1.100 joris 374: */
1.138 joris 375: l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
376: CVS_PATH_CVSDIR);
377: if (l == -1 || l >= MAXPATHLEN)
378: fatal("cvs_file_walkdir: overflow");
1.100 joris 379:
1.138 joris 380: if (stat(fpath, &st) == -1) {
381: xfree(fpath);
382: return;
1.100 joris 383: }
384:
385: /*
1.138 joris 386: * check for a local .cvsignore file
1.100 joris 387: */
1.138 joris 388: l = snprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
389: if (l == -1 || l >= MAXPATHLEN)
390: fatal("cvs_file_walkdir: overflow");
1.100 joris 391:
1.138 joris 392: if ((fp = fopen(fpath, "r")) != NULL) {
393: while (fgets(fpath, MAXPATHLEN, fp)) {
394: len = strlen(fpath);
395: if (fpath[len - 1] == '\n')
396: fpath[len - 1] = '\0';
1.26 jfb 397:
1.138 joris 398: cvs_file_ignore(fpath, &dir_ign_pats);
1.68 joris 399: }
1.26 jfb 400:
1.138 joris 401: (void)fclose(fp);
1.100 joris 402: }
403:
1.138 joris 404: if (fstat(cf->fd, &st) == -1)
405: fatal("cvs_file_walkdir: %s %s", cf->file_path,
406: strerror(errno));
1.100 joris 407:
1.138 joris 408: bufsize = st.st_size;
409: if (bufsize < st.st_blksize)
410: bufsize = st.st_blksize;
1.116 joris 411:
1.138 joris 412: buf = xmalloc(bufsize);
413: TAILQ_INIT(&fl);
414: TAILQ_INIT(&dl);
1.87 joris 415:
1.138 joris 416: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
417: ebuf = buf + nbytes;
418: cp = buf;
1.26 jfb 419:
1.138 joris 420: while (cp < ebuf) {
421: dp = (struct dirent *)cp;
422: if (!strcmp(dp->d_name, ".") ||
423: !strcmp(dp->d_name, "..") ||
424: !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
425: dp->d_reclen == 0) {
426: cp += dp->d_reclen;
427: continue;
428: }
1.13 jfb 429:
1.138 joris 430: if (cvs_file_chkign(dp->d_name)) {
431: cp += dp->d_reclen;
1.13 jfb 432: continue;
1.138 joris 433: }
1.13 jfb 434:
1.140 joris 435: if (!(cr->flags & CR_RECURSE_DIRS) &&
436: dp->d_type == DT_DIR) {
437: cp += dp->d_reclen;
438: continue;
439: }
440:
1.138 joris 441: l = snprintf(fpath, MAXPATHLEN, "%s/%s",
442: cf->file_path, dp->d_name);
443: if (l == -1 || l >= MAXPATHLEN)
444: fatal("cvs_file_walkdir: overflow");
1.13 jfb 445:
1.138 joris 446: /*
447: * Anticipate the file type to sort them,
448: * note that we do not determine the final
449: * type until we actually have the fd floating
450: * around.
451: */
452: if (dp->d_type == DT_DIR)
453: cvs_file_get(fpath, &dl);
454: else if (dp->d_type == DT_REG)
455: cvs_file_get(fpath, &fl);
1.13 jfb 456:
1.138 joris 457: cp += dp->d_reclen;
458: }
1.34 jfb 459: }
460:
1.138 joris 461: if (nbytes == -1)
462: fatal("cvs_file_walkdir: %s %s", cf->file_path,
463: strerror(errno));
1.22 jfb 464:
1.138 joris 465: xfree(buf);
1.22 jfb 466:
1.138 joris 467: while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
468: TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
469: xfree(ip);
1.91 joris 470: }
1.114 xsa 471:
1.138 joris 472: entlist = cvs_ent_open(cf->file_path);
473: TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
474: ent = cvs_ent_parse(line->buf);
1.61 xsa 475:
1.138 joris 476: l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
477: ent->ce_name);
478: if (l == -1 || l >= MAXPATHLEN)
479: fatal("cvs_file_walkdir: overflow");
1.100 joris 480:
1.140 joris 481: if (!(cr->flags & CR_RECURSE_DIRS) &&
482: ent->ce_type == CVS_ENT_DIR)
483: continue;
484:
1.138 joris 485: if (ent->ce_type == CVS_ENT_DIR)
486: cvs_file_get(fpath, &dl);
487: else if (ent->ce_type == CVS_ENT_FILE)
488: cvs_file_get(fpath, &fl);
1.68 joris 489:
1.138 joris 490: cvs_ent_free(ent);
1.128 joris 491: }
492:
1.138 joris 493: cvs_ent_close(entlist, ENT_NOSYNC);
1.91 joris 494:
1.140 joris 495: if (cr->flags & CR_REPO) {
496: repo = xmalloc(MAXPATHLEN);
497: cvs_get_repo(cf->file_path, repo, MAXPATHLEN);
498: cvs_repository_lock(repo);
1.68 joris 499:
1.140 joris 500: cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
501: (cr->flags & CR_RECURSE_DIRS));
502: }
1.68 joris 503:
1.138 joris 504: cvs_file_walklist(&fl, cr);
505: cvs_file_freelist(&fl);
1.68 joris 506:
1.140 joris 507: if (cr->flags & CR_REPO) {
508: cvs_repository_unlock(repo);
509: xfree(repo);
510: }
1.68 joris 511:
1.138 joris 512: cvs_file_walklist(&dl, cr);
513: cvs_file_freelist(&dl);
1.34 jfb 514:
1.138 joris 515: xfree(fpath);
1.26 jfb 516:
1.138 joris 517: if (cr->leavedir != NULL)
518: cr->leavedir(cf);
1.3 jfb 519: }
520:
521: void
1.138 joris 522: cvs_file_freelist(struct cvs_flisthead *fl)
1.3 jfb 523: {
1.138 joris 524: struct cvs_filelist *f;
1.62 jfb 525:
1.138 joris 526: while ((f = TAILQ_FIRST(fl)) != NULL) {
527: TAILQ_REMOVE(fl, f, flist);
528: xfree(f->file_path);
529: xfree(f);
1.62 jfb 530: }
1.5 jfb 531: }
532:
1.138 joris 533: void
1.143 joris 534: cvs_file_classify(struct cvs_file *cf, int loud)
1.3 jfb 535: {
1.138 joris 536: size_t len;
537: time_t mtime;
538: struct stat st;
1.143 joris 539: int rflags, l, ismodified, rcsdead, verbose;
1.138 joris 540: CVSENTRIES *entlist = NULL;
541: const char *state;
542: char *repo, *rcsfile, r1[16], r2[16];
543:
544: cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
545:
546: if (!strcmp(cf->file_path, ".")) {
547: cf->file_status = FILE_UPTODATE;
548: return;
549: }
550:
1.143 joris 551: verbose = (verbosity > 1 && loud == 1);
1.138 joris 552: entlist = cvs_ent_open(cf->file_wd);
553:
554: repo = xmalloc(MAXPATHLEN);
555: rcsfile = xmalloc(MAXPATHLEN);
556:
557: cvs_get_repo(cf->file_wd, repo, MAXPATHLEN);
558: l = snprintf(rcsfile, MAXPATHLEN, "%s/%s",
559: repo, cf->file_name);
560: if (l == -1 || l >= MAXPATHLEN)
561: fatal("cvs_file_classify: overflow");
562:
563: if (cf->file_type == CVS_FILE) {
564: len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
565: if (len >= MAXPATHLEN)
566: fatal("cvs_file_classify: truncation");
567: }
568:
569: cf->file_rpath = xstrdup(rcsfile);
570: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
571:
1.144 joris 572: if (cf->file_ent != NULL) {
573: if (cf->file_ent->ce_type == CVS_ENT_DIR &&
574: cf->file_type != CVS_DIR)
575: fatal("%s is suppose to be a directory, but it is not",
576: cf->file_path);
577: if (cf->file_ent->ce_type == CVS_ENT_FILE &&
578: cf->file_type != CVS_FILE)
579: fatal("%s is suppose to be a file, but it is not",
580: cf->file_path);
581: }
582:
1.138 joris 583: if (cf->file_type == CVS_DIR) {
584: if (cf->fd == -1 && stat(rcsfile, &st) != -1)
585: cf->file_status = DIR_CREATE;
586: else if (cf->file_ent != NULL)
587: cf->file_status = FILE_UPTODATE;
1.144 joris 588: else
589: cf->file_status = FILE_UNKNOWN;
590:
1.138 joris 591: xfree(repo);
592: xfree(rcsfile);
593: cvs_ent_close(entlist, ENT_NOSYNC);
594: return;
595: }
596:
597: rflags = 0;
598: cf->repo_fd = open(cf->file_rpath, O_RDONLY);
599: if (cf->repo_fd != -1) {
600: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
601: if (cf->file_rcs == NULL)
602: fatal("cvs_file_classify: rcs_open failed while it "
603: "shouldn't");
604: } else {
605: cf->file_rcs = NULL;
1.3 jfb 606: }
607:
1.138 joris 608: if (cf->file_ent != NULL)
609: rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
610: if (cf->file_rcs != NULL)
611: rcsnum_tostr(cf->file_rcs->rf_head, r2, sizeof(r2));
1.6 jfb 612:
1.138 joris 613: ismodified = rcsdead = 0;
614: if (cf->fd != -1 && cf->file_ent != NULL) {
615: if (fstat(cf->fd, &st) == -1)
616: fatal("cvs_file_classify: %s", strerror(errno));
1.6 jfb 617:
1.138 joris 618: mtime = cvs_hack_time(st.st_mtime, 1);
619: if (mtime == 0)
620: fatal("to gmt failed");
1.62 jfb 621:
1.138 joris 622: if (mtime != cf->file_ent->ce_mtime)
623: ismodified = 1;
1.62 jfb 624: }
625:
1.138 joris 626: if (cf->file_rcs != NULL) {
627: state = rcs_state_get(cf->file_rcs, cf->file_rcs->rf_head);
628: if (state == NULL)
629: fatal("failed to get state for HEAD for %s",
630: cf->file_path);
1.139 joris 631: if (!strcmp(state, RCS_STATE_DEAD))
1.138 joris 632: rcsdead = 1;
1.145 ! joris 633:
! 634: cf->file_rcs->rf_dead = rcsdead;
1.128 joris 635: }
636:
1.138 joris 637: /*
638: * 10 Sin
639: * 20 Goto hell
640: * (I welcome you if-else hell)
641: */
642: if (cf->file_ent == NULL) {
643: if (cf->file_rcs == NULL) {
644: if (cf->fd == -1) {
1.143 joris 645: if (verbose)
1.138 joris 646: cvs_log(LP_NOTICE,
647: "nothing known about '%s'",
648: cf->file_path);
649: } else {
1.143 joris 650: if (verbose)
1.138 joris 651: cvs_log(LP_NOTICE,
652: "use add to create an entry for %s",
653: cf->file_path);
654: }
1.130 joris 655:
1.138 joris 656: cf->file_status = FILE_UNKNOWN;
657: } else if (rcsdead == 1) {
658: if (cf->fd == -1) {
659: cf->file_status = FILE_UPTODATE;
660: } else {
1.143 joris 661: if (verbose)
662: cvs_log(LP_NOTICE,
663: "use add to create an entry for %s",
664: cf->file_path);
1.138 joris 665: cf->file_status = FILE_UNKNOWN;
1.44 jfb 666: }
1.138 joris 667: } else {
668: cf->file_status = FILE_CHECKOUT;
1.14 jfb 669: }
1.138 joris 670: } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
671: if (cf->fd == -1) {
1.143 joris 672: if (verbose)
1.138 joris 673: cvs_log(LP_NOTICE,
674: "warning: new-born %s has dissapeared",
675: cf->file_path);
676: cf->file_status = FILE_REMOVE_ENTRY;
677: } else if (cf->file_rcs == NULL || rcsdead == 1) {
678: cf->file_status = FILE_ADDED;
679: } else {
1.143 joris 680: if (verbose)
1.138 joris 681: cvs_log(LP_NOTICE,
682: "conflict: %s already created by others",
683: cf->file_path);
684: cf->file_status = FILE_CONFLICT;
685: }
686: } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
687: if (cf->fd != -1) {
1.143 joris 688: if (verbose)
1.138 joris 689: cvs_log(LP_NOTICE,
690: "%s should be removed but is still there",
691: cf->file_path);
692: cf->file_status = FILE_REMOVED;
693: } else if (cf->file_rcs == NULL || rcsdead == 1) {
694: cf->file_status = FILE_REMOVE_ENTRY;
1.66 joris 695: } else {
1.138 joris 696: if (strcmp(r1, r2)) {
1.143 joris 697: if (verbose)
1.138 joris 698: cvs_log(LP_NOTICE,
699: "conflict: removed %s was modified"
700: " by a second party",
701: cf->file_path);
702: cf->file_status = FILE_CONFLICT;
703: } else {
704: cf->file_status = FILE_REMOVED;
705: }
1.66 joris 706: }
1.138 joris 707: } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
708: if (cf->file_rcs == NULL || rcsdead == 1) {
709: if (cf->fd == -1) {
1.143 joris 710: if (verbose)
1.138 joris 711: cvs_log(LP_NOTICE,
712: "warning: %s's entry exists but"
713: " there is no longer a file"
714: " in the repository,"
715: " removing entry",
716: cf->file_path);
717: cf->file_status = FILE_REMOVE_ENTRY;
718: } else {
719: if (ismodified) {
1.143 joris 720: if (verbose)
1.138 joris 721: cvs_log(LP_NOTICE,
722: "conflict: %s is no longer "
723: "in the repository but is "
724: "locally modified",
725: cf->file_path);
726: cf->file_status = FILE_CONFLICT;
727: } else {
1.143 joris 728: if (verbose)
1.138 joris 729: cvs_log(LP_NOTICE,
730: "%s is no longer in the "
731: "repository",
732: cf->file_path);
1.128 joris 733:
1.138 joris 734: cf->file_status = FILE_UNLINK;
735: }
736: }
1.128 joris 737: } else {
1.138 joris 738: if (cf->fd == -1) {
1.143 joris 739: if (verbose)
1.138 joris 740: cvs_log(LP_NOTICE,
741: "warning: %s was lost",
742: cf->file_path);
743: cf->file_status = FILE_LOST;
744: } else {
745: if (ismodified == 1)
746: cf->file_status = FILE_MODIFIED;
747: else
748: cf->file_status = FILE_UPTODATE;
749:
750: if (strcmp(r1, r2)) {
751: if (cf->file_status == FILE_MODIFIED)
752: cf->file_status = FILE_MERGE;
753: else
754: cf->file_status = FILE_PATCH;
755: }
756: }
1.128 joris 757: }
758: }
759:
1.138 joris 760: xfree(repo);
761: xfree(rcsfile);
762: cvs_ent_close(entlist, ENT_NOSYNC);
763: }
1.14 jfb 764:
1.138 joris 765: void
766: cvs_file_free(struct cvs_file *cf)
767: {
768: xfree(cf->file_name);
769: xfree(cf->file_wd);
770: xfree(cf->file_path);
771:
772: if (cf->file_rpath != NULL)
773: xfree(cf->file_rpath);
774: if (cf->file_ent != NULL)
775: cvs_ent_free(cf->file_ent);
776: if (cf->file_rcs != NULL)
777: rcs_close(cf->file_rcs);
778: if (cf->fd != -1)
779: (void)close(cf->fd);
780: if (cf->repo_fd != -1)
781: (void)close(cf->repo_fd);
782: xfree(cf);
1.23 jfb 783: }
784:
785: static int
786: cvs_file_cmpname(const char *name1, const char *name2)
787: {
788: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
789: (strcasecmp(name1, name2));
1.14 jfb 790: }