blob: 3a7dd6b56c18469f917793df81bf09226ca2b31e [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * lsattr.c - List 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 */
Denys Vlasenko000eda42015-10-18 22:40:23 +020012//config:config LSATTR
13//config: bool "lsattr"
14//config: default y
15//config: select PLATFORM_LINUX
16//config: help
17//config: lsattr lists the file attributes on a second extended file system.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000018
Denys Vlasenko000eda42015-10-18 22:40:23 +020019//applet:IF_LSATTR(APPLET(lsattr, BB_DIR_BIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_LSATTR) += lsattr.o e2fs_lib.o
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000022
Pere Orga6a3e01d2011-04-01 22:56:30 +020023//usage:#define lsattr_trivial_usage
24//usage: "[-Radlv] [FILE]..."
25//usage:#define lsattr_full_usage "\n\n"
26//usage: "List file attributes on an ext2 fs\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020027//usage: "\n -R Recurse"
28//usage: "\n -a Don't hide entries starting with ."
29//usage: "\n -d List directory entries instead of contents"
30//usage: "\n -l List long flag names"
31//usage: "\n -v List the file's version/generation number"
32
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000034#include "e2fs_lib.h"
35
36enum {
37 OPT_RECUR = 0x1,
38 OPT_ALL = 0x2,
39 OPT_DIRS_OPT = 0x4,
40 OPT_PF_LONG = 0x8,
41 OPT_GENERATION = 0x10,
42};
43
44static void list_attributes(const char *name)
45{
46 unsigned long fsflags;
47 unsigned long generation;
48
Denis Vlasenko8acf5212007-04-15 11:48:27 +000049 if (fgetflags(name, &fsflags) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000050 goto read_err;
51
52 if (option_mask32 & OPT_GENERATION) {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000053 if (fgetversion(name, &generation) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000054 goto read_err;
55 printf("%5lu ", generation);
56 }
57
58 if (option_mask32 & OPT_PF_LONG) {
59 printf("%-28s ", name);
Denis Vlasenko53354ac2008-06-07 15:10:29 +000060 print_e2flags(stdout, fsflags, PFOPT_LONG);
Denis Vlasenko4daad902007-09-27 10:20:47 +000061 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000062 } else {
Denis Vlasenko53354ac2008-06-07 15:10:29 +000063 print_e2flags(stdout, fsflags, 0);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000064 printf(" %s\n", name);
65 }
66
67 return;
68 read_err:
69 bb_perror_msg("reading %s", name);
70}
71
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020072static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
73 struct dirent *de,
74 void *private UNUSED_PARAM)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000075{
76 struct stat st;
77 char *path;
78
79 path = concat_path_file(dir_name, de->d_name);
80
Denis Vlasenko8acf5212007-04-15 11:48:27 +000081 if (lstat(path, &st) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000082 bb_perror_msg("stat %s", path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000083 else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
84 list_attributes(path);
85 if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
Denis Vlasenko8acf5212007-04-15 11:48:27 +000086 && !DOT_OR_DOTDOT(de->d_name)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000087 ) {
88 printf("\n%s:\n", path);
89 iterate_on_dir(path, lsattr_dir_proc, NULL);
Denis Vlasenko4daad902007-09-27 10:20:47 +000090 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000091 }
92 }
93
94 free(path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000095 return 0;
96}
97
98static void lsattr_args(const char *name)
99{
100 struct stat st;
101
102 if (lstat(name, &st) == -1) {
103 bb_perror_msg("stat %s", name);
104 } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
105 iterate_on_dir(name, lsattr_dir_proc, NULL);
106 } else {
107 list_attributes(name);
108 }
109}
110
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000111int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000112int lsattr_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000113{
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000114 getopt32(argv, "Radlv");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000115 argv += optind;
116
117 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000118 *--argv = (char*)".";
119 do lsattr_args(*argv++); while (*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000120
121 return EXIT_SUCCESS;
122}