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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/* $Header: /home/ksheff/src/e2tools/RCS/rm.c,v 0.3 2004/04/06 23:39:28 ksheff Exp $ */
/*
* rm.c
*
* Copyright (C) 2002 Keith W Sheffield. This file may be redistributed
* under the terms of the GNU Public License.
*
* Derived from debugfs.c Copyright (C) 1993 Theodore Ts'o <tytso@mit.edu>
* and modified by Robert Sanders <gt8134b@prism.gatech.edu>
* as well as portions of the ext2fs library (C) 1993-1995 Theodore Ts'o
*/
#ifndef RM_C
#define RM_C
#endif
/* Description */
/*
* This file contains function used to remove files in an ext2fs filesystem.
*
*/
/*
* $Log: rm.c,v $
* Revision 0.3 2004/04/06 23:39:28 ksheff
* Corrected getopt and usage strings.
*
* Revision 0.2 2002/06/05 23:39:13 ksheff
* Added ability to delete files based on a regular expression and recursive
* deletion of directories.
*
* Revision 0.1 2002/04/10 10:42:40 ksheff
* initial revision
*
*/
/* Headers */
#include "e2tools.h"
#include <regex.h>
static ext2_filsys gbl_fs = NULL;
static char *gbl_filesys = NULL;
static char *gbl_path_end = NULL;
static char gbl_dir_name[8192];
struct regexp_args
{
int verbose;
int recursive;
regex_t *reg;
};
/* Local Prototypes */
long
e2rm(int argc, char *argv[]);
static
int rm_dir_proc(ext2_ino_t dir,int entry, struct ext2_dir_entry *dirent,
int offset, int blocksize, char *buf, void *verbose);
static int
recursive_rm(ext2_ino_t dir, char *name, struct ext2_dir_entry *dirent,
int verbose);
static int
rm_regex_proc(ext2_ino_t dir, int entry, struct ext2_dir_entry *dirent,
int offset, int blocksize, char *buf, void *private);
/* Name: e2rm()
*
* Description:
*
* This function extracts the command line parameters and creates
* directories based on user input
*
* Algorithm:
*
* Extract any command line switches
* Sort the remaining arguments
* For each argument
* Open a new filesystem if needed
* Remove the file if it exists.
*
* Global Variables:
*
* None
*
* Arguments:
*
* int argc; The number of arguments
* char *argv[]; the command line arguments
*
* Return Values:
*
* 0 - the filess were created successfully
* otherwise, an error code is returned
*
* Author: Keith W. Sheffield
* Date: 04/10/2002
*
* Modification History:
*
* MM/DD/YY Name Description
* 04/06/04 K.Sheffield Corrected getopt and usage strings.
*/
long
e2rm(int argc, char *argv[])
{
int verbose=0;
int errcnt=0;
int recursive=0;
char *last_filesys = NULL;
char *cur_filesys = NULL;
ext2_filsys fs = NULL;
ext2_ino_t root;
ext2_ino_t cwd;
char **cur_opt;
char *filespec;
long retval;
char *dir_name;
char *base_name;
ext2_ino_t curr_ino;
int c;
int num_files;
char *ptr;
struct regexp_args reg;
#ifdef HAVE_OPTRESET
optreset = 1; /* Makes BSD getopt happy */
#endif
while ((c = getopt(argc, argv, "vr")) != EOF)
{
switch (c)
{
case 'v':
verbose = 1;
break;
case 'r':
recursive = 1;
break;
default:
errcnt++;
break;
}
}
if (errcnt || argc == optind)
{
fputs("Usage: e2rm [-vr] filesystem:filepath...\n", stderr);
return(1);
}
num_files = argc - optind;
cur_opt = argv + optind;
if (num_files > 1 )
qsort(cur_opt, num_files, sizeof(char *), my_strcmp);
for(c=0;c<num_files;c++)
{
filespec = *cur_opt++;
/* dealing with a filesystem or a regular file */
if (NULL != (ptr = strchr(filespec, ':')))
{
/* separate the filesystem name from the file name */
*ptr++ = '\0';
cur_filesys = filespec;
filespec = ptr;
/* check to see if the filesystem has changed */
if (last_filesys == NULL ||
strcmp(last_filesys, cur_filesys) != 0)
{
if (last_filesys != NULL &&
(retval = ext2fs_close(fs)))
{
fprintf(stderr, "%s\n", error_message(retval));
return retval;
}
if ((retval = open_filesystem(cur_filesys, &fs, &root, 1)))
{
fprintf(stderr, "%s\n", error_message(retval));
fprintf(stderr, "Error opening fileystem %s\n",
cur_filesys);
return retval;
}
cwd = root;
last_filesys = cur_filesys;
} /* end of filesystem change? */
if (get_file_parts(fs, root, filespec, &cwd, &dir_name, &base_name))
{
ext2fs_close(fs);
return(-1);
}
if (is_file_regexp(base_name))
{
if (NULL == (reg.reg = (regex_t *) make_regexp(base_name)))
{
fprintf(stderr,
"Error creating regular expression for %s\n",
base_name);
continue;
}
gbl_fs = fs;
gbl_filesys = cur_filesys;
reg.verbose = verbose;
reg.recursive = recursive;
strcpy(gbl_dir_name, dir_name);
gbl_path_end = gbl_dir_name + strlen(gbl_dir_name);
retval = ext2fs_dir_iterate2(gbl_fs, cwd,
DIRENT_FLAG_INCLUDE_EMPTY, 0,
rm_regex_proc, (void *) ®);
regfree(reg.reg);
continue;
}
/* check to see if the file name exists in the current directory */
else if ((retval = ext2fs_namei(fs, cwd, cwd, base_name, &curr_ino)))
{
if (retval != EXT2_ET_FILE_NOT_FOUND)
{
fprintf(stderr, "%s\n",error_message(retval));
return(retval);
}
}
/* file name exists, let's see if is a directory */
else if ((retval = ext2fs_check_directory(fs, curr_ino)) == 0)
{
if (recursive)
{
gbl_fs = fs;
gbl_filesys = cur_filesys;
sprintf(gbl_dir_name, "%s/%s", (dir_name == NULL)?".":
dir_name, base_name);
gbl_path_end = gbl_dir_name + strlen(gbl_dir_name);
retval = ext2fs_dir_iterate2(gbl_fs, curr_ino,
DIRENT_FLAG_INCLUDE_EMPTY, 0,
rm_dir_proc,
(void *)
(verbose) ? &verbose : NULL);
}
else
{
printf("%s:%s/%s is a directory! Not removed\n", cur_filesys,
(dir_name == NULL)?".": dir_name, base_name);
continue;
}
}
if (retval != 0 && retval != EXT2_ET_NO_DIRECTORY)
{
fprintf(stderr, "%s\n",error_message(retval));
return(retval);
}
/* delete the existing file if needed */
if ((retval = rm_file(fs, cwd, base_name, curr_ino)))
{
fprintf(stderr, "%s\n",error_message(retval));
return(retval);
}
if (verbose)
printf("Removed %s:%s/%s\n", cur_filesys,
(dir_name == NULL)?".":dir_name, base_name);
}
else if (verbose)
{
printf("%s is not a valid e2fs filesystem specification. skipping\n",
filespec);
}
}
if (fs != NULL &&
(retval = ext2fs_close(fs)))
{
fprintf(stderr, "%s\n", error_message(retval));
return retval;
}
return(0);
} /* end of e2rm */
static int rm_dir_proc(ext2_ino_t dir, int entry,
struct ext2_dir_entry *dirent, int offset,
int blocksize, char *buf, void *verbose)
{
char name[EXT2_NAME_LEN];
int thislen;
thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
(dirent->name_len & 0xFF) : EXT2_NAME_LEN;
if (dirent->name[0] == '.' &&
(thislen == 1 || (thislen == 2 && dirent->name[1] == '.')))
return 0;
strncpy(name, dirent->name, thislen);
name[thislen] = '\0';
if (entry == DIRENT_DELETED_FILE)
return 0;
return (recursive_rm(dir, name, dirent, (verbose) ? 1 : 0));
}
static int
recursive_rm(ext2_ino_t dir, char *name, struct ext2_dir_entry *dirent,
int verbose)
{
long retval;
/* file name exists, let's see if is a directory */
if ((retval = ext2fs_check_directory(gbl_fs, dirent->inode)) == 0)
{
*gbl_path_end++ = '/';
strncpy(gbl_path_end, dirent->name, dirent->name_len);
gbl_path_end += dirent->name_len;
*gbl_path_end = '\0';
retval = ext2fs_dir_iterate2(gbl_fs, dirent->inode,
DIRENT_FLAG_INCLUDE_EMPTY, 0,
rm_dir_proc, (verbose) ? &verbose : NULL);
while (gbl_path_end > gbl_dir_name && *gbl_path_end != '/')
gbl_path_end--;
*gbl_path_end = '\0';
}
if (retval !=0 && retval != EXT2_ET_NO_DIRECTORY)
{
fprintf(stderr, "%s\n",error_message(retval));
return(retval);
}
/* delete the existing file if needed */
if ((retval = rm_file(gbl_fs, dir, name, dirent->inode)))
{
fprintf(stderr, "%s\n",error_message(retval));
return(retval);
}
if (verbose)
printf("Removed %s:%s/%s\n", gbl_filesys, gbl_dir_name, name);
return 0;
}
static int
rm_regex_proc(ext2_ino_t dir, int entry, struct ext2_dir_entry *dirent,
int offset, int blocksize, char *buf, void *arg)
{
long retval;
char name[EXT2_NAME_LEN];
int thislen;
struct regexp_args *reg = (struct regexp_args *) arg;
thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN) ?
(dirent->name_len & 0xFF) : EXT2_NAME_LEN;
if (dirent->name[0] == '.' &&
(thislen == 1 || (thislen == 2 && dirent->name[1] == '.')))
return 0;
strncpy(name, dirent->name, thislen);
name[thislen] = '\0';
if (entry == DIRENT_DELETED_FILE)
return 0;
if (0 == (retval = regexec(reg->reg, name, 0, NULL, 0)))
{
if (reg->recursive)
return (recursive_rm(dir, name, dirent, (reg->verbose) ? 1 : 0));
else
{
if ((retval = ext2fs_check_directory(gbl_fs, dirent->inode)))
{
if (retval != EXT2_ET_NO_DIRECTORY)
{
fprintf(stderr, "%s\n",error_message(retval));
return(retval);
}
/* delete the existing file if needed */
if ((retval = rm_file(gbl_fs, dir, name, dirent->inode)))
{
fprintf(stderr, "%s\n",error_message(retval));
return(retval);
}
if (reg->verbose)
printf("Removed %s:%s/%s\n", gbl_filesys, gbl_dir_name, name);
}
else
{
printf("%s:%s/%s is a directory! Not removed\n", gbl_filesys,
gbl_dir_name, name);
}
}
}
return (0);
}
|