+/* vi: set sw=4 ts=4: */
/*
* chattr.c - Change file attributes on an ext2 file system
*
* This file can be redistributed under the terms of the GNU General
* Public License
*/
-
-/*
- * History:
- * 93/10/30 - Creation
- * 93/11/13 - Replace stat() calls by lstat() to avoid loops
- * 94/02/27 - Integrated in Ted's distribution
- * 98/12/29 - Ignore symlinks when working recursively (G M Sipe)
- * 98/12/29 - Display version info only when -V specified (G M Sipe)
- */
-
-#include <sys/types.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <ext2fs/ext2_fs.h>
-
-#ifdef __GNUC__
-#define EXT2FS_ATTR(x) __attribute__(x)
-#else
-#define EXT2FS_ATTR(x)
-#endif
-
-#include "e2fsbb.h"
-#include "e2p/e2p.h"
+//config:config CHATTR
+//config: bool "chattr (3.2 kb)"
+//config: default y
+//config: help
+//config: chattr changes the file attributes on a second extended file system.
+
+//applet:IF_CHATTR(APPLET_NOEXEC(chattr, chattr, BB_DIR_BIN, BB_SUID_DROP, chattr))
+
+//kbuild:lib-$(CONFIG_CHATTR) += chattr.o e2fs_lib.o
+
+//usage:#define chattr_trivial_usage
+//usage: "[-R] [-v VERSION] [-+=AacDdijsStTu] FILE..."
+//usage:#define chattr_full_usage "\n\n"
+//usage: "Change ext2 file attributes\n"
+//usage: "\n -R Recurse"
+//usage: "\n -v VER Set version/generation number"
+//-V, -f accepted but ignored
+//usage: "\nModifiers:"
+//usage: "\n -,+,= Remove/add/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 synchronously"
+//usage: "\n t Disable tail-merging of partial blocks with other files"
+//usage: "\n u Allow file to be undeleted"
+
+#include "libbb.h"
+#include "e2fs_lib.h"
#define OPT_ADD 1
#define OPT_REM 2
#define OPT_SET 4
#define OPT_SET_VER 8
-static int flags;
-static int recursive;
-
-static unsigned long version;
-
-static unsigned long af;
-static unsigned long rf;
-static unsigned long sf;
-
-#ifdef CONFIG_LFS
-# define LSTAT lstat64
-# define STRUCT_STAT struct stat64
-#else
-# define LSTAT lstat
-# define STRUCT_STAT struct stat
-#endif
-
-struct flags_char {
- unsigned long flag;
- char optchar;
-};
-static const struct flags_char flags_array[] = {
- { EXT2_NOATIME_FL, 'A' },
- { EXT2_SYNC_FL, 'S' },
- { EXT2_DIRSYNC_FL, 'D' },
- { EXT2_APPEND_FL, 'a' },
- { EXT2_COMPR_FL, 'c' },
- { EXT2_NODUMP_FL, 'd' },
- { EXT2_IMMUTABLE_FL, 'i' },
- { EXT3_JOURNAL_DATA_FL, 'j' },
- { EXT2_SECRM_FL, 's' },
- { EXT2_UNRM_FL, 'u' },
- { EXT2_NOTAIL_FL, 't' },
- { EXT2_TOPDIR_FL, 'T' },
- { 0, 0 }
+struct globals {
+ unsigned long version;
+ unsigned long af;
+ unsigned long rf;
+ int flags;
+ smallint recursive;
};
static unsigned long get_flag(char c)
{
- const struct flags_char *fp;
- for (fp = flags_array; fp->flag; fp++)
- if (fp->optchar == c)
- return fp->flag;
+ const char *fp = strchr(e2attr_flags_sname_chattr, c);
+ if (fp)
+ return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
bb_show_usage();
- return 0;
}
-static int decode_arg(char *arg)
+static char** decode_arg(char **argv, struct globals *gp)
{
unsigned long *fl;
- char opt = *arg++;
+ const char *arg = *argv;
+ char opt = *arg;
+ fl = &gp->af;
if (opt == '-') {
- flags |= OPT_REM;
- fl = &rf;
+ gp->flags |= OPT_REM;
+ fl = &gp->rf;
} else if (opt == '+') {
- flags |= OPT_ADD;
- fl = ⁡
- } else if (opt == '=') {
- flags |= OPT_SET;
- fl = &sf;
- } else
- return EOF;
-
- for (; *arg ; ++arg)
- (*fl) |= get_flag(*arg);
-
- return 1;
+ gp->flags |= OPT_ADD;
+ } else { /* if (opt == '=') */
+ gp->flags |= OPT_SET;
+ }
+
+ 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;
+ }
+//TODO: "-p PROJECT_NUM" ?
+ /* not a known option, try as an attribute */
+ }
+ *fl |= get_flag(*arg);
+ }
+
+ return argv;
}
-static int chattr_dir_proc(const char *, struct dirent *, void *);
+static void change_attributes(const char *name, struct globals *gp);
-static void change_attributes(const char * name)
+static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
+{
+ char *path = concat_subpath_file(dir_name, de->d_name);
+ /* path is NULL if de->d_name is "." or "..", else... */
+ if (path) {
+ change_attributes(path, gp);
+ free(path);
+ }
+ return 0;
+}
+
+static void change_attributes(const char *name, struct globals *gp)
{
unsigned long fsflags;
- STRUCT_STAT st;
+ struct stat st;
- if (LSTAT(name, &st) == -1) {
- bb_error_msg("while trying to stat %s", name);
+ if (lstat(name, &st) != 0) {
+ bb_perror_msg("stat %s", name);
return;
}
- if (S_ISLNK(st.st_mode) && recursive)
+ if (S_ISLNK(st.st_mode) && gp->recursive)
return;
/* Don't try to open device files, fifos etc. We probably
if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
return;
- if (flags & OPT_SET_VER)
- if (fsetversion(name, version) == -1)
- bb_error_msg("while setting version on %s", name);
+ if (gp->flags & OPT_SET_VER)
+ if (fsetversion(name, gp->version) != 0)
+ bb_perror_msg("setting version on %s", name);
- if (flags & OPT_SET) {
- fsflags = sf;
+ if (gp->flags & OPT_SET) {
+ fsflags = gp->af;
} else {
- if (fgetflags(name, &fsflags) == -1) {
- bb_error_msg("while reading flags on %s", name);
+ if (fgetflags(name, &fsflags) != 0) {
+ bb_perror_msg("reading flags on %s", name);
goto skip_setflags;
}
- if (flags & OPT_REM)
- fsflags &= ~rf;
- if (flags & OPT_ADD)
- fsflags |= af;
+ /*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?
if (!S_ISDIR(st.st_mode))
fsflags &= ~EXT2_DIRSYNC_FL;
}
- if (fsetflags(name, fsflags) == -1)
- bb_error_msg("while setting flags on %s", name);
+ if (fsetflags(name, fsflags) != 0)
+ bb_perror_msg("setting flags on %s", name);
-skip_setflags:
- if (S_ISDIR(st.st_mode) && recursive)
- iterate_on_dir(name, chattr_dir_proc, NULL);
+ skip_setflags:
+ if (gp->recursive && S_ISDIR(st.st_mode))
+ iterate_on_dir(name, chattr_dir_proc, gp);
}
-static int chattr_dir_proc(const char *dir_name, struct dirent *de,
- void *private EXT2FS_ATTR((unused)))
+int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int chattr_main(int argc UNUSED_PARAM, char **argv)
{
- /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/
- if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \
- (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
- char *path;
- if (asprintf(&path, "%s/%s", dir_name, de->d_name) == -1)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- change_attributes(path);
- free(path);
- }
- return 0;
-}
+ struct globals g;
-int chattr_main(int argc, char **argv)
-{
- int i;
- char *arg;
+ memset(&g, 0, sizeof(g));
/* parse the args */
- for (i = 1; i < argc; ++i) {
- arg = argv[i];
-
- /* take care of -R and -v <version> */
- if (arg[0] == '-') {
- if (arg[1] == 'R' && arg[2] == '\0') {
- recursive = 1;
- continue;
- } else if (arg[1] == 'v' && arg[2] == '\0') {
- char *tmp;
- ++i;
- if (i >= argc)
- bb_show_usage();
- version = strtol(argv[i], &tmp, 0);
- if (*tmp) {
- bb_error_msg("bad version - %s", arg);
- bb_show_usage();
- }
- flags |= OPT_SET_VER;
- continue;
- }
- }
-
- if (decode_arg(arg) == EOF)
+ for (;;) {
+ char *arg = *++argv;
+ if (!arg)
+ bb_show_usage();
+ if (arg[0] != '-' && arg[0] != '+' && arg[0] != '=')
break;
+
+ argv = decode_arg(argv, &g);
}
+ /* note: on loop exit, remaining argv[] is never empty */
/* run sanity checks on all the arguments given us */
- if (i >= argc)
- bb_show_usage();
- if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM))) {
- bb_error_msg("= is incompatible with - and +");
- return EXIT_FAILURE;
- }
- if ((rf & af) != 0) {
- bb_error_msg("Can't both set and unset same flag");
- return EXIT_FAILURE;
- }
- if (!flags) {
- bb_error_msg("Must use '-v', =, - or +");
- return EXIT_FAILURE;
- }
+ if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
+ bb_error_msg_and_die("= is incompatible with - and +");
+ if (g.rf & g.af)
+ bb_error_msg_and_die("can't set and unset a flag");
+ if (!g.flags)
+ bb_error_msg_and_die("must use '-v', =, - or +");
/* now run chattr on all the files passed to us */
- while (i < argc)
- change_attributes(argv[i++]);
+ do change_attributes(*argv, &g); while (*++argv);
return EXIT_SUCCESS;
}