Annotation of src/usr.bin/rcs/ci.c, Revision 1.23
1.23 ! niallo 1: /* $OpenBSD: ci.c,v 1.22 2005/10/12 17:13:30 deraadt Exp $ */
1.1 niallo 2: /*
3: * Copyright (c) 2005 Niall O'Higgins <niallo@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 cinditions
8: * are met:
9: *
10: * 1. Redistributions of source cide must retain the above cipyright
11: * notice, this list of cinditions 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/param.h>
28: #include <sys/types.h>
29: #include <sys/stat.h>
30: #include <sys/wait.h>
31:
32: #include <err.h>
33: #include <pwd.h>
34: #include <errno.h>
35: #include <stdio.h>
36: #include <ctype.h>
37: #include <stdlib.h>
38: #include <unistd.h>
39: #include <string.h>
1.20 niallo 40: #include <time.h>
1.1 niallo 41:
42: #include "log.h"
43: #include "rcs.h"
1.4 niallo 44: #include "diff.h"
1.1 niallo 45: #include "rcsprog.h"
46:
1.9 niallo 47: #define LOCK_LOCK 1
48: #define LOCK_UNLOCK 2
49:
1.4 niallo 50: static char * checkin_diff_file(RCSFILE *, RCSNUM *, const char *);
1.12 niallo 51: static char * checkin_getlogmsg(char *, char *, RCSNUM *, RCSNUM *);
1.4 niallo 52:
1.1 niallo 53: void
54: checkin_usage(void)
55: {
56: fprintf(stderr,
1.22 deraadt 57: "usage: ci [-jMNqV] [-d date] [-k mode] [-l [rev]] [-m msg]\n"
58: " [-r [rev]] [-u [rev]] file ...\n");
1.1 niallo 59: }
60:
61: /*
62: * checkin_main()
63: *
64: * Handler for the `ci' program.
65: * Returns 0 on success, or >0 on error.
66: */
67: int
68: checkin_main(int argc, char **argv)
69: {
1.21 niallo 70: int i, ch, flags, lkmode, interactive, rflag, status;
1.1 niallo 71: mode_t fmode;
1.20 niallo 72: time_t date = -1;
1.1 niallo 73: RCSFILE *file;
1.12 niallo 74: RCSNUM *frev, *newrev;
1.1 niallo 75: char fpath[MAXPATHLEN];
1.12 niallo 76: char *rcs_msg, *filec, *deltatext, *username;
1.16 niallo 77: struct rcs_lock *lkp;
1.4 niallo 78: BUF *bp;
1.1 niallo 79:
80: flags = RCS_RDWR;
81: file = NULL;
1.12 niallo 82: rcs_msg = NULL;
83: newrev = NULL;
1.21 niallo 84: fmode = lkmode = verbose = rflag = status = 0;
1.6 niallo 85: interactive = 1;
1.1 niallo 86:
1.9 niallo 87: if ((username = getlogin()) == NULL) {
1.19 xsa 88: cvs_log(LP_ERRNO, "failed to get username");
1.9 niallo 89: exit(1);
90: }
91:
1.18 niallo 92: while ((ch = getopt(argc, argv, "j:l::M:N:qu::d:r::m:k:V")) != -1) {
1.1 niallo 93: switch (ch) {
1.20 niallo 94: case 'd':
95: if ((date = cvs_date_parse(optarg)) <= 0) {
96: cvs_log(LP_ERR, "invalide date");
97: exit(1);
98: }
99: break;
1.1 niallo 100: case 'h':
101: (usage)();
102: exit(0);
103: case 'm':
104: rcs_msg = optarg;
1.6 niallo 105: interactive = 0;
1.3 joris 106: break;
107: case 'q':
108: verbose = 0;
1.1 niallo 109: break;
110: case 'V':
111: printf("%s\n", rcs_version);
112: exit(0);
1.9 niallo 113: case 'l':
1.18 niallo 114: if (optarg != NULL) {
115: if ((newrev = rcsnum_parse(optarg)) == NULL) {
116: cvs_log(LP_ERR, "bad revision number");
117: exit(1);
118: }
119: }
1.9 niallo 120: lkmode = LOCK_LOCK;
121: break;
122: case 'u':
1.18 niallo 123: if (optarg != NULL) {
124: if ((newrev = rcsnum_parse(optarg)) == NULL) {
125: cvs_log(LP_ERR, "bad revision number");
126: exit(1);
127: }
128: }
1.9 niallo 129: lkmode = LOCK_UNLOCK;
130: break;
1.12 niallo 131: case 'r':
132: rflag = 1;
133: if (optarg != NULL) {
134: if ((newrev = rcsnum_parse(optarg)) == NULL) {
135: cvs_log(LP_ERR, "bad revision number");
136: exit(1);
137: }
138: }
139: break;
1.1 niallo 140: default:
141: (usage)();
142: exit(1);
143: }
144: }
145:
146: argc -= optind;
147: argv += optind;
148: if (argc == 0) {
149: cvs_log(LP_ERR, "no input file");
150: (usage)();
151: exit(1);
152: }
153:
154: for (i = 0; i < argc; i++) {
155: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
156: continue;
157:
1.4 niallo 158: file = rcs_open(fpath, RCS_RDWR, fmode);
1.1 niallo 159: if (file == NULL) {
1.4 niallo 160: cvs_log(LP_ERR, "failed to open rcsfile '%s'", fpath);
1.1 niallo 161: exit(1);
162: }
1.17 niallo 163: /*
164: * If rev is not specified on the command line,
165: * assume HEAD.
166: */
167: frev = file->rf_head;
168: /*
169: * If revision passed on command line is less than HEAD, bail.
170: */
171: if ((newrev != NULL) && (rcsnum_cmp(newrev, frev, 0) > 0)) {
172: cvs_log(LP_ERR, "revision is too low!");
173: status = 1;
174: rcs_close(file);
175: continue;
176: }
1.4 niallo 177:
178: /*
179: * Load file contents
180: */
181: if ((bp = cvs_buf_load(argv[i], BUF_AUTOEXT)) == NULL) {
182: cvs_log(LP_ERR, "failed to load '%s'", argv[i]);
183: exit(1);
184: }
185:
1.12 niallo 186: if (cvs_buf_putc(bp, '\0') < 0)
187: exit(1);
188:
1.23 ! niallo 189: filec = (char *)cvs_buf_release(bp);
1.13 joris 190:
1.6 niallo 191: /*
1.16 niallo 192: * Check for a lock belonging to this user. If none,
193: * abort check-in.
194: */
195: if (TAILQ_EMPTY(&(file->rf_locks))) {
196: cvs_log(LP_ERR, "%s: no lock set by %s", fpath,
197: username);
198: status = 1;
199: continue;
200: } else {
201: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
202: if ((strcmp(lkp->rl_name, username) != 0)
203: && (rcsnum_cmp(lkp->rl_num, frev, 0))) {
204: cvs_log(LP_ERR,
205: "%s: no lock set by %s", fpath,
206: username);
207: status = 1;
208: continue;
209: }
210: }
211: }
212:
213: /*
1.6 niallo 214: * If no log message specified, get it interactively.
215: */
1.14 joris 216: if (rcs_msg == NULL)
1.12 niallo 217: rcs_msg = checkin_getlogmsg(fpath, argv[i], frev, newrev);
1.4 niallo 218:
219: /*
220: * Remove the lock
221: */
222: if (rcs_lock_remove(file, frev) < 0) {
223: if (rcs_errno != RCS_ERR_NOENT)
224: cvs_log(LP_WARN, "failed to remove lock");
225: }
226:
227: /*
228: * Get RCS patch
229: */
230: if ((deltatext = checkin_diff_file(file, frev, argv[i])) == NULL) {
231: cvs_log(LP_ERR, "failed to get diff");
232: exit(1);
233: }
234:
235: /*
236: * Current head revision gets the RCS patch as rd_text
237: */
1.12 niallo 238: if (rcs_deltatext_set(file, frev, deltatext) == -1) {
1.7 niallo 239: cvs_log(LP_ERR,
240: "failed to set new rd_text for head rev");
1.4 niallo 241: exit (1);
242: }
243: /*
244: * Now add our new revision
245: */
1.12 niallo 246: if (rcs_rev_add(file, (newrev == NULL ? RCS_HEAD_REV : newrev),
1.20 niallo 247: rcs_msg, date) != 0) {
1.4 niallo 248: cvs_log(LP_ERR, "failed to add new revision");
249: exit(1);
250: }
251:
252: /*
1.12 niallo 253: * If we are checking in to a non-default (ie user-specified)
254: * revision, set head to this revision.
255: */
256: if (newrev != NULL)
257: rcs_head_set(file, newrev);
1.15 niallo 258: else
259: newrev = file->rf_head;
1.12 niallo 260: /*
1.4 niallo 261: * New head revision has to contain entire file;
262: */
263: if (rcs_deltatext_set(file, frev, filec) == -1) {
264: cvs_log(LP_ERR, "failed to set new head revision");
265: exit(1);
266: }
267:
268: free(deltatext);
269: free(filec);
1.9 niallo 270: (void)unlink(argv[i]);
1.4 niallo 271:
1.9 niallo 272: /*
273: * Do checkout if -u or -l are specified.
274: */
1.12 niallo 275: if (lkmode != 0 && !rflag) {
1.9 niallo 276: mode_t mode = 0;
1.12 niallo 277: if ((bp = rcs_getrev(file, newrev)) == NULL) {
1.9 niallo 278: cvs_log(LP_ERR, "cannot get revision");
279: goto err;
280: }
281: if (lkmode == LOCK_LOCK) {
282: mode = 0644;
1.12 niallo 283: if (rcs_lock_add(file, username, newrev) < 0) {
1.9 niallo 284: if (rcs_errno != RCS_ERR_DUPENT)
285: cvs_log(LP_ERR,
286: "failed to lock revision");
287: else
288: cvs_log(LP_ERR,
289: "you already have a lock");
290: }
291: } else if (lkmode == LOCK_UNLOCK) {
292: mode = 0444;
293: }
294: if (cvs_buf_write(bp, argv[i], mode) < 0) {
295: cvs_log(LP_ERR,
296: "failed to write revision to file");
297: }
298: cvs_buf_free(bp);
299: }
300: err:
1.4 niallo 301: /* File will NOW be synced */
1.1 niallo 302: rcs_close(file);
1.4 niallo 303:
1.6 niallo 304: if (interactive) {
305: free(rcs_msg);
306: rcs_msg = NULL;
307: }
1.4 niallo 308: }
309:
1.16 niallo 310: return (status);
1.4 niallo 311: }
312:
313: static char *
314: checkin_diff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
315: {
316: char path1[MAXPATHLEN], path2[MAXPATHLEN];
317: BUF *b1, *b2, *b3;
318: char rbuf[64], *deltatext;
319:
320: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
321:
322: if ((b1 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
323: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
324: return (NULL);
1.1 niallo 325: }
326:
1.4 niallo 327: if ((b2 = rcs_getrev(rfp, rev)) == NULL) {
328: cvs_log(LP_ERR, "failed to load revision");
329: cvs_buf_free(b1);
330: return (NULL);
331: }
332:
333: if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL) {
334: cvs_log(LP_ERR, "failed to allocated buffer for diff");
335: cvs_buf_free(b1);
336: cvs_buf_free(b2);
337: return (NULL);
338: }
339:
340: strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
341: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
342: cvs_log(LP_ERRNO, "could not write temporary file");
343: cvs_buf_free(b1);
344: cvs_buf_free(b2);
345: return (NULL);
346: }
347: cvs_buf_free(b1);
348:
349: strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
350: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
351: cvs_buf_free(b2);
352: (void)unlink(path1);
353: return (NULL);
354: }
355: cvs_buf_free(b2);
356:
1.5 niallo 357: diff_format = D_RCSDIFF;
1.4 niallo 358: cvs_diffreg(path1, path2, b3);
359: (void)unlink(path1);
360: (void)unlink(path2);
361:
362: cvs_buf_putc(b3, '\0');
363: deltatext = (char *)cvs_buf_release(b3);
364:
365: return (deltatext);
1.6 niallo 366: }
367:
368: /*
369: * Get log message from user interactively.
370: */
371: static char *
1.12 niallo 372: checkin_getlogmsg(char *rcsfile, char *workingfile, RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 373: {
374: char *rcs_msg, buf[128], nrev[16], prev[16];
375: BUF *logbuf;
376: RCSNUM *tmprev;
377:
1.7 niallo 378: rcs_msg = NULL;
1.6 niallo 379: tmprev = rcsnum_alloc();
380: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 381: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 382: if (rev2 == NULL)
383: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
384: else
385: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 386: rcsnum_free(tmprev);
387:
388: if ((logbuf = cvs_buf_alloc(64, BUF_AUTOEXT)) == NULL) {
1.7 niallo 389: cvs_log(LP_ERR, "failed to allocate log buffer");
1.6 niallo 390: return (NULL);
391: }
392: cvs_printf("%s <-- %s\n", rcsfile, workingfile);
1.7 niallo 393: cvs_printf("new revision: %s; previous revision: %s\n", nrev, prev);
1.6 niallo 394: cvs_printf("enter log message, terminated with single "
395: "'.' or end of file:\n");
396: cvs_printf(">> ");
397: for (;;) {
398: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 399: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 400: break;
401: cvs_buf_append(logbuf, buf, strlen(buf));
402: cvs_printf(">> ");
403: }
1.8 niallo 404: cvs_buf_putc(logbuf, '\0');
1.6 niallo 405: rcs_msg = (char *)cvs_buf_release(logbuf);
406: return (rcs_msg);
1.1 niallo 407: }