1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libe2access/host/test_listing.c Tue Dec 28 01:37:59 2021 +0100
1.3 @@ -0,0 +1,169 @@
1.4 +/*
1.5 + * Test filesystem listing concurrency limitations.
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 +/* Show directory entries when iterating. */
1.42 +
1.43 +static int image_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
1.44 + int blocksize, char *buf, void *priv_data)
1.45 +{
1.46 + ext2_filsys fs = (ext2_filsys) priv_data;
1.47 + struct ext2_inode inode;
1.48 + ext2_file_t file;
1.49 + __u64 size;
1.50 + char *to_remove[] = {"file0061", "file0062", "file0063", "file0125", "file0126", "file0127", 0};
1.51 + char **f;
1.52 +
1.53 + /* Obtain the inode details for metadata. */
1.54 +
1.55 + if (ext2fs_read_inode(fs, dirent->inode, &inode))
1.56 + return DIRENT_ABORT;
1.57 +
1.58 + /* Output details in the style of "ls -l" showing directory, permissions,
1.59 + owner, group and size information. */
1.60 +
1.61 + printf("%06d: %s%s %5d %5d %6d ",
1.62 + offset,
1.63 + _image_isdir(fs, dirent->inode) ? "d" : "-",
1.64 + get_permission_string(inode.i_mode),
1.65 + inode.i_uid,
1.66 + inode.i_gid,
1.67 + EXT2_I_SIZE(&inode));
1.68 +
1.69 + /* Output the name which is presumably not necessarily null-terminated. */
1.70 +
1.71 + fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);
1.72 + fputc((int) '\n', stdout);
1.73 +
1.74 + /* Remove files at an awkward point in time. */
1.75 +
1.76 + if (!strncmp(dirent->name, "file0060", ext2fs_dirent_name_len(dirent)))
1.77 + {
1.78 + for (f = to_remove; *f; f++)
1.79 + if (ext2fs_unlink(fs, EXT2_ROOT_INO, *f, 0, 0))
1.80 + printf("%s not removed!\n", *f);
1.81 + }
1.82 +
1.83 + return 0;
1.84 +}
1.85 +
1.86 +
1.87 +
1.88 +int main(int argc, char *argv[])
1.89 +{
1.90 + int flags = EXT2_FLAG_RW;
1.91 + char *fsname;
1.92 + ext2_filsys fs = NULL;
1.93 + errcode_t retval;
1.94 + int exitcode = 0;
1.95 + char basename[32];
1.96 + ext2_ino_t ino;
1.97 + ext2_file_t file;
1.98 + int i;
1.99 +
1.100 + if (argc < 2)
1.101 + {
1.102 + printf("Usage: %s <device or image file>\n", argv[0]);
1.103 + return 1;
1.104 + }
1.105 +
1.106 + fsname = argv[1];
1.107 +
1.108 + /* Open the filesystem image using the POSIX file access mechanism. */
1.109 +
1.110 + retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs);
1.111 + if (retval)
1.112 + {
1.113 + printf("Could not open filesystem using %s\n", fsname);
1.114 + return 1;
1.115 + }
1.116 +
1.117 + retval = ext2fs_read_bitmaps(fs);
1.118 + if (retval)
1.119 + {
1.120 + printf("Could not read bitmaps from %s\n", fsname);
1.121 + return 1;
1.122 + }
1.123 +
1.124 + printf("Filesystem with blocksize %d\n", fs->blocksize);
1.125 +
1.126 + /* Create some files. */
1.127 +
1.128 + for (i = 1; i <= 200; i++)
1.129 + {
1.130 + sprintf(basename, "file%04d", i);
1.131 +
1.132 + retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
1.133 + 1000, 1000, &ino);
1.134 +
1.135 + if (retval)
1.136 + {
1.137 + printf("Could not create file %s\n", basename);
1.138 + return 1;
1.139 + }
1.140 +
1.141 + if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
1.142 + {
1.143 + printf("Could not write file %s\n", basename);
1.144 + return 1;
1.145 + }
1.146 +
1.147 + ext2fs_file_flush(file);
1.148 + ext2fs_file_close(file);
1.149 + }
1.150 +
1.151 + image_list_dir(fs, "", image_list_dir_proc, fs);
1.152 + image_list_dir(fs, "", image_list_dir_proc, fs);
1.153 +
1.154 + /* Close the filesystem image. */
1.155 +
1.156 + retval = ext2fs_flush(fs);
1.157 + if (retval)
1.158 + {
1.159 + printf("Error flushing filesystem in %s\n", fsname);
1.160 + exitcode = 1;
1.161 + }
1.162 +
1.163 + retval = ext2fs_close(fs);
1.164 + if (retval)
1.165 + {
1.166 + printf("Error closing filesystem in %s\n", fsname);
1.167 + exitcode = 1;
1.168 + }
1.169 +
1.170 + ext2fs_free(fs);
1.171 + return exitcode;
1.172 +}