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

1.59    ! joris       1: /*     $OpenBSD: remove.c,v 1.58 2007/01/03 22:28:30 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"
1.55      xsa        22: #include "remote.h"
1.1       xsa        23:
                     24: extern char *__progname;
                     25:
1.46      joris      26: void           cvs_remove_local(struct cvs_file *);
1.59    ! joris      27: void           cvs_remove_force(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 = {
1.53      joris      34:        CVS_OP_REMOVE, 0, "remove",
1.16      jfb        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.48      xsa        52:        while ((ch = getopt(argc, argv, cvs_cmd_remove.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.48      xsa        63:                        fatal("%s", cvs_cmd_remove.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;
1.59    ! joris      72:        cr.flags = flags;
        !            73:
        !            74:        cr.fileproc = cvs_remove_force;
        !            75:        if (argc > 0)
        !            76:                cvs_file_run(argc, argv, &cr);
        !            77:        else
        !            78:                cvs_file_run(1, &arg, &cr);
1.55      xsa        79:
                     80:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                     81:                cr.fileproc = cvs_client_sendfile;
                     82:
                     83:                if (!(flags & CR_RECURSE_DIRS))
                     84:                        cvs_client_send_request("Argument -l");
                     85:        } else {
                     86:                cr.fileproc = cvs_remove_local;
                     87:        }
                     88:
1.46      joris      89:        if (argc > 0)
                     90:                cvs_file_run(argc, argv, &cr);
                     91:        else
                     92:                cvs_file_run(1, &arg, &cr);
1.11      xsa        93:
1.55      xsa        94:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                     95:                cvs_client_send_files(argv, argc);
                     96:                cvs_client_senddir(".");
                     97:                cvs_client_send_request("remove");
                     98:                cvs_client_get_responses();
                     99:        } else {
                    100:                if (existing != 0) {
1.56      xsa       101:                        cvs_log(LP_ERR, (existing == 1) ?
                    102:                            "%d file exists; remove it first" :
                    103:                            "%d files exist; remove them first", existing);
1.55      xsa       104:                }
1.49      joris     105:
1.55      xsa       106:                if (removed != 0) {
1.58      joris     107:                        if (verbosity > 0) {
1.55      xsa       108:                                cvs_log(LP_NOTICE,
                    109:                                    "use '%s commit' to remove %s "
                    110:                                    "permanently", __progname, (removed > 1) ?
                    111:                                    "these files" : "this file");
                    112:                        }
                    113:                }
1.49      joris     114:        }
                    115:
1.18      joris     116:        return (0);
                    117: }
                    118:
1.46      joris     119: void
1.59    ! joris     120: cvs_remove_force(struct cvs_file *cf)
        !           121: {
        !           122:        if (cf->file_type != CVS_DIR) {
        !           123:                if (force_remove == 1 && cvs_noexec == 0) {
        !           124:                        if (unlink(cf->file_path) == -1)
        !           125:                                fatal("cvs_remove_force: %s", strerror(errno));
        !           126:                        (void)close(cf->fd);
        !           127:                        cf->fd = -1;
        !           128:                }
        !           129:        }
        !           130: }
        !           131:
        !           132: void
1.46      joris     133: cvs_remove_local(struct cvs_file *cf)
1.18      joris     134: {
1.46      joris     135:        int l;
                    136:        CVSENTRIES *entlist;
                    137:        char *entry, buf[MAXPATHLEN], tbuf[32], rbuf[16];
1.20      xsa       138:
1.46      joris     139:        cvs_log(LP_TRACE, "cvs_remove_local(%s)", cf->file_path);
1.20      xsa       140:
1.46      joris     141:        if (cf->file_type == CVS_DIR) {
1.19      xsa       142:                if (verbosity > 1)
1.46      joris     143:                        cvs_log(LP_NOTICE, "Removing %s", cf->file_path);
                    144:                return;
1.19      xsa       145:        }
                    146:
1.50      joris     147:        cvs_file_classify(cf, NULL, 0);
1.51      joris     148:
                    149:        if (cf->file_status == FILE_UNKNOWN) {
                    150:                if (verbosity > 1)
                    151:                        cvs_log(LP_NOTICE, "nothing known about '%s'",
                    152:                            cf->file_path);
                    153:                return;
1.20      xsa       154:        }
                    155:
1.46      joris     156:        if (cf->fd != -1) {
                    157:                if (verbosity > 1)
                    158:                        cvs_log(LP_ERR, "file `%s' still in working directory",
                    159:                            cf->file_name);
1.20      xsa       160:                existing++;
                    161:        } else {
1.58      joris     162:                switch (cf->file_status) {
1.46      joris     163:                case FILE_ADDED:
                    164:                        entlist = cvs_ent_open(cf->file_wd);
                    165:                        cvs_ent_remove(entlist, cf->file_name);
                    166:                        cvs_ent_close(entlist, ENT_SYNC);
                    167:
                    168:                        l = snprintf(buf, sizeof(buf), "%s/%s/%s%s",
                    169:                            cf->file_path, CVS_PATH_CVSDIR, cf->file_name,
                    170:                            CVS_DESCR_FILE_EXT);
                    171:                        if (l == -1 || l >= (int)sizeof(buf))
                    172:                                fatal("cvs_remove_local: overflow");
                    173:
                    174:                        (void)unlink(buf);
                    175:
                    176:                        if (verbosity > 1) {
                    177:                                cvs_log(LP_NOTICE, "removed `%s'",
                    178:                                    cf->file_name);
                    179:                        }
                    180:                        return;
                    181:                case FILE_REMOVED:
1.58      joris     182:                        if (verbosity > 0) {
1.46      joris     183:                                cvs_log(LP_ERR,
                    184:                                    "file `%s' already scheduled for removal",
                    185:                                    cf->file_name);
                    186:                        }
                    187:                        return;
                    188:                default:
                    189:                        rcsnum_tostr(cf->file_ent->ce_rev, rbuf,
                    190:                             sizeof(rbuf));
1.34      xsa       191:
1.46      joris     192:                        ctime_r(&cf->file_ent->ce_mtime, tbuf);
                    193:                        if (tbuf[strlen(tbuf) - 1] == '\n')
                    194:                                tbuf[strlen(tbuf) - 1] = '\0';
                    195:
                    196:                        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    197:                        l = snprintf(entry, CVS_ENT_MAXLINELEN,
                    198:                             "/%s/-%s/%s//", cf->file_name, rbuf, tbuf);
                    199:                        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    200:                                fatal("cvs_remove_local: overflow");
                    201:
1.58      joris     202:                        if (cvs_server_active == 1) {
                    203:                                cvs_server_update_entry("Checked-in", cf);
                    204:                                cvs_remote_output(entry);
                    205:                        } else {
                    206:                                entlist = cvs_ent_open(cf->file_wd);
                    207:                                cvs_ent_add(entlist, entry);
                    208:                                cvs_ent_close(entlist, ENT_SYNC);
                    209:                        }
1.46      joris     210:
                    211:                        xfree(entry);
                    212:
1.58      joris     213:                        if (verbosity > 0) {
1.46      joris     214:                                cvs_log(LP_NOTICE,
                    215:                                    "scheduling file `%s' for removal",
                    216:                                    cf->file_name);
                    217:                        }
1.34      xsa       218:
1.46      joris     219:                        cf->file_status = FILE_REMOVED;
                    220:                        removed++;
                    221:                        break;
                    222:                }
1.20      xsa       223:        }
1.1       xsa       224: }