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

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