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

1.47    ! joris       1: /*     $OpenBSD: remove.c,v 1.46 2006/05/29 05:34:31 joris Exp $       */
1.1       xsa         2: /*
1.46      joris       3:  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1       xsa         4:  *
1.46      joris       5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       xsa         8:  *
1.46      joris       9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       xsa        16:  */
                     17:
1.40      xsa        18: #include "includes.h"
1.1       xsa        19:
                     20: #include "cvs.h"
                     21: #include "log.h"
                     22: #include "proto.h"
                     23:
                     24: extern char *__progname;
                     25:
1.46      joris      26: int            cvs_remove(int, char **);
                     27: void           cvs_remove_local(struct cvs_file *);
1.1       xsa        28:
1.46      joris      29: static int     force_remove = 0;
                     30: static int     removed = 0;
                     31: static int     existing = 0;
1.4       joris      32:
1.16      jfb        33: struct cvs_cmd cvs_cmd_remove = {
                     34:        CVS_OP_REMOVE, CVS_REQ_REMOVE, "remove",
                     35:        { "rm", "delete" },
                     36:        "Remove an entry from the repository",
                     37:        "[-flR] [file ...]",
                     38:        "flR",
1.4       joris      39:        NULL,
1.46      joris      40:        cvs_remove
1.4       joris      41: };
1.1       xsa        42:
1.46      joris      43: int
                     44: cvs_remove(int argc, char **argv)
1.1       xsa        45: {
1.4       joris      46:        int ch;
1.47    ! joris      47:        int flags;
1.46      joris      48:        char *arg = ".";
                     49:        struct cvs_recursion cr;
1.1       xsa        50:
1.47    ! joris      51:        flags = CR_RECURSE_DIRS;
1.46      joris      52:        while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1       xsa        53:                switch (ch) {
                     54:                case 'f':
1.3       xsa        55:                        force_remove = 1;
1.1       xsa        56:                        break;
                     57:                case 'l':
1.47    ! joris      58:                        flags &= ~CR_RECURSE_DIRS;
1.1       xsa        59:                        break;
                     60:                case 'R':
                     61:                        break;
                     62:                default:
1.46      joris      63:                        fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1       xsa        64:                }
                     65:        }
                     66:
                     67:        argc -= optind;
                     68:        argv += optind;
                     69:
1.46      joris      70:        cr.enterdir = NULL;
                     71:        cr.leavedir = NULL;
                     72:        cr.local = cvs_remove_local;
                     73:        cr.remote = NULL;
1.47    ! joris      74:        cr.flags = flags;
1.46      joris      75:
                     76:        if (argc > 0)
                     77:                cvs_file_run(argc, argv, &cr);
                     78:        else
                     79:                cvs_file_run(1, &arg, &cr);
1.11      xsa        80:
1.18      joris      81:        return (0);
                     82: }
                     83:
1.46      joris      84: void
                     85: cvs_remove_local(struct cvs_file *cf)
1.18      joris      86: {
1.46      joris      87:        int l;
                     88:        CVSENTRIES *entlist;
                     89:        char *entry, buf[MAXPATHLEN], tbuf[32], rbuf[16];
1.20      xsa        90:
1.46      joris      91:        cvs_log(LP_TRACE, "cvs_remove_local(%s)", cf->file_path);
1.20      xsa        92:
1.46      joris      93:        if (cf->file_type == CVS_DIR) {
1.19      xsa        94:                if (verbosity > 1)
1.46      joris      95:                        cvs_log(LP_NOTICE, "Removing %s", cf->file_path);
                     96:                return;
1.19      xsa        97:        }
                     98:
1.46      joris      99:        cvs_file_classify(cf, 0);
1.20      xsa       100:
1.46      joris     101:        if (force_remove == 1) {
                    102:                if (unlink(cf->file_path) == -1)
                    103:                        fatal("cvs_remove_local: %s", strerror(errno));
                    104:                (void)close(cf->fd);
                    105:                cf->fd = -1;
1.20      xsa       106:        }
                    107:
1.46      joris     108:        if (cf->fd != -1) {
                    109:                if (verbosity > 1)
                    110:                        cvs_log(LP_ERR, "file `%s' still in working directory",
                    111:                            cf->file_name);
1.20      xsa       112:                existing++;
                    113:        } else {
1.46      joris     114:                switch(cf->file_status) {
                    115:                case FILE_UNKNOWN:
                    116:                        if (verbosity > 1) {
                    117:                                cvs_log(LP_ERR, "nothing known about `%s'",
                    118:                                    cf->file_name);
                    119:                        }
                    120:                        return;
                    121:                case FILE_ADDED:
                    122:                        entlist = cvs_ent_open(cf->file_wd);
                    123:                        cvs_ent_remove(entlist, cf->file_name);
                    124:                        cvs_ent_close(entlist, ENT_SYNC);
                    125:
                    126:                        l = snprintf(buf, sizeof(buf), "%s/%s/%s%s",
                    127:                            cf->file_path, CVS_PATH_CVSDIR, cf->file_name,
                    128:                            CVS_DESCR_FILE_EXT);
                    129:                        if (l == -1 || l >= (int)sizeof(buf))
                    130:                                fatal("cvs_remove_local: overflow");
                    131:
                    132:                        (void)unlink(buf);
                    133:
                    134:                        if (verbosity > 1) {
                    135:                                cvs_log(LP_NOTICE, "removed `%s'",
                    136:                                    cf->file_name);
                    137:                        }
                    138:                        return;
                    139:                case FILE_REMOVED:
                    140:                        if (verbosity > 1 ) {
                    141:                                cvs_log(LP_ERR,
                    142:                                    "file `%s' already scheduled for removal",
                    143:                                    cf->file_name);
                    144:                        }
                    145:                        return;
                    146:                default:
                    147:                        rcsnum_tostr(cf->file_ent->ce_rev, rbuf,
                    148:                             sizeof(rbuf));
1.34      xsa       149:
1.46      joris     150:                        ctime_r(&cf->file_ent->ce_mtime, tbuf);
                    151:                        if (tbuf[strlen(tbuf) - 1] == '\n')
                    152:                                tbuf[strlen(tbuf) - 1] = '\0';
                    153:
                    154:                        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    155:                        l = snprintf(entry, CVS_ENT_MAXLINELEN,
                    156:                             "/%s/-%s/%s//", cf->file_name, rbuf, tbuf);
                    157:                        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    158:                                fatal("cvs_remove_local: overflow");
                    159:
                    160:                        entlist = cvs_ent_open(cf->file_wd);
                    161:                        cvs_ent_add(entlist, entry);
                    162:                        cvs_ent_close(entlist, ENT_SYNC);
                    163:
                    164:                        xfree(entry);
                    165:
                    166:                        if (verbosity > 1) {
                    167:                                cvs_log(LP_NOTICE,
                    168:                                    "scheduling file `%s' for removal",
                    169:                                    cf->file_name);
                    170:                        }
1.34      xsa       171:
1.46      joris     172:                        cf->file_status = FILE_REMOVED;
                    173:                        removed++;
                    174:                        break;
                    175:                }
1.20      xsa       176:        }
                    177:
1.46      joris     178:        if (cf->file_status == FILE_REMOVED) {
1.20      xsa       179:                if (verbosity > 0)
1.30      xsa       180:                        cvs_log(LP_NOTICE, "use '%s commit' to remove %s "
1.20      xsa       181:                            "permanently", __progname,
1.27      xsa       182:                            (removed == 1) ? "this file" : "these files");
1.35      xsa       183:        } else {
1.46      joris     184:                if (cf->fd != -1) {
                    185:                        cvs_log(LP_ERR, existing == 1 ?
                    186:                            "%d file exists; remove it first" :
                    187:                            "%d files exist; remove them first", existing);
                    188:                }
1.20      xsa       189:        }
1.1       xsa       190: }