Annotation of src/usr.bin/cvs/release.c, Revision 1.17
1.17 ! xsa 1: /* $OpenBSD: release.c,v 1.16 2005/07/22 16:27:29 joris Exp $ */
1.1 xsa 2: /*
3: * Copyright (c) 2005 Xavier Santolaria <xsa@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 conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions 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/types.h>
28: #include <sys/stat.h>
29:
30: #include <errno.h>
31: #include <fcntl.h>
32: #include <stdio.h>
33: #include <stdlib.h>
34: #include <string.h>
35: #include <unistd.h>
36:
37: #include "cvs.h"
38: #include "log.h"
39: #include "proto.h"
40:
1.3 xsa 41: #define UPDCMD_FLAGS "-n -q -d"
42:
43: extern char *__progname;
1.1 xsa 44:
1.7 jfb 45: static int cvs_release_init (struct cvs_cmd *, int, char **, int *);
46: static int cvs_release_pre_exec (struct cvsroot *);
47: static int cvs_release_yesno (void);
48: static int cvs_release_dir (CVSFILE *, void *);
49:
50: struct cvs_cmd cvs_cmd_release = {
51: CVS_OP_RELEASE, CVS_REQ_RELEASE, "release",
52: { },
53: "Release",
54: "[-d]",
55: "d",
56: NULL,
57: 0,
58: cvs_release_init,
59: cvs_release_pre_exec,
1.1 xsa 60: cvs_release_dir,
1.7 jfb 61: cvs_release_dir,
62: NULL,
63: NULL,
1.1 xsa 64: CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
65: };
66:
67: static int dflag; /* -d option */
68:
69: static int
1.7 jfb 70: cvs_release_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 xsa 71: {
72: int ch;
73:
1.7 jfb 74: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 xsa 75: switch (ch) {
76: case 'd':
77: dflag = 1;
78: break;
79: default:
80: return (CVS_EX_USAGE);
81: }
82: }
83:
84: argc -= optind;
85: argv += optind;
86: *arg = optind;
87:
88: if (argc == 0)
89: return (CVS_EX_USAGE);
90:
91: return (0);
92: }
93:
94: static int
1.7 jfb 95: cvs_release_pre_exec(struct cvsroot *root)
1.1 xsa 96: {
1.11 joris 97: if (root->cr_method != CVS_METHOD_LOCAL) {
98: if (dflag && cvs_sendarg(root, "-d", 0) < 0)
99: return (CVS_EX_PROTO);
100: }
1.1 xsa 101:
102: return (0);
103: }
104:
105: /*
106: * cvs_release_yesno()
107: *
108: * Read from standart input for `y' or `Y' character.
109: * Returns 0 on success, or -1 on failure.
110: */
111: static int
112: cvs_release_yesno(void)
113: {
114: int c, ret;
115:
116: ret = 0;
1.5 jfb 117:
1.1 xsa 118: fflush (stderr);
119: fflush (stdout);
120:
121: if ((c = getchar()) != 'y' && c != 'Y')
122: ret = -1;
123: else
124: while (c != EOF && c != '\n')
125: c = getchar();
126:
127: return (ret);
128: }
129:
130: /*
131: * cvs_release_dir()
132: *
133: * Release specified directorie(s).
134: * Returns 0 on success, or -1 on failure.
135: */
136: static int
1.15 xsa 137: cvs_release_dir(CVSFILE *cf, void *arg)
1.1 xsa 138: {
1.3 xsa 139: FILE *fp;
1.1 xsa 140: int j, l;
1.13 xsa 141: size_t len;
1.4 xsa 142: char *wdir, cwd[MAXPATHLEN];
1.15 xsa 143: char cdpath[MAXPATHLEN], dpath[MAXPATHLEN];
144: char buf[256], updcmd[1024];
1.1 xsa 145: struct stat st;
146: struct cvsroot *root;
147:
148: j = 0; /* number of altered files in the working copy */
149:
1.15 xsa 150: root = CVS_DIR_ROOT(cf);
1.1 xsa 151:
1.15 xsa 152: cvs_file_getpath(cf, dpath, sizeof(dpath));
1.1 xsa 153:
1.13 xsa 154: len = cvs_path_cat(dpath, CVS_PATH_CVSDIR, cdpath, sizeof(cdpath));
155: if (len >= sizeof(cdpath))
1.6 joris 156: return (CVS_EX_DATA);
1.1 xsa 157:
1.15 xsa 158: if (cf->cf_type == DT_DIR) {
1.16 joris 159: if (!strcmp(cf->cf_name, "."))
1.1 xsa 160: return (0);
161: else {
162: /* test if dir has CVS/ directory */
163: if (stat(cdpath, &st) == -1) {
1.14 xsa 164: if (verbosity > 0)
165: cvs_log(LP_ERR,
166: "no repository directory: %s",
167: dpath);
1.1 xsa 168: return (0);
169: }
170: }
171:
172: if (root->cr_method != CVS_METHOD_LOCAL) {
1.4 xsa 173: /* XXX kept for compat reason of `cvs update' output */
174: /* save current working directory for further use */
175: if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL)
1.12 joris 176: cvs_log(LP_ERRNO, "cannot get current dir");
1.4 xsa 177:
1.3 xsa 178: /* change dir before running the `cvs update' command */
1.17 ! xsa 179: if (cvs_chdir(dpath) == -1)
1.6 joris 180: return (CVS_EX_FILE);
1.3 xsa 181:
182: /* construct `cvs update' command */
183: l = snprintf(updcmd, sizeof(updcmd), "%s %s %s update",
184: __progname, UPDCMD_FLAGS, root->cr_str);
185: if (l == -1 || l >= (int)sizeof(updcmd))
1.6 joris 186: return (CVS_EX_DATA);
1.3 xsa 187:
188: /* XXX we should try to avoid a new connection ... */
189: if ((fp = popen(updcmd, "r")) == NULL) {
1.9 xsa 190: cvs_log(LP_ERR, "cannot run command `%s'",
1.3 xsa 191: updcmd);
1.6 joris 192: return (CVS_EX_DATA);
1.3 xsa 193: }
1.1 xsa 194:
1.3 xsa 195: while (fgets(buf, sizeof(buf), fp) != NULL) {
196: if (strchr("ACMPRU", buf[0]))
197: j++;
198: (void)fputs(buf, stdout);
199: }
200:
201: if (pclose(fp) != 0) {
1.9 xsa 202: cvs_log(LP_ERR, "unable to release `%s'",
1.3 xsa 203: dpath);
1.6 joris 204: return (CVS_EX_DATA);
1.3 xsa 205: }
1.1 xsa 206:
1.3 xsa 207: printf("You have [%d] altered file%s in this "
208: "repository.\n", j, j > 1 ? "s" : "");
1.4 xsa 209:
1.1 xsa 210: printf("Are you sure you want to release "
211: "%sdirectory `%s': ",
212: dflag ? "(and delete) " : "", dpath);
213:
214: if (cvs_release_yesno() == -1) { /* No */
215: (void)fprintf(stderr,
1.12 joris 216: "** `%s' aborted by user choice.\n",
1.1 xsa 217: cvs_command);
218: return (-1);
219: }
220:
1.4 xsa 221: /* change back to original working dir */
1.17 ! xsa 222: if (cvs_chdir(wdir) == -1)
1.6 joris 223: return (CVS_EX_FILE);
1.4 xsa 224:
1.1 xsa 225: if (dflag == 1) {
1.10 xsa 226: if (!cvs_noexec && cvs_remove_dir(dpath) != 0) {
1.1 xsa 227: cvs_log(LP_ERRNO,
1.12 joris 228: "deletion of directory `%s' failed",
1.1 xsa 229: dpath);
1.6 joris 230: return (CVS_EX_FILE);
1.1 xsa 231: }
232: }
233: }
234: } else {
1.14 xsa 235: if (verbosity > 0)
236: cvs_log(LP_ERR, "no such directory: %s", dpath);
1.6 joris 237: return (CVS_EX_DATA);
1.1 xsa 238: }
239:
240: return (0);
241: }