fatattr.c 2.6 KB

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