blob: b1c77faad628fc0bd5c050f8f0065d770145a5c1 [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* 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 Orga6a3e01d2011-04-01 22:56:30 +020022//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"
42//usage: "\nOptions:"
43//usage: "\n -R Recurse"
44//usage: "\n -v Set the file's version/generation number"
45
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000046#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000047#include "e2fs_lib.h"
48
49#define OPT_ADD 1
50#define OPT_REM 2
51#define OPT_SET 4
52#define OPT_SET_VER 8
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000053
Denis Vlasenko8acf5212007-04-15 11:48:27 +000054struct globals {
55 unsigned long version;
56 unsigned long af;
57 unsigned long rf;
58 smallint flags;
59 smallint recursive;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000060};
61
62static unsigned long get_flag(char c)
63{
Denis Vlasenkod059ddc2007-10-30 19:36:07 +000064 const char *fp = strchr(e2attr_flags_sname_chattr, c);
65 if (fp)
66 return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000067 bb_show_usage();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000068}
69
Denis Vlasenko8acf5212007-04-15 11:48:27 +000070static int decode_arg(const char *arg, struct globals *gp)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000071{
72 unsigned long *fl;
73 char opt = *arg++;
74
Denis Vlasenko8acf5212007-04-15 11:48:27 +000075 fl = &gp->af;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000076 if (opt == '-') {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000077 gp->flags |= OPT_REM;
78 fl = &gp->rf;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000079 } else if (opt == '+') {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000080 gp->flags |= OPT_ADD;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000081 } else if (opt == '=') {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000082 gp->flags |= OPT_SET;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000083 } else
84 return 0;
85
86 while (*arg)
87 *fl |= get_flag(*arg++);
88
89 return 1;
90}
91
Denis Vlasenko8acf5212007-04-15 11:48:27 +000092static void change_attributes(const char *name, struct globals *gp);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000093
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020094static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000095{
96 char *path = concat_subpath_file(dir_name, de->d_name);
97 /* path is NULL if de->d_name is "." or "..", else... */
98 if (path) {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000099 change_attributes(path, gp);
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000100 free(path);
101 }
102 return 0;
103}
104
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000105static void change_attributes(const char *name, struct globals *gp)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000106{
107 unsigned long fsflags;
108 struct stat st;
109
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000110 if (lstat(name, &st) != 0) {
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000111 bb_perror_msg("stat %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000112 return;
113 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000114 if (S_ISLNK(st.st_mode) && gp->recursive)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000115 return;
116
117 /* Don't try to open device files, fifos etc. We probably
118 * ought to display an error if the file was explicitly given
119 * on the command line (whether or not recursive was
120 * requested). */
121 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
122 return;
123
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000124 if (gp->flags & OPT_SET_VER)
125 if (fsetversion(name, gp->version) != 0)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000126 bb_perror_msg("setting version on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000127
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000128 if (gp->flags & OPT_SET) {
129 fsflags = gp->af;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000130 } else {
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000131 if (fgetflags(name, &fsflags) != 0) {
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000132 bb_perror_msg("reading flags on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000133 goto skip_setflags;
134 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000135 /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
136 fsflags &= ~gp->rf;
137 /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
138 fsflags |= gp->af;
139 /* What is this? And why it's not done for SET case? */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000140 if (!S_ISDIR(st.st_mode))
141 fsflags &= ~EXT2_DIRSYNC_FL;
142 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000143 if (fsetflags(name, fsflags) != 0)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000144 bb_perror_msg("setting flags on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000145
146 skip_setflags:
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000147 if (gp->recursive && S_ISDIR(st.st_mode))
148 iterate_on_dir(name, chattr_dir_proc, gp);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000149}
150
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000151int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000152int chattr_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000153{
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000154 struct globals g;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000155 char *arg;
156
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000157 memset(&g, 0, sizeof(g));
158
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000159 /* parse the args */
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000160 while ((arg = *++argv)) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000161 /* take care of -R and -v <version> */
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000162 if (arg[0] == '-'
163 && (arg[1] == 'R' || arg[1] == 'v')
164 && !arg[2]
165 ) {
166 if (arg[1] == 'R') {
167 g.recursive = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000168 continue;
169 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000170 /* arg[1] == 'v' */
171 if (!*++argv)
172 bb_show_usage();
173 g.version = xatoul(*argv);
174 g.flags |= OPT_SET_VER;
175 continue;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000176 }
177
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000178 if (!decode_arg(arg, &g))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000179 break;
180 }
181
182 /* run sanity checks on all the arguments given us */
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000183 if (!*argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000184 bb_show_usage();
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000185 if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000186 bb_error_msg_and_die("= is incompatible with - and +");
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000187 if (g.rf & g.af)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000188 bb_error_msg_and_die("can't set and unset a flag");
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000189 if (!g.flags)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000190 bb_error_msg_and_die("must use '-v', =, - or +");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000191
192 /* now run chattr on all the files passed to us */
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000193 do change_attributes(*argv, &g); while (*++argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000194
195 return EXIT_SUCCESS;
196}