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
|
/* $Header: /home/ksheff/src/e2tools/RCS/util.c,v 0.4 2002/06/05 20:38:16 ksheff Exp $ */
/*
* util.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>
*/
#ifndef UTIL_C
#define UTIL_C
#endif
/* Description */
/*
* Misc. utility functions
*
*/
/*
* $Log: util.c,v $
* Revision 0.4 2002/06/05 20:38:16 ksheff
* Added regular expression routines.
*
* Revision 0.3 2002/04/10 09:31:21 ksheff
* Added function init_stat_buf().
*
* Revision 0.2 2002/03/07 07:27:45 ksheff
* checked for return of ext2fs_unlink in rm_file().
*
* Revision 0.1 2002/02/27 04:49:11 ksheff
* initial revision
*
*/
/* Headers */
#include "e2tools.h"
#include <regex.h>
/* Macros */
/* Structures and Unions */
typedef struct
{
__u16 lmask;
mode_t mask;
} MODE_XLAT_T;
static MODE_XLAT_T xlat_tbl[] =
{
{ LINUX_S_IFREG, S_IFREG },
{ LINUX_S_IFDIR, S_IFDIR },
{ LINUX_S_IFCHR, S_IFCHR },
{ LINUX_S_IFIFO, S_IFIFO },
{ LINUX_S_ISUID, S_ISUID },
{ LINUX_S_ISGID, S_ISGID },
{ LINUX_S_ISVTX, S_ISVTX },
{ LINUX_S_IRUSR, S_IRUSR },
{ LINUX_S_IWUSR, S_IWUSR },
{ LINUX_S_IXUSR, S_IXUSR },
{ LINUX_S_IRGRP, S_IRGRP },
{ LINUX_S_IWGRP, S_IWGRP },
{ LINUX_S_IXGRP, S_IXGRP },
{ LINUX_S_IROTH, S_IROTH },
{ LINUX_S_IWOTH, S_IWOTH },
{ LINUX_S_IXOTH, S_IXOTH },
{ 0, 0 }
};
/* External Variables */
/* Global Variables */
/* Local Variables */
/* External Prototypes */
/* Local Prototypes */
mode_t
ext2_mode_xlate(__u16 lmode);
__u16
host_mode_xlate(mode_t hmode);
static int
release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
int blockcnt, void *private);
long
delete_file(ext2_filsys fs, ext2_ino_t inode);
/* translate a ext2 mode to the host OS representation */
mode_t ext2_mode_xlate(__u16 lmode)
{
mode_t mode = 0;
int i;
for (i=0; xlat_tbl[i].lmask; i++)
{
if (lmode & xlat_tbl[i].lmask)
mode |= xlat_tbl[i].mask;
}
return mode;
}
/* translate a host OS mode to the ext2 representation */
__u16 host_mode_xlate(mode_t hmode)
{
__u16 mode = 0;
int i;
for (i=0; xlat_tbl[i].lmask; i++)
{
if (hmode & xlat_tbl[i].mask)
mode |= xlat_tbl[i].lmask;
}
return mode;
}
long
open_filesystem(char *name, ext2_filsys *fs, ext2_ino_t *root, int rw_mode)
{
int retval;
int closeval;
if ((retval = ext2fs_open(name, (rw_mode) ? EXT2_FLAG_RW : 0, 0, 0,
unix_io_manager, fs)))
{
fprintf(stderr, "%s\n", error_message(retval));
*fs = NULL;
return retval;
}
if ((retval = ext2fs_read_inode_bitmap(*fs)))
{
fprintf(stderr, "%s\n", error_message(retval));
if ((closeval = ext2fs_close(*fs)))
fputs(error_message(closeval), stderr);
*fs = NULL;
return retval;
}
if ((retval = ext2fs_read_block_bitmap(*fs)))
{
fprintf(stderr, "%s\n", error_message(retval));
if ((closeval = ext2fs_close(*fs)))
fputs(error_message(closeval), stderr);
*fs = NULL;
return retval;
}
*root = EXT2_ROOT_INO;
return(0);
}
long
read_inode(ext2_filsys fs, ext2_ino_t file, struct ext2_inode *inode)
{
long retval;
if ((retval = ext2fs_read_inode(fs, file, inode)))
fprintf(stderr, "%s\n", error_message(retval));
return retval;
}
long
write_inode(ext2_filsys fs, ext2_ino_t file, struct ext2_inode *inode)
{
long retval;
if ((retval = ext2fs_write_inode(fs, file, inode)))
fprintf(stderr, "%s\n", error_message(retval));
return retval;
}
long
rm_file(ext2_filsys fs, ext2_ino_t cwd, char *outfile, ext2_ino_t delfile)
{
struct ext2_inode inode;
long retval;
if ((retval = read_inode(fs, delfile, &inode)))
return(retval);
--inode.i_links_count;
if ((retval = write_inode(fs, delfile, &inode)))
return(retval);
if ((retval = ext2fs_unlink(fs, cwd, outfile, 0, 0)))
return(retval);
if (inode.i_links_count == 0)
return(delete_file(fs, delfile));
return(0);
}
long
delete_file(ext2_filsys fs, ext2_ino_t inode)
{
struct ext2_inode inode_buf;
long retval;
if ((retval = read_inode(fs, inode, &inode_buf)))
{
fprintf(stderr, "%s\n", error_message(retval));
return(retval);
}
inode_buf.i_dtime = time(NULL);
if ((retval = write_inode(fs, inode, &inode_buf)))
{
fprintf(stderr, "%s\n", error_message(retval));
return(retval);
}
if ((retval = ext2fs_block_iterate(fs, inode, 0, NULL,
release_blocks_proc, NULL)))
{
fprintf(stderr, "%s\n", error_message(retval));
return(retval);
}
ext2fs_inode_alloc_stats(fs, inode, -1);
return(0);
}
static int
release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
int blockcnt, void *private)
{
blk_t block;
block = *blocknr;
ext2fs_block_alloc_stats(fs, block, -1);
return 0;
}
void
init_stat_buf(struct stat *buf)
{
if (buf)
{
memset(buf, 0, sizeof(struct stat));
buf->st_atime = buf->st_ctime = buf->st_mtime = time(NULL);
buf->st_uid = getuid();
buf->st_gid = getgid();
}
}
int
is_file_regexp(char *ptr)
{
char c;
while ((c = *ptr++) != '\0')
{
switch(c)
{
case '*':
case '[':
case ']':
case '?':
return(1);
break;
}
}
return(0);
}
regex_t *
make_regexp(char *shell)
{
char *tmpstr;
char *ptr;
static regex_t reg;
int l;
char c;
if (NULL == (tmpstr = alloca((strlen(shell)) << 1 + 3)))
{
perror("make_regexp");
return(NULL);
}
ptr = tmpstr;
*ptr++ = '^';
while ((c = *shell++) != '\0')
{
switch(c)
{
case '*':
*ptr++ = '.';
*ptr++ = c;
break;
case '?':
*ptr++ = '.';
break;
case '.':
*ptr++ = '\\';
*ptr++ = '.';
break;
default:
*ptr++ = c;
}
}
*ptr++ = '$';
*ptr = '\0';
if (regcomp(®, tmpstr, REG_NOSUB))
{
perror("make_regexp");
return(NULL);
}
return(®);
}
|