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@226 | 145 | ext2fs_file_flush(file); |
paul@226 | 146 | |
paul@226 | 147 | files[i - 1] = file; |
paul@226 | 148 | inos[i - 1] = ino; |
paul@226 | 149 | } |
paul@226 | 150 | |
paul@226 | 151 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 152 | printf("----\n"); |
paul@226 | 153 | |
paul@226 | 154 | /* Unlink the files. */ |
paul@226 | 155 | |
paul@226 | 156 | for (i = 1; i <= FILES; i++) |
paul@226 | 157 | { |
paul@226 | 158 | sprintf(basename, "file%04d", i); |
paul@226 | 159 | |
paul@226 | 160 | if (ext2fs_unlink(fs, EXT2_ROOT_INO, basename, 0, 0)) |
paul@226 | 161 | { |
paul@226 | 162 | printf("Could not unlink file %s\n", basename); |
paul@226 | 163 | return 1; |
paul@226 | 164 | } |
paul@226 | 165 | } |
paul@226 | 166 | |
paul@226 | 167 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 168 | printf("----\n"); |
paul@226 | 169 | |
paul@226 | 170 | /* Access and close unlinked files. */ |
paul@226 | 171 | |
paul@226 | 172 | for (i = 1; i <= FILES; i++) |
paul@226 | 173 | { |
paul@226 | 174 | sprintf(basename, "file%04d", i); |
paul@226 | 175 | sprintf(buf, "writing to file%04d", i); |
paul@226 | 176 | |
paul@226 | 177 | file = files[i - 1]; |
paul@226 | 178 | |
paul@226 | 179 | if (ext2fs_file_write(file, buf, strlen(buf), &transferred)) |
paul@226 | 180 | { |
paul@226 | 181 | printf("Could not write to file %s\n", basename); |
paul@226 | 182 | return 1; |
paul@226 | 183 | } |
paul@226 | 184 | |
paul@226 | 185 | ext2fs_file_flush(file); |
paul@226 | 186 | ext2fs_file_close(file); |
paul@226 | 187 | } |
paul@226 | 188 | |
paul@226 | 189 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 190 | printf("----\n"); |
paul@226 | 191 | |
paul@226 | 192 | /* Create some more files. */ |
paul@226 | 193 | |
paul@226 | 194 | for (i = 1; i <= FILES; i++) |
paul@226 | 195 | { |
paul@226 | 196 | sprintf(basename, "file%04d", i + FILES); |
paul@226 | 197 | |
paul@226 | 198 | retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644, |
paul@226 | 199 | 1000, 1000, &ino); |
paul@226 | 200 | |
paul@226 | 201 | if (retval) |
paul@226 | 202 | { |
paul@226 | 203 | printf("Could not create file %s\n", basename); |
paul@226 | 204 | return 1; |
paul@226 | 205 | } |
paul@226 | 206 | |
paul@226 | 207 | if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file)) |
paul@226 | 208 | { |
paul@226 | 209 | printf("Could not write file %s\n", basename); |
paul@226 | 210 | return 1; |
paul@226 | 211 | } |
paul@226 | 212 | |
paul@226 | 213 | ext2fs_file_flush(file); |
paul@226 | 214 | ext2fs_file_close(file); |
paul@226 | 215 | } |
paul@226 | 216 | |
paul@226 | 217 | image_list_dir(fs, "", image_list_dir_proc, fs); |
paul@226 | 218 | printf("----\n"); |
paul@226 | 219 | |
paul@226 | 220 | /* Re-open the original files and read from them. */ |
paul@226 | 221 | |
paul@226 | 222 | for (i = 1; i <= FILES; i++) |
paul@226 | 223 | { |
paul@226 | 224 | sprintf(basename, "file%04d", i); |
paul@226 | 225 | |
paul@226 | 226 | ino = inos[i - 1]; |
paul@226 | 227 | |
paul@226 | 228 | if (ext2fs_file_open(fs, ino, 0, &file)) |
paul@226 | 229 | { |
paul@226 | 230 | printf("Could not open file %s\n", basename); |
paul@226 | 231 | return 1; |
paul@226 | 232 | } |
paul@226 | 233 | |
paul@226 | 234 | if (ext2fs_file_read(file, buf, BUFSIZE, &transferred)) |
paul@226 | 235 | { |
paul@226 | 236 | printf("Could not read from file %s\n", basename); |
paul@226 | 237 | return 1; |
paul@226 | 238 | } |
paul@226 | 239 | |
paul@226 | 240 | buf[transferred] = '\0'; |
paul@226 | 241 | printf("Read from %s: %s\n", basename, buf); |
paul@226 | 242 | |
paul@226 | 243 | ext2fs_file_close(file); |
paul@226 | 244 | } |
paul@226 | 245 | |
paul@226 | 246 | /* Close the filesystem image. */ |
paul@226 | 247 | |
paul@226 | 248 | retval = ext2fs_flush(fs); |
paul@226 | 249 | if (retval) |
paul@226 | 250 | { |
paul@226 | 251 | printf("Error flushing filesystem in %s\n", fsname); |
paul@226 | 252 | exitcode = 1; |
paul@226 | 253 | } |
paul@226 | 254 | |
paul@226 | 255 | retval = ext2fs_close(fs); |
paul@226 | 256 | if (retval) |
paul@226 | 257 | { |
paul@226 | 258 | printf("Error closing filesystem in %s\n", fsname); |
paul@226 | 259 | exitcode = 1; |
paul@226 | 260 | } |
paul@226 | 261 | |
paul@226 | 262 | ext2fs_free(fs); |
paul@226 | 263 | return exitcode; |
paul@226 | 264 | } |