blob: 3e8d95687278dff83f2bff88398dd8ed28023ddc [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * See README for additional information
4 *
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00006 */
7
8#include "libbb.h"
9#include "e2fs_lib.h"
10
11#define HAVE_EXT2_IOCTLS 1
12
13#if INT_MAX == LONG_MAX
Denis Vlasenko8acf5212007-04-15 11:48:27 +000014#define IF_LONG_IS_SAME(...) __VA_ARGS__
15#define IF_LONG_IS_WIDER(...)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000016#else
Denis Vlasenko8acf5212007-04-15 11:48:27 +000017#define IF_LONG_IS_SAME(...)
18#define IF_LONG_IS_WIDER(...) __VA_ARGS__
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000019#endif
20
21static void close_silently(int fd)
22{
23 int e = errno;
24 close(fd);
25 errno = e;
26}
27
28
29/* Iterate a function on each entry of a directory */
30int iterate_on_dir(const char *dir_name,
31 int (*func)(const char *, struct dirent *, void *),
32 void * private)
33{
34 DIR *dir;
35 struct dirent *de, *dep;
36 int max_len, len;
37
38 max_len = PATH_MAX + sizeof(struct dirent);
39 de = xmalloc(max_len+1);
40 memset(de, 0, max_len+1);
41
42 dir = opendir(dir_name);
43 if (dir == NULL) {
44 free(de);
45 return -1;
46 }
47 while ((dep = readdir(dir))) {
48 len = sizeof(struct dirent);
49 if (len < dep->d_reclen)
50 len = dep->d_reclen;
51 if (len > max_len)
52 len = max_len;
53 memcpy(de, dep, len);
54 func(dir_name, de, private);
55 }
56 closedir(dir);
57 free(de);
58 return 0;
59}
60
61
62/* Get/set a file version on an ext2 file system */
63int fgetsetversion(const char *name, unsigned long *get_version, unsigned long set_version)
64{
65#if HAVE_EXT2_IOCTLS
66 int fd, r;
67 IF_LONG_IS_WIDER(int ver;)
68
69 fd = open(name, O_NONBLOCK);
70 if (fd == -1)
71 return -1;
72 if (!get_version) {
73 IF_LONG_IS_WIDER(
74 ver = (int) set_version;
75 r = ioctl(fd, EXT2_IOC_SETVERSION, &ver);
76 )
77 IF_LONG_IS_SAME(
78 r = ioctl(fd, EXT2_IOC_SETVERSION, (void*)&set_version);
79 )
80 } else {
81 IF_LONG_IS_WIDER(
82 r = ioctl(fd, EXT2_IOC_GETVERSION, &ver);
83 *get_version = ver;
84 )
85 IF_LONG_IS_SAME(
86 r = ioctl(fd, EXT2_IOC_GETVERSION, (void*)get_version);
87 )
88 }
89 close_silently(fd);
90 return r;
91#else /* ! HAVE_EXT2_IOCTLS */
92 errno = EOPNOTSUPP;
93 return -1;
94#endif /* ! HAVE_EXT2_IOCTLS */
95}
96
97
98/* Get/set a file flags on an ext2 file system */
99int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_flags)
100{
101#if HAVE_EXT2_IOCTLS
102 struct stat buf;
103 int fd, r;
104 IF_LONG_IS_WIDER(int f;)
105
106 if (stat(name, &buf) == 0 /* stat is ok */
107 && !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)
108 ) {
109 goto notsupp;
110 }
111 fd = open(name, O_NONBLOCK); /* neither read nor write asked for */
112 if (fd == -1)
113 return -1;
114
115 if (!get_flags) {
116 IF_LONG_IS_WIDER(
117 f = (int) set_flags;
118 r = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
119 )
120 IF_LONG_IS_SAME(
121 r = ioctl(fd, EXT2_IOC_SETFLAGS, (void*)&set_flags);
122 )
123 } else {
124 IF_LONG_IS_WIDER(
125 r = ioctl(fd, EXT2_IOC_GETFLAGS, &f);
126 *get_flags = f;
127 )
128 IF_LONG_IS_SAME(
129 r = ioctl(fd, EXT2_IOC_GETFLAGS, (void*)get_flags);
130 )
131 }
132
133 close_silently(fd);
134 return r;
135 notsupp:
136#endif /* HAVE_EXT2_IOCTLS */
137 errno = EOPNOTSUPP;
138 return -1;
139}
140
141
142/* Print file attributes on an ext2 file system */
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000143const uint32_t e2attr_flags_value[] = {
144#ifdef ENABLE_COMPRESSION
145 EXT2_COMPRBLK_FL,
146 EXT2_DIRTY_FL,
147 EXT2_NOCOMPR_FL,
148 EXT2_ECOMPR_FL,
149#endif
150 EXT2_INDEX_FL,
151 EXT2_SECRM_FL,
152 EXT2_UNRM_FL,
153 EXT2_SYNC_FL,
154 EXT2_DIRSYNC_FL,
155 EXT2_IMMUTABLE_FL,
156 EXT2_APPEND_FL,
157 EXT2_NODUMP_FL,
158 EXT2_NOATIME_FL,
159 EXT2_COMPR_FL,
160 EXT3_JOURNAL_DATA_FL,
161 EXT2_NOTAIL_FL,
162 EXT2_TOPDIR_FL
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000163};
164
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000165const char e2attr_flags_sname[] =
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000166#ifdef ENABLE_COMPRESSION
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000167 "BZXE"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000168#endif
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000169 "I"
170 "suSDiadAcjtT";
171
172static const char e2attr_flags_lname[] =
173#ifdef ENABLE_COMPRESSION
174 "Compressed_File" "\0"
175 "Compressed_Dirty_File" "\0"
176 "Compression_Raw_Access" "\0"
177 "Compression_Error" "\0"
178#endif
179 "Indexed_directory" "\0"
180 "Secure_Deletion" "\0"
181 "Undelete" "\0"
182 "Synchronous_Updates" "\0"
183 "Synchronous_Directory_Updates" "\0"
184 "Immutable" "\0"
185 "Append_Only" "\0"
186 "No_Dump" "\0"
187 "No_Atime" "\0"
188 "Compression_Requested" "\0"
189 "Journaled_Data" "\0"
190 "No_Tailmerging" "\0"
191 "Top_of_Directory_Hierarchies" "\0"
192 /* Another trailing NUL is added by compiler */;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000193
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000194void print_e2flags(FILE *f, unsigned long flags, unsigned options)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000195{
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000196 const uint32_t *fv;
197 const char *fn;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000198
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000199 fv = e2attr_flags_value;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000200 if (options & PFOPT_LONG) {
201 int first = 1;
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000202 fn = e2attr_flags_lname;
203 do {
204 if (flags & *fv) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000205 if (!first)
206 fputs(", ", f);
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000207 fputs(fn, f);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000208 first = 0;
209 }
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000210 fv++;
211 fn += strlen(fn) + 1;
212 } while (*fn);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000213 if (first)
214 fputs("---", f);
215 } else {
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000216 fn = e2attr_flags_sname;
217 do {
Denis Vlasenko407b8c02006-12-26 03:02:25 +0000218 char c = '-';
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000219 if (flags & *fv)
220 c = *fn;
Denis Vlasenko407b8c02006-12-26 03:02:25 +0000221 fputc(c, f);
Denis Vlasenkod059ddc2007-10-30 19:36:07 +0000222 fv++;
223 fn++;
224 } while (*fn);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000225 }
226}