summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/import.c
blob: 47d34231ef0a263b62efb2decd56a68d4332443c (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*	$OpenBSD: import.c,v 1.39 2006/02/08 18:42:41 joris Exp $	*/
/*
 * Copyright (c) 2004 Joris Vink <joris@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 "includes.h"

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


#define CVS_IMPORT_DEFBRANCH	"1.1.1"


static int	cvs_import_init(struct cvs_cmd *, int, char **, int *);
static int	cvs_import_pre_exec(struct cvsroot *);
static int	cvs_import_pre_exec(struct cvsroot *);
static int	cvs_import_post_exec(struct cvsroot *);
static int	cvs_import_remote(CVSFILE *, void *);
static int	cvs_import_local(CVSFILE *, void *);
static int	cvs_import_cleanup(void);

static int dflag = 0;
static int conflicts = 0;
static RCSNUM *imp_brnum;

static char *module, *vendor, *release;

struct cvs_cmd cvs_cmd_import = {
	CVS_OP_IMPORT, CVS_REQ_IMPORT, "import",
	{ "im", "imp" },
	"Import sources into CVS, using vendor branches",
	"[-d] [-b branch] [-I ign] [-k mode] [-m msg] [-W spec] module "
	"vendortag releasetag ...",
	"b:dI:k:m:W:",
	NULL,
	CF_RECURSE | CF_IGNORE | CF_NOSYMS,
	cvs_import_init,
	cvs_import_pre_exec,
	cvs_import_remote,
	cvs_import_local,
	cvs_import_post_exec,
	cvs_import_cleanup,
	CVS_CMD_SENDDIR
};

static int
cvs_import_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 'b':
			if ((imp_brnum = rcsnum_parse(optarg)) == NULL) {
				cvs_log(LP_ERR, "%s is not a numeric branch",
				    optarg);
				return (CVS_EX_USAGE);
			}
			break;
		case 'd':
			dflag = 1;
			break;
		case 'I':
			if (cvs_file_ignore(optarg) < 0) {
				cvs_log(LP_ERR, "failed to add `%s' to list "
				    "of ignore patterns", optarg);
				return (CVS_EX_USAGE);
			}
			break;
		case 'k':
			break;
		case 'm':
			cvs_msg = xstrdup(optarg);
			break;
		default:
			return (CVS_EX_USAGE);
		}
	}

	argc -= optind;
	argv += optind;
	if (argc != 3)
		return (CVS_EX_USAGE);

	if ((imp_brnum == NULL) &&
	    ((imp_brnum = rcsnum_parse(CVS_IMPORT_DEFBRANCH)) == NULL))
		fatal("cvs_import_init: rcsnum_parse failed");

	module = argv[0];
	vendor = argv[1];
	release = argv[2];

	*arg = optind + 3;

	if (cvs_msg == NULL)
		cvs_msg = cvs_logmsg_get(NULL, NULL, NULL, NULL);

	return (0);
}

static int
cvs_import_pre_exec(struct cvsroot *root)
{
	char numbuf[64], repodir[MAXPATHLEN];

	if (root->cr_method == CVS_METHOD_LOCAL) {
		if (cvs_path_cat(root->cr_dir, module, repodir,
		    sizeof(repodir)) >= sizeof(repodir))
			fatal("cvs_import_pre_exec: cvs_path_cat overflow");

		if (mkdir(repodir, 0700) == -1)
			fatal("cvs_import_pre_exec: mkdir `%s': %s",
			    repodir, strerror(errno));
	} else {
		rcsnum_tostr(imp_brnum, numbuf, sizeof(numbuf));

		cvs_sendarg(root, "-b", 0);
		cvs_sendarg(root, numbuf, 0);
		cvs_logmsg_send(root, cvs_msg);
		cvs_sendarg(root, module, 0);
		cvs_sendarg(root, vendor, 0);
		cvs_sendarg(root, release, 0);
	}

	return (0);
}

static int
cvs_import_post_exec(struct cvsroot *root)
{
	char buf[8];

	if (root->cr_method == CVS_METHOD_LOCAL) {
		if (conflicts > 0)
			snprintf(buf, sizeof(buf), "%d", conflicts);

		if (verbosity > 0)
			cvs_printf("\n%s conflicts created by this import\n\n",
			    conflicts == 0 ? "No" : buf);
	}

	return (CVS_EX_OK);
}

/*
 * cvs_import_remote()
 *
 * Perform the import of a single file or directory.
 */
static int
cvs_import_remote(CVSFILE *cf, void *arg)
{
	size_t sz;
	struct cvsroot *root;
	char fpath[MAXPATHLEN], repodir[MAXPATHLEN];
	char repo[MAXPATHLEN], date[32];

	root = CVS_DIR_ROOT(cf);

	if (cvs_path_cat(root->cr_dir, module, repo, sizeof(repo)) >=
	    sizeof(repo))
		fatal("cvs_import_remove: cvs_path_cat overflow");

	cvs_file_getpath(cf, fpath, sizeof(fpath));

	if (cf->cf_type == DT_DIR) {
		if (!strcmp(cf->cf_name, "."))
			strlcpy(repodir, repo, sizeof(repodir));
		else {
			if(cvs_path_cat(repo, fpath, repodir,
			    sizeof(repodir)) >= sizeof(repodir))
				fatal("cvs_import_remove: cvs_path_cat overflow");
		}

		cvs_sendreq(root, CVS_REQ_DIRECTORY, fpath);
		cvs_sendln(root, repodir);
		return (0);
	}

	if (dflag == 1) {
		ctime_r(&(cf->cf_mtime), date);
		sz = strlen(date);
		if ((sz > 0) && (date[sz - 1] == '\n'))
			date[--sz] = '\0';
		cvs_sendreq(root, CVS_REQ_CHECKINTIME, date);
	}

	cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
	cvs_sendfile(root, fpath);

	return (0);
}

static int
cvs_import_local(CVSFILE *cf, void *arg)
{
	time_t stamp;
	char *fcont;
	char fpath[MAXPATHLEN], rpath[MAXPATHLEN], repo[MAXPATHLEN];
	const char *comment;
	struct stat fst;
	struct timeval ts[2];
	struct cvsroot *root;
	RCSFILE *rf;
	RCSNUM *rev;
	BUF *bp;

	root = CVS_DIR_ROOT(cf);

	if (cvs_path_cat(root->cr_dir, module, repo, sizeof(repo)) >=
	    sizeof(repo))
		fatal("cvs_import_local: cvs_path_cat overflow");

	cvs_file_getpath(cf, fpath, sizeof(fpath));

	if (cf->cf_type == DT_DIR) {
		if (!strcmp(cf->cf_name, "."))
			strlcpy(rpath, repo, sizeof(rpath));
		else {
			if (cvs_path_cat(repo, fpath, rpath,
			    sizeof(rpath)) >= sizeof(rpath))
				fatal("cvs_import_local: cvs_path_cat overflow");

			cvs_printf("Importing %s\n", rpath);
			if (mkdir(rpath, 0700) == -1) {
				cvs_log(LP_ERRNO, "failed to create %s",
				    rpath);
			}
		}

		return (0);
	}

	/*
	 * If -d was given, use the file's last modification time as the
	 * timestamps for the initial revisions.
	 */
	if (dflag == 1) {
		if (stat(fpath, &fst) == -1)
			fatal("cvs_import_local: stat failed on `%s': %s",
			    fpath, strerror(errno));

		stamp = (time_t)fst.st_mtime;

		ts[0].tv_sec = stamp;
		ts[0].tv_usec = 0;
		ts[1].tv_sec = stamp;
		ts[1].tv_usec = 0;
	} else
		stamp = -1;

	if (strlcpy(rpath, repo, sizeof(rpath)) >= sizeof(rpath) ||
	    strlcat(rpath, "/", sizeof(rpath)) >= sizeof(rpath) ||
	    strlcat(rpath, fpath, sizeof(rpath)) >= sizeof(rpath) ||
	    strlcat(rpath, RCS_FILE_EXT, sizeof(rpath)) >= sizeof(rpath))
		fatal("cvs_import_local: path truncation");

	cvs_printf("N %s\n", fpath);

	if ((rf = rcs_open(rpath, RCS_RDWR|RCS_CREATE)) == NULL)
		fatal("cvs_import_local: rcs_open: `%s': %s", rpath,
		    rcs_errstr(rcs_errno));

	comment = rcs_comment_lookup(cf->cf_name);
	if ((comment != NULL) && (rcs_comment_set(rf, comment) < 0)) {
		cvs_log(LP_WARN, "failed to set RCS comment leader: %s",
		    rcs_errstr(rcs_errno));
		/* don't error out, no big deal */
	}

	rev = rcsnum_brtorev(imp_brnum);
	if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
		(void)unlink(rpath);
		fatal("cvs_import_local: rcs_rev_add failed: %s",
		    rcs_errstr(rcs_errno));
	}

	if (rcs_sym_add(rf, release, rev) < 0) {
		(void)unlink(rpath);
		fatal("cvs_import_local: rcs_sym_add failed: %s",
		    rcs_errstr(rcs_errno));
	}

	rcsnum_cpy(imp_brnum, rev, 2);
	if (rcs_rev_add(rf, rev, cvs_msg, stamp, NULL) < 0) {
		(void)unlink(rpath);
		fatal("cvs_import_local: rcs_rev_add failed: %s",
		    rcs_errstr(rcs_errno));
	}

	if (rcs_head_set(rf, rev) < 0) {
		(void)unlink(rpath);
		fatal("cvs_import_local: rcs_head_set failed: %s",
		    rcs_errstr(rcs_errno));
	}

	if (rcs_branch_set(rf, imp_brnum) < 0) {
		(void)unlink(rpath);
		fatal("cvs_import_local: rcs_branch_set failed: %s",
		    rcs_errstr(rcs_errno));
	}

	if (rcs_sym_add(rf, vendor, imp_brnum) < 0) {
		(void)unlink(rpath);
		fatal("cvs_import_local: rcs_sym_add failed: %s",
		    rcs_errstr(rcs_errno));
	}

	if ((bp = cvs_buf_load(fpath, BUF_AUTOEXT)) == NULL) {
		(void)unlink(rpath);
		fatal("cvs_import_local: cvs_buf_load failed");
	}

	cvs_buf_putc(bp, '\0');

	fcont = cvs_buf_release(bp);

	if (rcs_deltatext_set(rf, rev, fcont) < 0) {
		(void)unlink(rpath);
		fatal("cvs_import_local: rcs_deltatext_set failed");
	}

	/* add the vendor tag and release tag as symbols */
	rcs_close(rf);

	if ((dflag ==1) && (utimes(rpath, ts) == -1))
		cvs_log(LP_ERRNO, "failed to timestamp RCS file");

	return (0);
}

static int
cvs_import_cleanup(void)
{
	if (imp_brnum != NULL)
		rcsnum_free(imp_brnum);
	return (0);
}