Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.54
1.54 ! ray 1: /* $OpenBSD: logmsg.c,v 1.53 2010/07/23 08:31:19 ray Exp $ */
1.1 jfb 2: /*
1.30 joris 3: * Copyright (c) 2007 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.30 joris 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.38 otto 18: #include <sys/stat.h>
1.39 xsa 19: #include <sys/types.h>
20: #include <sys/wait.h>
1.38 otto 21:
22: #include <errno.h>
23: #include <fcntl.h>
1.49 tobias 24: #include <libgen.h>
1.39 xsa 25: #include <paths.h>
26: #include <signal.h>
1.45 chl 27: #include <stdlib.h>
1.38 otto 28: #include <string.h>
29: #include <unistd.h>
1.1 jfb 30:
31: #include "cvs.h"
32:
1.30 joris 33: #define CVS_LOGMSG_PREFIX "CVS:"
34: #define CVS_LOGMSG_LINE \
1.1 jfb 35: "----------------------------------------------------------------------"
36:
1.39 xsa 37: int cvs_logmsg_edit(const char *);
38:
1.17 xsa 39: char *
1.30 joris 40: cvs_logmsg_read(const char *path)
1.1 jfb 41: {
1.30 joris 42: int fd;
43: BUF *bp;
44: FILE *fp;
1.1 jfb 45: size_t len;
46: struct stat st;
1.30 joris 47: char *buf, *lbuf;
48:
49: if ((fd = open(path, O_RDONLY)) == -1)
50: fatal("cvs_logmsg_read: open %s", strerror(errno));
1.1 jfb 51:
1.30 joris 52: if (fstat(fd, &st) == -1)
53: fatal("cvs_logmsg_read: fstat %s", strerror(errno));
1.1 jfb 54:
1.24 xsa 55: if (!S_ISREG(st.st_mode))
1.30 joris 56: fatal("cvs_logmsg_read: file is not a regular file");
1.1 jfb 57:
1.30 joris 58: if ((fp = fdopen(fd, "r")) == NULL)
59: fatal("cvs_logmsg_read: fdopen %s", strerror(errno));
1.47 tobias 60:
61: if (st.st_size > SIZE_MAX)
1.48 tobias 62: fatal("cvs_logmsg_read: %s: file size too big", path);
1.1 jfb 63:
1.30 joris 64: lbuf = NULL;
1.54 ! ray 65: bp = buf_alloc(st.st_size);
1.30 joris 66: while ((buf = fgetln(fp, &len))) {
67: if (buf[len - 1] == '\n') {
68: buf[len - 1] = '\0';
1.51 ray 69: --len;
1.30 joris 70: } else {
71: lbuf = xmalloc(len + 1);
1.36 otto 72: memcpy(lbuf, buf, len);
73: lbuf[len] = '\0';
1.30 joris 74: buf = lbuf;
75: }
76:
77: if (!strncmp(buf, CVS_LOGMSG_PREFIX,
1.44 tobias 78: sizeof(CVS_LOGMSG_PREFIX) - 1))
1.1 jfb 79: continue;
80:
1.54 ! ray 81: buf_append(bp, buf, len);
! 82: buf_putc(bp, '\n');
1.30 joris 83: }
84:
85: if (lbuf != NULL)
86: xfree(lbuf);
1.1 jfb 87:
1.11 jfb 88: (void)fclose(fp);
89:
1.54 ! ray 90: buf_putc(bp, '\0');
! 91: return (buf_release(bp));
1.1 jfb 92: }
93:
1.17 xsa 94: char *
1.49 tobias 95: cvs_logmsg_create(char *dir, struct cvs_flisthead *added,
96: struct cvs_flisthead *removed, struct cvs_flisthead *modified)
1.1 jfb 97: {
1.50 joris 98: FILE *fp, *rp;
99: int c, fd, rd, saved_errno;
1.30 joris 100: struct cvs_filelist *cf;
1.1 jfb 101: struct stat st1, st2;
1.50 joris 102: char *fpath, *logmsg, repo[MAXPATHLEN];
103: struct stat st;
104: struct trigger_list *line_list;
105: struct trigger_line *line;
1.49 tobias 106: static int reuse = 0;
107: static char *prevmsg = NULL;
1.8 jfb 108:
1.49 tobias 109: if (reuse)
110: return xstrdup(prevmsg);
111:
1.35 xsa 112: (void)xasprintf(&fpath, "%s/cvsXXXXXXXXXX", cvs_tmpdir);
1.1 jfb 113:
1.45 chl 114: if ((fd = mkstemp(fpath)) == -1)
1.30 joris 115: fatal("cvs_logmsg_create: mkstemp %s", strerror(errno));
1.33 joris 116:
1.53 ray 117: worklist_add(fpath, &temp_files);
1.1 jfb 118:
1.24 xsa 119: if ((fp = fdopen(fd, "w")) == NULL) {
1.34 xsa 120: saved_errno = errno;
1.30 joris 121: (void)unlink(fpath);
1.34 xsa 122: fatal("cvs_logmsg_create: fdopen %s", strerror(saved_errno));
1.8 jfb 123: }
1.1 jfb 124:
1.49 tobias 125: if (prevmsg != NULL && prevmsg[0] != '\0')
126: fprintf(fp, "%s", prevmsg);
127: else
128: fputc('\n', fp);
129:
1.50 joris 130: line_list = cvs_trigger_getlines(CVS_PATH_RCSINFO, repo);
131: if (line_list != NULL) {
132: TAILQ_FOREACH(line, line_list, flist) {
133: if ((rd = open(line->line, O_RDONLY)) == -1)
134: fatal("cvs_logmsg_create: open %s",
135: strerror(errno));
136: if (fstat(rd, &st) == -1)
137: fatal("cvs_logmsg_create: fstat %s",
138: strerror(errno));
139: if (!S_ISREG(st.st_mode))
140: fatal("cvs_logmsg_create: file is not a "
141: "regular file");
142: if ((rp = fdopen(rd, "r")) == NULL)
143: fatal("cvs_logmsg_create: fdopen %s",
144: strerror(errno));
145: if (st.st_size > SIZE_MAX)
146: fatal("cvs_logmsg_create: %s: file size "
147: "too big", line->line);
148: logmsg = xmalloc(st.st_size);
149: fread(logmsg, st.st_size, 1, rp);
150: fwrite(logmsg, st.st_size, 1, fp);
151: xfree(logmsg);
152: (void)fclose(rp);
153: }
154: cvs_trigger_freelist(line_list);
155: }
156:
1.49 tobias 157: fprintf(fp, "%s %s\n%s Enter Log. Lines beginning with `%s' are "
1.8 jfb 158: "removed automatically\n%s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE,
159: CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX);
160:
1.49 tobias 161: if (cvs_cmdop == CVS_OP_COMMIT) {
162: fprintf(fp, "%s Committing in %s\n%s\n", CVS_LOGMSG_PREFIX,
163: dir != NULL ? dir : ".", CVS_LOGMSG_PREFIX);
164: }
165:
1.52 joris 166: if (added != NULL && !RB_EMPTY(added)) {
1.30 joris 167: fprintf(fp, "%s Added Files:", CVS_LOGMSG_PREFIX);
1.52 joris 168: RB_FOREACH(cf, cvs_flisthead, added)
1.49 tobias 169: fprintf(fp, "\n%s\t%s", CVS_LOGMSG_PREFIX,
170: dir != NULL ? basename(cf->file_path) :
171: cf->file_path);
1.30 joris 172: fputs("\n", fp);
173: }
174:
1.52 joris 175: if (removed != NULL && !RB_EMPTY(removed)) {
1.30 joris 176: fprintf(fp, "%s Removed Files:", CVS_LOGMSG_PREFIX);
1.52 joris 177: RB_FOREACH(cf, cvs_flisthead, removed)
1.49 tobias 178: fprintf(fp, "\n%s\t%s", CVS_LOGMSG_PREFIX,
179: dir != NULL ? basename(cf->file_path) :
180: cf->file_path);
1.30 joris 181: fputs("\n", fp);
182: }
183:
1.52 joris 184: if (modified != NULL && !RB_EMPTY(modified)) {
1.30 joris 185: fprintf(fp, "%s Modified Files:", CVS_LOGMSG_PREFIX);
1.52 joris 186: RB_FOREACH(cf, cvs_flisthead, modified)
1.49 tobias 187: fprintf(fp, "\n%s\t%s", CVS_LOGMSG_PREFIX,
188: dir != NULL ? basename(cf->file_path) :
189: cf->file_path);
1.30 joris 190: fputs("\n", fp);
191: }
1.1 jfb 192:
1.9 jfb 193: fprintf(fp, "%s %s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE);
1.1 jfb 194: (void)fflush(fp);
195:
196: if (fstat(fd, &st1) == -1) {
1.34 xsa 197: saved_errno = errno;
1.30 joris 198: (void)unlink(fpath);
1.34 xsa 199: fatal("cvs_logmsg_create: fstat %s", strerror(saved_errno));
1.1 jfb 200: }
201:
1.30 joris 202: logmsg = NULL;
203:
1.1 jfb 204: for (;;) {
1.41 ray 205: if (cvs_logmsg_edit(fpath) == -1)
1.1 jfb 206: break;
1.8 jfb 207:
1.1 jfb 208: if (fstat(fd, &st2) == -1) {
1.34 xsa 209: saved_errno = errno;
1.30 joris 210: (void)unlink(fpath);
1.34 xsa 211: fatal("cvs_logmsg_create: fstat %s",
212: strerror(saved_errno));
1.1 jfb 213: }
214:
1.30 joris 215: if (st1.st_mtime != st2.st_mtime) {
216: logmsg = cvs_logmsg_read(fpath);
1.49 tobias 217: if (prevmsg != NULL)
218: xfree(prevmsg);
219: prevmsg = xstrdup(logmsg);
1.1 jfb 220: break;
221: }
222:
1.30 joris 223: printf("\nLog message unchanged or not specified\n"
1.49 tobias 224: "a)bort, c)ontinue, e)dit, !)reuse this message "
225: "unchanged for remaining dirs\nAction: (continue) ");
1.30 joris 226: (void)fflush(stdout);
227:
228: c = getc(stdin);
1.43 tobias 229: if (c == EOF || c == 'a') {
1.30 joris 230: fatal("Aborted by user");
231: } else if (c == '\n' || c == 'c') {
1.49 tobias 232: if (prevmsg == NULL)
233: prevmsg = xstrdup("");
234: logmsg = xstrdup(prevmsg);
1.1 jfb 235: break;
1.30 joris 236: } else if (c == 'e') {
1.1 jfb 237: continue;
1.49 tobias 238: } else if (c == '!') {
239: reuse = 1;
240: if (prevmsg == NULL)
241: prevmsg = xstrdup("");
242: logmsg = xstrdup(prevmsg);
243: break;
1.30 joris 244: } else {
245: cvs_log(LP_ERR, "invalid input");
1.1 jfb 246: continue;
247: }
248: }
249:
250: (void)fclose(fp);
1.30 joris 251: (void)unlink(fpath);
252: xfree(fpath);
1.1 jfb 253:
1.30 joris 254: return (logmsg);
1.39 xsa 255: }
256:
1.41 ray 257: /*
258: * Execute an editor on the specified pathname, which is interpreted
259: * from the shell. This means flags may be included.
260: *
261: * Returns -1 on error, or the exit value on success.
262: */
1.39 xsa 263: int
264: cvs_logmsg_edit(const char *pathname)
265: {
266: char *argp[] = {"sh", "-c", NULL, NULL}, *p;
267: sig_t sighup, sigint, sigquit;
268: pid_t pid;
1.40 ray 269: int saved_errno, st;
1.39 xsa 270:
271: (void)xasprintf(&p, "%s %s", cvs_editor, pathname);
272: argp[2] = p;
273:
274: sighup = signal(SIGHUP, SIG_IGN);
275: sigint = signal(SIGINT, SIG_IGN);
276: sigquit = signal(SIGQUIT, SIG_IGN);
1.40 ray 277: if ((pid = fork()) == -1)
278: goto fail;
1.39 xsa 279: if (pid == 0) {
280: execv(_PATH_BSHELL, argp);
281: _exit(127);
282: }
1.40 ray 283: while (waitpid(pid, &st, 0) == -1)
284: if (errno != EINTR)
285: goto fail;
1.39 xsa 286: xfree(p);
287: (void)signal(SIGHUP, sighup);
288: (void)signal(SIGINT, sigint);
289: (void)signal(SIGQUIT, sigquit);
1.40 ray 290: if (!WIFEXITED(st)) {
291: errno = EINTR;
1.39 xsa 292: return (-1);
293: }
1.40 ray 294: return (WEXITSTATUS(st));
295:
296: fail:
297: saved_errno = errno;
298: (void)signal(SIGHUP, sighup);
299: (void)signal(SIGINT, sigint);
300: (void)signal(SIGQUIT, sigquit);
301: xfree(p);
302: errno = saved_errno;
303: return (-1);
1.1 jfb 304: }
1.50 joris 305:
306: int
307: cvs_logmsg_verify(char *logmsg)
308: {
309: int fd, ret = 0;
310: char *fpath;
311: struct trigger_list *line_list;
312: struct file_info_list files_info;
313: struct file_info *fi;
314:
315: line_list = cvs_trigger_getlines(CVS_PATH_VERIFYMSG, "DEFAULT");
316: if (line_list != NULL) {
317: TAILQ_INIT(&files_info);
318:
319: (void)xasprintf(&fpath, "%s/cvsXXXXXXXXXX", cvs_tmpdir);
320: if ((fd = mkstemp(fpath)) == -1)
321: fatal("cvs_logmsg_verify: mkstemp %s", strerror(errno));
322:
323: fi = xcalloc(1, sizeof(*fi));
324: fi->file_path = xstrdup(fpath);
325: TAILQ_INSERT_TAIL(&files_info, fi, flist);
326:
327: if (cvs_trigger_handle(CVS_TRIGGER_VERIFYMSG, NULL, NULL,
328: line_list, &files_info)) {
329: cvs_log(LP_ERR, "Log message check failed");
330: ret = 1;
331: }
332:
333: cvs_trigger_freeinfo(&files_info);
334: (void)close(fd);
335: (void)unlink(fpath);
336: xfree(fpath);
337: cvs_trigger_freelist(line_list);
338: }
339:
340: return ret;
341: }
342: