Whamcloud - gitweb
mke2fs: Use a fs_type of 'journal' when creating an external journal
[tools/e2fsprogs.git] / misc / chattr.c
index f0301e6..daae163 100644 (file)
@@ -18,6 +18,8 @@
  * 98/12/29    - Display version info only when -V specified (G M Sipe)
  */
 
+#define _LARGEFILE64_SOURCE
+
 #include <sys/types.h>
 #include <dirent.h>
 #include <fcntl.h>
 #endif
 #include <sys/param.h>
 #include <sys/stat.h>
-#include <linux/ext2_fs.h>
+#include "ext2fs/ext2_fs.h"
+
+#ifdef __GNUC__
+#define EXT2FS_ATTR(x) __attribute__(x)
+#else
+#define EXT2FS_ATTR(x)
+#endif
 
 #ifndef S_ISLNK                        /* So we can compile even with gcc-warn */
 # ifdef __S_IFLNK
@@ -57,20 +65,28 @@ static unsigned long version;
 
 static int recursive;
 static int verbose;
+static int silent;
 
 static unsigned long af;
 static unsigned long rf;
 static unsigned long sf;
 
-static void fatal_error(const char * fmt_string, int errcode)
+#ifdef _LFS64_LARGEFILE
+#define LSTAT          lstat64
+#define STRUCT_STAT    struct stat64
+#else
+#define LSTAT          lstat
+#define STRUCT_STAT    struct stat
+#endif
+
+static void usage(void)
 {
-       fprintf (stderr, fmt_string, program_name);
-       exit (errcode);
+       fprintf(stderr,
+               _("Usage: %s [-RVf] [-+=AacDdijsSu] [-v version] files...\n"),
+               program_name);
+       exit(1);
 }
 
-#define usage() fatal_error(_("usage: %s [-RV] [-+=AacdijsSu] [-v version] files...\n"), \
-                            1)
-
 struct flags_char {
        unsigned long   flag;
        char            optchar;
@@ -79,6 +95,7 @@ struct flags_char {
 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' },
@@ -86,6 +103,8 @@ static const struct flags_char flags_array[] = {
        { EXT3_JOURNAL_DATA_FL, 'j' },
        { EXT2_SECRM_FL, 's' },
        { EXT2_UNRM_FL, 'u' },
+       { EXT2_NOTAIL_FL, 't' },
+       { EXT2_TOPDIR_FL, 'T' },
        { 0, 0 }
 };
 
@@ -119,6 +138,10 @@ static int decode_arg (int * i, int argc, char ** argv)
                                verbose = 1;
                                continue;
                        }
+                       if (*p == 'f') {
+                               silent = 1;
+                               continue;
+                       }
                        if (*p == 'v') {
                                (*i)++;
                                if (*i >= argc)
@@ -126,7 +149,7 @@ static int decode_arg (int * i, int argc, char ** argv)
                                version = strtol (argv[*i], &tmp, 0);
                                if (*tmp) {
                                        com_err (program_name, 0,
-                                                _("bad version - %s\n"), 
+                                                _("bad version - %s\n"),
                                                 argv[*i]);
                                        usage ();
                                }
@@ -162,28 +185,19 @@ static int decode_arg (int * i, int argc, char ** argv)
        return 1;
 }
 
-static int chattr_dir_proc (const char *, struct dirent *, void *);
+static int chattr_dir_proc(const char *, struct dirent *, void *);
 
-static void change_attributes (const char * name)
+static int change_attributes(const char * name)
 {
        unsigned long flags;
-       struct stat st;
+       STRUCT_STAT     st;
 
-       if (lstat (name, &st) == -1) {
-               com_err (program_name, errno, _("while trying to stat %s"), 
-                        name);
-               return;
+       if (LSTAT (name, &st) == -1) {
+               if (!silent)
+                       com_err (program_name, errno,
+                                _("while trying to stat %s"), name);
+               return -1;
        }
-       if (S_ISLNK(st.st_mode) && recursive)
-               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;
 
        if (set) {
                if (verbose) {
@@ -194,10 +208,12 @@ static void change_attributes (const char * name)
                if (fsetflags (name, sf) == -1)
                        perror (name);
        } else {
-               if (fgetflags (name, &flags) == -1)
-                       com_err (program_name, errno,
-                                _("while reading flags on %s"), name);
-               else {
+               if (fgetflags (name, &flags) == -1) {
+                       if (!silent)
+                               com_err (program_name, errno,
+                                        _("while reading flags on %s"), name);
+                       return -1;
+               } else {
                        if (rem)
                                flags &= ~rf;
                        if (add)
@@ -207,46 +223,63 @@ static void change_attributes (const char * name)
                                print_flags (stdout, flags, 0);
                                printf ("\n");
                        }
-                       if (fsetflags (name, flags) == -1)
-                               com_err (program_name, errno,
-                                        _("while setting flags on %s"), name);
+                       if (!S_ISDIR(st.st_mode))
+                               flags &= ~EXT2_DIRSYNC_FL;
+                       if (fsetflags (name, flags) == -1) {
+                               if (!silent)
+                                       com_err(program_name, errno,
+                                               _("while setting flags on %s"),
+                                               name);
+                               return -1;
+                       }
                }
        }
        if (set_version) {
                if (verbose)
                        printf (_("Version of %s set as %lu\n"), name, version);
-               if (fsetversion (name, version) == -1)
-                       com_err (program_name, errno,
-                                _("while setting version on %s"), name);
+               if (fsetversion (name, version) == -1) {
+                       if (!silent)
+                               com_err (program_name, errno,
+                                        _("while setting version on %s"),
+                                        name);
+                       return -1;
+               }
        }
        if (S_ISDIR(st.st_mode) && recursive)
-               iterate_on_dir (name, chattr_dir_proc, NULL);
+               return iterate_on_dir (name, chattr_dir_proc, NULL);
+       return 0;
 }
 
 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
-                           void * unused_private)
+                           void * private EXT2FS_ATTR((unused)))
 {
+       int ret = 0;
+
        if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
                char *path;
 
                path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
-               if (!path)
-                       fatal_error(_("Couldn't allocate path variable "
-                                   "in chattr_dir_proc"), 1);
-               sprintf (path, "%s/%s", dir_name, de->d_name);
-               change_attributes (path);
+               if (!path) {
+                       fprintf(stderr, _("Couldn't allocate path variable "
+                                         "in chattr_dir_proc"));
+                       return -1;
+               }
+               sprintf(path, "%s/%s", dir_name, de->d_name);
+               ret = change_attributes(path);
                free(path);
        }
-       return 0;
+       return ret;
 }
 
 int main (int argc, char ** argv)
 {
        int i, j;
        int end_arg = 0;
+       int err, retval = 0;
 
 #ifdef ENABLE_NLS
        setlocale(LC_MESSAGES, "");
+       setlocale(LC_CTYPE, "");
        bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
        textdomain(NLS_CAT_NAME);
 #endif
@@ -254,7 +287,11 @@ int main (int argc, char ** argv)
                program_name = *argv;
        i = 1;
        while (i < argc && !end_arg) {
-               if (decode_arg (&i, argc, argv) == EOF)
+               /* '--' arg should end option processing */
+               if (strcmp(argv[i], "--") == 0) {
+                       i++;
+                       end_arg = 1;
+               } else if (decode_arg (&i, argc, argv) == EOF)
                        end_arg = 1;
                else
                        i++;
@@ -262,22 +299,24 @@ int main (int argc, char ** argv)
        if (i >= argc)
                usage ();
        if (set && (add || rem)) {
-               fprintf (stderr, _("= is incompatible with - and +\n"));
+               fputs(_("= is incompatible with - and +\n"), stderr);
                exit (1);
        }
        if ((rf & af) != 0) {
-               fprintf (stderr, "Can't both set and unset same flag.\n");
+               fputs("Can't both set and unset same flag.\n", stderr);
                exit (1);
        }
        if (!(add || rem || set || set_version)) {
-               fprintf (stderr, _("Must use '-v', =, - or +\n"));
+               fputs(_("Must use '-v', =, - or +\n"), stderr);
                exit (1);
        }
        if (verbose)
-               fprintf (stderr, _("chattr %s, %s for EXT2 FS %s, %s\n"),
-                        E2FSPROGS_VERSION, E2FSPROGS_DATE,
-                        EXT2FS_VERSION, EXT2FS_DATE);
-       for (j = i; j < argc; j++)
-               change_attributes (argv[j]);
-       exit(0);
+               fprintf (stderr, "chattr %s (%s)\n",
+                        E2FSPROGS_VERSION, E2FSPROGS_DATE);
+       for (j = i; j < argc; j++) {
+               err = change_attributes (argv[j]);
+               if (err)
+                       retval = 1;
+       }
+       exit(retval);
 }