1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libe2access/host/test_remove.c Tue Dec 28 01:37:59 2021 +0100
1.3 @@ -0,0 +1,264 @@
1.4 +/*
1.5 + * Test file removal semantics.
1.6 + *
1.7 + * Copyright (C) 2019, 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <stdio.h>
1.26 +#include <stdlib.h>
1.27 +#include <string.h>
1.28 +
1.29 +#include <sys/types.h>
1.30 +#include <sys/stat.h>
1.31 +
1.32 +#include <ext2fs/ext2fs.h>
1.33 +
1.34 +#include "file.h"
1.35 +#include "format.h"
1.36 +#include "image.h"
1.37 +#include "path.h"
1.38 +
1.39 +
1.40 +
1.41 +/* Buffer size for file access. */
1.42 +
1.43 +const int BUFSIZE = 4096;
1.44 +
1.45 +/* Number of open files. */
1.46 +
1.47 +const int FILES = 20;
1.48 +
1.49 +
1.50 +
1.51 +/* Show directory entries when iterating. */
1.52 +
1.53 +static int image_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
1.54 + int blocksize, char *buf, void *priv_data)
1.55 +{
1.56 + ext2_filsys fs = (ext2_filsys) priv_data;
1.57 + struct ext2_inode inode;
1.58 + ext2_file_t file;
1.59 + __u64 size;
1.60 +
1.61 + /* Obtain the inode details for metadata. */
1.62 +
1.63 + if (ext2fs_read_inode(fs, dirent->inode, &inode))
1.64 + return DIRENT_ABORT;
1.65 +
1.66 + /* Output details in the style of "ls -l" showing directory, permissions,
1.67 + owner, group and size information. */
1.68 +
1.69 + printf("%06d: %s%s %5d %5d %6d ",
1.70 + offset,
1.71 + _image_isdir(fs, dirent->inode) ? "d" : "-",
1.72 + get_permission_string(inode.i_mode),
1.73 + inode.i_uid,
1.74 + inode.i_gid,
1.75 + EXT2_I_SIZE(&inode));
1.76 +
1.77 + /* Output the name which is presumably not necessarily null-terminated. */
1.78 +
1.79 + fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);
1.80 + fputc((int) '\n', stdout);
1.81 +
1.82 + return 0;
1.83 +}
1.84 +
1.85 +
1.86 +
1.87 +int main(int argc, char *argv[])
1.88 +{
1.89 + int flags = EXT2_FLAG_RW;
1.90 + char *fsname;
1.91 + ext2_filsys fs = NULL;
1.92 + errcode_t retval;
1.93 + int exitcode = 0;
1.94 + char basename[32];
1.95 + ext2_ino_t ino, inos[FILES];
1.96 + ext2_file_t file, files[FILES];
1.97 + char buf[BUFSIZE];
1.98 + unsigned int transferred;
1.99 + int i;
1.100 +
1.101 + if (argc < 2)
1.102 + {
1.103 + printf("Usage: %s <device or image file>\n", argv[0]);
1.104 + return 1;
1.105 + }
1.106 +
1.107 + fsname = argv[1];
1.108 +
1.109 + /* Open the filesystem image using the POSIX file access mechanism. */
1.110 +
1.111 + retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs);
1.112 + if (retval)
1.113 + {
1.114 + printf("Could not open filesystem using %s\n", fsname);
1.115 + return 1;
1.116 + }
1.117 +
1.118 + retval = ext2fs_read_bitmaps(fs);
1.119 + if (retval)
1.120 + {
1.121 + printf("Could not read bitmaps from %s\n", fsname);
1.122 + return 1;
1.123 + }
1.124 +
1.125 + printf("Filesystem with blocksize %d\n", fs->blocksize);
1.126 +
1.127 + /* Create some files. */
1.128 +
1.129 + for (i = 1; i <= FILES; i++)
1.130 + {
1.131 + sprintf(basename, "file%04d", i);
1.132 +
1.133 + retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
1.134 + 1000, 1000, &ino);
1.135 +
1.136 + if (retval)
1.137 + {
1.138 + printf("Could not create file %s\n", basename);
1.139 + return 1;
1.140 + }
1.141 +
1.142 + if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
1.143 + {
1.144 + printf("Could not open file %s\n", basename);
1.145 + return 1;
1.146 + }
1.147 +
1.148 + ext2fs_file_flush(file);
1.149 +
1.150 + files[i - 1] = file;
1.151 + inos[i - 1] = ino;
1.152 + }
1.153 +
1.154 + image_list_dir(fs, "", image_list_dir_proc, fs);
1.155 + printf("----\n");
1.156 +
1.157 + /* Unlink the files. */
1.158 +
1.159 + for (i = 1; i <= FILES; i++)
1.160 + {
1.161 + sprintf(basename, "file%04d", i);
1.162 +
1.163 + if (ext2fs_unlink(fs, EXT2_ROOT_INO, basename, 0, 0))
1.164 + {
1.165 + printf("Could not unlink file %s\n", basename);
1.166 + return 1;
1.167 + }
1.168 + }
1.169 +
1.170 + image_list_dir(fs, "", image_list_dir_proc, fs);
1.171 + printf("----\n");
1.172 +
1.173 + /* Access and close unlinked files. */
1.174 +
1.175 + for (i = 1; i <= FILES; i++)
1.176 + {
1.177 + sprintf(basename, "file%04d", i);
1.178 + sprintf(buf, "writing to file%04d", i);
1.179 +
1.180 + file = files[i - 1];
1.181 +
1.182 + if (ext2fs_file_write(file, buf, strlen(buf), &transferred))
1.183 + {
1.184 + printf("Could not write to file %s\n", basename);
1.185 + return 1;
1.186 + }
1.187 +
1.188 + ext2fs_file_flush(file);
1.189 + ext2fs_file_close(file);
1.190 + }
1.191 +
1.192 + image_list_dir(fs, "", image_list_dir_proc, fs);
1.193 + printf("----\n");
1.194 +
1.195 + /* Create some more files. */
1.196 +
1.197 + for (i = 1; i <= FILES; i++)
1.198 + {
1.199 + sprintf(basename, "file%04d", i + FILES);
1.200 +
1.201 + retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
1.202 + 1000, 1000, &ino);
1.203 +
1.204 + if (retval)
1.205 + {
1.206 + printf("Could not create file %s\n", basename);
1.207 + return 1;
1.208 + }
1.209 +
1.210 + if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
1.211 + {
1.212 + printf("Could not write file %s\n", basename);
1.213 + return 1;
1.214 + }
1.215 +
1.216 + ext2fs_file_flush(file);
1.217 + ext2fs_file_close(file);
1.218 + }
1.219 +
1.220 + image_list_dir(fs, "", image_list_dir_proc, fs);
1.221 + printf("----\n");
1.222 +
1.223 + /* Re-open the original files and read from them. */
1.224 +
1.225 + for (i = 1; i <= FILES; i++)
1.226 + {
1.227 + sprintf(basename, "file%04d", i);
1.228 +
1.229 + ino = inos[i - 1];
1.230 +
1.231 + if (ext2fs_file_open(fs, ino, 0, &file))
1.232 + {
1.233 + printf("Could not open file %s\n", basename);
1.234 + return 1;
1.235 + }
1.236 +
1.237 + if (ext2fs_file_read(file, buf, BUFSIZE, &transferred))
1.238 + {
1.239 + printf("Could not read from file %s\n", basename);
1.240 + return 1;
1.241 + }
1.242 +
1.243 + buf[transferred] = '\0';
1.244 + printf("Read from %s: %s\n", basename, buf);
1.245 +
1.246 + ext2fs_file_close(file);
1.247 + }
1.248 +
1.249 + /* Close the filesystem image. */
1.250 +
1.251 + retval = ext2fs_flush(fs);
1.252 + if (retval)
1.253 + {
1.254 + printf("Error flushing filesystem in %s\n", fsname);
1.255 + exitcode = 1;
1.256 + }
1.257 +
1.258 + retval = ext2fs_close(fs);
1.259 + if (retval)
1.260 + {
1.261 + printf("Error closing filesystem in %s\n", fsname);
1.262 + exitcode = 1;
1.263 + }
1.264 +
1.265 + ext2fs_free(fs);
1.266 + return exitcode;
1.267 +}