Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.59
1.59 ! millert 1: /* $OpenBSD: logmsg.c,v 1.58 2016/08/16 19:00:59 tb 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.56 millert 27: #include <stdint.h>
1.45 chl 28: #include <stdlib.h>
1.38 otto 29: #include <string.h>
30: #include <unistd.h>
1.1 jfb 31:
32: #include "cvs.h"
33:
1.30 joris 34: #define CVS_LOGMSG_PREFIX "CVS:"
35: #define CVS_LOGMSG_LINE \
1.1 jfb 36: "----------------------------------------------------------------------"
37:
1.39 xsa 38: int cvs_logmsg_edit(const char *);
39:
1.17 xsa 40: char *
1.30 joris 41: cvs_logmsg_read(const char *path)
1.1 jfb 42: {
1.30 joris 43: int fd;
44: BUF *bp;
45: FILE *fp;
1.1 jfb 46: size_t len;
47: struct stat st;
1.30 joris 48: char *buf, *lbuf;
49:
50: if ((fd = open(path, O_RDONLY)) == -1)
51: fatal("cvs_logmsg_read: open %s", strerror(errno));
1.1 jfb 52:
1.30 joris 53: if (fstat(fd, &st) == -1)
54: fatal("cvs_logmsg_read: fstat %s", strerror(errno));
1.1 jfb 55:
1.24 xsa 56: if (!S_ISREG(st.st_mode))
1.30 joris 57: fatal("cvs_logmsg_read: file is not a regular file");
1.1 jfb 58:
1.30 joris 59: if ((fp = fdopen(fd, "r")) == NULL)
60: fatal("cvs_logmsg_read: fdopen %s", strerror(errno));
1.47 tobias 61:
1.59 ! millert 62: if ((uintmax_t)st.st_size > SIZE_MAX)
1.48 tobias 63: fatal("cvs_logmsg_read: %s: file size too big", path);
1.1 jfb 64:
1.30 joris 65: lbuf = NULL;
1.54 ray 66: bp = buf_alloc(st.st_size);
1.30 joris 67: while ((buf = fgetln(fp, &len))) {
68: if (buf[len - 1] == '\n') {
69: buf[len - 1] = '\0';
1.51 ray 70: --len;
1.30 joris 71: } else {
72: lbuf = xmalloc(len + 1);
1.36 otto 73: memcpy(lbuf, buf, len);
74: lbuf[len] = '\0';
1.30 joris 75: buf = lbuf;
76: }
77:
78: if (!strncmp(buf, CVS_LOGMSG_PREFIX,
1.44 tobias 79: sizeof(CVS_LOGMSG_PREFIX) - 1))
1.1 jfb 80: continue;
81:
1.54 ray 82: buf_append(bp, buf, len);
83: buf_putc(bp, '\n');
1.30 joris 84: }
85:
1.57 nicm 86: free(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.55 deraadt 102: char *fpath, *logmsg, repo[PATH_MAX];
1.50 joris 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));
1.59 ! millert 145: if ((uintmax_t)st.st_size > SIZE_MAX)
1.50 joris 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);
1.57 nicm 151: free(logmsg);
1.50 joris 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.57 nicm 217: free(prevmsg);
1.49 tobias 218: prevmsg = xstrdup(logmsg);
1.1 jfb 219: break;
220: }
221:
1.30 joris 222: printf("\nLog message unchanged or not specified\n"
1.49 tobias 223: "a)bort, c)ontinue, e)dit, !)reuse this message "
1.58 tb 224: "unchanged for remaining dirs\nAction: (abort) ");
1.30 joris 225: (void)fflush(stdout);
226:
227: c = getc(stdin);
1.58 tb 228: if (c == EOF || c == '\n' || c == 'a' || c == 'A') {
1.30 joris 229: fatal("Aborted by user");
1.58 tb 230: } else if (c == 'c' || c == 'C') {
1.49 tobias 231: if (prevmsg == NULL)
232: prevmsg = xstrdup("");
233: logmsg = xstrdup(prevmsg);
1.1 jfb 234: break;
1.58 tb 235: } else if (c == 'e' || c == 'E') {
1.1 jfb 236: continue;
1.49 tobias 237: } else if (c == '!') {
238: reuse = 1;
239: if (prevmsg == NULL)
240: prevmsg = xstrdup("");
241: logmsg = xstrdup(prevmsg);
242: break;
1.30 joris 243: } else {
244: cvs_log(LP_ERR, "invalid input");
1.1 jfb 245: continue;
246: }
247: }
248:
249: (void)fclose(fp);
1.30 joris 250: (void)unlink(fpath);
1.57 nicm 251: free(fpath);
1.1 jfb 252:
1.30 joris 253: return (logmsg);
1.39 xsa 254: }
255:
1.41 ray 256: /*
257: * Execute an editor on the specified pathname, which is interpreted
258: * from the shell. This means flags may be included.
259: *
260: * Returns -1 on error, or the exit value on success.
261: */
1.39 xsa 262: int
263: cvs_logmsg_edit(const char *pathname)
264: {
265: char *argp[] = {"sh", "-c", NULL, NULL}, *p;
266: sig_t sighup, sigint, sigquit;
267: pid_t pid;
1.40 ray 268: int saved_errno, st;
1.39 xsa 269:
270: (void)xasprintf(&p, "%s %s", cvs_editor, pathname);
271: argp[2] = p;
272:
273: sighup = signal(SIGHUP, SIG_IGN);
274: sigint = signal(SIGINT, SIG_IGN);
275: sigquit = signal(SIGQUIT, SIG_IGN);
1.40 ray 276: if ((pid = fork()) == -1)
277: goto fail;
1.39 xsa 278: if (pid == 0) {
279: execv(_PATH_BSHELL, argp);
280: _exit(127);
281: }
1.40 ray 282: while (waitpid(pid, &st, 0) == -1)
283: if (errno != EINTR)
284: goto fail;
1.57 nicm 285: free(p);
1.39 xsa 286: (void)signal(SIGHUP, sighup);
287: (void)signal(SIGINT, sigint);
288: (void)signal(SIGQUIT, sigquit);
1.40 ray 289: if (!WIFEXITED(st)) {
290: errno = EINTR;
1.39 xsa 291: return (-1);
292: }
1.40 ray 293: return (WEXITSTATUS(st));
294:
295: fail:
296: saved_errno = errno;
297: (void)signal(SIGHUP, sighup);
298: (void)signal(SIGINT, sigint);
299: (void)signal(SIGQUIT, sigquit);
1.57 nicm 300: free(p);
1.40 ray 301: errno = saved_errno;
302: return (-1);
1.1 jfb 303: }
1.50 joris 304:
305: int
306: cvs_logmsg_verify(char *logmsg)
307: {
308: int fd, ret = 0;
309: char *fpath;
310: struct trigger_list *line_list;
311: struct file_info_list files_info;
312: struct file_info *fi;
313:
314: line_list = cvs_trigger_getlines(CVS_PATH_VERIFYMSG, "DEFAULT");
315: if (line_list != NULL) {
316: TAILQ_INIT(&files_info);
317:
318: (void)xasprintf(&fpath, "%s/cvsXXXXXXXXXX", cvs_tmpdir);
319: if ((fd = mkstemp(fpath)) == -1)
320: fatal("cvs_logmsg_verify: mkstemp %s", strerror(errno));
321:
322: fi = xcalloc(1, sizeof(*fi));
323: fi->file_path = xstrdup(fpath);
324: TAILQ_INSERT_TAIL(&files_info, fi, flist);
325:
326: if (cvs_trigger_handle(CVS_TRIGGER_VERIFYMSG, NULL, NULL,
327: line_list, &files_info)) {
328: cvs_log(LP_ERR, "Log message check failed");
329: ret = 1;
330: }
331:
332: cvs_trigger_freeinfo(&files_info);
333: (void)close(fd);
334: (void)unlink(fpath);
1.57 nicm 335: free(fpath);
1.50 joris 336: cvs_trigger_freelist(line_list);
337: }
338:
339: return ret;
340: }
341: