tune2fs.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * tune2fs: utility to modify EXT2 filesystem
  4. *
  5. * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #include <linux/fs.h>
  11. #include <linux/ext2_fs.h>
  12. // storage helpers
  13. char BUG_wrong_field_size(void);
  14. #define STORE_LE(field, value) \
  15. do { \
  16. if (sizeof(field) == 4) \
  17. field = SWAP_LE32(value); \
  18. else if (sizeof(field) == 2) \
  19. field = SWAP_LE16(value); \
  20. else if (sizeof(field) == 1) \
  21. field = (value); \
  22. else \
  23. BUG_wrong_field_size(); \
  24. } while (0)
  25. #define FETCH_LE32(field) \
  26. (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
  27. enum {
  28. OPT_L = 1 << 0, // label
  29. };
  30. int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int tune2fs_main(int argc UNUSED_PARAM, char **argv)
  32. {
  33. unsigned opts;
  34. const char *label;
  35. struct ext2_super_block *sb;
  36. int fd;
  37. opt_complementary = "=1";
  38. opts = getopt32(argv, "L:", &label);
  39. argv += optind; // argv[0] -- device
  40. if (!opts)
  41. bb_show_usage();
  42. // read superblock
  43. fd = xopen(argv[0], O_RDWR);
  44. xlseek(fd, 1024, SEEK_SET);
  45. sb = xzalloc(1024);
  46. xread(fd, sb, 1024);
  47. // mangle superblock
  48. //STORE_LE(sb->s_wtime, time(NULL)); - why bother?
  49. // set the label
  50. if (1 /*opts & OPT_L*/)
  51. safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
  52. // write superblock
  53. xlseek(fd, 1024, SEEK_SET);
  54. xwrite(fd, sb, 1024);
  55. if (ENABLE_FEATURE_CLEAN_UP) {
  56. free(sb);
  57. }
  58. xclose(fd);
  59. return EXIT_SUCCESS;
  60. }