chattr.c revision 0796e660859724f304155e094b6cf5739a610ae4
1/* 2 * chattr.c - Change file attributes on an ext2 file system 3 * 4 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> 5 * Laboratoire MASI, Institut Blaise Pascal 6 * Universite Pierre et Marie Curie (Paris VI) 7 * 8 * This file can be redistributed under the terms of the GNU General 9 * Public License 10 */ 11 12/* 13 * History: 14 * 93/10/30 - Creation 15 * 93/11/13 - Replace stat() calls by lstat() to avoid loops 16 * 94/02/27 - Integrated in Ted's distribution 17 * 98/12/29 - Ignore symlinks when working recursively (G M Sipe) 18 * 98/12/29 - Display version info only when -V specified (G M Sipe) 19 */ 20 21#define _LARGEFILE64_SOURCE 22 23#include "config.h" 24#include <sys/types.h> 25#include <dirent.h> 26#include <fcntl.h> 27#include <stdio.h> 28#include <stdlib.h> 29#include <unistd.h> 30#include <string.h> 31#ifdef HAVE_ERRNO_H 32#include <errno.h> 33#endif 34#include <sys/param.h> 35#include <sys/stat.h> 36#include "ext2fs/ext2_fs.h" 37 38#ifdef __GNUC__ 39#define EXT2FS_ATTR(x) __attribute__(x) 40#else 41#define EXT2FS_ATTR(x) 42#endif 43 44#ifndef S_ISLNK /* So we can compile even with gcc-warn */ 45# ifdef __S_IFLNK 46# define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK) 47# else 48# define S_ISLNK(mode) 0 49# endif 50#endif 51 52#include "et/com_err.h" 53#include "e2p/e2p.h" 54 55#include "../version.h" 56#include "nls-enable.h" 57 58static const char * program_name = "chattr"; 59 60static int add; 61static int rem; 62static int set; 63static int set_version; 64 65static unsigned long version; 66 67static int recursive; 68static int verbose; 69static int silent; 70 71static unsigned long af; 72static unsigned long rf; 73static unsigned long sf; 74 75#ifdef _LFS64_LARGEFILE 76#define LSTAT lstat64 77#define STRUCT_STAT struct stat64 78#else 79#define LSTAT lstat 80#define STRUCT_STAT struct stat 81#endif 82 83static void usage(void) 84{ 85 fprintf(stderr, 86 _("Usage: %s [-RVf] [-+=AacDdeijsSu] [-v version] files...\n"), 87 program_name); 88 exit(1); 89} 90 91struct flags_char { 92 unsigned long flag; 93 char optchar; 94}; 95 96static const struct flags_char flags_array[] = { 97 { EXT2_NOATIME_FL, 'A' }, 98 { EXT2_SYNC_FL, 'S' }, 99 { EXT2_DIRSYNC_FL, 'D' }, 100 { EXT2_APPEND_FL, 'a' }, 101 { EXT2_COMPR_FL, 'c' }, 102 { EXT2_NODUMP_FL, 'd' }, 103 { EXT4_EXTENTS_FL, 'e'}, 104 { EXT2_IMMUTABLE_FL, 'i' }, 105 { EXT3_JOURNAL_DATA_FL, 'j' }, 106 { EXT2_SECRM_FL, 's' }, 107 { EXT2_UNRM_FL, 'u' }, 108 { EXT2_NOTAIL_FL, 't' }, 109 { EXT2_TOPDIR_FL, 'T' }, 110 { FS_NOCOW_FL, 'C' }, 111 { 0, 0 } 112}; 113 114static unsigned long get_flag(char c) 115{ 116 const struct flags_char *fp; 117 118 for (fp = flags_array; fp->flag != 0; fp++) { 119 if (fp->optchar == c) 120 return fp->flag; 121 } 122 return 0; 123} 124 125 126static int decode_arg (int * i, int argc, char ** argv) 127{ 128 char * p; 129 char * tmp; 130 unsigned long fl; 131 132 switch (argv[*i][0]) 133 { 134 case '-': 135 for (p = &argv[*i][1]; *p; p++) { 136 if (*p == 'R') { 137 recursive = 1; 138 continue; 139 } 140 if (*p == 'V') { 141 verbose = 1; 142 continue; 143 } 144 if (*p == 'f') { 145 silent = 1; 146 continue; 147 } 148 if (*p == 'v') { 149 (*i)++; 150 if (*i >= argc) 151 usage (); 152 version = strtol (argv[*i], &tmp, 0); 153 if (*tmp) { 154 com_err (program_name, 0, 155 _("bad version - %s\n"), 156 argv[*i]); 157 usage (); 158 } 159 set_version = 1; 160 continue; 161 } 162 if ((fl = get_flag(*p)) == 0) 163 usage(); 164 rf |= fl; 165 rem = 1; 166 } 167 break; 168 case '+': 169 add = 1; 170 for (p = &argv[*i][1]; *p; p++) { 171 if ((fl = get_flag(*p)) == 0) 172 usage(); 173 af |= fl; 174 } 175 break; 176 case '=': 177 set = 1; 178 for (p = &argv[*i][1]; *p; p++) { 179 if ((fl = get_flag(*p)) == 0) 180 usage(); 181 sf |= fl; 182 } 183 break; 184 default: 185 return EOF; 186 break; 187 } 188 return 1; 189} 190 191static int chattr_dir_proc(const char *, struct dirent *, void *); 192 193static int change_attributes(const char * name) 194{ 195 unsigned long flags; 196 STRUCT_STAT st; 197 int extent_file = 0; 198 199 if (LSTAT (name, &st) == -1) { 200 if (!silent) 201 com_err (program_name, errno, 202 _("while trying to stat %s"), name); 203 return -1; 204 } 205 206 if (fgetflags(name, &flags) == -1) { 207 if (!silent) 208 com_err(program_name, errno, 209 _("while reading flags on %s"), name); 210 return -1; 211 } 212 if (flags & EXT4_EXTENTS_FL) 213 extent_file = 1; 214 if (set) { 215 if (extent_file && !(sf & EXT4_EXTENTS_FL)) { 216 if (!silent) 217 com_err(program_name, 0, 218 _("Clearing extent flag not supported on %s"), 219 name); 220 return -1; 221 } 222 if (verbose) { 223 printf (_("Flags of %s set as "), name); 224 print_flags (stdout, sf, 0); 225 printf ("\n"); 226 } 227 if (fsetflags (name, sf) == -1) 228 perror (name); 229 } else { 230 if (rem) 231 flags &= ~rf; 232 if (add) 233 flags |= af; 234 if (extent_file && !(flags & EXT4_EXTENTS_FL)) { 235 if (!silent) 236 com_err(program_name, 0, 237 _("Clearing extent flag not supported on %s"), 238 name); 239 return -1; 240 } 241 if (verbose) { 242 printf(_("Flags of %s set as "), name); 243 print_flags(stdout, flags, 0); 244 printf("\n"); 245 } 246 if (!S_ISDIR(st.st_mode)) 247 flags &= ~EXT2_DIRSYNC_FL; 248 if (fsetflags(name, flags) == -1) { 249 if (!silent) { 250 com_err(program_name, errno, 251 _("while setting flags on %s"), 252 name); 253 } 254 return -1; 255 } 256 } 257 if (set_version) { 258 if (verbose) 259 printf (_("Version of %s set as %lu\n"), name, version); 260 if (fsetversion (name, version) == -1) { 261 if (!silent) 262 com_err (program_name, errno, 263 _("while setting version on %s"), 264 name); 265 return -1; 266 } 267 } 268 if (S_ISDIR(st.st_mode) && recursive) 269 return iterate_on_dir (name, chattr_dir_proc, NULL); 270 return 0; 271} 272 273static int chattr_dir_proc (const char * dir_name, struct dirent * de, 274 void * private EXT2FS_ATTR((unused))) 275{ 276 int ret = 0; 277 278 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) { 279 char *path; 280 281 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1); 282 if (!path) { 283 fprintf(stderr, _("Couldn't allocate path variable " 284 "in chattr_dir_proc")); 285 return -1; 286 } 287 sprintf(path, "%s/%s", dir_name, de->d_name); 288 ret = change_attributes(path); 289 free(path); 290 } 291 return ret; 292} 293 294int main (int argc, char ** argv) 295{ 296 int i, j; 297 int end_arg = 0; 298 int err, retval = 0; 299 300#ifdef ENABLE_NLS 301 setlocale(LC_MESSAGES, ""); 302 setlocale(LC_CTYPE, ""); 303 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 304 textdomain(NLS_CAT_NAME); 305 set_com_err_gettext(gettext); 306#endif 307 if (argc && *argv) 308 program_name = *argv; 309 i = 1; 310 while (i < argc && !end_arg) { 311 /* '--' arg should end option processing */ 312 if (strcmp(argv[i], "--") == 0) { 313 i++; 314 end_arg = 1; 315 } else if (decode_arg (&i, argc, argv) == EOF) 316 end_arg = 1; 317 else 318 i++; 319 } 320 if (i >= argc) 321 usage (); 322 if (set && (add || rem)) { 323 fputs(_("= is incompatible with - and +\n"), stderr); 324 exit (1); 325 } 326 if ((rf & af) != 0) { 327 fputs("Can't both set and unset same flag.\n", stderr); 328 exit (1); 329 } 330 if (!(add || rem || set || set_version)) { 331 fputs(_("Must use '-v', =, - or +\n"), stderr); 332 exit (1); 333 } 334 if (verbose) 335 fprintf (stderr, "chattr %s (%s)\n", 336 E2FSPROGS_VERSION, E2FSPROGS_DATE); 337 for (j = i; j < argc; j++) { 338 err = change_attributes (argv[j]); 339 if (err) 340 retval = 1; 341 } 342 exit(retval); 343} 344