hush/e2fsprogs/chattr.c

234 lines
5.2 KiB
C
Raw Normal View History

2005-04-24 05:07:59 +00:00
/*
* 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
*/
/*
* 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"
2005-04-25 04:10:35 +00:00
#define OPT_ADD 1
#define OPT_REM 2
#define OPT_SET 4
#define OPT_SET_VER 8
static int flags;
static int recursive;
2005-04-24 05:07:59 +00:00
static unsigned long version;
static unsigned long af;
static unsigned long rf;
static unsigned long sf;
2005-04-25 04:10:35 +00:00
#ifdef CONFIG_LFS
# define LSTAT lstat64
# define STRUCT_STAT struct stat64
2005-04-24 05:07:59 +00:00
#else
2005-04-25 04:10:35 +00:00
# define LSTAT lstat
# define STRUCT_STAT struct stat
2005-04-24 05:07:59 +00:00
#endif
struct flags_char {
2005-04-25 04:10:35 +00:00
unsigned long flag;
char optchar;
2005-04-24 05:07:59 +00:00
};
static const struct flags_char flags_array[] = {
2005-04-25 04:10:35 +00:00
{ 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' },
2005-04-24 05:07:59 +00:00
{ EXT3_JOURNAL_DATA_FL, 'j' },
2005-04-25 04:10:35 +00:00
{ EXT2_SECRM_FL, 's' },
{ EXT2_UNRM_FL, 'u' },
{ EXT2_NOTAIL_FL, 't' },
{ EXT2_TOPDIR_FL, 'T' },
2005-04-24 05:07:59 +00:00
{ 0, 0 }
};
static unsigned long get_flag(char c)
{
const struct flags_char *fp;
2005-04-25 04:10:35 +00:00
for (fp = flags_array; fp->flag; fp++)
2005-04-24 05:07:59 +00:00
if (fp->optchar == c)
return fp->flag;
2005-04-25 04:10:35 +00:00
bb_show_usage();
2005-04-24 05:07:59 +00:00
return 0;
}
2005-04-25 04:10:35 +00:00
static int decode_arg(char *arg)
2005-04-24 05:07:59 +00:00
{
2005-04-25 04:10:35 +00:00
unsigned long *fl;
char opt = *arg++;
if (opt == '-') {
flags |= OPT_REM;
fl = &rf;
} else if (opt == '+') {
flags |= OPT_ADD;
fl = &af;
} else if (opt == '=') {
flags |= OPT_SET;
fl = &sf;
} else
2005-04-24 05:07:59 +00:00
return EOF;
2005-04-25 04:10:35 +00:00
for (; *arg ; ++arg)
(*fl) |= get_flag(*arg);
2005-04-24 05:07:59 +00:00
return 1;
}
2005-04-25 04:10:35 +00:00
static int chattr_dir_proc(const char *, struct dirent *, void *);
2005-04-24 05:07:59 +00:00
2005-04-25 04:10:35 +00:00
static void change_attributes(const char * name)
2005-04-24 05:07:59 +00:00
{
2005-04-25 04:10:35 +00:00
unsigned long fsflags;
2005-04-24 05:07:59 +00:00
STRUCT_STAT st;
2005-04-25 04:10:35 +00:00
if (LSTAT(name, &st) == -1) {
bb_error_msg("while trying to stat %s", name);
2005-04-24 05:07:59 +00:00
return;
}
if (S_ISLNK(st.st_mode) && recursive)
return;
/* Don't try to open device files, fifos etc. We probably
2005-04-25 04:10:35 +00:00
* 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))
2005-04-24 05:07:59 +00:00
return;
2005-04-25 04:10:35 +00:00
if (flags & OPT_SET_VER)
if (fsetversion(name, version) == -1)
bb_error_msg("while setting version on %s", name);
if (flags & OPT_SET) {
fsflags = sf;
2005-04-24 05:07:59 +00:00
} else {
2005-04-25 04:10:35 +00:00
if (fgetflags(name, &fsflags) == -1) {
bb_error_msg("while reading flags on %s", name);
goto skip_setflags;
2005-04-24 05:07:59 +00:00
}
2005-04-25 04:10:35 +00:00
if (flags & OPT_REM)
fsflags &= ~rf;
if (flags & OPT_ADD)
fsflags |= af;
if (!S_ISDIR(st.st_mode))
fsflags &= ~EXT2_DIRSYNC_FL;
2005-04-24 05:07:59 +00:00
}
2005-04-25 04:10:35 +00:00
if (fsetflags(name, fsflags) == -1)
bb_error_msg("while setting flags on %s", name);
skip_setflags:
2005-04-24 05:07:59 +00:00
if (S_ISDIR(st.st_mode) && recursive)
2005-04-25 04:10:35 +00:00
iterate_on_dir(name, chattr_dir_proc, NULL);
2005-04-24 05:07:59 +00:00
}
2005-04-25 04:10:35 +00:00
static int chattr_dir_proc(const char *dir_name, struct dirent *de,
void *private EXT2FS_ATTR((unused)))
2005-04-24 05:07:59 +00:00
{
2005-04-25 04:10:35 +00:00
/*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)
2005-04-25 05:27:12 +00:00
bb_error_msg_and_die(bb_msg_memory_exhausted);
2005-04-25 04:10:35 +00:00
change_attributes(path);
2005-04-24 05:07:59 +00:00
free(path);
}
return 0;
}
2005-04-25 04:10:35 +00:00
int chattr_main(int argc, char **argv)
2005-04-24 05:07:59 +00:00
{
2005-04-25 04:10:35 +00:00
int i;
char *arg;
2005-04-24 05:07:59 +00:00
2005-04-25 04:10:35 +00:00
/* 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)
break;
2005-04-24 05:07:59 +00:00
}
2005-04-25 04:10:35 +00:00
/* run sanity checks on all the arguments given us */
2005-04-24 05:07:59 +00:00
if (i >= argc)
2005-04-25 04:10:35 +00:00
bb_show_usage();
if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM))) {
bb_error_msg("= is incompatible with - and +");
return EXIT_FAILURE;
2005-04-24 05:07:59 +00:00
}
if ((rf & af) != 0) {
2005-04-25 04:10:35 +00:00
bb_error_msg("Can't both set and unset same flag");
return EXIT_FAILURE;
2005-04-24 05:07:59 +00:00
}
2005-04-25 04:10:35 +00:00
if (!flags) {
bb_error_msg("Must use '-v', =, - or +");
return EXIT_FAILURE;
2005-04-24 05:07:59 +00:00
}
2005-04-25 04:10:35 +00:00
/* now run chattr on all the files passed to us */
while (i < argc)
change_attributes(argv[i++]);
return EXIT_SUCCESS;
2005-04-24 05:07:59 +00:00
}