L4Re/departure

Annotated libe2access/host/test_remove.c

635:18f77ccd5ea8
8 months ago Paul Boddie Updated dataspace size type usage.
paul@226 1
/*
paul@226 2
 * Test file removal semantics.
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
/* Buffer size for file access. */
paul@226 40
paul@226 41
const int BUFSIZE = 4096;
paul@226 42
paul@226 43
/* Number of open files. */
paul@226 44
paul@226 45
const int FILES = 20;
paul@226 46
paul@226 47
paul@226 48
paul@226 49
int main(int argc, char *argv[])
paul@226 50
{
paul@226 51
    int flags = EXT2_FLAG_RW;
paul@226 52
    char *fsname;
paul@226 53
    ext2_filsys fs = NULL;
paul@226 54
    errcode_t retval;
paul@226 55
    int exitcode = 0;
paul@226 56
    char basename[32];
paul@226 57
    ext2_ino_t ino, inos[FILES];
paul@226 58
    ext2_file_t file, files[FILES];
paul@226 59
    char buf[BUFSIZE];
paul@226 60
    unsigned int transferred;
paul@226 61
    int i;
paul@226 62
paul@226 63
    if (argc < 2)
paul@226 64
    {
paul@226 65
        printf("Usage: %s <device or image file>\n", argv[0]);
paul@226 66
        return 1;
paul@226 67
    }
paul@226 68
paul@226 69
    fsname = argv[1];
paul@226 70
paul@226 71
    /* Open the filesystem image using the POSIX file access mechanism. */
paul@226 72
paul@226 73
    retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs);
paul@226 74
    if (retval)
paul@226 75
    {
paul@226 76
        printf("Could not open filesystem using %s\n", fsname);
paul@226 77
        return 1;
paul@226 78
    }
paul@226 79
paul@226 80
    retval = ext2fs_read_bitmaps(fs);
paul@226 81
    if (retval)
paul@226 82
    {
paul@226 83
        printf("Could not read bitmaps from %s\n", fsname);
paul@226 84
        return 1;
paul@226 85
    }
paul@226 86
paul@226 87
    printf("Filesystem with blocksize %d\n", fs->blocksize);
paul@226 88
paul@226 89
    /* Create some files. */
paul@226 90
paul@226 91
    for (i = 1; i <= FILES; i++)
paul@226 92
    {
paul@226 93
        sprintf(basename, "file%04d", i);
paul@226 94
paul@226 95
        retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
paul@226 96
                                   1000, 1000, &ino);
paul@226 97
paul@226 98
        if (retval)
paul@226 99
        {
paul@226 100
            printf("Could not create file %s\n", basename);
paul@226 101
            return 1;
paul@226 102
        }
paul@226 103
paul@226 104
        if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
paul@226 105
        {
paul@226 106
            printf("Could not open file %s\n", basename);
paul@226 107
            return 1;
paul@226 108
        }
paul@226 109
paul@229 110
        sprintf(buf, "writing to file%04d", i);
paul@229 111
paul@229 112
        if (ext2fs_file_write(file, buf, strlen(buf), &transferred))
paul@229 113
        {
paul@229 114
            printf("Could not write to file %s\n", basename);
paul@229 115
            return 1;
paul@229 116
        }
paul@229 117
paul@226 118
        ext2fs_file_flush(file);
paul@226 119
paul@226 120
        files[i - 1] = file;
paul@226 121
        inos[i - 1] = ino;
paul@226 122
    }
paul@226 123
paul@250 124
    utils_list_dir(fs, "");
paul@226 125
    printf("----\n");
paul@226 126
paul@226 127
    /* Unlink the files. */
paul@226 128
paul@226 129
    for (i = 1; i <= FILES; i++)
paul@226 130
    {
paul@226 131
        sprintf(basename, "file%04d", i);
paul@226 132
paul@226 133
        if (ext2fs_unlink(fs, EXT2_ROOT_INO, basename, 0, 0))
paul@226 134
        {
paul@226 135
            printf("Could not unlink file %s\n", basename);
paul@226 136
            return 1;
paul@226 137
        }
paul@226 138
    }
paul@226 139
paul@250 140
    utils_list_dir(fs, "");
paul@226 141
    printf("----\n");
paul@226 142
paul@226 143
    /* Access and close unlinked files. */
paul@226 144
paul@226 145
    for (i = 1; i <= FILES; i++)
paul@226 146
    {
paul@226 147
        sprintf(basename, "file%04d", i);
paul@229 148
        sprintf(buf, "; writing to file%04d", i);
paul@226 149
paul@226 150
        file = files[i - 1];
paul@226 151
paul@226 152
        if (ext2fs_file_write(file, buf, strlen(buf), &transferred))
paul@226 153
        {
paul@226 154
            printf("Could not write to file %s\n", basename);
paul@226 155
            return 1;
paul@226 156
        }
paul@226 157
paul@226 158
        ext2fs_file_flush(file);
paul@226 159
        ext2fs_file_close(file);
paul@226 160
    }
paul@226 161
paul@250 162
    utils_list_dir(fs, "");
paul@226 163
    printf("----\n");
paul@226 164
paul@226 165
    /* Create some more files. */
paul@226 166
paul@226 167
    for (i = 1; i <= FILES; i++)
paul@226 168
    {
paul@226 169
        sprintf(basename, "file%04d", i + FILES);
paul@226 170
paul@226 171
        retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
paul@226 172
                                   1000, 1000, &ino);
paul@226 173
paul@226 174
        if (retval)
paul@226 175
        {
paul@226 176
            printf("Could not create file %s\n", basename);
paul@226 177
            return 1;
paul@226 178
        }
paul@226 179
paul@226 180
        if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
paul@226 181
        {
paul@226 182
            printf("Could not write file %s\n", basename);
paul@226 183
            return 1;
paul@226 184
        }
paul@226 185
paul@226 186
        ext2fs_file_flush(file);
paul@226 187
        ext2fs_file_close(file);
paul@226 188
    }
paul@226 189
paul@250 190
    utils_list_dir(fs, "");
paul@226 191
    printf("----\n");
paul@226 192
paul@226 193
    /* Re-open the original files and read from them. */
paul@226 194
paul@226 195
    for (i = 1; i <= FILES; i++)
paul@226 196
    {
paul@226 197
        sprintf(basename, "file%04d", i);
paul@226 198
paul@226 199
        ino = inos[i - 1];
paul@226 200
paul@226 201
        if (ext2fs_file_open(fs, ino, 0, &file))
paul@226 202
        {
paul@226 203
            printf("Could not open file %s\n", basename);
paul@226 204
            return 1;
paul@226 205
        }
paul@226 206
paul@226 207
        if (ext2fs_file_read(file, buf, BUFSIZE, &transferred))
paul@226 208
        {
paul@226 209
            printf("Could not read from file %s\n", basename);
paul@226 210
            return 1;
paul@226 211
        }
paul@226 212
paul@226 213
        buf[transferred] = '\0';
paul@226 214
        printf("Read from %s: %s\n", basename, buf);
paul@226 215
paul@226 216
        ext2fs_file_close(file);
paul@226 217
    }
paul@226 218
paul@229 219
    /* Create some more files with the same names as the original ones. */
paul@229 220
paul@229 221
    for (i = 1; i <= FILES; i++)
paul@229 222
    {
paul@229 223
        sprintf(basename, "file%04d", i);
paul@229 224
paul@229 225
        retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,
paul@229 226
                                   1000, 1000, &ino);
paul@229 227
paul@229 228
        if (retval)
paul@229 229
        {
paul@229 230
            printf("Could not create file %s\n", basename);
paul@229 231
            return 1;
paul@229 232
        }
paul@229 233
paul@229 234
        if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))
paul@229 235
        {
paul@229 236
            printf("Could not write file %s\n", basename);
paul@229 237
            return 1;
paul@229 238
        }
paul@229 239
paul@229 240
        ext2fs_file_flush(file);
paul@229 241
        ext2fs_file_close(file);
paul@229 242
    }
paul@229 243
paul@250 244
    utils_list_dir(fs, "");
paul@229 245
    printf("----\n");
paul@229 246
paul@229 247
    /* Re-open the original files and read from them again. */
paul@229 248
paul@229 249
    for (i = 1; i <= FILES; i++)
paul@229 250
    {
paul@229 251
        sprintf(basename, "file%04d", i);
paul@229 252
paul@229 253
        ino = inos[i - 1];
paul@229 254
paul@229 255
        if (ext2fs_file_open(fs, ino, 0, &file))
paul@229 256
        {
paul@229 257
            printf("Could not open file %s\n", basename);
paul@229 258
            return 1;
paul@229 259
        }
paul@229 260
paul@229 261
        if (ext2fs_file_read(file, buf, BUFSIZE, &transferred))
paul@229 262
        {
paul@229 263
            printf("Could not read from file %s\n", basename);
paul@229 264
            return 1;
paul@229 265
        }
paul@229 266
paul@229 267
        buf[transferred] = '\0';
paul@229 268
        printf("Read from %s: %s\n", basename, buf);
paul@229 269
paul@229 270
        ext2fs_file_close(file);
paul@229 271
    }
paul@229 272
paul@226 273
    /* Close the filesystem image. */
paul@226 274
paul@226 275
    retval = ext2fs_flush(fs);
paul@226 276
    if (retval)
paul@226 277
    {
paul@226 278
        printf("Error flushing filesystem in %s\n", fsname);
paul@226 279
        exitcode = 1;
paul@226 280
    }
paul@226 281
paul@226 282
    retval = ext2fs_close(fs);
paul@226 283
    if (retval)
paul@226 284
    {
paul@226 285
        printf("Error closing filesystem in %s\n", fsname);
paul@226 286
        exitcode = 1;
paul@226 287
    }
paul@226 288
paul@226 289
    ext2fs_free(fs);
paul@226 290
    return exitcode;
paul@226 291
}