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

Annotation of src/usr.bin/cvs/remove.c, Revision 1.2

1.2     ! jfb         1: /*     $OpenBSD: remove.c,v 1.1 2004/12/21 18:15:55 xsa Exp $  */
1.1       xsa         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * Copyright (c) 2004 Xavier Santolaria <xsa@openbsd.org>
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  *
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. The name of the author may not be used to endorse or promote products
                     14:  *    derived from this software without specific prior written permission.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     17:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     18:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     19:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     20:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     21:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     22:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     23:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     24:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     25:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
                     28: #include <sys/types.h>
                     29:
                     30: #include <errno.h>
1.2     ! jfb        31: #include <fcntl.h>
1.1       xsa        32: #include <stdio.h>
                     33: #include <stdlib.h>
                     34: #include <string.h>
1.2     ! jfb        35: #include <libgen.h>
1.1       xsa        36: #include <sysexits.h>
                     37: #include <unistd.h>
                     38:
                     39: #include "cvs.h"
                     40: #include "log.h"
                     41: #include "proto.h"
                     42:
                     43:
                     44: extern char *__progname;
                     45:
                     46:
                     47: int  cvs_remove_file (CVSFILE *, void *);
                     48:
                     49:
                     50: /*
                     51:  * cvs_remove()
                     52:  *
                     53:  * Handler for the `cvs remove' command.
                     54:  * Returns 0 on success, or one of the known system exit codes on failure.
                     55:  */
                     56: int
                     57: cvs_remove(int argc, char **argv)
                     58: {
                     59:        int i, ch;
                     60:        struct cvsroot *root;
                     61:
                     62:        while ((ch = getopt(argc, argv, "flR")) != -1) {
                     63:                switch (ch) {
                     64:                case 'f':
                     65:                        break;
                     66:                case 'l':
                     67:                        break;
                     68:                case 'R':
                     69:                        break;
                     70:                default:
                     71:                        return (EX_USAGE);
                     72:                }
                     73:        }
                     74:
                     75:        argc -= optind;
                     76:        argv += optind;
                     77:
                     78:        if (argc == 0)
                     79:                return (EX_USAGE);
                     80:
                     81:        cvs_files = cvs_file_getspec(argv, argc, 0);
                     82:        if (cvs_files == NULL)
                     83:                return (EX_DATAERR);
                     84:
                     85:        root = CVS_DIR_ROOT(cvs_files);
                     86:        if (root == NULL) {
                     87:                cvs_log(LP_ERR,
                     88:                    "No CVSROOT specified!  Please use the `-d' option");
                     89:                cvs_log(LP_ERR,
                     90:                    "or set the CVSROOT environment variable.");
                     91:                return (EX_USAGE);
                     92:        }
                     93:
                     94:        if ((root->cr_method != CVS_METHOD_LOCAL) && (cvs_connect(root) < 0))
                     95:                return (EX_PROTOCOL);
                     96:
                     97:        cvs_file_examine(cvs_files, cvs_remove_file, NULL);
                     98:
                     99:        if (root->cr_method != CVS_METHOD_LOCAL) {
                    100:                if (cvs_senddir(root, cvs_files) < 0)
                    101:                        return (EX_PROTOCOL);
                    102:
                    103:                for (i = 0; i < argc; i++)
                    104:                        if (cvs_sendarg(root, argv[i], 0) < 0)
                    105:                                return (EX_PROTOCOL);
                    106:
                    107:                if (cvs_sendreq(root, CVS_REQ_REMOVE, NULL) < 0)
                    108:                        return (EX_PROTOCOL);
                    109:        }
                    110:
                    111:        return (0);
                    112: }
                    113:
                    114:
                    115: int
                    116: cvs_remove_file(CVSFILE *cf, void *arg)
                    117: {
                    118:        int ret;
1.2     ! jfb       119:        char fpath[MAXPATHLEN];
1.1       xsa       120:        struct cvsroot *root;
1.2     ! jfb       121:        CVSENTRIES *entfile;
        !           122:        struct cvs_ent *ent;
1.1       xsa       123:
                    124:        ret = 0;
1.2     ! jfb       125:        ent = NULL;
1.1       xsa       126:        root = CVS_DIR_ROOT(cf);
                    127:
                    128:        if (cf->cf_type == DT_DIR) {
                    129:                if (root->cr_method != CVS_METHOD_LOCAL) {
                    130:                        if (cf->cf_cvstat == CVS_FST_UNKNOWN)
                    131:                                ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
                    132:                                    CVS_FILE_NAME(cf));
                    133:                        else
                    134:                                ret = cvs_senddir(root, cf);
                    135:                }
                    136:
                    137:                return (ret);
                    138:        }
                    139:
1.2     ! jfb       140:        cvs_file_getpath(cf, fpath, sizeof(fpath));
        !           141:
        !           142:        entfile = cvs_ent_open(dirname(fpath), O_RDWR);
        !           143:        if (entfile == NULL) {
        !           144:                cvs_log(LP_ERR, "failed to remove `%s'", fpath);
        !           145:                return (-1);
        !           146:        }
        !           147:
        !           148:        ent = cvs_ent_get(entfile, CVS_FILE_NAME(cf));
        !           149:
1.1       xsa       150:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.2     ! jfb       151:                if (ent != NULL)
        !           152:                        ret = cvs_sendentry(root, ent);
1.1       xsa       153:        } else {
                    154:                cvs_log(LP_INFO, "scheduling file `%s' for removal",
                    155:                    CVS_FILE_NAME(cf));
1.2     ! jfb       156:                cvs_log(LP_INFO,
        !           157:                    "use `%s commit' to remove this file permanently",
1.1       xsa       158:                    __progname);
                    159:        }
1.2     ! jfb       160:
        !           161:        cvs_ent_close(entfile);
1.1       xsa       162:
                    163:        return (ret);
                    164: }