summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/release.c
blob: 9c419ba3d1afecb87e375dc307d673ca9226ce1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*	$OpenBSD: release.c,v 1.25 2005/12/24 19:07:52 xsa Exp $	*/
/*
 * Copyright (c) 2005 Xavier Santolaria <xsa@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cvs.h"
#include "log.h"
#include "proto.h"

#define UPDCMD_FLAGS	"-n -q -d"

extern char *__progname;

static int	cvs_release_init(struct cvs_cmd *, int, char **, int *);
static int	cvs_release_pre_exec(struct cvsroot *);
static int	cvs_release_yesno(void);
static int	cvs_release_dir(CVSFILE *, void *);

struct cvs_cmd cvs_cmd_release = {
	CVS_OP_RELEASE, CVS_REQ_RELEASE, "release",
	{ "re", "rel" },
	"Release",
	"[-d]",
	"d",
	NULL,
	CF_NOFILES,
	cvs_release_init,
	cvs_release_pre_exec,
	cvs_release_dir,
	cvs_release_dir,
	NULL,
	NULL,
	CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
};

static int	dflag;	/* -d option */

static int
cvs_release_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
{
	int ch;

	while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
		switch (ch) {
		case 'd':
			dflag = 1;
			break;
		default:
			return (CVS_EX_USAGE);
		}
	}

	argc -= optind;
	argv += optind;
	*arg = optind;

	if (argc == 0)
		return (CVS_EX_USAGE);

	return (0);
}

static int
cvs_release_pre_exec(struct cvsroot *root)
{
	if (root->cr_method != CVS_METHOD_LOCAL) {
		if (dflag && cvs_sendarg(root, "-d", 0) < 0)
			return (CVS_EX_PROTO);
	}

	return (0);
}

/*
 * cvs_release_yesno()
 *
 * Read from standart input for `y' or `Y' character.
 * Returns 0 on success, or -1 on failure.
 */
static int
cvs_release_yesno(void)
{
	int c, ret;

	ret = 0;

	fflush(stderr);
	fflush(stdout);

	if ((c = getchar()) != 'y' && c != 'Y')
		ret = -1;
	else
		while (c != EOF && c != '\n')
			c = getchar();

	return (ret);
}

/*
 * cvs_release_dir()
 *
 * Release specified directorie(s).
 * Returns 0 on success, or -1 on failure.
 */
static int
cvs_release_dir(CVSFILE *cf, void *arg)
{
	FILE *fp;
	int j, l;
	char *wdir, cwd[MAXPATHLEN];
	char buf[256], dpath[MAXPATHLEN], updcmd[1024];
	struct stat st;
	struct cvsroot *root;

	j = 0;

	root = CVS_DIR_ROOT(cf);

	/* XXX kept for compat reason of `cvs update' output */
	/* save current working directory for further use */
	if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL) {
		cvs_log(LP_ERRNO, "cannot get current dir");
		return (CVS_EX_FILE);
	}

	cvs_file_getpath(cf, dpath, sizeof(dpath));

	if (cf->cf_type == DT_DIR) {
		if (!strcmp(cf->cf_name, "."))
			return (0);

		/* chdir before running the `cvs update' command */
		cvs_chdir(dpath, 0);

		/* test if dir has CVS/ directory */
		if (stat(CVS_PATH_CVSDIR, &st) == -1) {
			if (verbosity > 0)
				cvs_log(LP_ERR,
				    "no repository directory: %s", dpath);
			return (0);
		}
	} else {
		if (verbosity > 0)
			cvs_log(LP_ERR, "no such directory: %s", dpath);
		return (0);
	}

	/* construct `cvs update' command */
	l = snprintf(updcmd, sizeof(updcmd), "%s %s %s update",
	    __progname, UPDCMD_FLAGS, root->cr_str);
	if (l == -1 || l >= (int)sizeof(updcmd))
		return (CVS_EX_DATA);

	/* XXX we should try to avoid a new connection ... */
	cvs_log(LP_TRACE, "cvs_release_dir() popen(%s,r)", updcmd);
	if ((fp = popen(updcmd, "r")) == NULL) {
		cvs_log(LP_ERR, "cannot run command `%s'", updcmd);
		return (CVS_EX_DATA);
	}

	while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
		if (strchr("ACMPRU", buf[0]))
			j++;
		(void)fputs(buf, stdout);
	}

	if (pclose(fp) != 0) {
		cvs_log(LP_ERR, "unable to release `%s'", dpath);

		/* change back to original working dir */
		cvs_chdir(wdir, 0);
	}

	printf("You have [%d] altered file%s in this repository.\n",
	    j, j > 1 ? "s" : "");
	while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
		if (strchr("ACMPRU", buf[0]))
			j++;
		(void)fputs(buf, stdout);
	}

	printf("Are you sure you want to release %sdirectory `%s': ",
	    dflag ? "(and delete) " : "", dpath);

	if (cvs_release_yesno() == -1) {	/* No */
		fprintf(stderr,
		    "** `%s' aborted by user choice.\n", cvs_command);

		/* change back to original working dir */
		cvs_chdir(wdir, 0);

		return (-1);
	}

	/* change back to original working dir */
	cvs_chdir(wdir, 0);

	if (dflag == 1) {
		if (cvs_rmdir(dpath) != 0)
			return (CVS_EX_FILE);
	}

	return (0);
}