L4Re/departure

libe2access/host/test_remove.c

391:bc65615a8fed
2022-06-30 Paul Boddie Added missing structure members. mmap-region-flags
     1 /*     2  * Test file removal semantics.     3  *     4  * Copyright (C) 2019, 2021, 2022 Paul Boddie <paul@boddie.org.uk>     5  *     6  * This program is free software; you can redistribute it and/or     7  * modify it under the terms of the GNU General Public License as     8  * published by the Free Software Foundation; either version 2 of     9  * the License, or (at your option) any later version.    10  *    11  * This program is distributed in the hope that it will be useful,    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    14  * GNU General Public License for more details.    15  *    16  * You should have received a copy of the GNU General Public License    17  * along with this program; if not, write to the Free Software    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor,    19  * Boston, MA  02110-1301, USA    20  */    21     22 #include <stdio.h>    23 #include <stdlib.h>    24 #include <string.h>    25     26 #include <sys/types.h>    27 #include <sys/stat.h>    28     29 #include <ext2fs/ext2fs.h>    30     31 #include "file.h"    32 #include "format.h"    33 #include "image.h"    34 #include "path.h"    35 #include "utils.h"    36     37     38     39 /* Buffer size for file access. */    40     41 const int BUFSIZE = 4096;    42     43 /* Number of open files. */    44     45 const int FILES = 20;    46     47     48     49 int main(int argc, char *argv[])    50 {    51     int flags = EXT2_FLAG_RW;    52     char *fsname;    53     ext2_filsys fs = NULL;    54     errcode_t retval;    55     int exitcode = 0;    56     char basename[32];    57     ext2_ino_t ino, inos[FILES];    58     ext2_file_t file, files[FILES];    59     char buf[BUFSIZE];    60     unsigned int transferred;    61     int i;    62     63     if (argc < 2)    64     {    65         printf("Usage: %s <device or image file>\n", argv[0]);    66         return 1;    67     }    68     69     fsname = argv[1];    70     71     /* Open the filesystem image using the POSIX file access mechanism. */    72     73     retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs);    74     if (retval)    75     {    76         printf("Could not open filesystem using %s\n", fsname);    77         return 1;    78     }    79     80     retval = ext2fs_read_bitmaps(fs);    81     if (retval)    82     {    83         printf("Could not read bitmaps from %s\n", fsname);    84         return 1;    85     }    86     87     printf("Filesystem with blocksize %d\n", fs->blocksize);    88     89     /* Create some files. */    90     91     for (i = 1; i <= FILES; i++)    92     {    93         sprintf(basename, "file%04d", i);    94     95         retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,    96                                    1000, 1000, &ino);    97     98         if (retval)    99         {   100             printf("Could not create file %s\n", basename);   101             return 1;   102         }   103    104         if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))   105         {   106             printf("Could not open file %s\n", basename);   107             return 1;   108         }   109    110         sprintf(buf, "writing to file%04d", i);   111    112         if (ext2fs_file_write(file, buf, strlen(buf), &transferred))   113         {   114             printf("Could not write to file %s\n", basename);   115             return 1;   116         }   117    118         ext2fs_file_flush(file);   119    120         files[i - 1] = file;   121         inos[i - 1] = ino;   122     }   123    124     utils_list_dir(fs, "");   125     printf("----\n");   126    127     /* Unlink the files. */   128    129     for (i = 1; i <= FILES; i++)   130     {   131         sprintf(basename, "file%04d", i);   132    133         if (ext2fs_unlink(fs, EXT2_ROOT_INO, basename, 0, 0))   134         {   135             printf("Could not unlink file %s\n", basename);   136             return 1;   137         }   138     }   139    140     utils_list_dir(fs, "");   141     printf("----\n");   142    143     /* Access and close unlinked files. */   144    145     for (i = 1; i <= FILES; i++)   146     {   147         sprintf(basename, "file%04d", i);   148         sprintf(buf, "; writing to file%04d", i);   149    150         file = files[i - 1];   151    152         if (ext2fs_file_write(file, buf, strlen(buf), &transferred))   153         {   154             printf("Could not write to file %s\n", basename);   155             return 1;   156         }   157    158         ext2fs_file_flush(file);   159         ext2fs_file_close(file);   160     }   161    162     utils_list_dir(fs, "");   163     printf("----\n");   164    165     /* Create some more files. */   166    167     for (i = 1; i <= FILES; i++)   168     {   169         sprintf(basename, "file%04d", i + FILES);   170    171         retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,   172                                    1000, 1000, &ino);   173    174         if (retval)   175         {   176             printf("Could not create file %s\n", basename);   177             return 1;   178         }   179    180         if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))   181         {   182             printf("Could not write file %s\n", basename);   183             return 1;   184         }   185    186         ext2fs_file_flush(file);   187         ext2fs_file_close(file);   188     }   189    190     utils_list_dir(fs, "");   191     printf("----\n");   192    193     /* Re-open the original files and read from them. */   194    195     for (i = 1; i <= FILES; i++)   196     {   197         sprintf(basename, "file%04d", i);   198    199         ino = inos[i - 1];   200    201         if (ext2fs_file_open(fs, ino, 0, &file))   202         {   203             printf("Could not open file %s\n", basename);   204             return 1;   205         }   206    207         if (ext2fs_file_read(file, buf, BUFSIZE, &transferred))   208         {   209             printf("Could not read from file %s\n", basename);   210             return 1;   211         }   212    213         buf[transferred] = '\0';   214         printf("Read from %s: %s\n", basename, buf);   215    216         ext2fs_file_close(file);   217     }   218    219     /* Create some more files with the same names as the original ones. */   220    221     for (i = 1; i <= FILES; i++)   222     {   223         sprintf(basename, "file%04d", i);   224    225         retval = image_create_file(fs, EXT2_ROOT_INO, basename, 0644,   226                                    1000, 1000, &ino);   227    228         if (retval)   229         {   230             printf("Could not create file %s\n", basename);   231             return 1;   232         }   233    234         if (ext2fs_file_open(fs, ino, EXT2_FILE_WRITE | EXT2_FILE_CREATE, &file))   235         {   236             printf("Could not write file %s\n", basename);   237             return 1;   238         }   239    240         ext2fs_file_flush(file);   241         ext2fs_file_close(file);   242     }   243    244     utils_list_dir(fs, "");   245     printf("----\n");   246    247     /* Re-open the original files and read from them again. */   248    249     for (i = 1; i <= FILES; i++)   250     {   251         sprintf(basename, "file%04d", i);   252    253         ino = inos[i - 1];   254    255         if (ext2fs_file_open(fs, ino, 0, &file))   256         {   257             printf("Could not open file %s\n", basename);   258             return 1;   259         }   260    261         if (ext2fs_file_read(file, buf, BUFSIZE, &transferred))   262         {   263             printf("Could not read from file %s\n", basename);   264             return 1;   265         }   266    267         buf[transferred] = '\0';   268         printf("Read from %s: %s\n", basename, buf);   269    270         ext2fs_file_close(file);   271     }   272    273     /* Close the filesystem image. */   274    275     retval = ext2fs_flush(fs);   276     if (retval)   277     {   278         printf("Error flushing filesystem in %s\n", fsname);   279         exitcode = 1;   280     }   281    282     retval = ext2fs_close(fs);   283     if (retval)   284     {   285         printf("Error closing filesystem in %s\n", fsname);   286         exitcode = 1;   287     }   288    289     ext2fs_free(fs);   290     return exitcode;   291 }