Annotation of src/usr.bin/cvs/release.c, Revision 1.24
1.24 ! xsa 1: /* $OpenBSD: release.c,v 1.23 2005/10/07 21:47:32 reyk 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.18 xsa 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 *);
1.7 jfb 49:
50: struct cvs_cmd cvs_cmd_release = {
51: CVS_OP_RELEASE, CVS_REQ_RELEASE, "release",
1.20 xsa 52: { "re", "rel" },
1.7 jfb 53: "Release",
54: "[-d]",
55: "d",
56: NULL,
1.22 xsa 57: CF_NOFILES,
1.7 jfb 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.23 reyk 118: fflush(stderr);
119: fflush(stdout);
1.1 xsa 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.4 xsa 141: char *wdir, cwd[MAXPATHLEN];
1.22 xsa 142: char buf[256], dpath[MAXPATHLEN], updcmd[1024];
1.1 xsa 143: struct stat st;
144: struct cvsroot *root;
145:
1.22 xsa 146: j = 0;
1.1 xsa 147:
1.15 xsa 148: root = CVS_DIR_ROOT(cf);
1.1 xsa 149:
1.22 xsa 150: /* XXX kept for compat reason of `cvs update' output */
151: /* save current working directory for further use */
152: if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL) {
153: cvs_log(LP_ERRNO, "cannot get current dir");
154: return (CVS_EX_FILE);
155: }
156:
1.15 xsa 157: cvs_file_getpath(cf, dpath, sizeof(dpath));
1.1 xsa 158:
1.15 xsa 159: if (cf->cf_type == DT_DIR) {
1.16 joris 160: if (!strcmp(cf->cf_name, "."))
1.1 xsa 161: return (0);
162:
1.22 xsa 163: /* chdir before running the `cvs update' command */
1.24 ! xsa 164: cvs_chdir(dpath);
1.22 xsa 165:
166: /* test if dir has CVS/ directory */
167: if (stat(CVS_PATH_CVSDIR, &st) == -1) {
168: if (verbosity > 0)
169: cvs_log(LP_ERR,
170: "no repository directory: %s", dpath);
171: return (0);
1.1 xsa 172: }
173: } else {
1.14 xsa 174: if (verbosity > 0)
175: cvs_log(LP_ERR, "no such directory: %s", dpath);
1.22 xsa 176: return (0);
177: }
178:
179: /* construct `cvs update' command */
180: l = snprintf(updcmd, sizeof(updcmd), "%s %s %s update",
181: __progname, UPDCMD_FLAGS, root->cr_str);
182: if (l == -1 || l >= (int)sizeof(updcmd))
183: return (CVS_EX_DATA);
184:
185: /* XXX we should try to avoid a new connection ... */
186: cvs_log(LP_TRACE, "cvs_release_dir() popen(%s,r)", updcmd);
187: if ((fp = popen(updcmd, "r")) == NULL) {
188: cvs_log(LP_ERR, "cannot run command `%s'", updcmd);
1.6 joris 189: return (CVS_EX_DATA);
1.22 xsa 190: }
191:
192: while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
193: if (strchr("ACMPRU", buf[0]))
194: j++;
195: (void)fputs(buf, stdout);
196: }
197:
198: if (pclose(fp) != 0) {
199: cvs_log(LP_ERR, "unable to release `%s'", dpath);
200:
201: /* change back to original working dir */
1.24 ! xsa 202: cvs_chdir(wdir);
1.22 xsa 203: }
204:
205: printf("You have [%d] altered file%s in this repository.\n",
206: j, j > 1 ? "s" : "");
207: while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
208: if (strchr("ACMPRU", buf[0]))
209: j++;
210: (void)fputs(buf, stdout);
211: }
212:
213: printf("Are you sure you want to release %sdirectory `%s': ",
214: dflag ? "(and delete) " : "", dpath);
215:
216: if (cvs_release_yesno() == -1) { /* No */
217: fprintf(stderr,
218: "** `%s' aborted by user choice.\n", cvs_command);
219:
220: /* change back to original working dir */
1.24 ! xsa 221: cvs_chdir(wdir);
1.22 xsa 222:
223: return (-1);
224: }
225:
226: /* change back to original working dir */
1.24 ! xsa 227: cvs_chdir(wdir);
1.22 xsa 228:
229: if (dflag == 1) {
230: if (cvs_rmdir(dpath) != 0)
231: return (CVS_EX_FILE);
1.1 xsa 232: }
233:
234: return (0);
235: }