paul@226 | 1 | /* |
paul@226 | 2 | * Test file removal semantics. |
paul@226 | 3 | * |
paul@226 | 4 | * Copyright (C) 2019, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@226 | 5 | * |
paul@226 | 6 | * This program is free software; you can redistribute it and/or |
paul@226 | 7 | * modify it under the terms of the GNU General Public License as |
paul@226 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@226 | 9 | * the License, or (at your option) any later version. |
paul@226 | 10 | * |
paul@226 | 11 | * This program is distributed in the hope that it will be useful, |
paul@226 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@226 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@226 | 14 | * GNU General Public License for more details. |
paul@226 | 15 | * |
paul@226 | 16 | * You should have received a copy of the GNU General Public License |
paul@226 | 17 | * along with this program; if not, write to the Free Software |
paul@226 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@226 | 19 | * Boston, MA 02110-1301, USA |
paul@226 | 20 | */ |
paul@226 | 21 | |
paul@226 | 22 | #include <stdio.h> |
paul@226 | 23 | #include <stdlib.h> |
paul@226 | 24 | #include <string.h> |
paul@226 | 25 | |
paul@226 | 26 | #include <sys/types.h> |
paul@226 | 27 | #include <sys/stat.h> |
paul@226 | 28 | |
paul@226 | 29 | #include <ext2fs/ext2fs.h> |
paul@226 | 30 | |
paul@226 | 31 | #include "file.h" |
paul@226 | 32 | #include "format.h" |
paul@226 | 33 | #include "image.h" |
paul@226 | 34 | #include "path.h" |
paul@226 | 35 | |
paul@226 | 36 | |
paul@226 | 37 | |
paul@226 | 38 | /* Buffer size for file access. */ |
paul@226 | 39 | |
paul@226 | 40 | const int BUFSIZE = 4096; |
paul@226 | 41 | |
paul@226 | 42 | /* Number of open files. */ |
paul@226 | 43 | |
paul@226 | 44 | const int FILES = 20; |
paul@226 | 45 | |
paul@226 | 46 | |
paul@226 | 47 | |
paul@226 | 48 | /* Show directory entries when iterating. */ |
paul@226 | 49 | |
paul@226 | 50 | static int image_list_dir_proc(struct ext2_dir_entry *dirent, int offset, |
paul@226 | 51 | int blocksize, char *buf, void *priv_data) |
paul@226 | 52 | { |
paul@226 | 53 | ext2_filsys fs = (ext2_filsys) priv_data; |
paul@226 | 54 | struct ext2_inode inode; |
paul@226 | 55 | ext2_file_t file; |
paul@226 | 56 | __u64 size; |
paul@226 | 57 | |
paul@226 | 58 | /* Obtain the inode details for metadata. */ |
paul@226 | 59 | |
paul@226 | 60 | if (ext2fs_read_inode(fs, dirent->inode, &inode)) |
paul@226 | 61 | return DIRENT_ABORT; |
paul@226 | 62 | |
paul@226 | 63 | /* Output details in the style of "ls -l" showing directory, permissions, |
paul@226 | 64 | owner, group and size information. */ |
paul@226 | 65 | |
paul@226 | 66 | printf("%06d: %s%s %5d %5d %6d ", |
paul@226 | 67 | offset, |
paul@226 | 68 | _image_isdir(fs, dirent->inode) ? "d" : "-", |
paul@226 | 69 | get_permission_string(inode.i_mode), |
paul@226 | 70 | inode.i_uid, |
paul@226 | 71 | inode.i_gid, |
paul@226 | 72 | EXT2_I_SIZE(&inode)); |
paul@226 | 73 | |
paul@226 | 74 | /* Output the name which is presumably not necessarily null-terminated. */ |
paul@226 | 75 | |
paul@226 | 76 | fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout); |
paul@226 | 77 | fputc((int) '\n', stdout); |
paul@226 | 78 | |
paul@226 | 79 | return 0; |
paul@226 | 80 | } |
paul@226 | 81 | |
paul@226 | 82 | |
paul@226 | 83 | |
paul@226 | 84 | int main(int argc, char *argv[]) |
paul@226 | 85 | { |
paul@226 | 86 | int flags = EXT2_FLAG_RW; |
paul@226 | 87 | char *fsname; |
paul@226 | 88 | ext2_filsys fs = NULL; |
paul@226 | 89 | errcode_t retval; |
paul@226 | 90 | int exitcode = 0; |
paul@226 | 91 | char basename[32]; |
paul@226 | 92 | ext2_ino_t ino, inos[FILES]; |
paul@226 | 93 | ext2_file_t file, files[FILES]; |
paul@226 | 94 | char buf[BUFSIZE]; |
paul@226 | 95 | unsigned int transferred; |
paul@226 | 96 | int i; |
paul@226 | 97 | |
paul@226 | 98 | if (argc < 2) |
paul@226 | 99 | { |
paul@226 | 100 | printf("Usage: %s <device or image file>\n", argv[0]); |
paul@226 | 101 | return 1; |
paul@226 | 102 | } |
paul@226 | 103 | |
paul@226 | 104 | fsname = argv[1]; |
paul@226 | 105 | |
paul@226 | 106 | /* Open the filesystem image using the POSIX file access mechanism. */ |
paul@226 | 107 | |
paul@226 | 108 | retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); |
paul@226 | 109 | if (retval) |
paul@226 | 110 | { |
paul@226 | 111 | printf("Could not open filesystem using %s\n", fsname); |
paul@226 | 112 | return 1; |
paul@226 | 113 | } |
paul@226 | 114 | |
paul@226 | 115 | retval = ext2fs_read_bitmaps(fs); |
paul@226 | 116 | if (retval) |
paul@226 | 117 | { |
paul@226 | 118 | printf("Could not read bitmaps from %s\n", fsname); |
paul@226 | 119 | return 1; |
paul@226 | 120 | } |
paul@226 | 121 | |
paul@226 | 122 | printf("Filesystem with blocksize %d\n", fs->blocksize); |
paul@226 | 123 | |
paul@226 | 124 | /* Create some files. */ |
paul@226 | 125 | |
paul@226 | 126 | for (i = 1; i <= FILES; i++) |
paul@226 | 127 | { |
paul@226 | 128 | sprintf(basename, "file%04d", i); |
paul@226 | 129 | |
paul@226 | 130 | retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644, |
paul@226 | 131 | 1000, 1000, &ino); |
paul@226 | 132 | |
paul@226 | 133 | if (retval) |
paul@226 | 134 | { |
paul@226 | 135 | printf("Could not create file %s\n", basename); |
paul@226 | 136 | return 1; |
paul@226 | 137 | } |
paul@226 | 138 | |
paul@226 | 139 | if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file)) |
paul@226 | 140 | { |
paul@226 | 141 | printf("Could not open file %s\n", basename); |
paul@226 | 142 | return 1; |
paul@226 | 143 | } |
paul@226 | 144 | |
paul@229 | 145 | sprintf(buf, "writing to file%04d", i); |
paul@229 | 146 | |
paul@229 | 147 | if (ext2fs_file_write(file, buf, strlen(buf), &transferred)) |
paul@229 | 148 | { |
paul@229 | 149 | printf("Could not write to file %s\n", basename); |
paul@229 | 150 | return 1; |
paul@229 | 151 | } |
paul@229 | 152 | |
paul@226 | 153 | ext2fs_file_flush(file); |
paul@226 | 154 | |
paul@226 | 155 | files[i - 1] = file; |
paul@226 | 156 | inos[i - 1] = ino; |
paul@226 | 157 | } |
paul@226 | 158 | |
paul@226 | 159 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 160 | printf("----\n"); |
paul@226 | 161 | |
paul@226 | 162 | /* Unlink the files. */ |
paul@226 | 163 | |
paul@226 | 164 | for (i = 1; i <= FILES; i++) |
paul@226 | 165 | { |
paul@226 | 166 | sprintf(basename, "file%04d", i); |
paul@226 | 167 | |
paul@226 | 168 | if (ext2fs_unlink(fs, EXT2_ROOT_INO, basename, 0, 0)) |
paul@226 | 169 | { |
paul@226 | 170 | printf("Could not unlink file %s\n", basename); |
paul@226 | 171 | return 1; |
paul@226 | 172 | } |
paul@226 | 173 | } |
paul@226 | 174 | |
paul@226 | 175 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 176 | printf("----\n"); |
paul@226 | 177 | |
paul@226 | 178 | /* Access and close unlinked files. */ |
paul@226 | 179 | |
paul@226 | 180 | for (i = 1; i <= FILES; i++) |
paul@226 | 181 | { |
paul@226 | 182 | sprintf(basename, "file%04d", i); |
paul@229 | 183 | sprintf(buf, "; writing to file%04d", i); |
paul@226 | 184 | |
paul@226 | 185 | file = files[i - 1]; |
paul@226 | 186 | |
paul@226 | 187 | if (ext2fs_file_write(file, buf, strlen(buf), &transferred)) |
paul@226 | 188 | { |
paul@226 | 189 | printf("Could not write to file %s\n", basename); |
paul@226 | 190 | return 1; |
paul@226 | 191 | } |
paul@226 | 192 | |
paul@226 | 193 | ext2fs_file_flush(file); |
paul@226 | 194 | ext2fs_file_close(file); |
paul@226 | 195 | } |
paul@226 | 196 | |
paul@226 | 197 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 198 | printf("----\n"); |
paul@226 | 199 | |
paul@226 | 200 | /* Create some more files. */ |
paul@226 | 201 | |
paul@226 | 202 | for (i = 1; i <= FILES; i++) |
paul@226 | 203 | { |
paul@226 | 204 | sprintf(basename, "file%04d", i + FILES); |
paul@226 | 205 | |
paul@226 | 206 | retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644, |
paul@226 | 207 | 1000, 1000, &ino); |
paul@226 | 208 | |
paul@226 | 209 | if (retval) |
paul@226 | 210 | { |
paul@226 | 211 | printf("Could not create file %s\n", basename); |
paul@226 | 212 | return 1; |
paul@226 | 213 | } |
paul@226 | 214 | |
paul@226 | 215 | if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file)) |
paul@226 | 216 | { |
paul@226 | 217 | printf("Could not write file %s\n", basename); |
paul@226 | 218 | return 1; |
paul@226 | 219 | } |
paul@226 | 220 | |
paul@226 | 221 | ext2fs_file_flush(file); |
paul@226 | 222 | ext2fs_file_close(file); |
paul@226 | 223 | } |
paul@226 | 224 | |
paul@226 | 225 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 226 | printf("----\n"); |
paul@226 | 227 | |
paul@226 | 228 | /* Re-open the original files and read from them. */ |
paul@226 | 229 | |
paul@226 | 230 | for (i = 1; i <= FILES; i++) |
paul@226 | 231 | { |
paul@226 | 232 | sprintf(basename, "file%04d", i); |
paul@226 | 233 | |
paul@226 | 234 | ino = inos[i - 1]; |
paul@226 | 235 | |
paul@226 | 236 | if (ext2fs_file_open(fs, ino, 0, &file)) |
paul@226 | 237 | { |
paul@226 | 238 | printf("Could not open file %s\n", basename); |
paul@226 | 239 | return 1; |
paul@226 | 240 | } |
paul@226 | 241 | |
paul@226 | 242 | if (ext2fs_file_read(file, buf, BUFSIZE, &transferred)) |
paul@226 | 243 | { |
paul@226 | 244 | printf("Could not read from file %s\n", basename); |
paul@226 | 245 | return 1; |
paul@226 | 246 | } |
paul@226 | 247 | |
paul@226 | 248 | buf[transferred] = '\0'; |
paul@226 | 249 | printf("Read from %s: %s\n", basename, buf); |
paul@226 | 250 | |
paul@226 | 251 | ext2fs_file_close(file); |
paul@226 | 252 | } |
paul@226 | 253 | |
paul@229 | 254 | /* Create some more files with the same names as the original ones. */ |
paul@229 | 255 | |
paul@229 | 256 | for (i = 1; i <= FILES; i++) |
paul@229 | 257 | { |
paul@229 | 258 | sprintf(basename, "file%04d", i); |
paul@229 | 259 | |
paul@229 | 260 | retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644, |
paul@229 | 261 | 1000, 1000, &ino); |
paul@229 | 262 | |
paul@229 | 263 | if (retval) |
paul@229 | 264 | { |
paul@229 | 265 | printf("Could not create file %s\n", basename); |
paul@229 | 266 | return 1; |
paul@229 | 267 | } |
paul@229 | 268 | |
paul@229 | 269 | if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file)) |
paul@229 | 270 | { |
paul@229 | 271 | printf("Could not write file %s\n", basename); |
paul@229 | 272 | return 1; |
paul@229 | 273 | } |
paul@229 | 274 | |
paul@229 | 275 | ext2fs_file_flush(file); |
paul@229 | 276 | ext2fs_file_close(file); |
paul@229 | 277 | } |
paul@229 | 278 | |
paul@229 | 279 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@229 | 280 | printf("----\n"); |
paul@229 | 281 | |
paul@229 | 282 | /* Re-open the original files and read from them again. */ |
paul@229 | 283 | |
paul@229 | 284 | for (i = 1; i <= FILES; i++) |
paul@229 | 285 | { |
paul@229 | 286 | sprintf(basename, "file%04d", i); |
paul@229 | 287 | |
paul@229 | 288 | ino = inos[i - 1]; |
paul@229 | 289 | |
paul@229 | 290 | if (ext2fs_file_open(fs, ino, 0, &file)) |
paul@229 | 291 | { |
paul@229 | 292 | printf("Could not open file %s\n", basename); |
paul@229 | 293 | return 1; |
paul@229 | 294 | } |
paul@229 | 295 | |
paul@229 | 296 | if (ext2fs_file_read(file, buf, BUFSIZE, &transferred)) |
paul@229 | 297 | { |
paul@229 | 298 | printf("Could not read from file %s\n", basename); |
paul@229 | 299 | return 1; |
paul@229 | 300 | } |
paul@229 | 301 | |
paul@229 | 302 | buf[transferred] = '\0'; |
paul@229 | 303 | printf("Read from %s: %s\n", basename, buf); |
paul@229 | 304 | |
paul@229 | 305 | ext2fs_file_close(file); |
paul@229 | 306 | } |
paul@229 | 307 | |
paul@226 | 308 | /* Close the filesystem image. */ |
paul@226 | 309 | |
paul@226 | 310 | retval = ext2fs_flush(fs); |
paul@226 | 311 | if (retval) |
paul@226 | 312 | { |
paul@226 | 313 | printf("Error flushing filesystem in %s\n", fsname); |
paul@226 | 314 | exitcode = 1; |
paul@226 | 315 | } |
paul@226 | 316 | |
paul@226 | 317 | retval = ext2fs_close(fs); |
paul@226 | 318 | if (retval) |
paul@226 | 319 | { |
paul@226 | 320 | printf("Error closing filesystem in %s\n", fsname); |
paul@226 | 321 | exitcode = 1; |
paul@226 | 322 | } |
paul@226 | 323 | |
paul@226 | 324 | ext2fs_free(fs); |
paul@226 | 325 | return exitcode; |
paul@226 | 326 | } |