1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include "libbb.h"
39#include <linux/fs.h>
40#include "bb_e2fs_defs.h"
41
42
43char BUG_wrong_field_size(void);
44#define STORE_LE(field, value) \
45do { \
46 if (sizeof(field) == 4) \
47 field = SWAP_LE32(value); \
48 else if (sizeof(field) == 2) \
49 field = SWAP_LE16(value); \
50 else if (sizeof(field) == 1) \
51 field = (value); \
52 else \
53 BUG_wrong_field_size(); \
54} while (0)
55
56#define FETCH_LE32(field) \
57 (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
58
59enum {
60 OPT_L = 1 << 0,
61 OPT_c = 1 << 1,
62 OPT_i = 1 << 2,
63 OPT_C = 1 << 3,
64};
65
66int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
67int tune2fs_main(int argc UNUSED_PARAM, char **argv)
68{
69 unsigned opts;
70 const char *label, *str_c, *str_i, *str_C;
71 struct ext2_super_block *sb;
72 int fd;
73
74 opts = getopt32(argv, "^" "L:c:i:C:" "\0" "=1", &label, &str_c, &str_i, &str_C);
75 if (!opts)
76 bb_show_usage();
77 argv += optind;
78
79
80 fd = xopen(argv[0], O_RDWR);
81 xlseek(fd, 1024, SEEK_SET);
82 sb = xzalloc(1024);
83 xread(fd, sb, 1024);
84
85
86
87
88 if (opts & OPT_C) {
89 int n = xatoi_range(str_C, 1, 0xfffe);
90 STORE_LE(sb->s_mnt_count, (unsigned)n);
91 }
92
93
94 if (opts & OPT_L)
95 safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
96
97 if (opts & OPT_c) {
98 int n = xatoi_range(str_c, -1, 0xfffe);
99 if (n == 0)
100 n = -1;
101 STORE_LE(sb->s_max_mnt_count, (unsigned)n);
102 }
103
104 if (opts & OPT_i) {
105 unsigned n = xatou_range(str_i, 0, (unsigned)0xffffffff / (24*60*60)) * 24*60*60;
106 STORE_LE(sb->s_checkinterval, n);
107 }
108
109
110 xlseek(fd, 1024, SEEK_SET);
111 xwrite(fd, sb, 1024);
112
113 if (ENABLE_FEATURE_CLEAN_UP) {
114 free(sb);
115 }
116
117 xclose(fd);
118 return EXIT_SUCCESS;
119}
120