Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1 | /* 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 | */ |
| 12 | |
| 13 | /* |
| 14 | * History: |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 15 | * 93/10/30 - Creation |
| 16 | * 93/11/13 - Replace stat() calls by lstat() to avoid loops |
| 17 | * 94/02/27 - Integrated in Ted's distribution |
| 18 | * 98/12/29 - Display version info only when -V specified (G M Sipe) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <getopt.h> |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/param.h> |
| 31 | #include <sys/stat.h> |
| 32 | |
| 33 | #include "ext2fs/ext2_fs.h" |
| 34 | #include "e2fsbb.h" |
| 35 | #include "e2p/e2p.h" |
| 36 | |
| 37 | #define OPT_RECUR 1 |
| 38 | #define OPT_ALL 2 |
| 39 | #define OPT_DIRS_OPT 4 |
| 40 | #define OPT_PF_LONG 8 |
| 41 | #define OPT_GENERATION 16 |
| 42 | static int flags; |
| 43 | |
| 44 | static void list_attributes(const char *name) |
| 45 | { |
| 46 | unsigned long fsflags; |
| 47 | unsigned long generation; |
| 48 | |
| 49 | if (fgetflags(name, &fsflags) == -1) |
| 50 | goto read_err; |
| 51 | if (flags & OPT_GENERATION) { |
| 52 | if (fgetversion(name, &generation) == -1) |
| 53 | goto read_err; |
| 54 | printf("%5lu ", generation); |
| 55 | } |
| 56 | |
| 57 | if (flags & OPT_PF_LONG) { |
| 58 | printf("%-28s ", name); |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 59 | print_e2flags(stdout, fsflags, PFOPT_LONG); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 60 | bb_putchar('\n'); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 61 | } else { |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 62 | print_e2flags(stdout, fsflags, 0); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 63 | printf(" %s\n", name); |
| 64 | } |
| 65 | |
| 66 | return; |
| 67 | read_err: |
| 68 | bb_perror_msg("reading %s", name); |
| 69 | } |
| 70 | |
| 71 | static int lsattr_dir_proc(const char *, struct dirent *, void *); |
| 72 | |
| 73 | static void lsattr_args(const char *name) |
| 74 | { |
| 75 | struct stat st; |
| 76 | |
| 77 | if (lstat(name, &st) == -1) { |
| 78 | bb_perror_msg("stating %s", name); |
| 79 | } else { |
| 80 | if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT)) |
| 81 | iterate_on_dir(name, lsattr_dir_proc, NULL); |
| 82 | else |
| 83 | list_attributes(name); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static int lsattr_dir_proc(const char *dir_name, struct dirent *de, |
| 88 | void *private) |
| 89 | { |
| 90 | struct stat st; |
| 91 | char *path; |
| 92 | |
| 93 | path = concat_path_file(dir_name, de->d_name); |
| 94 | |
| 95 | if (lstat(path, &st) == -1) |
Denys Vlasenko | 9371043 | 2012-09-27 15:35:10 +0200 | [diff] [blame] | 96 | bb_simple_perror_msg(path); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 97 | else { |
| 98 | if (de->d_name[0] != '.' || (flags & OPT_ALL)) { |
| 99 | list_attributes(path); |
| 100 | if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) && |
| 101 | (de->d_name[0] != '.' && (de->d_name[1] != '\0' || |
| 102 | (de->d_name[1] != '.' && de->d_name[2] != '\0')))) { |
| 103 | printf("\n%s:\n", path); |
| 104 | iterate_on_dir(path, lsattr_dir_proc, NULL); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 105 | bb_putchar('\n'); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | free(path); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 115 | int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 116 | int lsattr_main(int argc, char **argv) |
| 117 | { |
| 118 | int i; |
| 119 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 120 | flags = getopt32(argv, "Radlv"); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 121 | |
| 122 | if (optind > argc - 1) |
| 123 | lsattr_args("."); |
| 124 | else |
| 125 | for (i = optind; i < argc; i++) |
| 126 | lsattr_args(argv[i]); |
| 127 | |
| 128 | return EXIT_SUCCESS; |
| 129 | } |