Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * chattr.c - Change file attributes on an ext2 file system |
| 4 | * |
| 5 | * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> |
| 6 | * Laboratoire MASI, Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | * |
| 9 | * This file can be redistributed under the terms of the GNU General |
| 10 | * Public License |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * History: |
| 15 | * 93/10/30 - Creation |
| 16 | * 93/11/13 - Replace stat() calls by lstat() to avoid loops |
| 17 | * 94/02/27 - Integrated in Ted's distribution |
| 18 | * 98/12/29 - Ignore symlinks when working recursively (G M Sipe) |
| 19 | * 98/12/29 - Display version info only when -V specified (G M Sipe) |
| 20 | */ |
| 21 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 22 | //usage:#define chattr_trivial_usage |
| 23 | //usage: "[-R] [-+=AacDdijsStTu] [-v VERSION] [FILE]..." |
| 24 | //usage:#define chattr_full_usage "\n\n" |
| 25 | //usage: "Change file attributes on an ext2 fs\n" |
| 26 | //usage: "\nModifiers:" |
| 27 | //usage: "\n - Remove attributes" |
| 28 | //usage: "\n + Add attributes" |
| 29 | //usage: "\n = Set attributes" |
| 30 | //usage: "\nAttributes:" |
| 31 | //usage: "\n A Don't track atime" |
| 32 | //usage: "\n a Append mode only" |
| 33 | //usage: "\n c Enable compress" |
| 34 | //usage: "\n D Write dir contents synchronously" |
| 35 | //usage: "\n d Don't backup with dump" |
| 36 | //usage: "\n i Cannot be modified (immutable)" |
| 37 | //usage: "\n j Write all data to journal first" |
| 38 | //usage: "\n s Zero disk storage when deleted" |
| 39 | //usage: "\n S Write file contents synchronously" |
| 40 | //usage: "\n t Disable tail-merging of partial blocks with other files" |
| 41 | //usage: "\n u Allow file to be undeleted" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 42 | //usage: "\n -R Recurse" |
| 43 | //usage: "\n -v Set the file's version/generation number" |
| 44 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 45 | #include "libbb.h" |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 46 | #include "e2fs_lib.h" |
| 47 | |
| 48 | #define OPT_ADD 1 |
| 49 | #define OPT_REM 2 |
| 50 | #define OPT_SET 4 |
| 51 | #define OPT_SET_VER 8 |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 53 | struct globals { |
| 54 | unsigned long version; |
| 55 | unsigned long af; |
| 56 | unsigned long rf; |
| 57 | smallint flags; |
| 58 | smallint recursive; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static unsigned long get_flag(char c) |
| 62 | { |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 63 | const char *fp = strchr(e2attr_flags_sname_chattr, c); |
| 64 | if (fp) |
| 65 | return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr]; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 66 | bb_show_usage(); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 69 | static int decode_arg(const char *arg, struct globals *gp) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 70 | { |
| 71 | unsigned long *fl; |
| 72 | char opt = *arg++; |
| 73 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 74 | fl = &gp->af; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 75 | if (opt == '-') { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 76 | gp->flags |= OPT_REM; |
| 77 | fl = &gp->rf; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 78 | } else if (opt == '+') { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 79 | gp->flags |= OPT_ADD; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 80 | } else if (opt == '=') { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 81 | gp->flags |= OPT_SET; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 82 | } else |
| 83 | return 0; |
| 84 | |
| 85 | while (*arg) |
| 86 | *fl |= get_flag(*arg++); |
| 87 | |
| 88 | return 1; |
| 89 | } |
| 90 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 91 | static void change_attributes(const char *name, struct globals *gp); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 92 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 93 | static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 94 | { |
| 95 | char *path = concat_subpath_file(dir_name, de->d_name); |
| 96 | /* path is NULL if de->d_name is "." or "..", else... */ |
| 97 | if (path) { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 98 | change_attributes(path, gp); |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 99 | free(path); |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 104 | static void change_attributes(const char *name, struct globals *gp) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 105 | { |
| 106 | unsigned long fsflags; |
| 107 | struct stat st; |
| 108 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 109 | if (lstat(name, &st) != 0) { |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 110 | bb_perror_msg("stat %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 111 | return; |
| 112 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 113 | if (S_ISLNK(st.st_mode) && gp->recursive) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 114 | return; |
| 115 | |
| 116 | /* Don't try to open device files, fifos etc. We probably |
| 117 | * ought to display an error if the file was explicitly given |
| 118 | * on the command line (whether or not recursive was |
| 119 | * requested). */ |
| 120 | if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode)) |
| 121 | return; |
| 122 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 123 | if (gp->flags & OPT_SET_VER) |
| 124 | if (fsetversion(name, gp->version) != 0) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 125 | bb_perror_msg("setting version on %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 127 | if (gp->flags & OPT_SET) { |
| 128 | fsflags = gp->af; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 129 | } else { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 130 | if (fgetflags(name, &fsflags) != 0) { |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 131 | bb_perror_msg("reading flags on %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 132 | goto skip_setflags; |
| 133 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 134 | /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */ |
| 135 | fsflags &= ~gp->rf; |
| 136 | /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */ |
| 137 | fsflags |= gp->af; |
| 138 | /* What is this? And why it's not done for SET case? */ |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 139 | if (!S_ISDIR(st.st_mode)) |
| 140 | fsflags &= ~EXT2_DIRSYNC_FL; |
| 141 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 142 | if (fsetflags(name, fsflags) != 0) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 143 | bb_perror_msg("setting flags on %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 144 | |
| 145 | skip_setflags: |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 146 | if (gp->recursive && S_ISDIR(st.st_mode)) |
| 147 | iterate_on_dir(name, chattr_dir_proc, gp); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 150 | int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 151 | int chattr_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 152 | { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 153 | struct globals g; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 154 | char *arg; |
| 155 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 156 | memset(&g, 0, sizeof(g)); |
| 157 | |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 158 | /* parse the args */ |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 159 | while ((arg = *++argv)) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 160 | /* take care of -R and -v <version> */ |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 161 | if (arg[0] == '-' |
| 162 | && (arg[1] == 'R' || arg[1] == 'v') |
| 163 | && !arg[2] |
| 164 | ) { |
| 165 | if (arg[1] == 'R') { |
| 166 | g.recursive = 1; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 167 | continue; |
| 168 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 169 | /* arg[1] == 'v' */ |
| 170 | if (!*++argv) |
| 171 | bb_show_usage(); |
| 172 | g.version = xatoul(*argv); |
| 173 | g.flags |= OPT_SET_VER; |
| 174 | continue; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 177 | if (!decode_arg(arg, &g)) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | |
| 181 | /* run sanity checks on all the arguments given us */ |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 182 | if (!*argv) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 183 | bb_show_usage(); |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 184 | if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM))) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 185 | bb_error_msg_and_die("= is incompatible with - and +"); |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 186 | if (g.rf & g.af) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 187 | bb_error_msg_and_die("can't set and unset a flag"); |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 188 | if (!g.flags) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 189 | bb_error_msg_and_die("must use '-v', =, - or +"); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 190 | |
| 191 | /* now run chattr on all the files passed to us */ |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 192 | do change_attributes(*argv, &g); while (*++argv); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 193 | |
| 194 | return EXIT_SUCCESS; |
| 195 | } |