[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.19

1.19    ! xsa         1: /*     $OpenBSD: init.c,v 1.18 2005/07/19 15:30:37 xsa Exp $   */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.8       tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.8       tedu        6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.8       tedu       10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.8       tedu       13:  *    derived from this software without specific prior written permission.
1.1       jfb        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
1.8       tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <errno.h>
1.17      xsa        31: #include <fcntl.h>
1.1       jfb        32: #include <stdio.h>
                     33: #include <stdlib.h>
1.17      xsa        34: #include <string.h>
1.1       jfb        35: #include <unistd.h>
                     36:
                     37: #include "cvs.h"
                     38: #include "log.h"
1.6       jfb        39: #include "proto.h"
1.1       jfb        40:
                     41:
1.19    ! xsa        42: #define CFT_FILE       1
        !            43: #define CFT_DIR                2
1.1       jfb        44:
                     45:
                     46: struct cvsroot_file {
1.19    ! xsa        47:        char    *cf_path;       /* path relative to CVS root directory */
        !            48:        u_int    cf_type;
        !            49:        mode_t   cf_mode;
1.1       jfb        50: } cvsroot_files[] = {
                     51:        { CVS_PATH_ROOT,   CFT_DIR, (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) },
1.18      xsa        52:        { CVS_PATH_EMPTYDIR, CFT_DIR, (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) },
1.1       jfb        53:        { CVS_PATH_COMMITINFO,  CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     54:        { CVS_PATH_CONFIG,      CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     55:        { CVS_PATH_CVSIGNORE,   CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     56:        { CVS_PATH_CVSWRAPPERS, CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     57:        { CVS_PATH_EDITINFO,    CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     58:        { CVS_PATH_HISTORY,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     59:        { CVS_PATH_LOGINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     60:        { CVS_PATH_MODULES,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     61:        { CVS_PATH_NOTIFY,      CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     62:        { CVS_PATH_RCSINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     63:        { CVS_PATH_TAGINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     64:        { CVS_PATH_VERIFYMSG,   CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     65: };
                     66:
1.19    ! xsa        67: static int     cvs_init_pre_exec(struct cvsroot *);
        !            68: static int     cvs_init_create_files(struct cvsroot *);
1.12      joris      69:
1.16      jfb        70: struct cvs_cmd cvs_cmd_init = {
                     71:        CVS_OP_INIT, CVS_REQ_INIT, "init",
                     72:        { },
                     73:        "Create a CVS repository if it doesn't exist",
                     74:        "",
                     75:        "",
1.12      joris      76:        NULL,
                     77:        0,
1.16      jfb        78:        NULL,
1.18      xsa        79:        cvs_init_pre_exec,
1.16      jfb        80:        NULL,
                     81:        NULL,
                     82:        NULL,
                     83:        NULL,
1.12      joris      84:        0
                     85: };
1.1       jfb        86:
1.2       jfb        87: /*
1.18      xsa        88:  * cvs_init_pre_exec()
                     89:  *
                     90:  * Local/remote handler for the "cvs init" command.
                     91:  * Returns 0 on success, -1 on failure.
                     92:  */
                     93: static int
                     94: cvs_init_pre_exec(struct cvsroot *root)
                     95: {
                     96:        if (root->cr_method == CVS_METHOD_LOCAL) {
                     97:                if (cvs_init_create_files(root) < 0)
                     98:                        return (CVS_EX_FILE);
                     99:        }
                    100:
                    101:        return (0);
                    102: }
                    103:
                    104: /*
                    105:  * cvs_init_create_files
1.2       jfb       106:  *
1.18      xsa       107:  * Create all required files for the "cvs init" command.
                    108:  * Used by the local handlers.
1.13      xsa       109:  * Returns 0 on success, -1 on failure.
1.18      xsa       110:  *
1.2       jfb       111:  */
1.18      xsa       112: static int
                    113: cvs_init_create_files(struct cvsroot *root)
1.1       jfb       114: {
1.18      xsa       115:        size_t len;
                    116:        int fd;
1.1       jfb       117:        u_int i;
                    118:        char path[MAXPATHLEN];
                    119:        RCSFILE *rfp;
1.18      xsa       120:        struct stat st;
1.4       jfb       121:
1.18      xsa       122:        /* Create repository root directory if it does not already exist */
                    123:        if (mkdir(root->cr_dir, 0777) == -1) {
                    124:                if (!(errno == EEXIST || (errno == EACCES &&
                    125:                    (stat(root->cr_dir, &st) == 0) && S_ISDIR(st.st_mode)))) {
                    126:                        cvs_log(LP_ERRNO, "cannot make directory %s",
                    127:                            root->cr_dir);
                    128:                        return (CVS_EX_FILE);
                    129:                }
                    130:        }
                    131:
                    132:        /* Create the repository administrative files */
1.1       jfb       133:        for (i = 0; i < sizeof(cvsroot_files)/sizeof(cvsroot_files[i]); i++) {
1.18      xsa       134:                len = cvs_path_cat(root->cr_dir, cvsroot_files[i].cf_path,
                    135:                    path, sizeof(path));
                    136:                if (len >= sizeof(path))
1.15      xsa       137:                        return (-1);
1.1       jfb       138:
                    139:                if (cvsroot_files[i].cf_type == CFT_DIR) {
                    140:                        if (mkdir(path, cvsroot_files[i].cf_mode) == -1) {
1.18      xsa       141:                                if (!(errno == EEXIST || (errno == EACCES &&
                    142:                                    (stat(path, &st) == 0) &&
                    143:                                    S_ISDIR(st.st_mode)))) {
                    144:                                        cvs_log(LP_ERRNO,
                    145:                                            "cannot make directory %s", path);
                    146:                                        return (CVS_EX_FILE);
                    147:                                }
1.1       jfb       148:                        }
1.7       deraadt   149:                } else if (cvsroot_files[i].cf_type == CFT_FILE) {
1.1       jfb       150:                        fd = open(path, O_WRONLY|O_CREAT|O_EXCL,
                    151:                            cvsroot_files[i].cf_mode);
                    152:                        if (fd == -1) {
                    153:                                cvs_log(LP_ERRNO, "failed to create `%s'",
                    154:                                    path);
1.14      joris     155:                                return (CVS_EX_FILE);
1.1       jfb       156:                        }
                    157:
                    158:                        (void)close(fd);
                    159:
                    160:                        strlcat(path, RCS_FILE_EXT, sizeof(path));
1.11      jfb       161:                        rfp = rcs_open(path, RCS_WRITE|RCS_CREATE, 0640);
1.1       jfb       162:                        if (rfp == NULL) {
1.14      joris     163:                                return (CVS_EX_DATA);
1.1       jfb       164:                        }
                    165:
                    166:                        rcs_close(rfp);
                    167:                }
                    168:        }
                    169:
                    170:        return (0);
                    171: }