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

1.27    ! xsa         1: /*     $OpenBSD: release.c,v 1.26 2005/12/30 02:03:28 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.18      xsa        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 *);
1.7       jfb        49:
                     50: struct cvs_cmd cvs_cmd_release = {
                     51:        CVS_OP_RELEASE, CVS_REQ_RELEASE, "release",
1.20      xsa        52:        { "re", "rel" },
1.7       jfb        53:        "Release",
                     54:        "[-d]",
                     55:        "d",
                     56:        NULL,
1.22      xsa        57:        CF_NOFILES,
1.7       jfb        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: {
1.11      joris      97:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.27    ! xsa        98:                if (dflag == 1)
1.26      joris      99:                        cvs_sendarg(root, "-d", 0);
1.11      joris     100:        }
1.1       xsa       101:        return (0);
                    102: }
                    103:
                    104: /*
                    105:  * cvs_release_yesno()
                    106:  *
                    107:  * Read from standart input for `y' or `Y' character.
                    108:  * Returns 0 on success, or -1 on failure.
                    109:  */
                    110: static int
                    111: cvs_release_yesno(void)
                    112: {
                    113:        int c, ret;
                    114:
                    115:        ret = 0;
1.5       jfb       116:
1.23      reyk      117:        fflush(stderr);
                    118:        fflush(stdout);
1.1       xsa       119:
                    120:        if ((c = getchar()) != 'y' && c != 'Y')
                    121:                ret = -1;
                    122:        else
                    123:                while (c != EOF && c != '\n')
                    124:                        c = getchar();
                    125:
                    126:        return (ret);
                    127: }
                    128:
                    129: /*
                    130:  * cvs_release_dir()
                    131:  *
                    132:  * Release specified directorie(s).
                    133:  * Returns 0 on success, or -1 on failure.
                    134:  */
                    135: static int
1.15      xsa       136: cvs_release_dir(CVSFILE *cf, void *arg)
1.1       xsa       137: {
1.3       xsa       138:        FILE *fp;
1.1       xsa       139:        int j, l;
1.4       xsa       140:        char *wdir, cwd[MAXPATHLEN];
1.22      xsa       141:        char buf[256], dpath[MAXPATHLEN], updcmd[1024];
1.1       xsa       142:        struct stat st;
                    143:        struct cvsroot *root;
                    144:
1.22      xsa       145:        j = 0;
1.1       xsa       146:
1.15      xsa       147:        root = CVS_DIR_ROOT(cf);
1.1       xsa       148:
1.22      xsa       149:        /* XXX kept for compat reason of `cvs update' output */
                    150:        /* save current working directory for further use */
                    151:        if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL) {
                    152:                cvs_log(LP_ERRNO, "cannot get current dir");
                    153:                return (CVS_EX_FILE);
                    154:        }
                    155:
1.15      xsa       156:        cvs_file_getpath(cf, dpath, sizeof(dpath));
1.1       xsa       157:
1.15      xsa       158:        if (cf->cf_type == DT_DIR) {
1.16      joris     159:                if (!strcmp(cf->cf_name, "."))
1.1       xsa       160:                        return (0);
                    161:
1.22      xsa       162:                /* chdir before running the `cvs update' command */
1.25      xsa       163:                cvs_chdir(dpath, 0);
1.22      xsa       164:
                    165:                /* test if dir has CVS/ directory */
                    166:                if (stat(CVS_PATH_CVSDIR, &st) == -1) {
                    167:                        if (verbosity > 0)
                    168:                                cvs_log(LP_ERR,
                    169:                                    "no repository directory: %s", dpath);
                    170:                        return (0);
1.1       xsa       171:                }
                    172:        } else {
1.14      xsa       173:                if (verbosity > 0)
                    174:                        cvs_log(LP_ERR, "no such directory: %s", dpath);
1.22      xsa       175:                return (0);
                    176:        }
                    177:
                    178:        /* construct `cvs update' command */
                    179:        l = snprintf(updcmd, sizeof(updcmd), "%s %s %s update",
                    180:            __progname, UPDCMD_FLAGS, root->cr_str);
                    181:        if (l == -1 || l >= (int)sizeof(updcmd))
                    182:                return (CVS_EX_DATA);
                    183:
                    184:        /* XXX we should try to avoid a new connection ... */
                    185:        cvs_log(LP_TRACE, "cvs_release_dir() popen(%s,r)", updcmd);
                    186:        if ((fp = popen(updcmd, "r")) == NULL) {
                    187:                cvs_log(LP_ERR, "cannot run command `%s'", updcmd);
1.6       joris     188:                return (CVS_EX_DATA);
1.22      xsa       189:        }
                    190:
                    191:        while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
                    192:                if (strchr("ACMPRU", buf[0]))
                    193:                        j++;
                    194:                (void)fputs(buf, stdout);
                    195:        }
                    196:
                    197:        if (pclose(fp) != 0) {
                    198:                cvs_log(LP_ERR, "unable to release `%s'", dpath);
                    199:
                    200:                /* change back to original working dir */
1.25      xsa       201:                cvs_chdir(wdir, 0);
1.22      xsa       202:        }
                    203:
                    204:        printf("You have [%d] altered file%s in this repository.\n",
                    205:            j, j > 1 ? "s" : "");
                    206:        while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
                    207:                if (strchr("ACMPRU", buf[0]))
                    208:                        j++;
                    209:                (void)fputs(buf, stdout);
                    210:        }
                    211:
                    212:        printf("Are you sure you want to release %sdirectory `%s': ",
                    213:            dflag ? "(and delete) " : "", dpath);
                    214:
                    215:        if (cvs_release_yesno() == -1) {        /* No */
                    216:                fprintf(stderr,
                    217:                    "** `%s' aborted by user choice.\n", cvs_command);
                    218:
                    219:                /* change back to original working dir */
1.25      xsa       220:                cvs_chdir(wdir, 0);
1.22      xsa       221:
                    222:                return (-1);
                    223:        }
                    224:
                    225:        /* change back to original working dir */
1.25      xsa       226:        cvs_chdir(wdir, 0);
1.22      xsa       227:
                    228:        if (dflag == 1) {
                    229:                if (cvs_rmdir(dpath) != 0)
                    230:                        return (CVS_EX_FILE);
1.1       xsa       231:        }
                    232:
                    233:        return (0);
                    234: }