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

1.12    ! xsa         1: /*     $OpenBSD: remove.c,v 1.11 2005/04/21 19:06:06 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>
                     35: #include <unistd.h>
                     36:
                     37: #include "cvs.h"
                     38: #include "log.h"
                     39: #include "proto.h"
                     40:
                     41:
                     42: extern char *__progname;
                     43:
                     44:
1.4       joris      45: int cvs_remove_file(CVSFILE *, void *);
                     46: int cvs_remove_options(char *, int, char **, int *);
1.1       xsa        47:
1.4       joris      48: static int     force_remove = 0;       /* -f option */
                     49:
                     50: struct cvs_cmd_info cvs_remove = {
                     51:        cvs_remove_options,
                     52:        NULL,
                     53:        cvs_remove_file,
                     54:        NULL, NULL,
1.10      xsa        55:        CF_IGNORE | CF_RECURSE,
1.4       joris      56:        CVS_REQ_REMOVE,
                     57:        CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
                     58: };
1.1       xsa        59:
                     60: int
1.4       joris      61: cvs_remove_options(char *opt, int argc, char **argv, int *arg)
1.1       xsa        62: {
1.4       joris      63:        int ch;
1.1       xsa        64:
1.4       joris      65:        while ((ch = getopt(argc, argv, opt)) != -1) {
1.1       xsa        66:                switch (ch) {
                     67:                case 'f':
1.3       xsa        68:                        force_remove = 1;
1.1       xsa        69:                        break;
                     70:                case 'l':
1.9       xsa        71:                        cvs_remove.file_flags &= ~CF_RECURSE;
1.1       xsa        72:                        break;
                     73:                case 'R':
1.9       xsa        74:                        cvs_remove.file_flags |= CF_RECURSE;
1.1       xsa        75:                        break;
                     76:                default:
1.7       joris      77:                        return (CVS_EX_USAGE);
1.1       xsa        78:                }
                     79:        }
                     80:
                     81:        argc -= optind;
                     82:        argv += optind;
                     83:
1.4       joris      84:        *arg = optind;
1.1       xsa        85:        return (0);
                     86: }
                     87:
                     88:
                     89: int
                     90: cvs_remove_file(CVSFILE *cf, void *arg)
                     91: {
                     92:        int ret;
1.2       jfb        93:        char fpath[MAXPATHLEN];
1.1       xsa        94:        struct cvsroot *root;
                     95:
                     96:        ret = 0;
                     97:        root = CVS_DIR_ROOT(cf);
                     98:
                     99:        if (cf->cf_type == DT_DIR) {
                    100:                if (root->cr_method != CVS_METHOD_LOCAL) {
                    101:                        if (cf->cf_cvstat == CVS_FST_UNKNOWN)
                    102:                                ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
                    103:                                    CVS_FILE_NAME(cf));
                    104:                        else
                    105:                                ret = cvs_senddir(root, cf);
                    106:                }
                    107:
                    108:                return (ret);
                    109:        }
                    110:
1.2       jfb       111:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    112:
1.1       xsa       113:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.3       xsa       114:                /* if -f option is used, physically remove the file */
                    115:                if (force_remove == 1) {
                    116:                        if((unlink(fpath) == -1) && (errno != ENOENT)) {
1.10      xsa       117:                                cvs_log(LP_ERRNO,
                    118:                                    "failed to unlink `%s'", fpath);
1.7       joris     119:                                return (CVS_EX_FILE);
1.3       xsa       120:                        }
                    121:                }
                    122:
1.10      xsa       123:                if (cvs_sendentry(root, cf) < 0)
                    124:                        return (CVS_EX_PROTO);
                    125:
                    126:                if (cf->cf_cvstat != CVS_FST_LOST && force_remove != 1) {
                    127:                        if (cvs_sendreq(root, CVS_REQ_MODIFIED,
                    128:                            CVS_FILE_NAME(cf)) < 0) {
                    129:                                return (CVS_EX_PROTO);
                    130:                        }
1.11      xsa       131:
                    132:                        if (cvs_sendfile(root, fpath) < 0)
                    133:                                return (CVS_EX_PROTO);
1.10      xsa       134:                }
                    135:        } else {
1.1       xsa       136:                cvs_log(LP_INFO, "scheduling file `%s' for removal",
                    137:                    CVS_FILE_NAME(cf));
1.2       jfb       138:                cvs_log(LP_INFO,
                    139:                    "use `%s commit' to remove this file permanently",
1.1       xsa       140:                    __progname);
                    141:        }
                    142:
                    143:        return (ret);
                    144: }