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 | * |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
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, |
| 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 */ |
| 63 | int 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 */ |
| 99 | int 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 Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 143 | const 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 Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 165 | const char e2attr_flags_sname[] = |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 166 | #ifdef ENABLE_COMPRESSION |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 167 | "BZXE" |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 168 | #endif |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 169 | "I" |
| 170 | "suSDiadAcjtT"; |
| 171 | |
| 172 | static 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 Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 193 | |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 194 | void print_e2flags(FILE *f, unsigned long flags, unsigned options) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 195 | { |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 196 | const uint32_t *fv; |
| 197 | const char *fn; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 199 | fv = e2attr_flags_value; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 200 | if (options & PFOPT_LONG) { |
| 201 | int first = 1; |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 202 | fn = e2attr_flags_lname; |
| 203 | do { |
| 204 | if (flags & *fv) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 205 | if (!first) |
| 206 | fputs(", ", f); |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 207 | fputs(fn, f); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 208 | first = 0; |
| 209 | } |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 210 | fv++; |
| 211 | fn += strlen(fn) + 1; |
| 212 | } while (*fn); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 213 | if (first) |
| 214 | fputs("---", f); |
| 215 | } else { |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 216 | fn = e2attr_flags_sname; |
| 217 | do { |
Denis Vlasenko | 407b8c0 | 2006-12-26 03:02:25 +0000 | [diff] [blame] | 218 | char c = '-'; |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 219 | if (flags & *fv) |
| 220 | c = *fn; |
Denis Vlasenko | 407b8c0 | 2006-12-26 03:02:25 +0000 | [diff] [blame] | 221 | fputc(c, f); |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 222 | fv++; |
| 223 | fn++; |
| 224 | } while (*fn); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 225 | } |
| 226 | } |