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