Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * See README for additional information |
| 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 6 | */ |
| 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 Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 14 | #define IF_LONG_IS_SAME(...) __VA_ARGS__ |
| 15 | #define IF_LONG_IS_WIDER(...) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 16 | #else |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 17 | #define IF_LONG_IS_SAME(...) |
| 18 | #define IF_LONG_IS_WIDER(...) __VA_ARGS__ |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | static 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 */ |
| 30 | int iterate_on_dir(const char *dir_name, |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 31 | int FAST_FUNC (*func)(const char *, struct dirent *, void *), |
| 32 | void *private) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 33 | { |
| 34 | DIR *dir; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 35 | struct dirent *de; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 36 | |
| 37 | dir = opendir(dir_name); |
| 38 | if (dir == NULL) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 39 | return -1; |
| 40 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 41 | while ((de = readdir(dir)) != NULL) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 42 | func(dir_name, de, private); |
| 43 | } |
| 44 | closedir(dir); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* Get/set a file version on an ext2 file system */ |
| 50 | int fgetsetversion(const char *name, unsigned long *get_version, unsigned long set_version) |
| 51 | { |
| 52 | #if HAVE_EXT2_IOCTLS |
| 53 | int fd, r; |
| 54 | IF_LONG_IS_WIDER(int ver;) |
| 55 | |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 56 | fd = open(name, O_RDONLY | O_NONBLOCK); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 57 | if (fd == -1) |
| 58 | return -1; |
| 59 | if (!get_version) { |
| 60 | IF_LONG_IS_WIDER( |
| 61 | ver = (int) set_version; |
| 62 | r = ioctl(fd, EXT2_IOC_SETVERSION, &ver); |
| 63 | ) |
| 64 | IF_LONG_IS_SAME( |
| 65 | r = ioctl(fd, EXT2_IOC_SETVERSION, (void*)&set_version); |
| 66 | ) |
| 67 | } else { |
| 68 | IF_LONG_IS_WIDER( |
| 69 | r = ioctl(fd, EXT2_IOC_GETVERSION, &ver); |
| 70 | *get_version = ver; |
| 71 | ) |
| 72 | IF_LONG_IS_SAME( |
| 73 | r = ioctl(fd, EXT2_IOC_GETVERSION, (void*)get_version); |
| 74 | ) |
| 75 | } |
| 76 | close_silently(fd); |
| 77 | return r; |
| 78 | #else /* ! HAVE_EXT2_IOCTLS */ |
| 79 | errno = EOPNOTSUPP; |
| 80 | return -1; |
| 81 | #endif /* ! HAVE_EXT2_IOCTLS */ |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* Get/set a file flags on an ext2 file system */ |
| 86 | int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_flags) |
| 87 | { |
| 88 | #if HAVE_EXT2_IOCTLS |
| 89 | struct stat buf; |
| 90 | int fd, r; |
| 91 | IF_LONG_IS_WIDER(int f;) |
| 92 | |
| 93 | if (stat(name, &buf) == 0 /* stat is ok */ |
| 94 | && !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) |
| 95 | ) { |
| 96 | goto notsupp; |
| 97 | } |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 98 | fd = open(name, O_RDONLY | O_NONBLOCK); /* neither read nor write asked for */ |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 99 | if (fd == -1) |
| 100 | return -1; |
| 101 | |
| 102 | if (!get_flags) { |
| 103 | IF_LONG_IS_WIDER( |
| 104 | f = (int) set_flags; |
| 105 | r = ioctl(fd, EXT2_IOC_SETFLAGS, &f); |
| 106 | ) |
| 107 | IF_LONG_IS_SAME( |
| 108 | r = ioctl(fd, EXT2_IOC_SETFLAGS, (void*)&set_flags); |
| 109 | ) |
| 110 | } else { |
| 111 | IF_LONG_IS_WIDER( |
| 112 | r = ioctl(fd, EXT2_IOC_GETFLAGS, &f); |
| 113 | *get_flags = f; |
| 114 | ) |
| 115 | IF_LONG_IS_SAME( |
| 116 | r = ioctl(fd, EXT2_IOC_GETFLAGS, (void*)get_flags); |
| 117 | ) |
| 118 | } |
| 119 | |
| 120 | close_silently(fd); |
| 121 | return r; |
| 122 | notsupp: |
| 123 | #endif /* HAVE_EXT2_IOCTLS */ |
| 124 | errno = EOPNOTSUPP; |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* Print file attributes on an ext2 file system */ |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 130 | const uint32_t e2attr_flags_value[] = { |
| 131 | #ifdef ENABLE_COMPRESSION |
| 132 | EXT2_COMPRBLK_FL, |
| 133 | EXT2_DIRTY_FL, |
| 134 | EXT2_NOCOMPR_FL, |
| 135 | EXT2_ECOMPR_FL, |
| 136 | #endif |
| 137 | EXT2_INDEX_FL, |
| 138 | EXT2_SECRM_FL, |
| 139 | EXT2_UNRM_FL, |
| 140 | EXT2_SYNC_FL, |
| 141 | EXT2_DIRSYNC_FL, |
| 142 | EXT2_IMMUTABLE_FL, |
| 143 | EXT2_APPEND_FL, |
| 144 | EXT2_NODUMP_FL, |
| 145 | EXT2_NOATIME_FL, |
| 146 | EXT2_COMPR_FL, |
| 147 | EXT3_JOURNAL_DATA_FL, |
| 148 | EXT2_NOTAIL_FL, |
| 149 | EXT2_TOPDIR_FL |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 152 | const char e2attr_flags_sname[] = |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 153 | #ifdef ENABLE_COMPRESSION |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 154 | "BZXE" |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 155 | #endif |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 156 | "I" |
| 157 | "suSDiadAcjtT"; |
| 158 | |
| 159 | static const char e2attr_flags_lname[] = |
| 160 | #ifdef ENABLE_COMPRESSION |
| 161 | "Compressed_File" "\0" |
| 162 | "Compressed_Dirty_File" "\0" |
| 163 | "Compression_Raw_Access" "\0" |
| 164 | "Compression_Error" "\0" |
| 165 | #endif |
| 166 | "Indexed_directory" "\0" |
| 167 | "Secure_Deletion" "\0" |
| 168 | "Undelete" "\0" |
| 169 | "Synchronous_Updates" "\0" |
| 170 | "Synchronous_Directory_Updates" "\0" |
| 171 | "Immutable" "\0" |
| 172 | "Append_Only" "\0" |
| 173 | "No_Dump" "\0" |
| 174 | "No_Atime" "\0" |
| 175 | "Compression_Requested" "\0" |
| 176 | "Journaled_Data" "\0" |
| 177 | "No_Tailmerging" "\0" |
| 178 | "Top_of_Directory_Hierarchies" "\0" |
| 179 | /* Another trailing NUL is added by compiler */; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 181 | void print_e2flags(FILE *f, unsigned long flags, unsigned options) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 182 | { |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 183 | const uint32_t *fv; |
| 184 | const char *fn; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 186 | fv = e2attr_flags_value; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 187 | if (options & PFOPT_LONG) { |
| 188 | int first = 1; |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 189 | fn = e2attr_flags_lname; |
| 190 | do { |
| 191 | if (flags & *fv) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 192 | if (!first) |
| 193 | fputs(", ", f); |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 194 | fputs(fn, f); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 195 | first = 0; |
| 196 | } |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 197 | fv++; |
| 198 | fn += strlen(fn) + 1; |
| 199 | } while (*fn); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 200 | if (first) |
| 201 | fputs("---", f); |
| 202 | } else { |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 203 | fn = e2attr_flags_sname; |
| 204 | do { |
Denis Vlasenko | 407b8c0 | 2006-12-26 03:02:25 +0000 | [diff] [blame] | 205 | char c = '-'; |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 206 | if (flags & *fv) |
| 207 | c = *fn; |
Denis Vlasenko | 407b8c0 | 2006-12-26 03:02:25 +0000 | [diff] [blame] | 208 | fputc(c, f); |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 209 | fv++; |
| 210 | fn++; |
| 211 | } while (*fn); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 212 | } |
| 213 | } |