3
0

fatattr.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Display or change file attributes on a fat file system
  4. *
  5. * Copyright 2005 H. Peter Anvin
  6. * Busybox'ed (2014) by Pascal Bellard <pascal.bellard@ads-lu.com>
  7. *
  8. * This file can be redistributed under the terms of the GNU General
  9. * Public License
  10. */
  11. //config:config FATATTR
  12. //config: bool "fatattr (2.2 kb)"
  13. //config: default y
  14. //config: help
  15. //config: fatattr lists or changes the file attributes on a fat file system.
  16. //applet:IF_FATATTR(APPLET_NOEXEC(fatattr, fatattr, BB_DIR_BIN, BB_SUID_DROP, fatattr))
  17. //kbuild:lib-$(CONFIG_FATATTR) += fatattr.o
  18. //usage:#define fatattr_trivial_usage
  19. //usage: "[-+rhsvda] FILE..."
  20. //usage:#define fatattr_full_usage "\n\n"
  21. //usage: "Change file attributes on FAT filesystem\n"
  22. //usage: "\n - Clear attributes"
  23. //usage: "\n + Set attributes"
  24. //usage: "\n r Read only"
  25. //usage: "\n h Hidden"
  26. //usage: "\n s System"
  27. //usage: "\n v Volume label"
  28. //usage: "\n d Directory"
  29. //usage: "\n a Archive"
  30. #include "libbb.h"
  31. /* linux/msdos_fs.h says: */
  32. #ifndef FAT_IOCTL_GET_ATTRIBUTES
  33. # define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, uint32_t)
  34. # define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, uint32_t)
  35. #endif
  36. /* Currently supports only the FAT flags, not the NTFS ones.
  37. * Extra space at the end is a hack to print space separator in file listing.
  38. * Let's hope no one ever passes space as an option char :)
  39. */
  40. static const char bit_to_char[] ALIGN1 = "rhsvda67 ";
  41. static inline unsigned long get_flag(char c)
  42. {
  43. const char *fp = strchr(bit_to_char, c);
  44. if (!fp)
  45. bb_error_msg_and_die("invalid character '%c'", c);
  46. return 1 << (fp - bit_to_char);
  47. }
  48. static unsigned decode_arg(const char *arg)
  49. {
  50. unsigned fl = 0;
  51. while (*++arg)
  52. fl |= get_flag(*arg);
  53. return fl;
  54. }
  55. int fatattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  56. int fatattr_main(int argc UNUSED_PARAM, char **argv)
  57. {
  58. unsigned set_mask = 0;
  59. unsigned clear_mask = 0;
  60. for (;;) {
  61. unsigned fl;
  62. char *arg = *++argv;
  63. if (!arg)
  64. bb_show_usage();
  65. if (arg[0] != '-' && arg[0] != '+')
  66. break;
  67. fl = decode_arg(arg);
  68. if (arg[0] == '+')
  69. set_mask |= fl;
  70. else
  71. clear_mask |= fl;
  72. }
  73. do {
  74. int fd, i;
  75. uint32_t attr;
  76. fd = xopen(*argv, O_RDONLY);
  77. xioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attr);
  78. attr = (attr | set_mask) & ~clear_mask;
  79. if (set_mask | clear_mask)
  80. xioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
  81. else {
  82. for (i = 0; bit_to_char[i]; i++) {
  83. bb_putchar((attr & 1) ? bit_to_char[i] : ' ');
  84. attr >>= 1;
  85. }
  86. puts(*argv);
  87. }
  88. close(fd);
  89. } while (*++argv);
  90. return EXIT_SUCCESS;
  91. }