lsattr.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. //config:config LSATTR
  13. //config: bool "lsattr (5.7 kb)"
  14. //config: default y
  15. //config: help
  16. //config: lsattr lists the file attributes on a second extended file system.
  17. //applet:IF_LSATTR(APPLET_NOEXEC(lsattr, lsattr, BB_DIR_BIN, BB_SUID_DROP, lsattr))
  18. /* ls is NOEXEC, so we should be too! ;) */
  19. //kbuild:lib-$(CONFIG_LSATTR) += lsattr.o e2fs_lib.o
  20. //usage:#define lsattr_trivial_usage
  21. //usage: "[-Radlpv] [FILE]..."
  22. //usage:#define lsattr_full_usage "\n\n"
  23. //usage: "List ext2 file attributes\n"
  24. //usage: "\n -R Recurse"
  25. //usage: "\n -a Include names starting with ."
  26. //usage: "\n -d List directory names, not contents"
  27. // -a,-d text should match ls --help
  28. //usage: "\n -l List long flag names"
  29. //usage: "\n -p List project ID"
  30. //usage: "\n -v List version/generation number"
  31. #include "libbb.h"
  32. #include "e2fs_lib.h"
  33. enum {
  34. OPT_RECUR = 1 << 0,
  35. OPT_ALL = 1 << 1,
  36. OPT_DIRS_OPT = 1 << 2,
  37. OPT_PF_LONG = 1 << 3,
  38. OPT_GENERATION = 1 << 4,
  39. OPT_PROJID = 1 << 5,
  40. };
  41. static void list_attributes(const char *name)
  42. {
  43. unsigned fsflags;
  44. int fd, r;
  45. /* There is no way to run needed ioctls on a symlink.
  46. * open(O_PATH | O_NOFOLLOW) _can_ be used to get a fd referring to the symlink,
  47. * but ioctls fail on such a fd (tried on 4.12.0 kernel).
  48. * e2fsprogs-1.46.2 uses open(O_NOFOLLOW), it fails on symlinks.
  49. */
  50. fd = open_or_warn(name, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOFOLLOW);
  51. if (fd < 0)
  52. return;
  53. if (option_mask32 & OPT_PROJID) {
  54. struct ext2_fsxattr fsxattr;
  55. r = ioctl(fd, EXT2_IOC_FSGETXATTR, &fsxattr);
  56. /* note: ^^^ may fail in 32-bit userspace on 64-bit kernel (seen on 4.12.0) */
  57. if (r != 0)
  58. goto read_err;
  59. printf("%5u ", (unsigned)fsxattr.fsx_projid);
  60. }
  61. if (option_mask32 & OPT_GENERATION) {
  62. unsigned generation;
  63. r = ioctl(fd, EXT2_IOC_GETVERSION, &generation);
  64. if (r != 0)
  65. goto read_err;
  66. printf("%-10u ", generation);
  67. }
  68. r = ioctl(fd, EXT2_IOC_GETFLAGS, &fsflags);
  69. if (r != 0)
  70. goto read_err;
  71. close(fd);
  72. if (option_mask32 & OPT_PF_LONG) {
  73. printf("%-28s ", name);
  74. print_e2flags_long(fsflags);
  75. bb_putchar('\n');
  76. } else {
  77. print_e2flags(fsflags);
  78. printf(" %s\n", name);
  79. }
  80. return;
  81. read_err:
  82. bb_perror_msg("reading %s", name);
  83. close(fd);
  84. }
  85. static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
  86. struct dirent *de,
  87. void *private UNUSED_PARAM)
  88. {
  89. struct stat st;
  90. char *path;
  91. path = concat_path_file(dir_name, de->d_name);
  92. if (lstat(path, &st) != 0)
  93. bb_perror_msg("can't stat '%s'", path);
  94. else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
  95. /* Don't try to open device files, fifos etc */
  96. if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode) || S_ISDIR(st.st_mode))
  97. list_attributes(path);
  98. if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
  99. && !DOT_OR_DOTDOT(de->d_name)
  100. ) {
  101. printf("\n%s:\n", path);
  102. iterate_on_dir(path, lsattr_dir_proc, NULL);
  103. bb_putchar('\n');
  104. }
  105. }
  106. free(path);
  107. return 0;
  108. }
  109. static void lsattr_args(const char *name)
  110. {
  111. struct stat st;
  112. if (lstat(name, &st) == -1) {
  113. bb_perror_msg("can't 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. int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  121. int lsattr_main(int argc UNUSED_PARAM, char **argv)
  122. {
  123. getopt32(argv, "Radlvp");
  124. argv += optind;
  125. if (!*argv)
  126. *--argv = (char*)".";
  127. do lsattr_args(*argv++); while (*argv);
  128. return EXIT_SUCCESS;
  129. }