]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/cvs/src/remove.c
This commit was generated by cvs2svn to compensate for changes in r49795,
[FreeBSD/FreeBSD.git] / contrib / cvs / src / remove.c
1 /*
2  * Copyright (c) 1992, Brian Berliner and Jeff Polk
3  * Copyright (c) 1989-1992, Brian Berliner
4  * 
5  * You may distribute under the terms of the GNU General Public License as
6  * specified in the README file that comes with the CVS source distribution.
7  * 
8  * Remove a File
9  * 
10  * Removes entries from the present version. The entries will be removed from
11  * the RCS repository upon the next "commit".
12  * 
13  * "remove" accepts no options, only file names that are to be removed.  The
14  * file must not exist in the current directory for "remove" to work
15  * correctly.
16  */
17
18 #include "cvs.h"
19
20 #ifdef CLIENT_SUPPORT
21 static int remove_force_fileproc PROTO ((void *callerdat,
22                                          struct file_info *finfo));
23 #endif
24 static int remove_fileproc PROTO ((void *callerdat, struct file_info *finfo));
25 static Dtype remove_dirproc PROTO ((void *callerdat, char *dir,
26                                     char *repos, char *update_dir,
27                                     List *entries));
28
29 static int force;
30 static int local;
31 static int removed_files;
32 static int existing_files;
33
34 static const char *const remove_usage[] =
35 {
36     "Usage: %s %s [-flR] [files...]\n",
37     "\t-f\tDelete the file before removing it.\n",
38     "\t-l\tProcess this directory only (not recursive).\n",
39     "\t-R\tProcess directories recursively.\n",
40     "(Specify the --help global option for a list of other help options)\n",
41     NULL
42 };
43
44 int
45 cvsremove (argc, argv)
46     int argc;
47     char **argv;
48 {
49     int c, err;
50
51     if (argc == -1)
52         usage (remove_usage);
53
54     optind = 0;
55     while ((c = getopt (argc, argv, "+flR")) != -1)
56     {
57         switch (c)
58         {
59             case 'f':
60                 force = 1;
61                 break;
62             case 'l':
63                 local = 1;
64                 break;
65             case 'R':
66                 local = 0;
67                 break;
68             case '?':
69             default:
70                 usage (remove_usage);
71                 break;
72         }
73     }
74     argc -= optind;
75     argv += optind;
76
77     wrap_setup ();
78
79 #ifdef CLIENT_SUPPORT
80     if (client_active) {
81         /* Call expand_wild so that the local removal of files will
82            work.  It's ok to do it always because we have to send the
83            file names expanded anyway.  */
84         expand_wild (argc, argv, &argc, &argv);
85         
86         if (force)
87         {
88             if (!noexec)
89             {
90                 start_recursion (remove_force_fileproc, (FILESDONEPROC) NULL,
91                                  (DIRENTPROC) NULL, (DIRLEAVEPROC) NULL,
92                                  (void *) NULL, argc, argv, local, W_LOCAL,
93                                  0, 0, (char *) NULL, 0);
94             }
95             /* else FIXME should probably act as if the file doesn't exist
96                in doing the following checks.  */
97         }
98
99         start_server ();
100         ign_setup ();
101         if (local)
102             send_arg("-l");
103         send_file_names (argc, argv, 0);
104         /* FIXME: Can't we set SEND_NO_CONTENTS here?  Needs investigation.  */
105         send_files (argc, argv, local, 0, 0);
106         send_to_server ("remove\012", 0);
107         return get_responses_and_close ();
108     }
109 #endif
110
111     /* start the recursion processor */
112     err = start_recursion (remove_fileproc, (FILESDONEPROC) NULL,
113                            remove_dirproc, (DIRLEAVEPROC) NULL, NULL,
114                            argc, argv,
115                            local, W_LOCAL, 0, 1, (char *) NULL, 1);
116
117     if (removed_files)
118         error (0, 0, "use '%s commit' to remove %s permanently", program_name,
119                (removed_files == 1) ? "this file" : "these files");
120
121     if (existing_files)
122         error (0, 0,
123                ((existing_files == 1) ?
124                 "%d file exists; remove it first" :
125                 "%d files exist; remove them first"),
126                existing_files);
127
128     return (err);
129 }
130
131 #ifdef CLIENT_SUPPORT
132
133 /*
134  * This is called via start_recursion if we are running as the client
135  * and the -f option was used.  We just physically remove the file.
136  */
137
138 /*ARGSUSED*/
139 static int
140 remove_force_fileproc (callerdat, finfo)
141      void *callerdat;
142      struct file_info *finfo;
143 {
144     if (CVS_UNLINK (finfo->file) < 0 && ! existence_error (errno))
145         error (0, errno, "unable to remove %s", finfo->fullname);
146     return 0;
147 }
148
149 #endif
150
151 /*
152  * remove the file, only if it has already been physically removed
153  */
154 /* ARGSUSED */
155 static int
156 remove_fileproc (callerdat, finfo)
157     void *callerdat;
158     struct file_info *finfo;
159 {
160     Vers_TS *vers;
161
162     if (force)
163     {
164         if (!noexec)
165         {
166             if ( CVS_UNLINK (finfo->file) < 0 && ! existence_error (errno))
167             {
168                 error (0, errno, "unable to remove %s", finfo->fullname);
169             }
170         }
171         /* else FIXME should probably act as if the file doesn't exist
172            in doing the following checks.  */
173     }
174
175     vers = Version_TS (finfo, NULL, NULL, NULL, 0, 0);
176
177     if (vers->ts_user != NULL)
178     {
179         existing_files++;
180         if (!quiet)
181             error (0, 0, "file `%s' still in working directory",
182                    finfo->fullname);
183     }
184     else if (vers->vn_user == NULL)
185     {
186         if (!quiet)
187             error (0, 0, "nothing known about `%s'", finfo->fullname);
188     }
189     else if (vers->vn_user[0] == '0' && vers->vn_user[1] == '\0')
190     {
191         char *fname;
192
193         /*
194          * It's a file that has been added, but not commited yet. So,
195          * remove the ,t file for it and scratch it from the
196          * entries file.  */
197         Scratch_Entry (finfo->entries, finfo->file);
198         fname = xmalloc (strlen (finfo->file)
199                          + sizeof (CVSADM)
200                          + sizeof (CVSEXT_LOG)
201                          + 10);
202         (void) sprintf (fname, "%s/%s%s", CVSADM, finfo->file, CVSEXT_LOG);
203         (void) unlink_file (fname);
204         if (!quiet)
205             error (0, 0, "removed `%s'", finfo->fullname);
206
207 #ifdef SERVER_SUPPORT
208         if (server_active)
209             server_checked_in (finfo->file, finfo->update_dir, finfo->repository);
210 #endif
211         free (fname);
212     }
213     else if (vers->vn_user[0] == '-')
214     {
215         if (!quiet)
216             error (0, 0, "file `%s' already scheduled for removal",
217                    finfo->fullname);
218     }
219     else if (vers->tag != NULL && isdigit (*vers->tag))
220     {
221         /* Commit will just give an error, and so there seems to be
222            little reason to allow the remove.  I mean, conflicts that
223            arise out of parallel development are one thing, but conflicts
224            that arise from sticky tags are quite another.
225
226            I would have thought that non-branch sticky tags should be the
227            same but at least now, removing a file with a non-branch sticky
228            tag means to delete the tag from the file.  I'm not sure that
229            is a good behavior, but until it is changed, we need to allow
230            it.  */
231         error (0, 0, "\
232 cannot remove file `%s' which has a numeric sticky tag of `%s'",
233                finfo->fullname, vers->tag);
234     }
235     else
236     {
237         char *fname;
238
239         /* Re-register it with a negative version number.  */
240         fname = xmalloc (strlen (vers->vn_user) + 5);
241         (void) strcpy (fname, "-");
242         (void) strcat (fname, vers->vn_user);
243         Register (finfo->entries, finfo->file, fname, vers->ts_rcs, vers->options,
244                   vers->tag, vers->date, vers->ts_conflict);
245         if (!quiet)
246             error (0, 0, "scheduling `%s' for removal", finfo->fullname);
247         removed_files++;
248
249 #ifdef SERVER_SUPPORT
250         if (server_active)
251             server_checked_in (finfo->file, finfo->update_dir, finfo->repository);
252 #endif
253         free (fname);
254     }
255
256     freevers_ts (&vers);
257     return (0);
258 }
259
260 /*
261  * Print a warm fuzzy message
262  */
263 /* ARGSUSED */
264 static Dtype
265 remove_dirproc (callerdat, dir, repos, update_dir, entries)
266     void *callerdat;
267     char *dir;
268     char *repos;
269     char *update_dir;
270     List *entries;
271 {
272     if (!quiet)
273         error (0, 0, "Removing %s", update_dir);
274     return (R_PROCESS);
275 }