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

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     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
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     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"
                     41:
                     42: extern struct cvsroot *cvs_root;
                     43:
                     44:
                     45: extern char cvs_loginfo_data[];
                     46:
                     47:
                     48: #define CFT_FILE   1
                     49: #define CFT_DIR    2
                     50:
                     51:
                     52: struct cvsroot_file {
                     53:        char   *cf_path;   /* path relative to CVS root directory */
                     54:        u_int   cf_type;
                     55:        mode_t  cf_mode;
                     56: } cvsroot_files[] = {
                     57:        { CVS_PATH_ROOT,   CFT_DIR, (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) },
                     58:
                     59:        { CVS_PATH_COMMITINFO,  CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     60:        { CVS_PATH_CONFIG,      CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     61:        { CVS_PATH_CVSIGNORE,   CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     62:        { CVS_PATH_CVSWRAPPERS, CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     63:        { CVS_PATH_EDITINFO,    CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     64:        { CVS_PATH_HISTORY,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     65:        { CVS_PATH_LOGINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     66:        { CVS_PATH_MODULES,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     67:        { CVS_PATH_NOTIFY,      CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     68:        { CVS_PATH_RCSINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     69:        { CVS_PATH_TAGINFO,     CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     70:        { CVS_PATH_VERIFYMSG,   CFT_FILE, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) },
                     71: };
                     72:
                     73:
                     74:
1.2     ! jfb        75: /*
        !            76:  * cvs_init()
        !            77:  *
        !            78:  * Handler for the `cvs init' command which is used to initialize a CVS
        !            79:  * repository.
        !            80:  * Returns 0 on success, or the appropriate exit status on failure.
        !            81:  */
1.1       jfb        82:
                     83: int
                     84: cvs_init(int argc, char **argv)
                     85: {
                     86:        int fd;
                     87:        u_int i;
                     88:        char path[MAXPATHLEN];
                     89:        RCSFILE *rfp;
                     90:        struct stat st;
                     91:
                     92:        if (argc != 1)
                     93:                return (EX_USAGE);
                     94:
                     95:        for (i = 0; i < sizeof(cvsroot_files)/sizeof(cvsroot_files[i]); i++) {
                     96:                snprintf(path, sizeof(path), "%s/%s", cvs_root->cr_dir,
                     97:                    cvsroot_files[i].cf_path);
                     98:
                     99:                if (cvsroot_files[i].cf_type == CFT_DIR) {
                    100:                        if (mkdir(path, cvsroot_files[i].cf_mode) == -1) {
                    101:                                cvs_log(LP_ERRNO, "failed to create `%s'",
                    102:                                    path);
                    103:                                return (EX_CANTCREAT);
                    104:                        }
                    105:                }
                    106:                else if (cvsroot_files[i].cf_type == CFT_FILE) {
                    107:                        fd = open(path, O_WRONLY|O_CREAT|O_EXCL,
                    108:                            cvsroot_files[i].cf_mode);
                    109:                        if (fd == -1) {
                    110:                                cvs_log(LP_ERRNO, "failed to create `%s'",
                    111:                                    path);
                    112:                                return (EX_CANTCREAT);
                    113:                        }
                    114:
                    115:                        (void)close(fd);
                    116:
                    117:                        strlcat(path, RCS_FILE_EXT, sizeof(path));
                    118:                        rfp = rcs_open(path, RCS_MODE_WRITE);
                    119:                        if (rfp == NULL) {
                    120:                                return (EX_CANTCREAT);
                    121:                        }
                    122:
                    123:                        if (rcs_write(rfp) < 0) {
                    124:                                rcs_close(rfp);
                    125:                                return (EX_CANTCREAT);
                    126:                        }
                    127:
                    128:                        rcs_close(rfp);
                    129:                }
                    130:        }
                    131:
                    132:        return (0);
                    133: }