1997-04-26 17:21:57 +04: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
|
1999-01-05 10:02:39 +03:00
|
|
|
* 98/12/29 - Ignore symlinks when working recursively (G M Sipe)
|
|
|
|
* 98/12/29 - Display version info only when -V specified (G M Sipe)
|
1997-04-26 17:21:57 +04:00
|
|
|
*/
|
|
|
|
|
2001-02-08 06:06:43 +03:00
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
|
2011-09-19 01:34:37 +04:00
|
|
|
#include "config.h"
|
1997-04-26 18:00:26 +04:00
|
|
|
#include <sys/types.h>
|
1997-04-26 17:21:57 +04:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
1997-04-29 20:17:09 +04:00
|
|
|
#include <string.h>
|
1997-04-26 18:00:26 +04:00
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
1997-04-26 17:21:57 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2image.c, findsuper.c, lsattr.c,
mke2fs.c, mklost+found.c, tune2fs.c, util.c: Change location of
ext2_fs.h to be ext2fs/ext2_fs.h
ChangeLog, Makefile.in, resize2fs.h:
resize2fs.h: Change location of ext2_fs.h to be ext2fs/ext2_fs.h
ChangeLog, Makefile.in, debugfs.h:
debugfs.h: Change location of ext2_fs.h to be ext2fs/ext2_fs.h
ChangeLog, Makefile.in, e2fsck.h, scantest.c:
e2fsck.h, scantest.c: Change location of ext2_fs.h to be
ext2fs/ext2_fs.h
ChangeLog, Makefile.in, tst_uuid.c, uuid_time.c:
tst_uuid.c, uuid_time.c: Remove unneeded #include of ext2_fs.h
ChangeLog, Makefile.in, e2p.h:
e2p.h: Change location of ext2_fs.h to be ext2fs/ext2_fs.h
ChangeLog, Makefile.in, test_icount.c, test_rel.c:
test_icount.c, test_rel.c: Change location of ext2_fs.h to be
ext2fs/ext2_fs.h
2001-05-14 15:45:38 +04:00
|
|
|
#include "ext2fs/ext2_fs.h"
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2003-12-07 09:28:50 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define EXT2FS_ATTR(x) __attribute__(x)
|
|
|
|
#else
|
|
|
|
#define EXT2FS_ATTR(x)
|
|
|
|
#endif
|
|
|
|
|
1999-10-26 18:29:22 +04:00
|
|
|
#ifndef S_ISLNK /* So we can compile even with gcc-warn */
|
|
|
|
# ifdef __S_IFLNK
|
|
|
|
# define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
|
|
|
|
# else
|
|
|
|
# define S_ISLNK(mode) 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
#include "et/com_err.h"
|
|
|
|
#include "e2p/e2p.h"
|
|
|
|
|
|
|
|
#include "../version.h"
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 03:47:55 +03:00
|
|
|
#include "nls-enable.h"
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2000-02-11 08:00:19 +03:00
|
|
|
static const char * program_name = "chattr";
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2000-12-31 16:48:12 +03:00
|
|
|
static int add;
|
|
|
|
static int rem;
|
|
|
|
static int set;
|
|
|
|
static int set_version;
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2000-02-11 08:00:19 +03:00
|
|
|
static unsigned long version;
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2000-12-31 16:48:12 +03:00
|
|
|
static int recursive;
|
|
|
|
static int verbose;
|
2007-10-22 16:51:39 +04:00
|
|
|
static int silent;
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2000-02-11 08:00:19 +03:00
|
|
|
static unsigned long af;
|
|
|
|
static unsigned long rf;
|
|
|
|
static unsigned long sf;
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2001-06-08 06:53:20 +04:00
|
|
|
#ifdef _LFS64_LARGEFILE
|
|
|
|
#define LSTAT lstat64
|
|
|
|
#define STRUCT_STAT struct stat64
|
|
|
|
#else
|
|
|
|
#define LSTAT lstat
|
|
|
|
#define STRUCT_STAT struct stat
|
|
|
|
#endif
|
|
|
|
|
2006-11-15 07:38:17 +03:00
|
|
|
static void usage(void)
|
1997-04-26 17:21:57 +04:00
|
|
|
{
|
2007-10-22 16:51:39 +04:00
|
|
|
fprintf(stderr,
|
2009-01-06 09:37:14 +03:00
|
|
|
_("Usage: %s [-RVf] [-+=AacDdeijsSu] [-v version] files...\n"),
|
2006-11-15 07:38:17 +03:00
|
|
|
program_name);
|
|
|
|
exit(1);
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
|
2000-12-31 16:48:12 +03:00
|
|
|
struct flags_char {
|
|
|
|
unsigned long flag;
|
|
|
|
char optchar;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct flags_char flags_array[] = {
|
|
|
|
{ EXT2_NOATIME_FL, 'A' },
|
|
|
|
{ EXT2_SYNC_FL, 'S' },
|
2002-06-16 02:58:39 +04:00
|
|
|
{ EXT2_DIRSYNC_FL, 'D' },
|
2000-12-31 16:48:12 +03:00
|
|
|
{ EXT2_APPEND_FL, 'a' },
|
|
|
|
{ EXT2_COMPR_FL, 'c' },
|
|
|
|
{ EXT2_NODUMP_FL, 'd' },
|
2009-01-06 09:37:14 +03:00
|
|
|
{ EXT4_EXTENTS_FL, 'e'},
|
2000-12-31 16:48:12 +03:00
|
|
|
{ EXT2_IMMUTABLE_FL, 'i' },
|
|
|
|
{ EXT3_JOURNAL_DATA_FL, 'j' },
|
|
|
|
{ EXT2_SECRM_FL, 's' },
|
|
|
|
{ EXT2_UNRM_FL, 'u' },
|
2001-11-06 03:22:02 +03:00
|
|
|
{ EXT2_NOTAIL_FL, 't' },
|
2002-11-01 09:53:52 +03:00
|
|
|
{ EXT2_TOPDIR_FL, 'T' },
|
2000-12-31 16:48:12 +03:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long get_flag(char c)
|
|
|
|
{
|
|
|
|
const struct flags_char *fp;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2000-12-31 16:48:12 +03:00
|
|
|
for (fp = flags_array; fp->flag != 0; fp++) {
|
|
|
|
if (fp->optchar == c)
|
|
|
|
return fp->flag;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
static int decode_arg (int * i, int argc, char ** argv)
|
|
|
|
{
|
|
|
|
char * p;
|
|
|
|
char * tmp;
|
2000-12-31 16:48:12 +03:00
|
|
|
unsigned long fl;
|
1997-04-26 17:21:57 +04:00
|
|
|
|
|
|
|
switch (argv[*i][0])
|
|
|
|
{
|
|
|
|
case '-':
|
2000-12-31 16:48:12 +03:00
|
|
|
for (p = &argv[*i][1]; *p; p++) {
|
|
|
|
if (*p == 'R') {
|
1997-04-26 17:21:57 +04:00
|
|
|
recursive = 1;
|
2000-12-31 16:48:12 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*p == 'V') {
|
1997-04-26 17:21:57 +04:00
|
|
|
verbose = 1;
|
2000-12-31 16:48:12 +03:00
|
|
|
continue;
|
|
|
|
}
|
2007-10-22 16:51:39 +04:00
|
|
|
if (*p == 'f') {
|
|
|
|
silent = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2000-12-31 16:48:12 +03:00
|
|
|
if (*p == 'v') {
|
1997-04-29 18:53:37 +04:00
|
|
|
(*i)++;
|
1997-04-26 17:21:57 +04:00
|
|
|
if (*i >= argc)
|
|
|
|
usage ();
|
|
|
|
version = strtol (argv[*i], &tmp, 0);
|
2000-12-31 16:48:12 +03:00
|
|
|
if (*tmp) {
|
1997-04-26 17:21:57 +04:00
|
|
|
com_err (program_name, 0,
|
2007-10-22 16:51:39 +04:00
|
|
|
_("bad version - %s\n"),
|
2000-12-31 16:48:12 +03:00
|
|
|
argv[*i]);
|
1997-04-26 17:21:57 +04:00
|
|
|
usage ();
|
|
|
|
}
|
|
|
|
set_version = 1;
|
2000-12-31 16:48:12 +03:00
|
|
|
continue;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
2000-12-31 16:48:12 +03:00
|
|
|
if ((fl = get_flag(*p)) == 0)
|
|
|
|
usage();
|
|
|
|
rf |= fl;
|
|
|
|
rem = 1;
|
|
|
|
}
|
1997-04-26 17:21:57 +04:00
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
add = 1;
|
2000-12-31 16:48:12 +03:00
|
|
|
for (p = &argv[*i][1]; *p; p++) {
|
|
|
|
if ((fl = get_flag(*p)) == 0)
|
|
|
|
usage();
|
|
|
|
af |= fl;
|
|
|
|
}
|
1997-04-26 17:21:57 +04:00
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
set = 1;
|
2000-12-31 16:48:12 +03:00
|
|
|
for (p = &argv[*i][1]; *p; p++) {
|
|
|
|
if ((fl = get_flag(*p)) == 0)
|
|
|
|
usage();
|
|
|
|
sf |= fl;
|
|
|
|
}
|
1997-04-26 17:21:57 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EOF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-18 06:17:13 +03:00
|
|
|
static int chattr_dir_proc(const char *, struct dirent *, void *);
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2008-03-18 06:17:13 +03:00
|
|
|
static int change_attributes(const char * name)
|
1997-04-26 17:21:57 +04:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2001-06-08 06:53:20 +04:00
|
|
|
STRUCT_STAT st;
|
2009-01-06 09:37:14 +03:00
|
|
|
int extent_file = 0;
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2001-06-08 06:53:20 +04:00
|
|
|
if (LSTAT (name, &st) == -1) {
|
2007-10-22 16:51:39 +04:00
|
|
|
if (!silent)
|
|
|
|
com_err (program_name, errno,
|
|
|
|
_("while trying to stat %s"), name);
|
|
|
|
return -1;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
2000-02-11 08:00:19 +03:00
|
|
|
|
2009-01-06 09:37:14 +03:00
|
|
|
if (fgetflags(name, &flags) == -1) {
|
|
|
|
if (!silent)
|
|
|
|
com_err(program_name, errno,
|
|
|
|
_("while reading flags on %s"), name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (flags & EXT4_EXTENTS_FL)
|
|
|
|
extent_file = 1;
|
2000-02-11 08:00:19 +03:00
|
|
|
if (set) {
|
2009-01-06 09:37:14 +03:00
|
|
|
if (extent_file && !(sf & EXT4_EXTENTS_FL)) {
|
|
|
|
if (!silent)
|
|
|
|
com_err(program_name, 0,
|
|
|
|
_("Clearing extent flag not supported on %s"),
|
|
|
|
name);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-11 08:00:19 +03:00
|
|
|
if (verbose) {
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 03:47:55 +03:00
|
|
|
printf (_("Flags of %s set as "), name);
|
1997-04-26 17:34:30 +04:00
|
|
|
print_flags (stdout, sf, 0);
|
1997-04-26 17:21:57 +04:00
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
if (fsetflags (name, sf) == -1)
|
|
|
|
perror (name);
|
2000-02-11 08:00:19 +03:00
|
|
|
} else {
|
2009-01-06 09:37:14 +03:00
|
|
|
if (rem)
|
|
|
|
flags &= ~rf;
|
|
|
|
if (add)
|
|
|
|
flags |= af;
|
|
|
|
if (extent_file && !(flags & EXT4_EXTENTS_FL)) {
|
2007-10-22 16:51:39 +04:00
|
|
|
if (!silent)
|
2009-01-06 09:37:14 +03:00
|
|
|
com_err(program_name, 0,
|
|
|
|
_("Clearing extent flag not supported on %s"),
|
|
|
|
name);
|
2007-10-22 16:51:39 +04:00
|
|
|
return -1;
|
2009-01-06 09:37:14 +03:00
|
|
|
}
|
|
|
|
if (verbose) {
|
|
|
|
printf(_("Flags of %s set as "), name);
|
|
|
|
print_flags(stdout, flags, 0);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
flags &= ~EXT2_DIRSYNC_FL;
|
|
|
|
if (fsetflags(name, flags) == -1) {
|
|
|
|
if (!silent) {
|
|
|
|
com_err(program_name, errno,
|
2007-10-22 16:51:39 +04:00
|
|
|
_("while setting flags on %s"),
|
|
|
|
name);
|
|
|
|
}
|
2009-01-06 09:37:14 +03:00
|
|
|
return -1;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
}
|
2000-02-11 08:00:19 +03:00
|
|
|
if (set_version) {
|
1997-04-26 17:21:57 +04:00
|
|
|
if (verbose)
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 03:47:55 +03:00
|
|
|
printf (_("Version of %s set as %lu\n"), name, version);
|
2007-10-22 16:51:39 +04:00
|
|
|
if (fsetversion (name, version) == -1) {
|
|
|
|
if (!silent)
|
|
|
|
com_err (program_name, errno,
|
|
|
|
_("while setting version on %s"),
|
|
|
|
name);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
if (S_ISDIR(st.st_mode) && recursive)
|
2007-10-22 16:51:39 +04:00
|
|
|
return iterate_on_dir (name, chattr_dir_proc, NULL);
|
|
|
|
return 0;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
|
2000-02-11 08:00:19 +03:00
|
|
|
static int chattr_dir_proc (const char * dir_name, struct dirent * de,
|
2003-12-07 09:28:50 +03:00
|
|
|
void * private EXT2FS_ATTR((unused)))
|
1997-04-26 17:21:57 +04:00
|
|
|
{
|
2007-10-22 16:51:39 +04:00
|
|
|
int ret = 0;
|
|
|
|
|
2000-02-11 08:00:19 +03:00
|
|
|
if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
|
1997-04-26 18:00:26 +04:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
|
2006-11-15 07:38:17 +03:00
|
|
|
if (!path) {
|
|
|
|
fprintf(stderr, _("Couldn't allocate path variable "
|
|
|
|
"in chattr_dir_proc"));
|
2007-10-22 16:51:39 +04:00
|
|
|
return -1;
|
2006-11-15 07:38:17 +03:00
|
|
|
}
|
2007-10-22 16:51:39 +04:00
|
|
|
sprintf(path, "%s/%s", dir_name, de->d_name);
|
2008-03-18 06:17:13 +03:00
|
|
|
ret = change_attributes(path);
|
1997-04-26 18:00:26 +04:00
|
|
|
free(path);
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
2007-10-22 16:51:39 +04:00
|
|
|
return ret;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
|
1997-09-16 06:13:52 +04:00
|
|
|
int main (int argc, char ** argv)
|
1997-04-26 17:21:57 +04:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int end_arg = 0;
|
2007-10-22 16:51:39 +04:00
|
|
|
int err, retval = 0;
|
1997-04-26 17:21:57 +04:00
|
|
|
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 03:47:55 +03:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
setlocale(LC_MESSAGES, "");
|
2002-03-05 11:26:52 +03:00
|
|
|
setlocale(LC_CTYPE, "");
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 03:47:55 +03:00
|
|
|
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
|
|
|
|
textdomain(NLS_CAT_NAME);
|
2011-10-05 09:00:30 +04:00
|
|
|
set_com_err_gettext(gettext);
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 03:47:55 +03:00
|
|
|
#endif
|
1997-04-26 17:21:57 +04:00
|
|
|
if (argc && *argv)
|
|
|
|
program_name = *argv;
|
|
|
|
i = 1;
|
2000-02-11 08:00:19 +03:00
|
|
|
while (i < argc && !end_arg) {
|
2004-01-20 21:39:01 +03:00
|
|
|
/* '--' arg should end option processing */
|
|
|
|
if (strcmp(argv[i], "--") == 0) {
|
|
|
|
i++;
|
|
|
|
end_arg = 1;
|
|
|
|
} else if (decode_arg (&i, argc, argv) == EOF)
|
1997-04-26 17:21:57 +04:00
|
|
|
end_arg = 1;
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i >= argc)
|
|
|
|
usage ();
|
2000-02-11 08:00:19 +03:00
|
|
|
if (set && (add || rem)) {
|
2003-12-07 09:28:50 +03:00
|
|
|
fputs(_("= is incompatible with - and +\n"), stderr);
|
1997-04-26 17:21:57 +04:00
|
|
|
exit (1);
|
|
|
|
}
|
2000-02-11 08:00:19 +03:00
|
|
|
if ((rf & af) != 0) {
|
2003-12-07 09:28:50 +03:00
|
|
|
fputs("Can't both set and unset same flag.\n", stderr);
|
2000-02-11 08:00:19 +03:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (!(add || rem || set || set_version)) {
|
2003-12-07 09:28:50 +03:00
|
|
|
fputs(_("Must use '-v', =, - or +\n"), stderr);
|
1997-04-26 17:21:57 +04:00
|
|
|
exit (1);
|
|
|
|
}
|
1999-01-05 10:02:39 +03:00
|
|
|
if (verbose)
|
2001-08-27 20:44:23 +04:00
|
|
|
fprintf (stderr, "chattr %s (%s)\n",
|
|
|
|
E2FSPROGS_VERSION, E2FSPROGS_DATE);
|
2007-10-22 16:51:39 +04:00
|
|
|
for (j = i; j < argc; j++) {
|
2008-03-18 06:17:13 +03:00
|
|
|
err = change_attributes (argv[j]);
|
2007-10-22 16:51:39 +04:00
|
|
|
if (err)
|
|
|
|
retval = 1;
|
|
|
|
}
|
|
|
|
exit(retval);
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|