L4Re/departure

Annotated libe2access/host/test_listing.c

226:8ad94f3addb2
2021-12-28 Paul Boddie Updated libe2access sources and added host-based programs and tests.
paul@226 1
/*
paul@226 2
 * Test filesystem listing concurrency limitations.
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
/* Show directory entries when iterating. */
paul@226 39
paul@226 40
static int image_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
paul@226 41
                               int blocksize, char *buf, void *priv_data)
paul@226 42
{
paul@226 43
    ext2_filsys fs = (ext2_filsys) priv_data;
paul@226 44
    struct ext2_inode inode;
paul@226 45
    ext2_file_t file;
paul@226 46
    __u64 size;
paul@226 47
    char *to_remove[] = {"file0061", "file0062", "file0063", "file0125", "file0126", "file0127", 0};
paul@226 48
    char **f;
paul@226 49
paul@226 50
    /* Obtain the inode details for metadata. */
paul@226 51
paul@226 52
    if (ext2fs_read_inode(fs, dirent->inode, &inode))
paul@226 53
        return DIRENT_ABORT;
paul@226 54
paul@226 55
    /* Output details in the style of "ls -l" showing directory, permissions,
paul@226 56
       owner, group and size information. */
paul@226 57
paul@226 58
    printf("%06d: %s%s   %5d %5d      %6d ",
paul@226 59
        offset,
paul@226 60
        _image_isdir(fs, dirent->inode) ? "d" : "-",
paul@226 61
        get_permission_string(inode.i_mode),
paul@226 62
        inode.i_uid,
paul@226 63
        inode.i_gid,
paul@226 64
        EXT2_I_SIZE(&inode));
paul@226 65
paul@226 66
    /* Output the name which is presumably not necessarily null-terminated. */
paul@226 67
paul@226 68
    fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);
paul@226 69
    fputc((int) '\n', stdout);
paul@226 70
paul@226 71
    /* Remove files at an awkward point in time. */
paul@226 72
paul@226 73
    if (!strncmp(dirent->name, "file0060", ext2fs_dirent_name_len(dirent)))
paul@226 74
    {
paul@226 75
        for (f = to_remove; *f; f++)
paul@226 76
            if (ext2fs_unlink(fs, EXT2_ROOT_INO, *f, 0, 0))
paul@226 77
                printf("%s not removed!\n", *f);
paul@226 78
    }
paul@226 79
paul@226 80
    return 0;
paul@226 81
}
paul@226 82
paul@226 83
paul@226 84
paul@226 85
int main(int argc, char *argv[])
paul@226 86
{
paul@226 87
    int flags = EXT2_FLAG_RW;
paul@226 88
    char *fsname;
paul@226 89
    ext2_filsys fs = NULL;
paul@226 90
    errcode_t retval;
paul@226 91
    int exitcode = 0;
paul@226 92
    char basename[32];
paul@226 93
    ext2_ino_t ino;
paul@226 94
    ext2_file_t file;
paul@226 95
    int i;
paul@226 96
paul@226 97
    if (argc < 2)
paul@226 98
    {
paul@226 99
        printf("Usage: %s <device or image file>\n", argv[0]);
paul@226 100
        return 1;
paul@226 101
    }
paul@226 102
paul@226 103
    fsname = argv[1];
paul@226 104
paul@226 105
    /* Open the filesystem image using the POSIX file access mechanism. */
paul@226 106
paul@226 107
    retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs);
paul@226 108
    if (retval)
paul@226 109
    {
paul@226 110
        printf("Could not open filesystem using %s\n", fsname);
paul@226 111
        return 1;
paul@226 112
    }
paul@226 113
paul@226 114
    retval = ext2fs_read_bitmaps(fs);
paul@226 115
    if (retval)
paul@226 116
    {
paul@226 117
        printf("Could not read bitmaps from %s\n", fsname);
paul@226 118
        return 1;
paul@226 119
    }
paul@226 120
paul@226 121
    printf("Filesystem with blocksize %d\n", fs->blocksize);
paul@226 122
paul@226 123
    /* Create some files. */
paul@226 124
paul@226 125
    for (i = 1; i <= 200; i++)
paul@226 126
    {
paul@226 127
        sprintf(basename, "file%04d", i);
paul@226 128
paul@226 129
        retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
paul@226 130
                                   1000, 1000, &ino);
paul@226 131
paul@226 132
        if (retval)
paul@226 133
        {
paul@226 134
            printf("Could not create file %s\n", basename);
paul@226 135
            return 1;
paul@226 136
        }
paul@226 137
paul@226 138
        if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
paul@226 139
        {
paul@226 140
            printf("Could not write file %s\n", basename);
paul@226 141
            return 1;
paul@226 142
        }
paul@226 143
paul@226 144
        ext2fs_file_flush(file);
paul@226 145
        ext2fs_file_close(file);
paul@226 146
    }
paul@226 147
paul@226 148
    image_list_dir(fs, "", image_list_dir_proc, fs);
paul@226 149
    image_list_dir(fs, "", image_list_dir_proc, fs);
paul@226 150
paul@226 151
    /* Close the filesystem image. */
paul@226 152
paul@226 153
    retval = ext2fs_flush(fs);
paul@226 154
    if (retval)
paul@226 155
    {
paul@226 156
        printf("Error flushing filesystem in %s\n", fsname);
paul@226 157
        exitcode = 1;
paul@226 158
    }
paul@226 159
paul@226 160
    retval = ext2fs_close(fs);
paul@226 161
    if (retval)
paul@226 162
    {
paul@226 163
        printf("Error closing filesystem in %s\n", fsname);
paul@226 164
        exitcode = 1;
paul@226 165
    }
paul@226 166
paul@226 167
    ext2fs_free(fs);
paul@226 168
    return exitcode;
paul@226 169
}