Annotation of src/usr.bin/cvs/init.c, Revision 1.33
1.33 ! tobias 1: /* $OpenBSD: init.c,v 1.32 2007/02/22 06:42:09 otto Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.23 xsa 4: * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
1.8 tedu 5: * All rights reserved.
1.1 jfb 6: *
1.8 tedu 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
1.1 jfb 10: *
1.8 tedu 11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
1.1 jfb 13: * 2. The name of the author may not be used to endorse or promote products
1.8 tedu 14: * derived from this software without specific prior written permission.
1.1 jfb 15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.8 tedu 25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 26: */
27:
1.32 otto 28: #include <sys/stat.h>
29:
30: #include <errno.h>
31: #include <fcntl.h>
32: #include <string.h>
33: #include <unistd.h>
1.1 jfb 34:
35: #include "cvs.h"
1.23 xsa 36: #include "init.h"
1.26 xsa 37: #include "remote.h"
1.1 jfb 38:
1.23 xsa 39: void cvs_init_local(void);
1.1 jfb 40:
1.23 xsa 41: static void init_mkdir(const char *, mode_t);
42: static void init_mkfile(char *, const char *const *);
1.1 jfb 43:
1.23 xsa 44: struct cvsroot_file {
45: char *cf_path;
46: const char *const *cf_content;
47: };
48:
49: static const struct cvsroot_file cvsroot_files[] = {
50: { CVS_PATH_CHECKOUTLIST, NULL },
51: { CVS_PATH_COMMITINFO, NULL },
52: { CVS_PATH_CONFIG, config_contents },
53: { CVS_PATH_CVSWRAPPERS, NULL },
54: { CVS_PATH_EDITINFO, NULL },
55: { CVS_PATH_HISTORY, NULL },
56: { CVS_PATH_LOGINFO, NULL },
57: { CVS_PATH_MODULES, NULL },
58: { CVS_PATH_NOTIFY_R, NULL },
59: { CVS_PATH_RCSINFO, NULL },
60: { CVS_PATH_TAGINFO, NULL },
61: { CVS_PATH_VALTAGS, NULL },
62: { CVS_PATH_VERIFYMSG, NULL }
63: };
1.1 jfb 64:
1.23 xsa 65: static const char *cvsroot_dirs[2] = {
66: CVS_PATH_ROOT, CVS_PATH_EMPTYDIR
1.1 jfb 67: };
68:
1.23 xsa 69: #define INIT_NFILES (sizeof(cvsroot_files)/sizeof(cvsroot_files[0]))
70: #define INIT_NDIRS (sizeof(cvsroot_dirs)/sizeof(cvsroot_dirs[0]))
1.12 joris 71:
1.16 jfb 72: struct cvs_cmd cvs_cmd_init = {
1.25 joris 73: CVS_OP_INIT, 0, "init",
1.16 jfb 74: { },
75: "Create a CVS repository if it doesn't exist",
76: "",
77: "",
1.12 joris 78: NULL,
1.23 xsa 79: cvs_init
1.12 joris 80: };
1.1 jfb 81:
1.23 xsa 82: int
83: cvs_init(int argc, char **argv)
1.18 xsa 84: {
1.23 xsa 85: if (argc > 1)
1.24 joris 86: fatal("init does not take any extra arguments");
1.23 xsa 87:
1.26 xsa 88: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.27 joris 89: cvs_client_connect_to_server();
1.26 xsa 90: cvs_client_send_request("init %s", current_cvsroot->cr_dir);
91: cvs_client_get_responses();
92: } else
1.23 xsa 93: cvs_init_local();
1.18 xsa 94:
95: return (0);
96: }
97:
1.23 xsa 98: void
99: cvs_init_local(void)
100: {
101: u_int i;
1.29 otto 102: char path[MAXPATHLEN];
1.23 xsa 103:
104: cvs_log(LP_TRACE, "cvs_init_local()");
105:
106: /* Create repository root directory if it does not already exist */
107: init_mkdir(current_cvsroot->cr_dir, 0777);
108:
109: for (i = 0; i < INIT_NDIRS; i++) {
1.31 xsa 110: (void)xsnprintf(path, MAXPATHLEN, "%s/%s",
111: current_cvsroot->cr_dir, cvsroot_dirs[i]);
1.23 xsa 112:
113: init_mkdir(path, 0777);
114: }
115:
116: for (i = 0; i < INIT_NFILES; i++) {
1.31 xsa 117: (void)xsnprintf(path, MAXPATHLEN, "%s/%s",
118: current_cvsroot->cr_dir, cvsroot_files[i].cf_path);
1.23 xsa 119:
120: init_mkfile(path, cvsroot_files[i].cf_content);
121: }
122: }
123:
1.21 xsa 124: static void
1.23 xsa 125: init_mkdir(const char *path, mode_t mode)
1.1 jfb 126: {
1.18 xsa 127: struct stat st;
1.4 jfb 128:
1.23 xsa 129: if (mkdir(path, mode) == -1) {
130: if (!(errno == EEXIST ||
131: (errno == EACCES && (stat(path, &st) == 0) &&
132: S_ISDIR(st.st_mode)))) {
133: fatal("init_mkdir: mkdir: `%s': %s",
134: path, strerror(errno));
1.18 xsa 135: }
136: }
1.23 xsa 137: }
138:
139: static void
140: init_mkfile(char *path, const char *const *content)
141: {
142: BUF *b;
143: size_t len;
144: int fd, openflags, rcsflags;
1.30 xsa 145: char rpath[MAXPATHLEN];
1.23 xsa 146: const char *const *p;
147: RCSFILE *file;
148:
149: len = 0;
150: fd = -1;
151: openflags = O_WRONLY|O_CREAT|O_EXCL;
152: rcsflags = RCS_RDWR|RCS_CREATE;
153:
154: if ((fd = open(path, openflags, 0444)) == -1)
155: fatal("init_mkfile: open: `%s': %s", path, strerror(errno));
156:
157: if (content != NULL) {
158: for (p = content; *p != NULL; ++p) {
159: len = strlen(*p);
1.33 ! tobias 160: b = cvs_buf_alloc(len);
1.18 xsa 161:
1.33 ! tobias 162: cvs_buf_append(b, *p, strlen(*p));
1.1 jfb 163:
1.23 xsa 164: if (cvs_buf_write_fd(b, fd) < 0)
165: fatal("init_mkfile: cvs_buf_write_fd");
166:
167: cvs_buf_free(b);
1.1 jfb 168: }
169: }
1.23 xsa 170:
171: /*
172: * Make sure history and val-tags files are world-writable.
173: * Every user should be able to write to them.
174: */
175: if (strcmp(strrchr(CVS_PATH_HISTORY, '/'), strrchr(path, '/')) == 0 ||
176: strcmp(strrchr(CVS_PATH_VALTAGS, '/'), strrchr(path, '/')) == 0) {
177: (void)fchmod(fd, 0666);
178: goto out;
179: }
180:
1.30 xsa 181: (void)xsnprintf(rpath, MAXPATHLEN, "%s%s", path, RCS_FILE_EXT);
1.23 xsa 182:
183: if ((file = rcs_open(rpath, fd, rcsflags, 0444)) == NULL)
184: fatal("failed to create RCS file for `%s'", path);
185:
1.33 ! tobias 186: b = cvs_buf_load(path);
1.23 xsa 187:
188: if (rcs_rev_add(file, RCS_HEAD_REV, "initial checkin", -1, NULL) == -1)
189: fatal("init_mkfile: failed to add new revision");
190:
1.28 joris 191: /* b buffer is free'd in rcs_deltatext_set */
192: if (rcs_deltatext_set(file, file->rf_head, b) == -1)
1.23 xsa 193: fatal("init_mkfile: failed to set delta");
194:
195: file->rf_flags &= ~RCS_SYNCED;
196: rcs_close(file);
197: out:
198: (void)close(fd);
1.1 jfb 199: }