L4Re/departure

Annotated libe2access/host/test_listing.c

252:76be2dc3b294
2022-02-16 Paul Boddie Fixed directory link count decrementing, added support for showing link counts.
paul@226 1
/*
paul@226 2
 * Test filesystem listing concurrency limitations.
paul@226 3
 *
paul@250 4
 * Copyright (C) 2019, 2021, 2022 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@250 35
#include "utils.h"
paul@226 36
paul@226 37
paul@226 38
paul@226 39
/* Show directory entries when iterating. */
paul@226 40
paul@226 41
static int image_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
paul@226 42
                               int blocksize, char *buf, void *priv_data)
paul@226 43
{
paul@250 44
    struct utils_list_dir_data *data = (struct utils_list_dir_data *) priv_data;
paul@250 45
    ext2_filsys fs = (ext2_filsys) data->fs;
paul@226 46
    struct ext2_inode inode;
paul@226 47
    ext2_file_t file;
paul@226 48
    __u64 size;
paul@226 49
    char *to_remove[] = {"file0061", "file0062", "file0063", "file0125", "file0126", "file0127", 0};
paul@226 50
    char **f;
paul@226 51
paul@250 52
    /* Call the formatting utility function. */
paul@226 53
paul@250 54
    utils_list_dir_proc(dirent, offset, blocksize, buf, priv_data);
paul@226 55
paul@226 56
    /* Remove files at an awkward point in time. */
paul@226 57
paul@226 58
    if (!strncmp(dirent->name, "file0060", ext2fs_dirent_name_len(dirent)))
paul@226 59
    {
paul@226 60
        for (f = to_remove; *f; f++)
paul@226 61
            if (ext2fs_unlink(fs, EXT2_ROOT_INO, *f, 0, 0))
paul@226 62
                printf("%s not removed!\n", *f);
paul@226 63
    }
paul@226 64
paul@226 65
    return 0;
paul@226 66
}
paul@226 67
paul@226 68
paul@226 69
paul@226 70
int main(int argc, char *argv[])
paul@226 71
{
paul@226 72
    int flags = EXT2_FLAG_RW;
paul@226 73
    char *fsname;
paul@226 74
    ext2_filsys fs = NULL;
paul@226 75
    errcode_t retval;
paul@226 76
    int exitcode = 0;
paul@226 77
    char basename[32];
paul@226 78
    ext2_ino_t ino;
paul@226 79
    ext2_file_t file;
paul@226 80
    int i;
paul@250 81
    struct utils_list_dir_data list_dir_data;
paul@226 82
paul@226 83
    if (argc < 2)
paul@226 84
    {
paul@226 85
        printf("Usage: %s <device or image file>\n", argv[0]);
paul@226 86
        return 1;
paul@226 87
    }
paul@226 88
paul@226 89
    fsname = argv[1];
paul@226 90
paul@226 91
    /* Open the filesystem image using the POSIX file access mechanism. */
paul@226 92
paul@226 93
    retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs);
paul@226 94
    if (retval)
paul@226 95
    {
paul@226 96
        printf("Could not open filesystem using %s\n", fsname);
paul@226 97
        return 1;
paul@226 98
    }
paul@226 99
paul@226 100
    retval = ext2fs_read_bitmaps(fs);
paul@226 101
    if (retval)
paul@226 102
    {
paul@226 103
        printf("Could not read bitmaps from %s\n", fsname);
paul@226 104
        return 1;
paul@226 105
    }
paul@226 106
paul@226 107
    printf("Filesystem with blocksize %d\n", fs->blocksize);
paul@226 108
paul@226 109
    /* Create some files. */
paul@226 110
paul@226 111
    for (i = 1; i <= 200; i++)
paul@226 112
    {
paul@226 113
        sprintf(basename, "file%04d", i);
paul@226 114
paul@226 115
        retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
paul@226 116
                                   1000, 1000, &ino);
paul@226 117
paul@226 118
        if (retval)
paul@226 119
        {
paul@226 120
            printf("Could not create file %s\n", basename);
paul@226 121
            return 1;
paul@226 122
        }
paul@226 123
paul@226 124
        if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
paul@226 125
        {
paul@226 126
            printf("Could not write file %s\n", basename);
paul@226 127
            return 1;
paul@226 128
        }
paul@226 129
paul@226 130
        ext2fs_file_flush(file);
paul@226 131
        ext2fs_file_close(file);
paul@226 132
    }
paul@226 133
paul@250 134
    list_dir_data.fs = fs;
paul@250 135
    list_dir_data.filename = NULL;
paul@252 136
    list_dir_data.link_count = 0;
paul@250 137
paul@250 138
    image_list_dir(fs, "", image_list_dir_proc, &list_dir_data);
paul@250 139
    image_list_dir(fs, "", image_list_dir_proc, &list_dir_data);
paul@226 140
paul@226 141
    /* Close the filesystem image. */
paul@226 142
paul@226 143
    retval = ext2fs_flush(fs);
paul@226 144
    if (retval)
paul@226 145
    {
paul@226 146
        printf("Error flushing filesystem in %s\n", fsname);
paul@226 147
        exitcode = 1;
paul@226 148
    }
paul@226 149
paul@226 150
    retval = ext2fs_close(fs);
paul@226 151
    if (retval)
paul@226 152
    {
paul@226 153
        printf("Error closing filesystem in %s\n", fsname);
paul@226 154
        exitcode = 1;
paul@226 155
    }
paul@226 156
paul@226 157
    ext2fs_free(fs);
paul@226 158
    return exitcode;
paul@226 159
}