blob: c9f353ce5f1e5f7555bbea8ef51937706b840bd1 [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{
Denys Vlasenko96436fb2021-06-23 12:45:51 +020049 unsigned fsflags;
50 int fd, r;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000051
Denys Vlasenko9468ea02021-06-24 01:07:56 +020052 /* There is no way to run needed ioctls on a symlink.
53 * open(O_PATH | O_NOFOLLOW) _can_ be used to get a fd referring to the symlink,
54 * but ioctls fail on such a fd (tried on 4.12.0 kernel).
55 * e2fsprogs-1.46.2 uses open(O_NOFOLLOW), it fails on symlinks.
56 */
57 fd = open_or_warn(name, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOFOLLOW);
58 if (fd < 0)
Denys Vlasenko96436fb2021-06-23 12:45:51 +020059 return;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000060
Denys Vlasenko526b8342021-06-20 10:57:24 +020061 if (option_mask32 & OPT_PROJID) {
Denys Vlasenko96436fb2021-06-23 12:45:51 +020062 struct ext2_fsxattr fsxattr;
63 r = ioctl(fd, EXT2_IOC_FSGETXATTR, &fsxattr);
Denys Vlasenko85a5bc92021-06-23 15:33:22 +020064 /* note: ^^^ may fail in 32-bit userspace on 64-bit kernel (seen on 4.12.0) */
Denys Vlasenko96436fb2021-06-23 12:45:51 +020065 if (r != 0)
Denys Vlasenko526b8342021-06-20 10:57:24 +020066 goto read_err;
Denys Vlasenko96436fb2021-06-23 12:45:51 +020067 printf("%5u ", (unsigned)fsxattr.fsx_projid);
Denys Vlasenko526b8342021-06-20 10:57:24 +020068 }
69
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000070 if (option_mask32 & OPT_GENERATION) {
Denys Vlasenko96436fb2021-06-23 12:45:51 +020071 unsigned generation;
72 r = ioctl(fd, EXT2_IOC_GETVERSION, &generation);
73 if (r != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000074 goto read_err;
Denys Vlasenko96436fb2021-06-23 12:45:51 +020075 printf("%-10u ", generation);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000076 }
77
Denys Vlasenko96436fb2021-06-23 12:45:51 +020078 r = ioctl(fd, EXT2_IOC_GETFLAGS, &fsflags);
79 if (r != 0)
80 goto read_err;
81
82 close(fd);
83
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000084 if (option_mask32 & OPT_PF_LONG) {
85 printf("%-28s ", name);
Denys Vlasenko87c40cf2021-06-24 09:31:57 +020086 print_e2flags_long(fsflags);
Denis Vlasenko4daad902007-09-27 10:20:47 +000087 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000088 } else {
Denys Vlasenko87c40cf2021-06-24 09:31:57 +020089 print_e2flags(fsflags);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000090 printf(" %s\n", name);
91 }
92
93 return;
94 read_err:
95 bb_perror_msg("reading %s", name);
Denys Vlasenko96436fb2021-06-23 12:45:51 +020096 close(fd);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000097}
98
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020099static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
100 struct dirent *de,
101 void *private UNUSED_PARAM)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000102{
103 struct stat st;
104 char *path;
105
106 path = concat_path_file(dir_name, de->d_name);
107
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000108 if (lstat(path, &st) != 0)
Denys Vlasenko85a5bc92021-06-23 15:33:22 +0200109 bb_perror_msg("can't stat '%s'", path);
Denys Vlasenko9468ea02021-06-24 01:07:56 +0200110
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000111 else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
Denys Vlasenko9468ea02021-06-24 01:07:56 +0200112 /* Don't try to open device files, fifos etc */
113 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode) || S_ISDIR(st.st_mode))
114 list_attributes(path);
115
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000116 if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
Denis Vlasenko8acf5212007-04-15 11:48:27 +0000117 && !DOT_OR_DOTDOT(de->d_name)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000118 ) {
119 printf("\n%s:\n", path);
120 iterate_on_dir(path, lsattr_dir_proc, NULL);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000121 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000122 }
123 }
124
125 free(path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000126 return 0;
127}
128
129static void lsattr_args(const char *name)
130{
131 struct stat st;
132
133 if (lstat(name, &st) == -1) {
Denys Vlasenko85a5bc92021-06-23 15:33:22 +0200134 bb_perror_msg("can't stat '%s'", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000135 } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
136 iterate_on_dir(name, lsattr_dir_proc, NULL);
137 } else {
138 list_attributes(name);
139 }
140}
141
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000142int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000143int lsattr_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000144{
Denys Vlasenko526b8342021-06-20 10:57:24 +0200145 getopt32(argv, "Radlvp");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000146 argv += optind;
147
148 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000149 *--argv = (char*)".";
150 do lsattr_args(*argv++); while (*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000151
152 return EXIT_SUCCESS;
153}