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

Annotation of src/usr.bin/cvs/release.c, Revision 1.7

1.7     ! jfb         1: /*     $OpenBSD: release.c,v 1.6 2005/05/20 20:00:53 joris Exp $       */
1.1       xsa         2: /*
                      3:  * Copyright (c) 2005 Xavier Santolaria <xsa@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/types.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <errno.h>
                     31: #include <fcntl.h>
                     32: #include <stdio.h>
                     33: #include <stdlib.h>
                     34: #include <string.h>
                     35: #include <unistd.h>
                     36:
                     37: #include "cvs.h"
                     38: #include "log.h"
                     39: #include "proto.h"
                     40:
1.3       xsa        41: #define UPDCMD_FLAGS   "-n -q -d"
                     42:
                     43: extern char *__progname;
1.1       xsa        44:
1.7     ! jfb        45: static int cvs_release_init     (struct cvs_cmd *, int, char **, int *);
        !            46: static int cvs_release_pre_exec (struct cvsroot *);
        !            47: static int cvs_release_yesno    (void);
        !            48: static int cvs_release_dir      (CVSFILE *, void *);
        !            49:
        !            50: struct cvs_cmd cvs_cmd_release = {
        !            51:        CVS_OP_RELEASE, CVS_REQ_RELEASE, "release",
        !            52:        { },
        !            53:        "Release",
        !            54:        "[-d]",
        !            55:        "d",
        !            56:        NULL,
        !            57:        0,
        !            58:        cvs_release_init,
        !            59:        cvs_release_pre_exec,
1.1       xsa        60:        cvs_release_dir,
1.7     ! jfb        61:        cvs_release_dir,
        !            62:        NULL,
        !            63:        NULL,
1.1       xsa        64:        CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
                     65: };
                     66:
                     67: static int     dflag;  /* -d option */
                     68:
                     69: static int
1.7     ! jfb        70: cvs_release_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       xsa        71: {
                     72:        int ch;
                     73:
1.7     ! jfb        74:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       xsa        75:                switch (ch) {
                     76:                case 'd':
                     77:                        dflag = 1;
                     78:                        break;
                     79:                default:
                     80:                        return (CVS_EX_USAGE);
                     81:                }
                     82:        }
                     83:
                     84:        argc -= optind;
                     85:        argv += optind;
                     86:        *arg = optind;
                     87:
                     88:        if (argc == 0)
                     89:                return (CVS_EX_USAGE);
                     90:
                     91:        return (0);
                     92: }
                     93:
                     94: static int
1.7     ! jfb        95: cvs_release_pre_exec(struct cvsroot *root)
1.1       xsa        96: {
                     97:        if (dflag && cvs_sendarg(root, "-d", 0) < 0)
                     98:                return (CVS_EX_PROTO);
                     99:
                    100:        return (0);
                    101: }
                    102:
                    103: /*
                    104:  * cvs_release_yesno()
                    105:  *
                    106:  * Read from standart input for `y' or `Y' character.
                    107:  * Returns 0 on success, or -1 on failure.
                    108:  */
                    109: static int
                    110: cvs_release_yesno(void)
                    111: {
                    112:        int c, ret;
                    113:
                    114:        ret = 0;
1.5       jfb       115:
1.1       xsa       116:        fflush (stderr);
                    117:        fflush (stdout);
                    118:
                    119:        if ((c = getchar()) != 'y' && c != 'Y')
                    120:                ret = -1;
                    121:        else
                    122:                while (c != EOF && c != '\n')
                    123:                        c = getchar();
                    124:
                    125:        return (ret);
                    126: }
                    127:
                    128: /*
                    129:  * cvs_release_dir()
                    130:  *
                    131:  * Release specified directorie(s).
                    132:  * Returns 0 on success, or -1 on failure.
                    133:  */
                    134: static int
                    135: cvs_release_dir(CVSFILE *cdir, void *arg)
                    136: {
1.3       xsa       137:        FILE *fp;
1.1       xsa       138:        int j, l;
1.4       xsa       139:        char *wdir, cwd[MAXPATHLEN];
1.3       xsa       140:        char buf[256], cdpath[MAXPATHLEN], dpath[MAXPATHLEN];
                    141:        char updcmd[MAXPATHLEN];        /* XXX find a better size; malloc()?? */
1.1       xsa       142:        struct stat st;
                    143:        struct cvsroot *root;
                    144:
                    145:        j = 0;          /* number of altered files in the working copy */
                    146:
                    147:        root = CVS_DIR_ROOT(cdir);
                    148:
                    149:        cvs_file_getpath(cdir, dpath, sizeof(dpath));
                    150:
                    151:        l = snprintf(cdpath, sizeof(cdpath), "%s/" CVS_PATH_CVSDIR, dpath);
                    152:        if (l == -1 || l >= (int)sizeof(cdpath)) {
                    153:                errno = ENAMETOOLONG;
                    154:                cvs_log(LP_ERRNO, "%s", cdpath);
1.6       joris     155:                return (CVS_EX_DATA);
1.1       xsa       156:        }
                    157:
                    158:        if (cdir->cf_type == DT_DIR) {
                    159:                if (!strcmp(CVS_FILE_NAME(cdir), "."))
                    160:                        return (0);
                    161:                else {
                    162:                        /* test if dir has CVS/ directory */
                    163:                        if (stat(cdpath, &st) == -1) {
                    164:                                cvs_log(LP_ERR,
                    165:                                    "no repository directory: %s", dpath);
                    166:                                return (0);
                    167:                        }
                    168:                }
                    169:
                    170:                if (root->cr_method != CVS_METHOD_LOCAL) {
1.4       xsa       171:                        /* XXX kept for compat reason of `cvs update' output */
                    172:                        /* save current working directory for further use */
                    173:                        if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL)
                    174:                                cvs_log(LP_ERRNO, "cannot get current dir");
                    175:
1.3       xsa       176:                        /* change dir before running the `cvs update' command */
1.4       xsa       177:                        if (chdir(dpath) == -1) {
                    178:                                cvs_log(LP_ERRNO, "cannot change to dir `%s'",
                    179:                                    dpath);
1.6       joris     180:                                return (CVS_EX_FILE);
1.4       xsa       181:                        }
1.3       xsa       182:
                    183:                        /* construct `cvs update' command */
                    184:                        l = snprintf(updcmd, sizeof(updcmd), "%s %s %s update",
                    185:                            __progname, UPDCMD_FLAGS, root->cr_str);
                    186:                        if (l == -1 || l >= (int)sizeof(updcmd))
1.6       joris     187:                                return (CVS_EX_DATA);
1.3       xsa       188:
                    189:                        /* XXX we should try to avoid a new connection ... */
                    190:                        if ((fp = popen(updcmd, "r")) == NULL) {
                    191:                                cvs_log(LP_ERROR, "cannot run command `%s'",
                    192:                                    updcmd);
1.6       joris     193:                                return (CVS_EX_DATA);
1.3       xsa       194:                        }
1.1       xsa       195:
1.3       xsa       196:                        while (fgets(buf, sizeof(buf), fp) != NULL) {
                    197:                                if (strchr("ACMPRU", buf[0]))
                    198:                                        j++;
                    199:                                (void)fputs(buf, stdout);
                    200:                        }
                    201:
                    202:                        if (pclose(fp) != 0) {
                    203:                                cvs_log(LP_ERROR, "unable to release `%s'",
                    204:                                    dpath);
1.6       joris     205:                                return (CVS_EX_DATA);
1.3       xsa       206:                        }
1.1       xsa       207:
1.3       xsa       208:                        printf("You have [%d] altered file%s in this "
                    209:                            "repository.\n", j, j > 1 ? "s" : "");
1.4       xsa       210:
1.1       xsa       211:                        printf("Are you sure you want to release "
                    212:                            "%sdirectory `%s': ",
                    213:                            dflag ? "(and delete) " : "", dpath);
                    214:
                    215:                        if (cvs_release_yesno() == -1) {        /* No */
                    216:                                (void)fprintf(stderr,
                    217:                                    "** `%s' aborted by user choice.\n",
                    218:                                    cvs_command);
                    219:                                return (-1);
                    220:                        }
                    221:
1.4       xsa       222:                        /* change back to original working dir */
                    223:                        if (chdir(wdir) == -1) {
                    224:                                cvs_log(LP_ERRNO, "cannot change to original "
                    225:                                    "working dir `%s'", wdir);
1.6       joris     226:                                return (CVS_EX_FILE);
1.4       xsa       227:                        }
                    228:
1.1       xsa       229:                        if (dflag == 1) {
1.4       xsa       230:                                if (cvs_remove_dir(dpath) != CVS_EX_OK) {
1.1       xsa       231:                                        cvs_log(LP_ERRNO,
1.4       xsa       232:                                            "deletion of directory `%s' failed",
1.1       xsa       233:                                            dpath);
1.6       joris     234:                                        return (CVS_EX_FILE);
1.1       xsa       235:                                }
                    236:                        }
                    237:                }
                    238:        } else {
                    239:                cvs_log(LP_ERR, "no such directory: %s", dpath);
1.6       joris     240:                return (CVS_EX_DATA);
1.1       xsa       241:        }
                    242:
                    243:        return (0);
                    244: }