2006-12-26 01:30:59 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* See README for additional information
|
|
|
|
*
|
2008-12-07 00:52:58 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
2006-12-26 01:30:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "e2fs_lib.h"
|
|
|
|
|
|
|
|
#define HAVE_EXT2_IOCTLS 1
|
|
|
|
|
|
|
|
#if INT_MAX == LONG_MAX
|
2007-04-15 11:48:27 +00:00
|
|
|
#define IF_LONG_IS_SAME(...) __VA_ARGS__
|
|
|
|
#define IF_LONG_IS_WIDER(...)
|
2006-12-26 01:30:59 +00:00
|
|
|
#else
|
2007-04-15 11:48:27 +00:00
|
|
|
#define IF_LONG_IS_SAME(...)
|
|
|
|
#define IF_LONG_IS_WIDER(...) __VA_ARGS__
|
2006-12-26 01:30:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void close_silently(int fd)
|
|
|
|
{
|
|
|
|
int e = errno;
|
|
|
|
close(fd);
|
|
|
|
errno = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Iterate a function on each entry of a directory */
|
|
|
|
int iterate_on_dir(const char *dir_name,
|
2009-06-05 10:06:05 +00:00
|
|
|
int FAST_FUNC (*func)(const char *, struct dirent *, void *),
|
|
|
|
void *private)
|
2006-12-26 01:30:59 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2009-06-05 10:06:05 +00:00
|
|
|
struct dirent *de;
|
2006-12-26 01:30:59 +00:00
|
|
|
|
|
|
|
dir = opendir(dir_name);
|
|
|
|
if (dir == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-05 10:06:05 +00:00
|
|
|
while ((de = readdir(dir)) != NULL) {
|
2006-12-26 01:30:59 +00:00
|
|
|
func(dir_name, de, private);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get/set a file version on an ext2 file system */
|
|
|
|
int fgetsetversion(const char *name, unsigned long *get_version, unsigned long set_version)
|
|
|
|
{
|
|
|
|
#if HAVE_EXT2_IOCTLS
|
|
|
|
int fd, r;
|
|
|
|
IF_LONG_IS_WIDER(int ver;)
|
|
|
|
|
|
|
|
fd = open(name, O_NONBLOCK);
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
if (!get_version) {
|
|
|
|
IF_LONG_IS_WIDER(
|
|
|
|
ver = (int) set_version;
|
|
|
|
r = ioctl(fd, EXT2_IOC_SETVERSION, &ver);
|
|
|
|
)
|
|
|
|
IF_LONG_IS_SAME(
|
|
|
|
r = ioctl(fd, EXT2_IOC_SETVERSION, (void*)&set_version);
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
IF_LONG_IS_WIDER(
|
|
|
|
r = ioctl(fd, EXT2_IOC_GETVERSION, &ver);
|
|
|
|
*get_version = ver;
|
|
|
|
)
|
|
|
|
IF_LONG_IS_SAME(
|
|
|
|
r = ioctl(fd, EXT2_IOC_GETVERSION, (void*)get_version);
|
|
|
|
)
|
|
|
|
}
|
|
|
|
close_silently(fd);
|
|
|
|
return r;
|
|
|
|
#else /* ! HAVE_EXT2_IOCTLS */
|
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
#endif /* ! HAVE_EXT2_IOCTLS */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get/set a file flags on an ext2 file system */
|
|
|
|
int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_flags)
|
|
|
|
{
|
|
|
|
#if HAVE_EXT2_IOCTLS
|
|
|
|
struct stat buf;
|
|
|
|
int fd, r;
|
|
|
|
IF_LONG_IS_WIDER(int f;)
|
|
|
|
|
|
|
|
if (stat(name, &buf) == 0 /* stat is ok */
|
|
|
|
&& !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)
|
|
|
|
) {
|
|
|
|
goto notsupp;
|
|
|
|
}
|
|
|
|
fd = open(name, O_NONBLOCK); /* neither read nor write asked for */
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!get_flags) {
|
|
|
|
IF_LONG_IS_WIDER(
|
|
|
|
f = (int) set_flags;
|
|
|
|
r = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
|
|
|
|
)
|
|
|
|
IF_LONG_IS_SAME(
|
|
|
|
r = ioctl(fd, EXT2_IOC_SETFLAGS, (void*)&set_flags);
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
IF_LONG_IS_WIDER(
|
|
|
|
r = ioctl(fd, EXT2_IOC_GETFLAGS, &f);
|
|
|
|
*get_flags = f;
|
|
|
|
)
|
|
|
|
IF_LONG_IS_SAME(
|
|
|
|
r = ioctl(fd, EXT2_IOC_GETFLAGS, (void*)get_flags);
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
close_silently(fd);
|
|
|
|
return r;
|
|
|
|
notsupp:
|
|
|
|
#endif /* HAVE_EXT2_IOCTLS */
|
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Print file attributes on an ext2 file system */
|
2007-10-30 19:36:07 +00:00
|
|
|
const uint32_t e2attr_flags_value[] = {
|
|
|
|
#ifdef ENABLE_COMPRESSION
|
|
|
|
EXT2_COMPRBLK_FL,
|
|
|
|
EXT2_DIRTY_FL,
|
|
|
|
EXT2_NOCOMPR_FL,
|
|
|
|
EXT2_ECOMPR_FL,
|
|
|
|
#endif
|
|
|
|
EXT2_INDEX_FL,
|
|
|
|
EXT2_SECRM_FL,
|
|
|
|
EXT2_UNRM_FL,
|
|
|
|
EXT2_SYNC_FL,
|
|
|
|
EXT2_DIRSYNC_FL,
|
|
|
|
EXT2_IMMUTABLE_FL,
|
|
|
|
EXT2_APPEND_FL,
|
|
|
|
EXT2_NODUMP_FL,
|
|
|
|
EXT2_NOATIME_FL,
|
|
|
|
EXT2_COMPR_FL,
|
|
|
|
EXT3_JOURNAL_DATA_FL,
|
|
|
|
EXT2_NOTAIL_FL,
|
|
|
|
EXT2_TOPDIR_FL
|
2006-12-26 01:30:59 +00:00
|
|
|
};
|
|
|
|
|
2007-10-30 19:36:07 +00:00
|
|
|
const char e2attr_flags_sname[] =
|
2006-12-26 01:30:59 +00:00
|
|
|
#ifdef ENABLE_COMPRESSION
|
2007-10-30 19:36:07 +00:00
|
|
|
"BZXE"
|
2006-12-26 01:30:59 +00:00
|
|
|
#endif
|
2007-10-30 19:36:07 +00:00
|
|
|
"I"
|
|
|
|
"suSDiadAcjtT";
|
|
|
|
|
|
|
|
static const char e2attr_flags_lname[] =
|
|
|
|
#ifdef ENABLE_COMPRESSION
|
|
|
|
"Compressed_File" "\0"
|
|
|
|
"Compressed_Dirty_File" "\0"
|
|
|
|
"Compression_Raw_Access" "\0"
|
|
|
|
"Compression_Error" "\0"
|
|
|
|
#endif
|
|
|
|
"Indexed_directory" "\0"
|
|
|
|
"Secure_Deletion" "\0"
|
|
|
|
"Undelete" "\0"
|
|
|
|
"Synchronous_Updates" "\0"
|
|
|
|
"Synchronous_Directory_Updates" "\0"
|
|
|
|
"Immutable" "\0"
|
|
|
|
"Append_Only" "\0"
|
|
|
|
"No_Dump" "\0"
|
|
|
|
"No_Atime" "\0"
|
|
|
|
"Compression_Requested" "\0"
|
|
|
|
"Journaled_Data" "\0"
|
|
|
|
"No_Tailmerging" "\0"
|
|
|
|
"Top_of_Directory_Hierarchies" "\0"
|
|
|
|
/* Another trailing NUL is added by compiler */;
|
2006-12-26 01:30:59 +00:00
|
|
|
|
2008-06-07 15:10:29 +00:00
|
|
|
void print_e2flags(FILE *f, unsigned long flags, unsigned options)
|
2006-12-26 01:30:59 +00:00
|
|
|
{
|
2007-10-30 19:36:07 +00:00
|
|
|
const uint32_t *fv;
|
|
|
|
const char *fn;
|
2006-12-26 01:30:59 +00:00
|
|
|
|
2007-10-30 19:36:07 +00:00
|
|
|
fv = e2attr_flags_value;
|
2006-12-26 01:30:59 +00:00
|
|
|
if (options & PFOPT_LONG) {
|
|
|
|
int first = 1;
|
2007-10-30 19:36:07 +00:00
|
|
|
fn = e2attr_flags_lname;
|
|
|
|
do {
|
|
|
|
if (flags & *fv) {
|
2006-12-26 01:30:59 +00:00
|
|
|
if (!first)
|
|
|
|
fputs(", ", f);
|
2007-10-30 19:36:07 +00:00
|
|
|
fputs(fn, f);
|
2006-12-26 01:30:59 +00:00
|
|
|
first = 0;
|
|
|
|
}
|
2007-10-30 19:36:07 +00:00
|
|
|
fv++;
|
|
|
|
fn += strlen(fn) + 1;
|
|
|
|
} while (*fn);
|
2006-12-26 01:30:59 +00:00
|
|
|
if (first)
|
|
|
|
fputs("---", f);
|
|
|
|
} else {
|
2007-10-30 19:36:07 +00:00
|
|
|
fn = e2attr_flags_sname;
|
|
|
|
do {
|
2006-12-26 03:02:25 +00:00
|
|
|
char c = '-';
|
2007-10-30 19:36:07 +00:00
|
|
|
if (flags & *fv)
|
|
|
|
c = *fn;
|
2006-12-26 03:02:25 +00:00
|
|
|
fputc(c, f);
|
2007-10-30 19:36:07 +00:00
|
|
|
fv++;
|
|
|
|
fn++;
|
|
|
|
} while (*fn);
|
2006-12-26 01:30:59 +00:00
|
|
|
}
|
|
|
|
}
|