Annotation of src/usr.bin/cvs/edit.c, Revision 1.33
1.33 ! xsa 1: /* $OpenBSD: edit.c,v 1.32 2007/02/09 03:49:15 joris Exp $ */
1.1 jfb 2: /*
1.15 xsa 3: * Copyright (c) 2006, 2007 Xavier Santolaria <xsa@openbsd.org>
1.1 jfb 4: *
1.14 xsa 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.11 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.14 xsa 22: #include "remote.h"
1.1 jfb 23:
1.18 xsa 24: #define E_COMMIT 0x01
25: #define E_EDIT 0x02
26: #define E_UNEDIT 0x04
27: #define E_ALL (E_EDIT|E_COMMIT|E_UNEDIT)
28:
1.23 xsa 29: #define BASE_ADD 0x01
30: #define BASE_GET 0x02
31: #define BASE_REMOVE 0x04
32:
1.18 xsa 33: static void cvs_edit_local(struct cvs_file *);
1.14 xsa 34: static void cvs_editors_local(struct cvs_file *);
1.15 xsa 35: static void cvs_unedit_local(struct cvs_file *);
1.1 jfb 36:
1.23 xsa 37: static RCSNUM *cvs_base_handle(struct cvs_file *, int);
38:
1.18 xsa 39: static int edit_aflags = 0;
40:
41: struct cvs_cmd cvs_cmd_edit = {
42: CVS_OP_EDIT, 0, "edit",
43: { },
44: "Get ready to edit a watched file",
45: "[-lR] [-a action] [file ...]",
46: "a:lR",
47: NULL,
48: cvs_edit
49: };
50:
1.1 jfb 51: struct cvs_cmd cvs_cmd_editors = {
1.14 xsa 52: CVS_OP_EDITORS, 0, "editors",
1.1 jfb 53: { },
1.14 xsa 54: "See who is editing a watched file",
1.3 xsa 55: "[-lR] [file ...]",
1.1 jfb 56: "lR",
57: NULL,
1.14 xsa 58: cvs_editors
1.1 jfb 59: };
60:
1.15 xsa 61: struct cvs_cmd cvs_cmd_unedit = {
62: CVS_OP_UNEDIT, 0, "unedit",
63: { },
64: "Undo an edit command",
65: "[-lR] [file ...]",
66: "lR",
67: NULL,
68: cvs_unedit
69: };
70:
1.14 xsa 71: int
1.18 xsa 72: cvs_edit(int argc, char **argv)
73: {
74: int ch;
75: int flags;
76: struct cvs_recursion cr;
77:
78: flags = CR_RECURSE_DIRS;
79:
80: while ((ch = getopt(argc, argv, cvs_cmd_edit.cmd_opts)) != -1) {
81: switch (ch) {
82: case 'a':
83: if (strcmp(optarg, "edit") == 0)
84: edit_aflags |= E_EDIT;
85: else if (strcmp(optarg, "unedit") == 0)
86: edit_aflags |= E_UNEDIT;
87: else if (strcmp(optarg, "commit") == 0)
88: edit_aflags |= E_COMMIT;
89: else if (strcmp(optarg, "all") == 0)
90: edit_aflags |= E_ALL;
91: else if (strcmp(optarg, "none") == 0)
92: edit_aflags &= ~E_ALL;
93: else
94: fatal("%s", cvs_cmd_edit.cmd_synopsis);
95: break;
96: case 'l':
97: flags &= ~CR_RECURSE_DIRS;
98: break;
99: case 'R':
100: break;
101: default:
102: fatal("%s", cvs_cmd_edit.cmd_synopsis);
103: }
104: }
105:
106: argc -= optind;
107: argv += optind;
108:
109: if (argc == 0)
110: fatal("%s", cvs_cmd_edit.cmd_synopsis);
111:
112: if (edit_aflags == 0)
113: edit_aflags |= E_ALL;
114:
115: cr.enterdir = NULL;
116: cr.leavedir = NULL;
117:
118: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.27 joris 119: cvs_client_connect_to_server();
1.18 xsa 120: cr.fileproc = cvs_client_sendfile;
121:
122: if (!(flags & CR_RECURSE_DIRS))
123: cvs_client_send_request("Argument -l");
124: } else {
125: cr.fileproc = cvs_edit_local;
126: }
127:
128: cr.flags = flags;
129:
130: cvs_file_run(argc, argv, &cr);
131:
132: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
133: cvs_client_send_files(argv, argc);
134: cvs_client_senddir(".");
135: cvs_client_send_request("edit");
136: cvs_client_get_responses();
137: }
138:
139: return (0);
140: }
141:
142: int
1.14 xsa 143: cvs_editors(int argc, char **argv)
1.1 jfb 144: {
1.12 xsa 145: int ch;
1.14 xsa 146: int flags;
147: struct cvs_recursion cr;
148:
149: flags = CR_RECURSE_DIRS;
1.1 jfb 150:
1.14 xsa 151: while ((ch = getopt(argc, argv, cvs_cmd_editors.cmd_opts)) != -1) {
1.1 jfb 152: switch (ch) {
153: case 'l':
1.14 xsa 154: flags &= ~CR_RECURSE_DIRS;
1.1 jfb 155: break;
156: case 'R':
157: break;
158: default:
1.14 xsa 159: fatal("%s", cvs_cmd_editors.cmd_synopsis);
1.1 jfb 160: }
161: }
162:
1.14 xsa 163: argc -= optind;
164: argv += optind;
1.1 jfb 165:
1.14 xsa 166: if (argc == 0)
167: fatal("%s", cvs_cmd_editors.cmd_synopsis);
1.1 jfb 168:
1.14 xsa 169: cr.enterdir = NULL;
170: cr.leavedir = NULL;
1.1 jfb 171:
1.14 xsa 172: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.27 joris 173: cvs_client_connect_to_server();
1.14 xsa 174: cr.fileproc = cvs_client_sendfile;
175:
176: if (!(flags & CR_RECURSE_DIRS))
177: cvs_client_send_request("Argument -l");
178: } else {
179: cr.fileproc = cvs_editors_local;
180: }
1.1 jfb 181:
1.14 xsa 182: cr.flags = flags;
1.9 xsa 183:
1.14 xsa 184: cvs_file_run(argc, argv, &cr);
1.9 xsa 185:
1.14 xsa 186: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
187: cvs_client_send_files(argv, argc);
188: cvs_client_senddir(".");
189: cvs_client_send_request("editors");
190: cvs_client_get_responses();
1.9 xsa 191: }
192:
1.14 xsa 193: return (0);
194: }
1.9 xsa 195:
1.15 xsa 196: int
197: cvs_unedit(int argc, char **argv)
198: {
199: int ch;
200: int flags;
201: struct cvs_recursion cr;
202:
203: flags = CR_RECURSE_DIRS;
204:
205: while ((ch = getopt(argc, argv, cvs_cmd_unedit.cmd_opts)) != -1) {
206: switch (ch) {
207: case 'l':
208: flags &= ~CR_RECURSE_DIRS;
209: break;
210: case 'R':
211: break;
212: default:
213: fatal("%s", cvs_cmd_unedit.cmd_synopsis);
214: }
215: }
216:
217: argc -= optind;
218: argv += optind;
219:
220: if (argc == 0)
221: fatal("%s", cvs_cmd_unedit.cmd_synopsis);
222:
223: cr.enterdir = NULL;
224: cr.leavedir = NULL;
225:
226: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.28 xsa 227: cvs_client_connect_to_server();
1.15 xsa 228: cr.fileproc = cvs_client_sendfile;
229:
230: if (!(flags & CR_RECURSE_DIRS))
231: cvs_client_send_request("Argument -l");
232: } else {
233: cr.fileproc = cvs_unedit_local;
234: }
235:
236: cr.flags = flags;
237:
238: cvs_file_run(argc, argv, &cr);
239:
240: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
241: cvs_client_send_files(argv, argc);
242: cvs_client_senddir(".");
243: cvs_client_send_request("unedit");
244: cvs_client_get_responses();
245: }
246:
247: return (0);
1.18 xsa 248: }
249:
250: static void
251: cvs_edit_local(struct cvs_file *cf)
252: {
253: FILE *fp;
254: struct tm *t;
255: time_t now;
1.31 otto 256: char bfpath[MAXPATHLEN], timebuf[64], thishost[MAXHOSTNAMELEN];
257: char wdir[MAXPATHLEN];
1.18 xsa 258:
259: if (cvs_noexec == 1)
260: return;
261:
1.24 xsa 262: cvs_log(LP_TRACE, "cvs_edit_local(%s)", cf->file_path);
263:
1.32 joris 264: cvs_file_classify(cf, NULL);
1.23 xsa 265:
1.18 xsa 266: if ((fp = fopen(CVS_PATH_NOTIFY, "a")) == NULL)
267: fatal("cvs_edit_local: fopen: `%s': %s",
268: CVS_PATH_NOTIFY, strerror(errno));
269:
270: (void)time(&now);
271: if ((t = gmtime(&now)) == NULL)
272: fatal("gmtime failed");
273:
1.20 xsa 274: asctime_r(t, timebuf);
275: if (timebuf[strlen(timebuf) - 1] == '\n')
276: timebuf[strlen(timebuf) - 1] = '\0';
1.18 xsa 277:
1.19 xsa 278: if (gethostname(thishost, sizeof(thishost)) == -1)
279: fatal("gethostname failed");
280:
1.21 xsa 281: if (getcwd(wdir, sizeof(wdir)) == NULL)
282: fatal("getcwd failed");
283:
1.25 xsa 284: (void)fprintf(fp, "E%s\t%s GMT\t%s\t%s\t",
1.21 xsa 285: cf->file_name, timebuf, thishost, wdir);
1.18 xsa 286:
287: if (edit_aflags & E_EDIT)
288: (void)fprintf(fp, "E");
289: if (edit_aflags & E_UNEDIT)
290: (void)fprintf(fp, "U");
291: if (edit_aflags & E_COMMIT)
292: (void)fprintf(fp, "C");
293:
294: (void)fprintf(fp, "\n");
295:
296: (void)fclose(fp);
297:
298: if (fchmod(cf->fd, 0644) == -1)
299: fatal("cvs_edit_local: fchmod %s", strerror(errno));
300:
1.33 ! xsa 301: (void)xsnprintf(bfpath, MAXPATHLEN, "%s/%s",
! 302: CVS_PATH_BASEDIR, cf->file_name);
1.22 xsa 303:
304: if (mkdir(CVS_PATH_BASEDIR, 0755) == -1 && errno != EEXIST)
305: fatal("cvs_edit_local: `%s': %s", CVS_PATH_BASEDIR,
306: strerror(errno));
1.18 xsa 307:
1.26 xsa 308: if (cvs_file_copy(cf->file_path, bfpath) == -1)
309: fatal("cvs_edit_local: cvs_file_copy failed");
1.18 xsa 310:
1.23 xsa 311: (void)cvs_base_handle(cf, BASE_ADD);
1.15 xsa 312: }
313:
1.14 xsa 314: static void
315: cvs_editors_local(struct cvs_file *cf)
316: {
1.15 xsa 317: }
318:
319: static void
320: cvs_unedit_local(struct cvs_file *cf)
321: {
322: FILE *fp;
323: struct stat st;
324: struct tm *t;
325: time_t now;
1.31 otto 326: char bfpath[MAXPATHLEN], timebuf[64], thishost[MAXHOSTNAMELEN];
327: char wdir[MAXPATHLEN];
1.24 xsa 328: RCSNUM *ba_rev;
1.15 xsa 329:
330: if (cvs_noexec == 1)
331: return;
332:
1.24 xsa 333: cvs_log(LP_TRACE, "cvs_unedit_local(%s)", cf->file_path);
334:
1.32 joris 335: cvs_file_classify(cf, NULL);
1.23 xsa 336:
1.33 ! xsa 337: (void)xsnprintf(bfpath, MAXPATHLEN, "%s/%s",
! 338: CVS_PATH_BASEDIR, cf->file_name);
1.15 xsa 339:
1.31 otto 340: if (stat(bfpath, &st) == -1)
1.15 xsa 341: return;
342:
1.17 xsa 343: if (cvs_file_cmp(cf->file_path, bfpath) != 0) {
344: cvs_printf("%s has been modified; revert changes? ",
345: cf->file_name);
346:
1.31 otto 347: if (cvs_yesno() == -1)
1.17 xsa 348: return;
349: }
1.15 xsa 350:
351: cvs_rename(bfpath, cf->file_path);
352:
353: if ((fp = fopen(CVS_PATH_NOTIFY, "a")) == NULL)
354: fatal("cvs_unedit_local: fopen: `%s': %s",
355: CVS_PATH_NOTIFY, strerror(errno));
356:
357: (void)time(&now);
358: if ((t = gmtime(&now)) == NULL)
359: fatal("gmtime failed");
360:
1.20 xsa 361: asctime_r(t, timebuf);
362: if (timebuf[strlen(timebuf) - 1] == '\n')
363: timebuf[strlen(timebuf) - 1] = '\0';
1.15 xsa 364:
1.19 xsa 365: if (gethostname(thishost, sizeof(thishost)) == -1)
366: fatal("gethostname failed");
367:
1.21 xsa 368: if (getcwd(wdir, sizeof(wdir)) == NULL)
1.23 xsa 369: fatal("getcwd failed");
1.21 xsa 370:
1.15 xsa 371: (void)fprintf(fp, "U%s\t%s GMT\t%s\t%s\t\n",
1.21 xsa 372: cf->file_name, timebuf, thishost, wdir);
1.15 xsa 373:
374: (void)fclose(fp);
375:
1.29 xsa 376: if ((ba_rev = cvs_base_handle(cf, BASE_GET)) == NULL) {
377: cvs_log(LP_ERR, "%s not mentioned in %s",
378: cf->file_name, CVS_PATH_BASEREV);
379: return;
380: }
381:
1.24 xsa 382: if (cf->file_ent != NULL) {
1.29 xsa 383: CVSENTRIES *entlist;
384: struct cvs_ent *ent;
385: char *entry, rbuf[16];
386:
387: entlist = cvs_ent_open(cf->file_wd);
388:
389: if ((ent = cvs_ent_get(entlist, cf->file_name)) == NULL)
1.30 xsa 390: fatal("cvs_unedit_local: cvs_ent_get failed");
1.29 xsa 391:
392: (void)rcsnum_tostr(ba_rev, rbuf, sizeof(rbuf));
393:
394: memset(timebuf, 0, sizeof(timebuf));
395: ctime_r(&cf->file_ent->ce_mtime, timebuf);
396: if (timebuf[strlen(timebuf) - 1] == '\n')
397: timebuf[strlen(timebuf) - 1] = '\0';
398:
399: (void)xasprintf(&entry, "/%s/%s/%s/%s/%s",
400: cf->file_name, rbuf, timebuf,
401: (cf->file_ent->ce_tag) ? cf->file_ent->ce_tag : "",
402: (cf->file_ent->ce_opts) ? cf->file_ent->ce_opts : "");
403:
404: cvs_ent_add(entlist, entry);
405:
406: cvs_ent_free(ent);
407: cvs_ent_close(entlist, ENT_SYNC);
408:
409: xfree(entry);
1.24 xsa 410: }
1.29 xsa 411:
412: rcsnum_free(ba_rev);
1.24 xsa 413:
414: (void)cvs_base_handle(cf, BASE_REMOVE);
1.16 xsa 415:
416: if (fchmod(cf->fd, 0644) == -1)
417: fatal("cvs_unedit_local: fchmod %s", strerror(errno));
1.23 xsa 418: }
419:
420: static RCSNUM *
421: cvs_base_handle(struct cvs_file *cf, int flags)
422: {
423: FILE *fp, *tfp;
424: RCSNUM *ba_rev;
425: size_t len;
1.24 xsa 426: int i;
427: char *dp, *sp;
428: char buf[MAXPATHLEN], *fields[2], rbuf[16];
1.23 xsa 429:
430: cvs_log(LP_TRACE, "cvs_base_handle(%s)", cf->file_path);
431:
432: tfp = NULL;
433: ba_rev = NULL;
434:
1.24 xsa 435: if (((fp = fopen(CVS_PATH_BASEREV, "r")) == NULL) && errno != ENOENT) {
1.23 xsa 436: cvs_log(LP_ERRNO, "%s", CVS_PATH_BASEREV);
437: goto out;
438: }
439:
440: if (flags & (BASE_ADD|BASE_REMOVE)) {
441: if ((tfp = fopen(CVS_PATH_BASEREVTMP, "w")) == NULL) {
442: cvs_log(LP_ERRNO, "%s", CVS_PATH_BASEREVTMP);
443: goto out;
444: }
445: }
446:
1.24 xsa 447: if (fp != NULL) {
448: while(fgets(buf, sizeof(buf), fp)) {
449: len = strlen(buf);
450: if (len > 0 && buf[len - 1] == '\n')
451: buf[len - 1] = '\0';
452:
453: if (buf[0] != 'B')
454: continue;
455:
456: sp = buf + 1;
457: i = 0;
458: do {
459: if ((dp = strchr(sp, '/')) != NULL)
460: *(dp++) = '\0';
461: fields[i++] = sp;
462: sp = dp;
463: } while (dp != NULL && i < 2);
464:
465: if (cvs_file_cmpname(fields[0], cf->file_path) == 0) {
466: if (flags & BASE_GET) {
467: ba_rev = rcsnum_parse(fields[1]);
468: if (ba_rev == NULL)
469: fatal("cvs_base_handle: "
470: "rcsnum_parse");
471: goto got_rev;
472: }
473: } else {
474: if (flags & (BASE_ADD|BASE_REMOVE))
475: (void)fprintf(tfp, "%s\n", buf);
1.23 xsa 476: }
477: }
478: }
479:
480: got_rev:
481: if (flags & (BASE_ADD)) {
482: (void)rcsnum_tostr(cf->file_ent->ce_rev, rbuf, sizeof(rbuf));
483: (void)fprintf(tfp, "B%s/%s/\n", cf->file_path, rbuf);
484: }
485:
486: out:
487: if (fp != NULL)
488: (void)fclose(fp);
489:
490: if (tfp != NULL) {
491: (void)fclose(tfp);
492: (void)cvs_rename(CVS_PATH_BASEREVTMP, CVS_PATH_BASEREV);
493: }
494:
495: return (ba_rev);
1.1 jfb 496: }