blob: 0f19af8e416f938e9b3fd40122e54e8744e5f12e [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000023#include "e2fs_lib.h"
24
25#define OPT_ADD 1
26#define OPT_REM 2
27#define OPT_SET 4
28#define OPT_SET_VER 8
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000029
Denis Vlasenko8acf5212007-04-15 11:48:27 +000030struct globals {
31 unsigned long version;
32 unsigned long af;
33 unsigned long rf;
34 smallint flags;
35 smallint recursive;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000036};
37
38static unsigned long get_flag(char c)
39{
Denis Vlasenkod059ddc2007-10-30 19:36:07 +000040 const char *fp = strchr(e2attr_flags_sname_chattr, c);
41 if (fp)
42 return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000043 bb_show_usage();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000044}
45
Denis Vlasenko8acf5212007-04-15 11:48:27 +000046static int decode_arg(const char *arg, struct globals *gp)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000047{
48 unsigned long *fl;
49 char opt = *arg++;
50
Denis Vlasenko8acf5212007-04-15 11:48:27 +000051 fl = &gp->af;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000052 if (opt == '-') {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000053 gp->flags |= OPT_REM;
54 fl = &gp->rf;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000055 } else if (opt == '+') {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000056 gp->flags |= OPT_ADD;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000057 } else if (opt == '=') {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000058 gp->flags |= OPT_SET;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000059 } else
60 return 0;
61
62 while (*arg)
63 *fl |= get_flag(*arg++);
64
65 return 1;
66}
67
Denis Vlasenko8acf5212007-04-15 11:48:27 +000068static void change_attributes(const char *name, struct globals *gp);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000069
Denis Vlasenko8acf5212007-04-15 11:48:27 +000070static int chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000071{
72 char *path = concat_subpath_file(dir_name, de->d_name);
73 /* path is NULL if de->d_name is "." or "..", else... */
74 if (path) {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000075 change_attributes(path, gp);
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000076 free(path);
77 }
78 return 0;
79}
80
Denis Vlasenko8acf5212007-04-15 11:48:27 +000081static void change_attributes(const char *name, struct globals *gp)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000082{
83 unsigned long fsflags;
84 struct stat st;
85
Denis Vlasenko8acf5212007-04-15 11:48:27 +000086 if (lstat(name, &st) != 0) {
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000087 bb_perror_msg("stat %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000088 return;
89 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +000090 if (S_ISLNK(st.st_mode) && gp->recursive)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000091 return;
92
93 /* Don't try to open device files, fifos etc. We probably
94 * ought to display an error if the file was explicitly given
95 * on the command line (whether or not recursive was
96 * requested). */
97 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
98 return;
99
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000100 if (gp->flags & OPT_SET_VER)
101 if (fsetversion(name, gp->version) != 0)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000102 bb_perror_msg("setting version on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000103
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000104 if (gp->flags & OPT_SET) {
105 fsflags = gp->af;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000106 } else {
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000107 if (fgetflags(name, &fsflags) != 0) {
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000108 bb_perror_msg("reading flags on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000109 goto skip_setflags;
110 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000111 /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
112 fsflags &= ~gp->rf;
113 /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
114 fsflags |= gp->af;
115 /* What is this? And why it's not done for SET case? */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000116 if (!S_ISDIR(st.st_mode))
117 fsflags &= ~EXT2_DIRSYNC_FL;
118 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000119 if (fsetflags(name, fsflags) != 0)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000120 bb_perror_msg("setting flags on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000121
122 skip_setflags:
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000123 if (gp->recursive && S_ISDIR(st.st_mode))
124 iterate_on_dir(name, chattr_dir_proc, gp);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000125}
126
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000127int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000128int chattr_main(int argc, char **argv)
129{
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000130 struct globals g;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000131 char *arg;
132
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000133 memset(&g, 0, sizeof(g));
134
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000135 /* parse the args */
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000136 while ((arg = *++argv)) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000137 /* take care of -R and -v <version> */
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000138 if (arg[0] == '-'
139 && (arg[1] == 'R' || arg[1] == 'v')
140 && !arg[2]
141 ) {
142 if (arg[1] == 'R') {
143 g.recursive = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000144 continue;
145 }
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000146 /* arg[1] == 'v' */
147 if (!*++argv)
148 bb_show_usage();
149 g.version = xatoul(*argv);
150 g.flags |= OPT_SET_VER;
151 continue;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000152 }
153
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000154 if (!decode_arg(arg, &g))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000155 break;
156 }
157
158 /* run sanity checks on all the arguments given us */
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000159 if (!*argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000160 bb_show_usage();
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000161 if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000162 bb_error_msg_and_die("= is incompatible with - and +");
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000163 if (g.rf & g.af)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000164 bb_error_msg_and_die("can't set and unset a flag");
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000165 if (!g.flags)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000166 bb_error_msg_and_die("must use '-v', =, - or +");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000167
168 /* now run chattr on all the files passed to us */
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000169 do change_attributes(*argv, &g); while (*++argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000170
171 return EXIT_SUCCESS;
172}