blob: 56c1187c172ee6834c64f749a51e28ef44a8cab5 [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "lsattr (5 kb)"
Denys Vlasenko000eda42015-10-18 22:40:23 +020014//config: default y
15//config: select PLATFORM_LINUX
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: lsattr lists the file attributes on a second extended file system.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000018
Denys Vlasenko99125c02017-08-05 20:38:04 +020019//applet:IF_LSATTR(APPLET_NOEXEC(lsattr, lsattr, BB_DIR_BIN, BB_SUID_DROP, lsattr))
20/* ls is NOEXEC, so we should be too! ;) */
Denys Vlasenko000eda42015-10-18 22:40:23 +020021
22//kbuild:lib-$(CONFIG_LSATTR) += lsattr.o e2fs_lib.o
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000023
Pere Orga6a3e01d2011-04-01 22:56:30 +020024//usage:#define lsattr_trivial_usage
25//usage: "[-Radlv] [FILE]..."
26//usage:#define lsattr_full_usage "\n\n"
Denys Vlasenko36647ab2015-10-19 01:29:20 +020027//usage: "List ext2 file attributes\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020028//usage: "\n -R Recurse"
29//usage: "\n -a Don't hide entries starting with ."
30//usage: "\n -d List directory entries instead of contents"
31//usage: "\n -l List long flag names"
Denys Vlasenko36647ab2015-10-19 01:29:20 +020032//usage: "\n -v List version/generation number"
Pere Orga6a3e01d2011-04-01 22:56:30 +020033
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000035#include "e2fs_lib.h"
36
37enum {
38 OPT_RECUR = 0x1,
39 OPT_ALL = 0x2,
40 OPT_DIRS_OPT = 0x4,
41 OPT_PF_LONG = 0x8,
42 OPT_GENERATION = 0x10,
43};
44
45static void list_attributes(const char *name)
46{
47 unsigned long fsflags;
48 unsigned long generation;
49
Denis Vlasenko8acf5212007-04-15 11:48:27 +000050 if (fgetflags(name, &fsflags) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000051 goto read_err;
52
53 if (option_mask32 & OPT_GENERATION) {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000054 if (fgetversion(name, &generation) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000055 goto read_err;
56 printf("%5lu ", generation);
57 }
58
59 if (option_mask32 & OPT_PF_LONG) {
60 printf("%-28s ", name);
Denis Vlasenko53354ac2008-06-07 15:10:29 +000061 print_e2flags(stdout, fsflags, PFOPT_LONG);
Denis Vlasenko4daad902007-09-27 10:20:47 +000062 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000063 } else {
Denis Vlasenko53354ac2008-06-07 15:10:29 +000064 print_e2flags(stdout, fsflags, 0);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000065 printf(" %s\n", name);
66 }
67
68 return;
69 read_err:
70 bb_perror_msg("reading %s", name);
71}
72
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020073static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
74 struct dirent *de,
75 void *private UNUSED_PARAM)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000076{
77 struct stat st;
78 char *path;
79
80 path = concat_path_file(dir_name, de->d_name);
81
Denis Vlasenko8acf5212007-04-15 11:48:27 +000082 if (lstat(path, &st) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000083 bb_perror_msg("stat %s", path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000084 else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
85 list_attributes(path);
86 if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
Denis Vlasenko8acf5212007-04-15 11:48:27 +000087 && !DOT_OR_DOTDOT(de->d_name)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000088 ) {
89 printf("\n%s:\n", path);
90 iterate_on_dir(path, lsattr_dir_proc, NULL);
Denis Vlasenko4daad902007-09-27 10:20:47 +000091 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000092 }
93 }
94
95 free(path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000096 return 0;
97}
98
99static void lsattr_args(const char *name)
100{
101 struct stat st;
102
103 if (lstat(name, &st) == -1) {
104 bb_perror_msg("stat %s", name);
105 } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
106 iterate_on_dir(name, lsattr_dir_proc, NULL);
107 } else {
108 list_attributes(name);
109 }
110}
111
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000112int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000113int lsattr_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000114{
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000115 getopt32(argv, "Radlv");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000116 argv += optind;
117
118 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000119 *--argv = (char*)".";
120 do lsattr_args(*argv++); while (*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000121
122 return EXIT_SUCCESS;
123}