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

1.9     ! jfb         1: /*     $OpenBSD: init.c,v 1.8 2004/12/07 17:10:56 tedu 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>
                     31: #include <stdio.h>
                     32: #include <fcntl.h>
                     33: #include <stdlib.h>
                     34: #include <unistd.h>
                     35: #include <string.h>
                     36: #include <sysexits.h>
                     37:
                     38: #include "cvs.h"
                     39: #include "rcs.h"
                     40: #include "log.h"
1.6       jfb        41: #include "proto.h"
1.1       jfb        42:
                     43:
                     44: #define CFT_FILE   1
                     45: #define CFT_DIR    2
                     46:
                     47:
                     48: struct cvsroot_file {
                     49:        char   *cf_path;   /* path relative to CVS root directory */
                     50:        u_int   cf_type;
                     51:        mode_t  cf_mode;
                     52: } cvsroot_files[] = {
                     53:        { CVS_PATH_ROOT,   CFT_DIR, (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) },
                     54:
                     55:        { CVS_PATH_COMMITINFO,  CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     56:        { CVS_PATH_CONFIG,      CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     57:        { CVS_PATH_CVSIGNORE,   CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     58:        { CVS_PATH_CVSWRAPPERS, CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     59:        { CVS_PATH_EDITINFO,    CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     60:        { CVS_PATH_HISTORY,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     61:        { CVS_PATH_LOGINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     62:        { CVS_PATH_MODULES,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     63:        { CVS_PATH_NOTIFY,      CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     64:        { CVS_PATH_RCSINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     65:        { CVS_PATH_TAGINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     66:        { CVS_PATH_VERIFYMSG,   CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     67: };
                     68:
                     69:
1.2       jfb        70: /*
                     71:  * cvs_init()
                     72:  *
                     73:  * Handler for the `cvs init' command which is used to initialize a CVS
                     74:  * repository.
                     75:  * Returns 0 on success, or the appropriate exit status on failure.
                     76:  */
1.1       jfb        77: int
                     78: cvs_init(int argc, char **argv)
                     79: {
                     80:        int fd;
                     81:        u_int i;
                     82:        char path[MAXPATHLEN];
                     83:        RCSFILE *rfp;
1.4       jfb        84:        struct cvsroot *root;
1.1       jfb        85:
                     86:        if (argc != 1)
                     87:                return (EX_USAGE);
                     88:
1.4       jfb        89:        root = cvsroot_get(".");
1.9     ! jfb        90:        if (root == NULL) {
        !            91:                cvs_log(LP_ERR,
        !            92:                    "No CVSROOT specified!  Please use the `-d' option");
        !            93:                cvs_log(LP_ERR,
        !            94:                    "or set the CVSROOT environment variable.");
        !            95:                return (EX_USAGE);
        !            96:        }
        !            97:
1.6       jfb        98:        if (root->cr_method != CVS_METHOD_LOCAL) {
                     99:                if (cvs_connect(root) < 0)
                    100:                        return (EX_DATAERR);
                    101:
                    102:                if (cvs_sendreq(root, CVS_REQ_INIT, root->cr_dir) < 0)
                    103:                        return (EX_DATAERR);
                    104:
                    105:                cvs_disconnect(root);
                    106:                return (0);
                    107:        }
1.4       jfb       108:
1.1       jfb       109:        for (i = 0; i < sizeof(cvsroot_files)/sizeof(cvsroot_files[i]); i++) {
1.4       jfb       110:                snprintf(path, sizeof(path), "%s/%s", root->cr_dir,
1.1       jfb       111:                    cvsroot_files[i].cf_path);
                    112:
                    113:                if (cvsroot_files[i].cf_type == CFT_DIR) {
                    114:                        if (mkdir(path, cvsroot_files[i].cf_mode) == -1) {
                    115:                                cvs_log(LP_ERRNO, "failed to create `%s'",
                    116:                                    path);
                    117:                                return (EX_CANTCREAT);
                    118:                        }
1.7       deraadt   119:                } else if (cvsroot_files[i].cf_type == CFT_FILE) {
1.1       jfb       120:                        fd = open(path, O_WRONLY|O_CREAT|O_EXCL,
                    121:                            cvsroot_files[i].cf_mode);
                    122:                        if (fd == -1) {
                    123:                                cvs_log(LP_ERRNO, "failed to create `%s'",
                    124:                                    path);
                    125:                                return (EX_CANTCREAT);
                    126:                        }
                    127:
                    128:                        (void)close(fd);
                    129:
                    130:                        strlcat(path, RCS_FILE_EXT, sizeof(path));
                    131:                        rfp = rcs_open(path, RCS_MODE_WRITE);
                    132:                        if (rfp == NULL) {
                    133:                                return (EX_CANTCREAT);
                    134:                        }
                    135:
                    136:                        if (rcs_write(rfp) < 0) {
                    137:                                rcs_close(rfp);
                    138:                                return (EX_CANTCREAT);
                    139:                        }
                    140:
                    141:                        rcs_close(rfp);
                    142:                }
                    143:        }
                    144:
                    145:        return (0);
                    146: }