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