1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/* Clean up working files.
Copyright (C) 2010-2012 Thien-Thi Nguyen
Copyright (C) 1991, 1992, 1993, 1994, 1995 Paul Eggert
This file is part of GNU RCS.
GNU RCS is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base.h"
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <dirent.h>
#include "same-inode.h"
#include "rcsclean.help"
#include "b-complain.h"
#include "b-divvy.h"
#include "b-esds.h"
#include "b-excwho.h"
#include "b-fb.h"
#include "b-feph.h"
#include "b-fro.h"
struct top *top;
static struct fro *workptr;
static int exitstatus;
static void
cleanup (void)
{
if (FLOW (erroneousp))
exitstatus = EXIT_FAILURE;
fro_zclose (&FLOW (from));
fro_zclose (&workptr);
Ozclose (&FLOW (res));
ORCSclose ();
dirtempunlink ();
}
static bool
unlock (struct delta *delta)
{
struct link box, *tp;
if (delta && delta->lockedby
&& caller_login_p (delta->lockedby)
&& (box.next = GROK (locks))
&& (tp = lock_delta_memq (&box, delta)))
{
lock_drop (&box, tp);
return true;
}
return false;
}
static int
get_directory (char const *dirname, char ***aargv)
/* Put a vector of all DIRNAME's directory entries names into *AARGV.
Ignore names of RCS files.
Return the number of entries found. Terminate the vector with 0.
Allocate the storage for the vector and entry names.
Do not sort the names. Do not include '.' and '..'. */
{
DIR *d;
struct dirent *e;
struct divvy *justme = make_space ("justme");
size_t i, entries = 0;
struct wlink head, *tp;
if (!(d = opendir (dirname)))
fatal_sys (dirname);
head.next = NULL;
tp = &head;
while ((errno = 0, e = readdir (d)))
{
char const *en = e->d_name;
if (en[0] == '.' && (!en[1] || (en[1] == '.' && !en[2])))
continue;
if (rcssuffix (en))
continue;
tp = wextend (tp, str_save (en), justme);
entries++;
}
if (errno || PROB (closedir (d)))
fatal_sys (dirname);
*aargv = pointer_array (PLEXUS, entries);
for (tp = head.next, i = 0; i < entries; tp = tp->next, i++)
(*aargv)[i] = tp->entry;
close_space (justme);
return entries;
}
int
main (int argc, char **argv)
{
char *a, **newargv;
char const *rev, *p;
bool dounlock, perform, unlocked, unlockflag, waslocked, Ttimeflag;
int expmode;
struct wlink *deltas;
struct delta *delta;
struct stat workstat;
const struct program program =
{
.invoke = argv[0],
.name = "rcsclean",
.help = rcsclean_help,
.tyag = BOG_FULL
};
CHECK_HV ();
gnurcs_init (&program);
setrid ();
expmode = -1;
rev = NULL;
BE (pe) = X_DEFAULT;
perform = true;
unlockflag = false;
Ttimeflag = false;
argc = getRCSINIT (argc, argv, &newargv);
argv = newargv;
for (;;)
{
if (--argc < 1)
{
argc = get_directory (".", &newargv);
argv = newargv;
break;
}
a = *++argv;
if (!*a || *a++ != '-')
break;
switch (*a++)
{
case 'k':
if (0 <= expmode)
redefined ('k');
if (PROB (expmode = str2expmode (a)))
goto unknown;
break;
case 'n':
perform = false;
goto handle_revision;
case 'q':
BE (quiet) = true;
/* fall into */
case 'r':
handle_revision:
if (*a)
{
if (rev)
PWARN ("redefinition of %s", ks_revno);
rev = a;
}
break;
case 'T':
if (*a)
goto unknown;
Ttimeflag = true;
break;
case 'u':
unlockflag = true;
goto handle_revision;
case 'V':
setRCSversion (*argv);
break;
case 'x':
BE (pe) = a;
break;
case 'z':
zone_set (a);
break;
default:
unknown:
bad_option (*argv);
}
}
dounlock = perform & unlockflag;
if (FLOW (erroneousp))
cleanup ();
else
for (; 0 < argc; cleanup (), ++argv, --argc)
{
struct stat *repo_stat;
char const *mani_filename;
ffree ();
if (!(0 < pairnames (argc, argv,
dounlock ? rcswriteopen : rcsreadopen,
true, true)
&& (mani_filename = MANI (filename))
&& (workptr = fro_open (mani_filename, FOPEN_R_WORK, &workstat))))
continue;
repo_stat = &REPO (stat);
if (SAME_INODE (REPO (stat), workstat))
{
RERR ("RCS file is the same as working file %s.", mani_filename);
continue;
}
p = NULL;
if (rev)
{
struct cbuf numeric;
if (!fully_numeric (&numeric, rev, workptr))
continue;
p = numeric.string;
}
else if (REPO (tip))
switch (unlockflag ? findlock (false, &delta) : 0)
{
default:
continue;
case 0:
p = GROK (branch) ? GROK (branch) : "";
break;
case 1:
p = delta->num;
break;
}
delta = NULL;
deltas = NULL; /* Keep lint happy. */
if (p && !(delta = gr_revno (p, &deltas)))
continue;
waslocked = delta && delta->lockedby;
BE (inclusive_of_Locker_in_Id_val) = unlock (delta);
unlocked = BE (inclusive_of_Locker_in_Id_val) & unlockflag;
if (unlocked < waslocked
&& workstat.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
continue;
if (unlocked && !checkaccesslist ())
continue;
if (PROB (dorewrite (dounlock, unlocked)))
continue;
if (0 <= expmode)
BE (kws) = expmode;
else if (waslocked
&& BE (kws) == kwsub_kv
&& WORKMODE (repo_stat->st_mode, true) == workstat.st_mode)
BE (kws) = kwsub_kvl;
write_desc_maybe (FLOW (to));
if (!delta
? workstat.st_size != 0
: 0 < rcsfcmp (workptr, &workstat,
buildrevision (deltas, delta, NULL, false),
delta))
continue;
if (BE (quiet) < unlocked)
aprintf (stdout, "rcs -u%s %s\n", delta->num, REPO (filename));
if (perform & unlocked)
{
struct fro *from = FLOW (from);
SAME_AFTER (from, delta->text);
if (deltas->entry != delta)
fro_trundling (true, from);
if (PROB (donerewrite (true, Ttimeflag
? repo_stat->st_mtime
: (time_t) - 1)))
continue;
}
if (!BE (quiet))
aprintf (stdout, "rm -f %s\n", mani_filename);
fro_zclose (&workptr);
if (perform && PROB (un_link (mani_filename)))
syserror_errno (mani_filename);
}
tempunlink ();
if (!BE (quiet))
fclose (stdout);
gnurcs_goodbye ();
return exitstatus;
}
/*:help
[options] file ...
Options:
-r[REV] Specify revision.
-u[REV] Unlock if is locked and no differences found.
-n[REV] Dry run (no act, don't operate).
-q[REV] Quiet mode.
-kSUBST Substitute using mode SUBST (see co(1)).
-T Preserve the modification time on the RCS file
even if it changes because a lock is removed.
-V Like --version.
-VN Emulate RCS version N.
-xSUFF Specify SUFF as a slash-separated list of suffixes
used to identify RCS file names.
-zZONE Specify date output format in keyword-substitution.
REV defaults to the latest revision on the default branch.
*/
/* rcsclean.c ends here */
|