2006-12-26 07:00:59 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* chattr.c - Change file attributes on an ext2 file system
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
|
|
|
|
* Laboratoire MASI, Institut Blaise Pascal
|
|
|
|
* Universite Pierre et Marie Curie (Paris VI)
|
|
|
|
*
|
|
|
|
* This file can be redistributed under the terms of the GNU General
|
|
|
|
* Public License
|
|
|
|
*/
|
2015-10-19 02:10:23 +05:30
|
|
|
//config:config CHATTR
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "chattr (3.8 kb)"
|
2015-10-19 02:10:23 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: chattr changes the file attributes on a second extended file system.
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2017-08-06 00:08:04 +05:30
|
|
|
//applet:IF_CHATTR(APPLET_NOEXEC(chattr, chattr, BB_DIR_BIN, BB_SUID_DROP, chattr))
|
2015-10-19 02:10:23 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_CHATTR) += chattr.o e2fs_lib.o
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2011-04-02 02:26:30 +05:30
|
|
|
//usage:#define chattr_trivial_usage
|
2021-06-20 14:27:24 +05:30
|
|
|
//usage: "[-R] [-v VERSION] [-p PROJID] [-+=AacDdijsStTu] FILE..."
|
2011-04-02 02:26:30 +05:30
|
|
|
//usage:#define chattr_full_usage "\n\n"
|
2015-10-19 04:59:20 +05:30
|
|
|
//usage: "Change ext2 file attributes\n"
|
2017-08-06 00:03:48 +05:30
|
|
|
//usage: "\n -R Recurse"
|
2021-06-15 00:17:20 +05:30
|
|
|
//usage: "\n -v NUM Set version/generation number"
|
2021-06-20 14:27:24 +05:30
|
|
|
//usage: "\n -p NUM Set project number"
|
2017-08-06 00:03:48 +05:30
|
|
|
//-V, -f accepted but ignored
|
2011-04-02 02:26:30 +05:30
|
|
|
//usage: "\nModifiers:"
|
2015-10-19 04:59:20 +05:30
|
|
|
//usage: "\n -,+,= Remove/add/set attributes"
|
2011-04-02 02:26:30 +05:30
|
|
|
//usage: "\nAttributes:"
|
2021-06-15 00:17:20 +05:30
|
|
|
//usage: "\n A No atime"
|
|
|
|
//usage: "\n a Append only"
|
2021-06-23 13:12:01 +05:30
|
|
|
//usage: "\n C No copy-on-write"
|
2021-06-15 00:17:20 +05:30
|
|
|
//usage: "\n c Compressed"
|
|
|
|
//usage: "\n D Synchronous dir updates"
|
2011-04-02 02:26:30 +05:30
|
|
|
//usage: "\n d Don't backup with dump"
|
2021-06-23 13:12:01 +05:30
|
|
|
//usage: "\n E Encrypted"
|
|
|
|
//usage: "\n e File uses extents"
|
|
|
|
//usage: "\n F Case-insensitive dir"
|
|
|
|
//usage: "\n I Indexed dir"
|
2021-06-15 00:17:20 +05:30
|
|
|
//usage: "\n i Immutable"
|
|
|
|
//usage: "\n j Write data to journal first"
|
2021-06-23 13:12:01 +05:30
|
|
|
//usage: "\n N File is stored in inode"
|
|
|
|
//usage: "\n P Hierarchical project ID dir"
|
2021-06-15 00:17:20 +05:30
|
|
|
//usage: "\n S Synchronous file updates"
|
2021-06-23 13:12:01 +05:30
|
|
|
//usage: "\n s Zero storage when deleted"
|
|
|
|
//usage: "\n T Top of dir hierarchy"
|
2021-06-15 00:17:20 +05:30
|
|
|
//usage: "\n t Don't tail-merge with other files"
|
|
|
|
//usage: "\n u Allow undelete"
|
2021-06-23 13:12:01 +05:30
|
|
|
//usage: "\n V Verity"
|
2011-04-02 02:26:30 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-12-26 07:00:59 +05:30
|
|
|
#include "e2fs_lib.h"
|
|
|
|
|
2021-06-20 14:27:24 +05:30
|
|
|
#define OPT_ADD (1 << 0)
|
|
|
|
#define OPT_REM (1 << 1)
|
|
|
|
#define OPT_SET (1 << 2)
|
|
|
|
#define OPT_SET_VER (1 << 3)
|
|
|
|
#define OPT_SET_PROJ (1 << 4)
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
struct globals {
|
2021-06-23 16:15:51 +05:30
|
|
|
unsigned version;
|
|
|
|
unsigned af;
|
|
|
|
unsigned rf;
|
2017-08-06 00:03:48 +05:30
|
|
|
int flags;
|
2021-06-20 14:27:24 +05:30
|
|
|
uint32_t projid;
|
2007-04-15 17:18:27 +05:30
|
|
|
smallint recursive;
|
2006-12-26 07:00:59 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long get_flag(char c)
|
|
|
|
{
|
2007-10-31 01:06:07 +05:30
|
|
|
const char *fp = strchr(e2attr_flags_sname_chattr, c);
|
|
|
|
if (fp)
|
|
|
|
return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
|
2006-12-26 07:00:59 +05:30
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
2017-08-06 00:03:48 +05:30
|
|
|
static char** decode_arg(char **argv, struct globals *gp)
|
2006-12-26 07:00:59 +05:30
|
|
|
{
|
2021-06-23 16:15:51 +05:30
|
|
|
unsigned *fl;
|
2017-08-06 00:03:48 +05:30
|
|
|
const char *arg = *argv;
|
|
|
|
char opt = *arg;
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
fl = &gp->af;
|
2006-12-26 07:00:59 +05:30
|
|
|
if (opt == '-') {
|
2007-04-15 17:18:27 +05:30
|
|
|
gp->flags |= OPT_REM;
|
|
|
|
fl = &gp->rf;
|
2006-12-26 07:00:59 +05:30
|
|
|
} else if (opt == '+') {
|
2007-04-15 17:18:27 +05:30
|
|
|
gp->flags |= OPT_ADD;
|
2017-08-06 00:03:48 +05:30
|
|
|
} else { /* if (opt == '=') */
|
2007-04-15 17:18:27 +05:30
|
|
|
gp->flags |= OPT_SET;
|
2017-08-06 00:03:48 +05:30
|
|
|
}
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2017-08-06 00:03:48 +05:30
|
|
|
while (*++arg) {
|
|
|
|
if (opt == '-') {
|
|
|
|
//e2fsprogs-1.43.1 accepts:
|
|
|
|
// "-RRR", "-RRRv VER" and even "-ARRRva VER" and "-vvv V1 V2 V3"
|
|
|
|
// but not "-vVER".
|
|
|
|
// IOW: options are parsed as part of "remove attrs" strings,
|
|
|
|
// if "v" is seen, next argv[] is VER, even if more opts/attrs follow in this argv[]!
|
|
|
|
if (*arg == 'R') {
|
|
|
|
gp->recursive = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*arg == 'V') {
|
|
|
|
/*"verbose and print program version" (nop for now) */;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*arg == 'f') {
|
|
|
|
/*"suppress most error messages" (nop) */;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*arg == 'v') {
|
|
|
|
if (!*++argv)
|
|
|
|
bb_show_usage();
|
|
|
|
gp->version = xatoul(*argv);
|
|
|
|
gp->flags |= OPT_SET_VER;
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-20 14:27:24 +05:30
|
|
|
if (*arg == 'p') {
|
|
|
|
if (!*++argv)
|
|
|
|
bb_show_usage();
|
|
|
|
gp->projid = xatou32(*argv);
|
|
|
|
gp->flags |= OPT_SET_PROJ;
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-06 00:03:48 +05:30
|
|
|
/* not a known option, try as an attribute */
|
|
|
|
}
|
|
|
|
*fl |= get_flag(*arg);
|
|
|
|
}
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2017-08-06 00:03:48 +05:30
|
|
|
return argv;
|
2006-12-26 07:00:59 +05:30
|
|
|
}
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
static void change_attributes(const char *name, struct globals *gp);
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
|
2006-12-26 09:06:28 +05:30
|
|
|
{
|
|
|
|
char *path = concat_subpath_file(dir_name, de->d_name);
|
|
|
|
/* path is NULL if de->d_name is "." or "..", else... */
|
|
|
|
if (path) {
|
2007-04-15 17:18:27 +05:30
|
|
|
change_attributes(path, gp);
|
2006-12-26 09:06:28 +05:30
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
static void change_attributes(const char *name, struct globals *gp)
|
2006-12-26 07:00:59 +05:30
|
|
|
{
|
2021-06-23 16:15:51 +05:30
|
|
|
unsigned fsflags;
|
|
|
|
int fd;
|
2006-12-26 07:00:59 +05:30
|
|
|
struct stat st;
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
if (lstat(name, &st) != 0) {
|
2021-06-23 19:03:22 +05:30
|
|
|
bb_perror_msg("can't stat '%s'", name);
|
2006-12-26 07:00:59 +05:30
|
|
|
return;
|
|
|
|
}
|
2007-04-15 17:18:27 +05:30
|
|
|
if (S_ISLNK(st.st_mode) && gp->recursive)
|
2006-12-26 07:00:59 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
/* Don't try to open device files, fifos etc. We probably
|
|
|
|
* ought to display an error if the file was explicitly given
|
|
|
|
* on the command line (whether or not recursive was
|
|
|
|
* requested). */
|
|
|
|
if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
|
|
|
|
return;
|
|
|
|
|
2021-06-23 16:15:51 +05:30
|
|
|
fd = open_or_warn(name, O_RDONLY | O_NONBLOCK); /* neither read nor write asked for */
|
|
|
|
if (fd >= 0) {
|
|
|
|
int r;
|
2021-06-20 14:27:24 +05:30
|
|
|
|
2021-06-23 16:15:51 +05:30
|
|
|
if (gp->flags & OPT_SET_VER) {
|
|
|
|
r = ioctl(fd, EXT2_IOC_SETVERSION, &gp->version);
|
|
|
|
if (r != 0)
|
|
|
|
bb_perror_msg("setting %s on %s", "version", name);
|
|
|
|
}
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2021-06-23 16:15:51 +05:30
|
|
|
if (gp->flags & OPT_SET_PROJ) {
|
|
|
|
struct ext2_fsxattr fsxattr;
|
|
|
|
r = ioctl(fd, EXT2_IOC_FSGETXATTR, &fsxattr);
|
2021-06-23 19:03:22 +05:30
|
|
|
/* note: ^^^ may fail in 32-bit userspace on 64-bit kernel (seen on 4.12.0) */
|
2021-06-23 16:15:51 +05:30
|
|
|
if (r != 0)
|
|
|
|
bb_perror_msg("getting %s on %s", "project ID", name);
|
|
|
|
fsxattr.fsx_projid = gp->projid;
|
|
|
|
r = ioctl(fd, EXT2_IOC_FSSETXATTR, &fsxattr);
|
|
|
|
if (r != 0)
|
|
|
|
bb_perror_msg("setting %s on %s", "project ID", name);
|
2006-12-26 07:00:59 +05:30
|
|
|
}
|
2021-06-23 16:15:51 +05:30
|
|
|
|
|
|
|
if (gp->flags & OPT_SET) {
|
|
|
|
fsflags = gp->af;
|
|
|
|
} else {
|
|
|
|
r = ioctl(fd, EXT2_IOC_GETFLAGS, &fsflags);
|
|
|
|
if (r != 0) {
|
|
|
|
bb_perror_msg("getting %s on %s", "flags", name);
|
|
|
|
goto skip_setflags;
|
|
|
|
}
|
|
|
|
/*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
|
|
|
|
fsflags &= ~gp->rf;
|
|
|
|
/*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
|
|
|
|
fsflags |= gp->af;
|
2017-08-06 00:03:48 +05:30
|
|
|
// What is this? And why it's not done for SET case?
|
2021-06-23 16:15:51 +05:30
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
fsflags &= ~EXT2_DIRSYNC_FL;
|
|
|
|
}
|
|
|
|
r = ioctl(fd, EXT2_IOC_SETFLAGS, &fsflags);
|
|
|
|
if (r != 0)
|
|
|
|
bb_perror_msg("setting %s on %s", "flags", name);
|
|
|
|
skip_setflags:
|
|
|
|
close(fd);
|
2006-12-26 07:00:59 +05:30
|
|
|
}
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
if (gp->recursive && S_ISDIR(st.st_mode))
|
|
|
|
iterate_on_dir(name, chattr_dir_proc, gp);
|
2006-12-26 07:00:59 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int chattr_main(int argc UNUSED_PARAM, char **argv)
|
2006-12-26 07:00:59 +05:30
|
|
|
{
|
2007-04-15 17:18:27 +05:30
|
|
|
struct globals g;
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
memset(&g, 0, sizeof(g));
|
|
|
|
|
2006-12-26 07:00:59 +05:30
|
|
|
/* parse the args */
|
2017-08-06 00:03:48 +05:30
|
|
|
for (;;) {
|
|
|
|
char *arg = *++argv;
|
|
|
|
if (!arg)
|
|
|
|
bb_show_usage();
|
|
|
|
if (arg[0] != '-' && arg[0] != '+' && arg[0] != '=')
|
2006-12-26 07:00:59 +05:30
|
|
|
break;
|
2017-08-06 00:03:48 +05:30
|
|
|
|
|
|
|
argv = decode_arg(argv, &g);
|
2006-12-26 07:00:59 +05:30
|
|
|
}
|
2017-08-06 00:03:48 +05:30
|
|
|
/* note: on loop exit, remaining argv[] is never empty */
|
2006-12-26 07:00:59 +05:30
|
|
|
|
|
|
|
/* run sanity checks on all the arguments given us */
|
2007-04-15 17:18:27 +05:30
|
|
|
if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("= is incompatible with - and +");
|
2007-04-15 17:18:27 +05:30
|
|
|
if (g.rf & g.af)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("can't set and unset a flag");
|
2007-04-15 17:18:27 +05:30
|
|
|
if (!g.flags)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("must use '-v', =, - or +");
|
2006-12-26 07:00:59 +05:30
|
|
|
|
|
|
|
/* now run chattr on all the files passed to us */
|
2007-04-15 17:18:27 +05:30
|
|
|
do change_attributes(*argv, &g); while (*++argv);
|
2006-12-26 07:00:59 +05:30
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|