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

1.27    ! xsa         1: /*     $OpenBSD: remove.c,v 1.26 2005/07/25 12:13:08 xsa Exp $ */
1.1       xsa         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.20      xsa         4:  * Copyright (c) 2004, 2005 Xavier Santolaria <xsa@openbsd.org>
1.1       xsa         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.26      xsa        45: static int     cvs_remove_init(struct cvs_cmd *, int, char **, int *);
                     46: static int     cvs_remove_remote(CVSFILE *, void *);
                     47: static int     cvs_remove_local(CVSFILE *, void *);
                     48: static int     cvs_remove_file(const char *);
1.1       xsa        49:
1.4       joris      50: static int     force_remove = 0;       /* -f option */
1.20      xsa        51: static int     nuked = 0;
1.4       joris      52:
1.16      jfb        53: struct cvs_cmd cvs_cmd_remove = {
                     54:        CVS_OP_REMOVE, CVS_REQ_REMOVE, "remove",
                     55:        { "rm", "delete" },
                     56:        "Remove an entry from the repository",
                     57:        "[-flR] [file ...]",
                     58:        "flR",
1.4       joris      59:        NULL,
1.16      jfb        60:        CF_IGNORE | CF_RECURSE,
                     61:        cvs_remove_init,
                     62:        NULL,
1.18      joris      63:        cvs_remove_remote,
                     64:        cvs_remove_local,
1.16      jfb        65:        NULL,
                     66:        NULL,
1.4       joris      67:        CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC
                     68: };
1.1       xsa        69:
1.13      xsa        70: static int
1.16      jfb        71: cvs_remove_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       xsa        72: {
1.4       joris      73:        int ch;
1.1       xsa        74:
1.16      jfb        75:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       xsa        76:                switch (ch) {
                     77:                case 'f':
1.3       xsa        78:                        force_remove = 1;
1.1       xsa        79:                        break;
                     80:                case 'l':
1.16      jfb        81:                        cmd->file_flags &= ~CF_RECURSE;
1.1       xsa        82:                        break;
                     83:                case 'R':
1.16      jfb        84:                        cmd->file_flags |= CF_RECURSE;
1.1       xsa        85:                        break;
                     86:                default:
1.7       joris      87:                        return (CVS_EX_USAGE);
1.1       xsa        88:                }
                     89:        }
                     90:
                     91:        argc -= optind;
                     92:        argv += optind;
                     93:
1.4       joris      94:        *arg = optind;
1.1       xsa        95:        return (0);
                     96: }
                     97:
                     98:
1.13      xsa        99: static int
1.18      joris     100: cvs_remove_remote(CVSFILE *cf, void *arg)
1.1       xsa       101: {
                    102:        int ret;
1.2       jfb       103:        char fpath[MAXPATHLEN];
1.1       xsa       104:        struct cvsroot *root;
                    105:
                    106:        ret = 0;
                    107:        root = CVS_DIR_ROOT(cf);
                    108:
                    109:        if (cf->cf_type == DT_DIR) {
1.18      joris     110:                if (cf->cf_cvstat == CVS_FST_UNKNOWN)
                    111:                        ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
1.20      xsa       112:                            cf->cf_name);
1.18      joris     113:                else
                    114:                        ret = cvs_senddir(root, cf);
1.1       xsa       115:
1.18      joris     116:                if (ret == -1)
                    117:                        ret = CVS_EX_PROTO;
1.1       xsa       118:                return (ret);
                    119:        }
                    120:
1.2       jfb       121:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    122:
1.20      xsa       123:        if (cvs_remove_file(fpath) < 0)
                    124:                return (CVS_EX_FILE);
1.18      joris     125:
                    126:        if (cvs_sendentry(root, cf) < 0)
                    127:                return (CVS_EX_PROTO);
1.3       xsa       128:
1.18      joris     129:        if (cf->cf_cvstat != CVS_FST_LOST && force_remove != 1) {
1.22      joris     130:                if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0) {
1.10      xsa       131:                        return (CVS_EX_PROTO);
1.18      joris     132:                }
1.17      joris     133:
1.18      joris     134:                if (cvs_sendfile(root, fpath) < 0)
                    135:                        return (CVS_EX_PROTO);
                    136:        }
1.11      xsa       137:
1.18      joris     138:        return (0);
                    139: }
                    140:
                    141: static int
                    142: cvs_remove_local(CVSFILE *cf, void *arg)
                    143: {
1.21      xsa       144:        int existing, l, removed;
                    145:        char buf[MAXPATHLEN], fpath[MAXPATHLEN];
1.20      xsa       146:
                    147:        existing = removed = 0;
                    148:
1.19      xsa       149:        if (cf->cf_type == DT_DIR) {
                    150:                if (verbosity > 1)
                    151:                        cvs_log(LP_INFO, "Removing %s", cf->cf_name);
                    152:                return (0);
                    153:        }
                    154:
1.20      xsa       155:        if (cvs_cmdop != CVS_OP_SERVER) {
                    156:                cvs_file_getpath(cf, fpath, sizeof(fpath));
                    157:
                    158:                if (cvs_remove_file(fpath) < 0)
                    159:                        return (CVS_EX_FILE);
                    160:        }
                    161:
1.25      xsa       162:        if (nuked == 0) {
1.20      xsa       163:                existing++;
                    164:                if (verbosity > 1)
                    165:                        cvs_log(LP_WARN, "file `%s' still in working directory",
                    166:                            cf->cf_name);
                    167:        } else if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
                    168:                if (verbosity > 1)
                    169:                        cvs_log(LP_WARN, "nothing known about `%s'",
                    170:                            cf->cf_name);
                    171:                return (0);
                    172:        } else if (cf->cf_cvstat == CVS_FST_ADDED) {
1.21      xsa       173:                l = snprintf(buf, sizeof(buf), "%s/%s%s",
                    174:                    CVS_PATH_CVSDIR, cf->cf_name, CVS_DESCR_FILE_EXT);
                    175:                if (l == -1 || l >= (int)sizeof(buf)) {
                    176:                        errno = ENAMETOOLONG;
                    177:                        cvs_log(LP_ERRNO, "%s", buf);
                    178:                        return (CVS_EX_DATA);
                    179:                }
1.24      xsa       180:                if ((cvs_unlink(buf) == -1) && (errno != ENOENT))
1.21      xsa       181:                        return (CVS_EX_FILE);
1.24      xsa       182:
1.20      xsa       183:                if (verbosity > 1)
                    184:                        cvs_log(LP_INFO, "removed `%s'", cf->cf_name);
                    185:                return (0);
                    186:        } else if (cf->cf_cvstat == CVS_FST_REMOVED) {
                    187:                if (verbosity > 1 )
                    188:                        cvs_log(LP_WARN,
                    189:                            "file `%s' already scheduled for removal",
                    190:                            cf->cf_name);
                    191:                return (0);
                    192:        } else {
                    193:                if (verbosity > 1)
                    194:                        cvs_log(LP_INFO, "scheduling file `%s' for removal",
                    195:                            cf->cf_name);
                    196:                removed++;
                    197:        }
                    198:
1.25      xsa       199:        if (removed != 0) {
1.20      xsa       200:                if (verbosity > 0)
                    201:                        cvs_log(LP_INFO, "use '%s commit' to remove %s "
                    202:                            "permanently", __progname,
1.27    ! xsa       203:                            (removed == 1) ? "this file" : "these files");
1.20      xsa       204:                return (0);
                    205:        }
                    206:
1.25      xsa       207:        if (existing != 0) {
1.20      xsa       208:                cvs_log(LP_WARN, ((existing == 1) ?
                    209:                    "%d file exists; remove it first" :
                    210:                    "%d files exist; remove them first"), existing);
                    211:                return (0);
                    212:        }
                    213:
                    214:        return (0);
                    215: }
                    216:
                    217: /*
                    218:  * cvs_remove_file()
                    219:  *
                    220:  * Physically remove the file.
                    221:  * Used by both remote and local handlers.
                    222:  * Returns 0 on success, -1 on failure.
                    223:  */
                    224: static
                    225: int cvs_remove_file(const char *fpath)
                    226: {
                    227:        /* if -f option is used, physically remove the file */
1.24      xsa       228:        if (force_remove == 1) {
                    229:                if((cvs_unlink(fpath) == -1) && (errno != ENOENT))
1.20      xsa       230:                        return (-1);
                    231:                nuked++;
                    232:        }
1.1       xsa       233:
1.18      joris     234:        return (0);
1.1       xsa       235: }