[BACK]Return to init.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/init.c, Revision 1.29

1.29    ! otto        1: /*     $OpenBSD: init.c,v 1.28 2007/01/13 20:29:46 joris 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.20      xsa        28: #include "includes.h"
1.1       jfb        29:
                     30: #include "cvs.h"
1.23      xsa        31: #include "init.h"
1.1       jfb        32: #include "log.h"
1.26      xsa        33: #include "remote.h"
1.1       jfb        34:
1.23      xsa        35: void   cvs_init_local(void);
1.1       jfb        36:
1.23      xsa        37: static void init_mkdir(const char *, mode_t);
                     38: static void init_mkfile(char *, const char *const *);
1.1       jfb        39:
1.23      xsa        40: struct cvsroot_file {
                     41:        char                    *cf_path;
                     42:        const char *const       *cf_content;
                     43: };
                     44:
                     45: static const struct cvsroot_file cvsroot_files[] = {
                     46:        { CVS_PATH_CHECKOUTLIST,        NULL                    },
                     47:        { CVS_PATH_COMMITINFO,          NULL                    },
                     48:        { CVS_PATH_CONFIG,              config_contents         },
                     49:        { CVS_PATH_CVSWRAPPERS,         NULL                    },
                     50:        { CVS_PATH_EDITINFO,            NULL                    },
                     51:        { CVS_PATH_HISTORY,             NULL                    },
                     52:        { CVS_PATH_LOGINFO,             NULL                    },
                     53:        { CVS_PATH_MODULES,             NULL                    },
                     54:        { CVS_PATH_NOTIFY_R,            NULL                    },
                     55:        { CVS_PATH_RCSINFO,             NULL                    },
                     56:        { CVS_PATH_TAGINFO,             NULL                    },
                     57:        { CVS_PATH_VALTAGS,             NULL                    },
                     58:        { CVS_PATH_VERIFYMSG,           NULL                    }
                     59: };
1.1       jfb        60:
1.23      xsa        61: static const char *cvsroot_dirs[2] = {
                     62:        CVS_PATH_ROOT, CVS_PATH_EMPTYDIR
1.1       jfb        63: };
                     64:
1.23      xsa        65: #define INIT_NFILES    (sizeof(cvsroot_files)/sizeof(cvsroot_files[0]))
                     66: #define INIT_NDIRS     (sizeof(cvsroot_dirs)/sizeof(cvsroot_dirs[0]))
1.12      joris      67:
1.16      jfb        68: struct cvs_cmd cvs_cmd_init = {
1.25      joris      69:        CVS_OP_INIT, 0, "init",
1.16      jfb        70:        { },
                     71:        "Create a CVS repository if it doesn't exist",
                     72:        "",
                     73:        "",
1.12      joris      74:        NULL,
1.23      xsa        75:        cvs_init
1.12      joris      76: };
1.1       jfb        77:
1.23      xsa        78: int
                     79: cvs_init(int argc, char **argv)
1.18      xsa        80: {
1.23      xsa        81:        if (argc > 1)
1.24      joris      82:                fatal("init does not take any extra arguments");
1.23      xsa        83:
1.26      xsa        84:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.27      joris      85:                cvs_client_connect_to_server();
1.26      xsa        86:                cvs_client_send_request("init %s", current_cvsroot->cr_dir);
                     87:                cvs_client_get_responses();
                     88:        } else
1.23      xsa        89:                cvs_init_local();
1.18      xsa        90:
                     91:        return (0);
                     92: }
                     93:
1.23      xsa        94: void
                     95: cvs_init_local(void)
                     96: {
                     97:        u_int i;
1.29    ! otto       98:        char path[MAXPATHLEN];
1.23      xsa        99:
                    100:        cvs_log(LP_TRACE, "cvs_init_local()");
                    101:
                    102:        /* Create repository root directory if it does not already exist */
                    103:        init_mkdir(current_cvsroot->cr_dir, 0777);
                    104:
                    105:        for (i = 0; i < INIT_NDIRS; i++) {
                    106:                if (cvs_path_cat(current_cvsroot->cr_dir,
                    107:                    cvsroot_dirs[i], path, MAXPATHLEN) >= MAXPATHLEN)
                    108:                        fatal("cvs_init_local: truncation");
                    109:
                    110:                init_mkdir(path, 0777);
                    111:        }
                    112:
                    113:        for (i = 0; i < INIT_NFILES; i++) {
                    114:                if (cvs_path_cat(current_cvsroot->cr_dir,
                    115:                    cvsroot_files[i].cf_path, path, MAXPATHLEN) >= MAXPATHLEN)
                    116:                        fatal("cvs_init_local: truncation");
                    117:
                    118:                init_mkfile(path, cvsroot_files[i].cf_content);
                    119:        }
                    120: }
                    121:
1.21      xsa       122: static void
1.23      xsa       123: init_mkdir(const char *path, mode_t mode)
1.1       jfb       124: {
1.18      xsa       125:        struct stat st;
1.4       jfb       126:
1.23      xsa       127:        if (mkdir(path, mode) == -1) {
                    128:                if (!(errno == EEXIST ||
                    129:                    (errno == EACCES && (stat(path, &st) == 0) &&
                    130:                    S_ISDIR(st.st_mode)))) {
                    131:                        fatal("init_mkdir: mkdir: `%s': %s",
                    132:                            path, strerror(errno));
1.18      xsa       133:                }
                    134:        }
1.23      xsa       135: }
                    136:
                    137: static void
                    138: init_mkfile(char *path, const char *const *content)
                    139: {
                    140:        BUF *b;
                    141:        size_t len;
                    142:        int fd, openflags, rcsflags;
1.28      joris     143:        char *rpath;
1.23      xsa       144:        const char *const *p;
                    145:        RCSFILE *file;
                    146:
                    147:        len = 0;
                    148:        fd = -1;
                    149:        openflags = O_WRONLY|O_CREAT|O_EXCL;
                    150:        rcsflags = RCS_RDWR|RCS_CREATE;
                    151:
                    152:        if ((fd = open(path, openflags, 0444)) == -1)
                    153:                fatal("init_mkfile: open: `%s': %s", path, strerror(errno));
                    154:
                    155:        if (content != NULL) {
                    156:                for (p = content; *p != NULL; ++p) {
                    157:                        len = strlen(*p);
                    158:                        b = cvs_buf_alloc(len, BUF_AUTOEXT);
1.18      xsa       159:
1.23      xsa       160:                        if (cvs_buf_append(b, *p, strlen(*p)) < 0)
                    161:                                fatal("init_mkfile: cvs_buf_append");
1.1       jfb       162:
1.23      xsa       163:                        if (cvs_buf_write_fd(b, fd) < 0)
                    164:                                fatal("init_mkfile: cvs_buf_write_fd");
                    165:
                    166:                        cvs_buf_free(b);
1.1       jfb       167:                }
                    168:        }
1.23      xsa       169:
                    170:        /*
                    171:         * Make sure history and val-tags files are world-writable.
                    172:         * Every user should be able to write to them.
                    173:         */
                    174:        if (strcmp(strrchr(CVS_PATH_HISTORY, '/'), strrchr(path, '/')) == 0 ||
                    175:            strcmp(strrchr(CVS_PATH_VALTAGS, '/'), strrchr(path, '/')) == 0) {
                    176:                (void)fchmod(fd, 0666);
                    177:                goto out;
                    178:        }
                    179:
                    180:        rpath = xstrdup(path);
                    181:        if (strlcat(rpath, RCS_FILE_EXT, MAXPATHLEN) >= MAXPATHLEN)
                    182:                fatal("init_mkfile: truncation");
                    183:
                    184:        if ((file = rcs_open(rpath, fd, rcsflags, 0444)) == NULL)
                    185:                fatal("failed to create RCS file for `%s'", path);
                    186:
                    187:        if ((b = cvs_buf_load(path, BUF_AUTOEXT)) == NULL)
                    188:                fatal("init_mkfile: failed to load %s", path);
                    189:
                    190:        if (rcs_rev_add(file, RCS_HEAD_REV, "initial checkin", -1, NULL) == -1)
                    191:                fatal("init_mkfile: failed to add new revision");
                    192:
1.28      joris     193:        /* b buffer is free'd in rcs_deltatext_set */
                    194:        if (rcs_deltatext_set(file, file->rf_head, b) == -1)
1.23      xsa       195:                fatal("init_mkfile: failed to set delta");
                    196:
                    197:        file->rf_flags &= ~RCS_SYNCED;
                    198:        rcs_close(file);
                    199:        xfree(rpath);
                    200: out:
                    201:        (void)close(fd);
1.1       jfb       202: }