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

1.69    ! tobias      1: /*     $OpenBSD: remove.c,v 1.68 2008/01/28 20:31:07 tobias 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.64      otto       18: #include <errno.h>
                     19: #include <string.h>
                     20: #include <unistd.h>
1.1       xsa        21:
                     22: #include "cvs.h"
1.55      xsa        23: #include "remote.h"
1.1       xsa        24:
                     25: extern char *__progname;
                     26:
1.46      joris      27: void           cvs_remove_local(struct cvs_file *);
1.59      joris      28: void           cvs_remove_force(struct cvs_file *);
1.1       xsa        29:
1.46      joris      30: static int     force_remove = 0;
                     31: static int     removed = 0;
                     32: static int     existing = 0;
1.4       joris      33:
1.16      jfb        34: struct cvs_cmd cvs_cmd_remove = {
1.69    ! tobias     35:        CVS_OP_REMOVE, CVS_USE_WDIR, "remove",
1.16      jfb        36:        { "rm", "delete" },
                     37:        "Remove an entry from the repository",
                     38:        "[-flR] [file ...]",
                     39:        "flR",
1.4       joris      40:        NULL,
1.46      joris      41:        cvs_remove
1.4       joris      42: };
1.1       xsa        43:
1.46      joris      44: int
                     45: cvs_remove(int argc, char **argv)
1.1       xsa        46: {
1.4       joris      47:        int ch;
1.47      joris      48:        int flags;
1.46      joris      49:        char *arg = ".";
                     50:        struct cvs_recursion cr;
1.1       xsa        51:
1.47      joris      52:        flags = CR_RECURSE_DIRS;
1.48      xsa        53:        while ((ch = getopt(argc, argv, cvs_cmd_remove.cmd_opts)) != -1) {
1.1       xsa        54:                switch (ch) {
                     55:                case 'f':
1.3       xsa        56:                        force_remove = 1;
1.1       xsa        57:                        break;
                     58:                case 'l':
1.47      joris      59:                        flags &= ~CR_RECURSE_DIRS;
1.1       xsa        60:                        break;
                     61:                case 'R':
1.68      tobias     62:                        flags |= CR_RECURSE_DIRS;
1.1       xsa        63:                        break;
                     64:                default:
1.48      xsa        65:                        fatal("%s", cvs_cmd_remove.cmd_synopsis);
1.1       xsa        66:                }
                     67:        }
                     68:
                     69:        argc -= optind;
                     70:        argv += optind;
                     71:
1.46      joris      72:        cr.enterdir = NULL;
                     73:        cr.leavedir = NULL;
1.59      joris      74:        cr.flags = flags;
                     75:
                     76:        cr.fileproc = cvs_remove_force;
                     77:        if (argc > 0)
                     78:                cvs_file_run(argc, argv, &cr);
                     79:        else
                     80:                cvs_file_run(1, &arg, &cr);
1.55      xsa        81:
                     82:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.60      joris      83:                cvs_client_connect_to_server();
1.55      xsa        84:                cr.fileproc = cvs_client_sendfile;
                     85:
                     86:                if (!(flags & CR_RECURSE_DIRS))
                     87:                        cvs_client_send_request("Argument -l");
                     88:        } else {
                     89:                cr.fileproc = cvs_remove_local;
                     90:        }
                     91:
1.46      joris      92:        if (argc > 0)
                     93:                cvs_file_run(argc, argv, &cr);
                     94:        else
                     95:                cvs_file_run(1, &arg, &cr);
1.11      xsa        96:
1.55      xsa        97:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                     98:                cvs_client_send_files(argv, argc);
                     99:                cvs_client_senddir(".");
                    100:                cvs_client_send_request("remove");
                    101:                cvs_client_get_responses();
                    102:        } else {
                    103:                if (existing != 0) {
1.56      xsa       104:                        cvs_log(LP_ERR, (existing == 1) ?
                    105:                            "%d file exists; remove it first" :
                    106:                            "%d files exist; remove them first", existing);
1.55      xsa       107:                }
1.49      joris     108:
1.55      xsa       109:                if (removed != 0) {
1.58      joris     110:                        if (verbosity > 0) {
1.55      xsa       111:                                cvs_log(LP_NOTICE,
                    112:                                    "use '%s commit' to remove %s "
                    113:                                    "permanently", __progname, (removed > 1) ?
                    114:                                    "these files" : "this file");
                    115:                        }
                    116:                }
1.49      joris     117:        }
                    118:
1.18      joris     119:        return (0);
                    120: }
                    121:
1.46      joris     122: void
1.59      joris     123: cvs_remove_force(struct cvs_file *cf)
                    124: {
                    125:        if (cf->file_type != CVS_DIR) {
1.61      joris     126:                if (cf->fd != -1 && force_remove == 1 && cvs_noexec == 0) {
1.59      joris     127:                        if (unlink(cf->file_path) == -1)
                    128:                                fatal("cvs_remove_force: %s", strerror(errno));
                    129:                        (void)close(cf->fd);
                    130:                        cf->fd = -1;
                    131:                }
                    132:        }
                    133: }
                    134:
                    135: void
1.46      joris     136: cvs_remove_local(struct cvs_file *cf)
1.18      joris     137: {
1.46      joris     138:        CVSENTRIES *entlist;
1.65      xsa       139:        char *entry, buf[MAXPATHLEN], tbuf[CVS_TIME_BUFSZ], rbuf[CVS_REV_BUFSZ];
1.20      xsa       140:
1.46      joris     141:        cvs_log(LP_TRACE, "cvs_remove_local(%s)", cf->file_path);
1.20      xsa       142:
1.46      joris     143:        if (cf->file_type == CVS_DIR) {
1.19      xsa       144:                if (verbosity > 1)
1.46      joris     145:                        cvs_log(LP_NOTICE, "Removing %s", cf->file_path);
                    146:                return;
1.19      xsa       147:        }
                    148:
1.66      joris     149:        cvs_file_classify(cf, cvs_directory_tag);
1.51      joris     150:
                    151:        if (cf->file_status == FILE_UNKNOWN) {
                    152:                if (verbosity > 1)
                    153:                        cvs_log(LP_NOTICE, "nothing known about '%s'",
                    154:                            cf->file_path);
                    155:                return;
1.20      xsa       156:        }
                    157:
1.46      joris     158:        if (cf->fd != -1) {
                    159:                if (verbosity > 1)
                    160:                        cvs_log(LP_ERR, "file `%s' still in working directory",
                    161:                            cf->file_name);
1.20      xsa       162:                existing++;
                    163:        } else {
1.58      joris     164:                switch (cf->file_status) {
1.61      joris     165:                case FILE_REMOVE_ENTRY:
1.46      joris     166:                        entlist = cvs_ent_open(cf->file_wd);
                    167:                        cvs_ent_remove(entlist, cf->file_name);
                    168:                        cvs_ent_close(entlist, ENT_SYNC);
                    169:
1.62      xsa       170:                        (void)xsnprintf(buf, sizeof(buf), "%s/%s/%s%s",
1.46      joris     171:                            cf->file_path, CVS_PATH_CVSDIR, cf->file_name,
                    172:                            CVS_DESCR_FILE_EXT);
                    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);
1.67      tobias    193:                        tbuf[strcspn(tbuf, "\n")] = '\0';
1.46      joris     194:
                    195:                        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.62      xsa       196:                        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN,
1.46      joris     197:                             "/%s/-%s/%s//", cf->file_name, rbuf, tbuf);
                    198:
1.58      joris     199:                        if (cvs_server_active == 1) {
                    200:                                cvs_server_update_entry("Checked-in", cf);
                    201:                                cvs_remote_output(entry);
                    202:                        } else {
                    203:                                entlist = cvs_ent_open(cf->file_wd);
                    204:                                cvs_ent_add(entlist, entry);
                    205:                                cvs_ent_close(entlist, ENT_SYNC);
                    206:                        }
1.46      joris     207:
                    208:                        xfree(entry);
                    209:
1.58      joris     210:                        if (verbosity > 0) {
1.46      joris     211:                                cvs_log(LP_NOTICE,
                    212:                                    "scheduling file `%s' for removal",
                    213:                                    cf->file_name);
                    214:                        }
1.34      xsa       215:
1.46      joris     216:                        cf->file_status = FILE_REMOVED;
                    217:                        removed++;
                    218:                        break;
                    219:                }
1.20      xsa       220:        }
1.1       xsa       221: }