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
|
|
|
|
//config: bool "chattr"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: chattr changes the file attributes on a second extended file system.
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2015-10-19 02:10:23 +05:30
|
|
|
//applet:IF_CHATTR(APPLET(chattr, BB_DIR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//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
|
|
|
|
//usage: "[-R] [-+=AacDdijsStTu] [-v VERSION] [FILE]..."
|
|
|
|
//usage:#define chattr_full_usage "\n\n"
|
|
|
|
//usage: "Change file attributes on an ext2 fs\n"
|
|
|
|
//usage: "\nModifiers:"
|
|
|
|
//usage: "\n - Remove attributes"
|
|
|
|
//usage: "\n + Add attributes"
|
|
|
|
//usage: "\n = Set attributes"
|
|
|
|
//usage: "\nAttributes:"
|
|
|
|
//usage: "\n A Don't track atime"
|
|
|
|
//usage: "\n a Append mode only"
|
|
|
|
//usage: "\n c Enable compress"
|
|
|
|
//usage: "\n D Write dir contents synchronously"
|
|
|
|
//usage: "\n d Don't backup with dump"
|
|
|
|
//usage: "\n i Cannot be modified (immutable)"
|
|
|
|
//usage: "\n j Write all data to journal first"
|
|
|
|
//usage: "\n s Zero disk storage when deleted"
|
|
|
|
//usage: "\n S Write file contents synchronously"
|
|
|
|
//usage: "\n t Disable tail-merging of partial blocks with other files"
|
|
|
|
//usage: "\n u Allow file to be undeleted"
|
|
|
|
//usage: "\n -R Recurse"
|
|
|
|
//usage: "\n -v Set the file's version/generation number"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-12-26 07:00:59 +05:30
|
|
|
#include "e2fs_lib.h"
|
|
|
|
|
|
|
|
#define OPT_ADD 1
|
|
|
|
#define OPT_REM 2
|
|
|
|
#define OPT_SET 4
|
|
|
|
#define OPT_SET_VER 8
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
struct globals {
|
|
|
|
unsigned long version;
|
|
|
|
unsigned long af;
|
|
|
|
unsigned long rf;
|
|
|
|
smallint flags;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
static int decode_arg(const char *arg, struct globals *gp)
|
2006-12-26 07:00:59 +05:30
|
|
|
{
|
|
|
|
unsigned long *fl;
|
|
|
|
char opt = *arg++;
|
|
|
|
|
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;
|
2006-12-26 07:00:59 +05:30
|
|
|
} else if (opt == '=') {
|
2007-04-15 17:18:27 +05:30
|
|
|
gp->flags |= OPT_SET;
|
2006-12-26 07:00:59 +05:30
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (*arg)
|
|
|
|
*fl |= get_flag(*arg++);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
unsigned long fsflags;
|
|
|
|
struct stat st;
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
if (lstat(name, &st) != 0) {
|
2006-12-26 09:06:28 +05:30
|
|
|
bb_perror_msg("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;
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
if (gp->flags & OPT_SET_VER)
|
|
|
|
if (fsetversion(name, gp->version) != 0)
|
2006-12-26 09:06:28 +05:30
|
|
|
bb_perror_msg("setting version on %s", name);
|
2006-12-26 07:00:59 +05:30
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
if (gp->flags & OPT_SET) {
|
|
|
|
fsflags = gp->af;
|
2006-12-26 07:00:59 +05:30
|
|
|
} else {
|
2007-04-15 17:18:27 +05:30
|
|
|
if (fgetflags(name, &fsflags) != 0) {
|
2006-12-26 09:06:28 +05:30
|
|
|
bb_perror_msg("reading flags on %s", name);
|
2006-12-26 07:00:59 +05:30
|
|
|
goto skip_setflags;
|
|
|
|
}
|
2007-04-15 17:18:27 +05:30
|
|
|
/*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;
|
|
|
|
/* What is this? And why it's not done for SET case? */
|
2006-12-26 07:00:59 +05:30
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
fsflags &= ~EXT2_DIRSYNC_FL;
|
|
|
|
}
|
2007-04-15 17:18:27 +05:30
|
|
|
if (fsetflags(name, fsflags) != 0)
|
2006-12-26 09:06:28 +05:30
|
|
|
bb_perror_msg("setting flags on %s", name);
|
2006-12-26 07:00:59 +05:30
|
|
|
|
|
|
|
skip_setflags:
|
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
|
|
|
char *arg;
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
memset(&g, 0, sizeof(g));
|
|
|
|
|
2006-12-26 07:00:59 +05:30
|
|
|
/* parse the args */
|
2006-12-26 09:06:28 +05:30
|
|
|
while ((arg = *++argv)) {
|
2006-12-26 07:00:59 +05:30
|
|
|
/* take care of -R and -v <version> */
|
2007-04-15 17:18:27 +05:30
|
|
|
if (arg[0] == '-'
|
|
|
|
&& (arg[1] == 'R' || arg[1] == 'v')
|
|
|
|
&& !arg[2]
|
|
|
|
) {
|
|
|
|
if (arg[1] == 'R') {
|
|
|
|
g.recursive = 1;
|
2006-12-26 07:00:59 +05:30
|
|
|
continue;
|
|
|
|
}
|
2007-04-15 17:18:27 +05:30
|
|
|
/* arg[1] == 'v' */
|
|
|
|
if (!*++argv)
|
|
|
|
bb_show_usage();
|
|
|
|
g.version = xatoul(*argv);
|
|
|
|
g.flags |= OPT_SET_VER;
|
|
|
|
continue;
|
2006-12-26 07:00:59 +05:30
|
|
|
}
|
|
|
|
|
2007-04-15 17:18:27 +05:30
|
|
|
if (!decode_arg(arg, &g))
|
2006-12-26 07:00:59 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* run sanity checks on all the arguments given us */
|
2006-12-26 09:06:28 +05:30
|
|
|
if (!*argv)
|
2006-12-26 07:00:59 +05:30
|
|
|
bb_show_usage();
|
2007-04-15 17:18:27 +05:30
|
|
|
if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
|
2006-12-26 07:00:59 +05:30
|
|
|
bb_error_msg_and_die("= is incompatible with - and +");
|
2007-04-15 17:18:27 +05:30
|
|
|
if (g.rf & g.af)
|
2006-12-26 09:06:28 +05:30
|
|
|
bb_error_msg_and_die("can't set and unset a flag");
|
2007-04-15 17:18:27 +05:30
|
|
|
if (!g.flags)
|
2006-12-26 09:06:28 +05:30
|
|
|
bb_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;
|
|
|
|
}
|