aboutsummaryrefslogtreecommitdiff
path: root/e2fsprogs/e2fs_lib.c
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-06-28 07:46:32 +0100
committerRon Yorston <rmy@pobox.com>2021-06-28 07:46:32 +0100
commite1ad66c0b8fd58a7158d40771175a7dab224202d (patch)
tree959d687eee9637151ad5798322586174de331141 /e2fsprogs/e2fs_lib.c
parent0fdf99bee07b6c38795eb5415b5e337ab82cfba8 (diff)
parent5dbbd0a6f52befe6bc57baf97d39168e595197f1 (diff)
downloadbusybox-w32-e1ad66c0b8fd58a7158d40771175a7dab224202d.tar.gz
busybox-w32-e1ad66c0b8fd58a7158d40771175a7dab224202d.tar.bz2
busybox-w32-e1ad66c0b8fd58a7158d40771175a7dab224202d.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'e2fsprogs/e2fs_lib.c')
-rw-r--r--e2fsprogs/e2fs_lib.c209
1 files changed, 53 insertions, 156 deletions
diff --git a/e2fsprogs/e2fs_lib.c b/e2fsprogs/e2fs_lib.c
index d0cacf14c..48c3b68b5 100644
--- a/e2fsprogs/e2fs_lib.c
+++ b/e2fsprogs/e2fs_lib.c
@@ -8,125 +8,7 @@
8#include "libbb.h" 8#include "libbb.h"
9#include "e2fs_lib.h" 9#include "e2fs_lib.h"
10 10
11#if !ENABLE_PLATFORM_MINGW32 11#if ENABLE_PLATFORM_MINGW32
12#define HAVE_EXT2_IOCTLS 1
13
14#if INT_MAX == LONG_MAX
15#define IF_LONG_IS_SAME(...) __VA_ARGS__
16#define IF_LONG_IS_WIDER(...)
17#else
18#define IF_LONG_IS_SAME(...)
19#define IF_LONG_IS_WIDER(...) __VA_ARGS__
20#endif
21
22static void close_silently(int fd)
23{
24 int e = errno;
25 close(fd);
26 errno = e;
27}
28#endif
29
30
31/* Iterate a function on each entry of a directory */
32int iterate_on_dir(const char *dir_name,
33 int FAST_FUNC (*func)(const char *, struct dirent *, void *),
34 void *private)
35{
36 DIR *dir;
37 struct dirent *de;
38
39 dir = opendir(dir_name);
40 if (dir == NULL) {
41 return -1;
42 }
43 while ((de = readdir(dir)) != NULL) {
44 func(dir_name, de, private);
45 }
46 closedir(dir);
47 return 0;
48}
49
50
51#if !ENABLE_PLATFORM_MINGW32
52/* Get/set a file version on an ext2 file system */
53int fgetsetversion(const char *name, unsigned long *get_version, unsigned long set_version)
54{
55#if HAVE_EXT2_IOCTLS
56 int fd, r;
57 IF_LONG_IS_WIDER(int ver;)
58
59 fd = open(name, O_RDONLY | O_NONBLOCK);
60 if (fd == -1)
61 return -1;
62 if (!get_version) {
63 IF_LONG_IS_WIDER(
64 ver = (int) set_version;
65 r = ioctl(fd, EXT2_IOC_SETVERSION, &ver);
66 )
67 IF_LONG_IS_SAME(
68 r = ioctl(fd, EXT2_IOC_SETVERSION, (void*)&set_version);
69 )
70 } else {
71 IF_LONG_IS_WIDER(
72 r = ioctl(fd, EXT2_IOC_GETVERSION, &ver);
73 *get_version = ver;
74 )
75 IF_LONG_IS_SAME(
76 r = ioctl(fd, EXT2_IOC_GETVERSION, (void*)get_version);
77 )
78 }
79 close_silently(fd);
80 return r;
81#else /* ! HAVE_EXT2_IOCTLS */
82 errno = EOPNOTSUPP;
83 return -1;
84#endif /* ! HAVE_EXT2_IOCTLS */
85}
86
87/* Get/set a file flags on an ext2 file system */
88int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_flags)
89{
90#if HAVE_EXT2_IOCTLS
91 struct stat buf;
92 int fd, r;
93 IF_LONG_IS_WIDER(int f;)
94
95 if (stat(name, &buf) == 0 /* stat is ok */
96 && !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)
97 ) {
98 goto notsupp;
99 }
100 fd = open(name, O_RDONLY | O_NONBLOCK); /* neither read nor write asked for */
101 if (fd == -1)
102 return -1;
103
104 if (!get_flags) {
105 IF_LONG_IS_WIDER(
106 f = (int) set_flags;
107 r = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
108 )
109 IF_LONG_IS_SAME(
110 r = ioctl(fd, EXT2_IOC_SETFLAGS, (void*)&set_flags);
111 )
112 } else {
113 IF_LONG_IS_WIDER(
114 r = ioctl(fd, EXT2_IOC_GETFLAGS, &f);
115 *get_flags = f;
116 )
117 IF_LONG_IS_SAME(
118 r = ioctl(fd, EXT2_IOC_GETFLAGS, (void*)get_flags);
119 )
120 }
121
122 close_silently(fd);
123 return r;
124 notsupp:
125#endif /* HAVE_EXT2_IOCTLS */
126 errno = EOPNOTSUPP;
127 return -1;
128}
129#else /* ENABLE_PLATFORM_MINGW32 */
130/* Only certain attributes can be set using SetFileAttributes() */ 12/* Only certain attributes can be set using SetFileAttributes() */
131#define CHATTR_MASK (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | \ 13#define CHATTR_MASK (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | \
132 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE | \ 14 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE | \
@@ -134,7 +16,7 @@ int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_f
134 FILE_ATTRIBUTE_OFFLINE) 16 FILE_ATTRIBUTE_OFFLINE)
135 17
136/* Get/set file attributes on a Windows file system */ 18/* Get/set file attributes on a Windows file system */
137int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_flags) 19int fgetsetflags(const char *name, unsigned *get_flags, unsigned set_flags)
138{ 20{
139 struct stat buf; 21 struct stat buf;
140 22
@@ -156,7 +38,6 @@ int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_f
156} 38}
157#endif 39#endif
158 40
159
160#if !ENABLE_PLATFORM_MINGW32 41#if !ENABLE_PLATFORM_MINGW32
161/* Print file attributes on an ext2 file system */ 42/* Print file attributes on an ext2 file system */
162const uint32_t e2attr_flags_value[] ALIGN4 = { 43const uint32_t e2attr_flags_value[] ALIGN4 = {
@@ -164,9 +45,7 @@ const uint32_t e2attr_flags_value[] ALIGN4 = {
164 EXT2_COMPRBLK_FL, 45 EXT2_COMPRBLK_FL,
165 EXT2_DIRTY_FL, 46 EXT2_DIRTY_FL,
166 EXT2_NOCOMPR_FL, 47 EXT2_NOCOMPR_FL,
167 EXT2_ECOMPR_FL,
168#endif 48#endif
169 EXT2_INDEX_FL,
170 EXT2_SECRM_FL, 49 EXT2_SECRM_FL,
171 EXT2_UNRM_FL, 50 EXT2_UNRM_FL,
172 EXT2_SYNC_FL, 51 EXT2_SYNC_FL,
@@ -176,26 +55,31 @@ const uint32_t e2attr_flags_value[] ALIGN4 = {
176 EXT2_NODUMP_FL, 55 EXT2_NODUMP_FL,
177 EXT2_NOATIME_FL, 56 EXT2_NOATIME_FL,
178 EXT2_COMPR_FL, 57 EXT2_COMPR_FL,
58 EXT2_ECOMPR_FL,
179 EXT3_JOURNAL_DATA_FL, 59 EXT3_JOURNAL_DATA_FL,
60 EXT2_INDEX_FL,
180 EXT2_NOTAIL_FL, 61 EXT2_NOTAIL_FL,
181 EXT2_TOPDIR_FL 62 EXT2_TOPDIR_FL,
63 EXT2_EXTENT_FL,
64 EXT2_NOCOW_FL,
65 EXT2_CASEFOLD_FL,
66 EXT2_INLINE_DATA_FL,
67 EXT2_PROJINHERIT_FL,
68 EXT2_VERITY_FL,
182}; 69};
183 70
184const char e2attr_flags_sname[] ALIGN1 = 71const char e2attr_flags_sname[] ALIGN1 =
185#ifdef ENABLE_COMPRESSION 72#ifdef ENABLE_COMPRESSION
186 "BZXE" 73 "BZX"
187#endif 74#endif
188 "I" 75 "suSDiadAcEjItTeCFNPV";
189 "suSDiadAcjtT";
190 76
191static const char e2attr_flags_lname[] ALIGN1 = 77static const char e2attr_flags_lname[] ALIGN1 =
192#ifdef ENABLE_COMPRESSION 78#ifdef ENABLE_COMPRESSION
193 "Compressed_File" "\0" 79 "Compressed_File" "\0"
194 "Compressed_Dirty_File" "\0" 80 "Compressed_Dirty_File" "\0"
195 "Compression_Raw_Access" "\0" 81 "Compression_Raw_Access" "\0"
196 "Compression_Error" "\0"
197#endif 82#endif
198 "Indexed_directory" "\0"
199 "Secure_Deletion" "\0" 83 "Secure_Deletion" "\0"
200 "Undelete" "\0" 84 "Undelete" "\0"
201 "Synchronous_Updates" "\0" 85 "Synchronous_Updates" "\0"
@@ -205,9 +89,17 @@ static const char e2attr_flags_lname[] ALIGN1 =
205 "No_Dump" "\0" 89 "No_Dump" "\0"
206 "No_Atime" "\0" 90 "No_Atime" "\0"
207 "Compression_Requested" "\0" 91 "Compression_Requested" "\0"
92 "Encrypted" "\0"
208 "Journaled_Data" "\0" 93 "Journaled_Data" "\0"
94 "Indexed_directory" "\0"
209 "No_Tailmerging" "\0" 95 "No_Tailmerging" "\0"
210 "Top_of_Directory_Hierarchies" "\0" 96 "Top_of_Directory_Hierarchies" "\0"
97 "Extents" "\0"
98 "No_COW" "\0"
99 "Casefold" "\0"
100 "Inline_Data" "\0"
101 "Project_Hierarchy" "\0"
102 "Verity" "\0"
211 /* Another trailing NUL is added by compiler */; 103 /* Another trailing NUL is added by compiler */;
212#else /* ENABLE_PLATFORM_MINGW32 */ 104#else /* ENABLE_PLATFORM_MINGW32 */
213/* Print file attributes on a Windows file system */ 105/* Print file attributes on a Windows file system */
@@ -243,36 +135,41 @@ static const char e2attr_flags_lname[] ALIGN1 =
243 /* Another trailing NUL is added by compiler */; 135 /* Another trailing NUL is added by compiler */;
244#endif 136#endif
245 137
246void print_e2flags(FILE *f, unsigned long flags, unsigned options) 138void print_e2flags_long(unsigned flags)
247{ 139{
248 const uint32_t *fv; 140 const uint32_t *fv;
249 const char *fn; 141 const char *fn;
142 int first = 1;
250 143
251 fv = e2attr_flags_value; 144 fv = e2attr_flags_value;
252 if (options & PFOPT_LONG) { 145 fn = e2attr_flags_lname;
253 int first = 1; 146 do {
254 fn = e2attr_flags_lname; 147 if (flags & *fv) {
255 do { 148 if (!first)
256 if (flags & *fv) { 149 fputs(", ", stdout);
257 if (!first) 150 fputs(fn, stdout);
258 fputs(", ", f); 151 first = 0;
259 fputs(fn, f); 152 }
260 first = 0; 153 fv++;
261 } 154 fn += strlen(fn) + 1;
262 fv++; 155 } while (*fn);
263 fn += strlen(fn) + 1; 156 if (first)
264 } while (*fn); 157 fputs("---", stdout);
265 if (first) 158}
266 fputs("---", f); 159
267 } else { 160void print_e2flags(unsigned flags)
268 fn = e2attr_flags_sname; 161{
269 do { 162 const uint32_t *fv;
270 char c = '-'; 163 const char *fn;
271 if (flags & *fv) 164
272 c = *fn; 165 fv = e2attr_flags_value;
273 fputc(c, f); 166 fn = e2attr_flags_sname;
274 fv++; 167 do {
275 fn++; 168 char c = '-';
276 } while (*fn); 169 if (flags & *fv)
277 } 170 c = *fn;
171 putchar(c);
172 fv++;
173 fn++;
174 } while (*fn);
278} 175}