blob: 9b035f584ebf93e9762bc5504dd947a2b6e16773 [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 Vlasenkob097a842018-12-28 03:20:17 +010013//config: bool "lsattr (5.5 kb)"
Denys Vlasenko000eda42015-10-18 22:40:23 +020014//config: default y
Denys Vlasenko000eda42015-10-18 22:40:23 +020015//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: lsattr lists the file attributes on a second extended file system.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000017
Denys Vlasenko99125c02017-08-05 20:38:04 +020018//applet:IF_LSATTR(APPLET_NOEXEC(lsattr, lsattr, BB_DIR_BIN, BB_SUID_DROP, lsattr))
19/* ls is NOEXEC, so we should be too! ;) */
Denys Vlasenko000eda42015-10-18 22:40:23 +020020
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
Denys Vlasenko526b8342021-06-20 10:57:24 +020024//usage: "[-Radlpv] [FILE]..."
Pere Orga6a3e01d2011-04-01 22:56:30 +020025//usage:#define lsattr_full_usage "\n\n"
Denys Vlasenko36647ab2015-10-19 01:29:20 +020026//usage: "List ext2 file attributes\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020027//usage: "\n -R Recurse"
Denys Vlasenko526b8342021-06-20 10:57:24 +020028//usage: "\n -a Include names starting with ."
29//usage: "\n -d List directory names, not contents"
30// -a,-d text should match ls --help
Pere Orga6a3e01d2011-04-01 22:56:30 +020031//usage: "\n -l List long flag names"
Denys Vlasenko526b8342021-06-20 10:57:24 +020032//usage: "\n -p List project ID"
Denys Vlasenko36647ab2015-10-19 01:29:20 +020033//usage: "\n -v List version/generation number"
Pere Orga6a3e01d2011-04-01 22:56:30 +020034
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000036#include "e2fs_lib.h"
37
38enum {
Denys Vlasenko526b8342021-06-20 10:57:24 +020039 OPT_RECUR = 1 << 0,
40 OPT_ALL = 1 << 1,
41 OPT_DIRS_OPT = 1 << 2,
42 OPT_PF_LONG = 1 << 3,
43 OPT_GENERATION = 1 << 4,
44 OPT_PROJID = 1 << 5,
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000045};
46
47static void list_attributes(const char *name)
48{
49 unsigned long fsflags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000050
Denis Vlasenko8acf5212007-04-15 11:48:27 +000051 if (fgetflags(name, &fsflags) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000052 goto read_err;
53
Denys Vlasenko526b8342021-06-20 10:57:24 +020054 if (option_mask32 & OPT_PROJID) {
55 uint32_t p;
56 if (fgetprojid(name, &p) != 0)
57 goto read_err;
58 printf("%5lu ", (unsigned long)p);
59 }
60
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000061 if (option_mask32 & OPT_GENERATION) {
Denys Vlasenko526b8342021-06-20 10:57:24 +020062 unsigned long generation;
Denis Vlasenko8acf5212007-04-15 11:48:27 +000063 if (fgetversion(name, &generation) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000064 goto read_err;
Denys Vlasenko526b8342021-06-20 10:57:24 +020065 printf("%-10lu ", generation);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000066 }
67
68 if (option_mask32 & OPT_PF_LONG) {
69 printf("%-28s ", name);
Denis Vlasenko53354ac2008-06-07 15:10:29 +000070 print_e2flags(stdout, fsflags, PFOPT_LONG);
Denis Vlasenko4daad902007-09-27 10:20:47 +000071 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000072 } else {
Denis Vlasenko53354ac2008-06-07 15:10:29 +000073 print_e2flags(stdout, fsflags, 0);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000074 printf(" %s\n", name);
75 }
76
77 return;
78 read_err:
79 bb_perror_msg("reading %s", name);
80}
81
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020082static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
83 struct dirent *de,
84 void *private UNUSED_PARAM)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000085{
86 struct stat st;
87 char *path;
88
89 path = concat_path_file(dir_name, de->d_name);
90
Denis Vlasenko8acf5212007-04-15 11:48:27 +000091 if (lstat(path, &st) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000092 bb_perror_msg("stat %s", path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000093 else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
94 list_attributes(path);
95 if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
Denis Vlasenko8acf5212007-04-15 11:48:27 +000096 && !DOT_OR_DOTDOT(de->d_name)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000097 ) {
98 printf("\n%s:\n", path);
99 iterate_on_dir(path, lsattr_dir_proc, NULL);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000100 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000101 }
102 }
103
104 free(path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000105 return 0;
106}
107
108static void lsattr_args(const char *name)
109{
110 struct stat st;
111
112 if (lstat(name, &st) == -1) {
113 bb_perror_msg("stat %s", name);
114 } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
115 iterate_on_dir(name, lsattr_dir_proc, NULL);
116 } else {
117 list_attributes(name);
118 }
119}
120
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000121int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000122int lsattr_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000123{
Denys Vlasenko526b8342021-06-20 10:57:24 +0200124 getopt32(argv, "Radlvp");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000125 argv += optind;
126
127 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000128 *--argv = (char*)".";
129 do lsattr_args(*argv++); while (*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000130
131 return EXIT_SUCCESS;
132}