1 /* 2 * Test removal operations. 3 * 4 * Copyright (C) 2020, 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 <l4/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <stdio.h> 26 #include <string.h> 27 #include <stdlib.h> 28 29 #include <fsclient/client.h> 30 #include <systypes/fcntl.h> 31 #include <systypes/format.h> 32 33 #define FMToffsetd "%" pFMToffset "d" 34 35 36 37 static void read_from_file(file_t *file) 38 { 39 char buf[20]; 40 offset_t nread; 41 42 client_seek(file, 0, SEEK_SET); 43 44 printf("Reading...\n"); 45 46 nread = client_read(file, buf, 20); 47 48 printf("Read " FMToffsetd " bytes...\n", nread); 49 fwrite(buf, sizeof(char), nread, stdout); 50 fputs("\n", stdout); 51 } 52 53 static void write_to_file(file_t *file, const char *buf) 54 { 55 offset_t nwritten; 56 57 client_seek(file, 0, SEEK_SET); 58 59 printf("Writing...\n"); 60 61 nwritten = client_write(file, buf, strlen(buf)); 62 63 printf("Wrote " FMToffsetd " bytes: ", nwritten); 64 fwrite(buf, sizeof(char), nwritten, stdout); 65 fputs("\n", stdout); 66 } 67 68 int main(int argc, char *argv[]) 69 { 70 if (argc < 2) 71 { 72 printf("Need a file to remove.\n"); 73 return 1; 74 } 75 76 char *filename = argv[1]; 77 78 printf("Creating %s...\n", filename); 79 80 file_t *file = client_open(filename, O_WRONLY | O_CREAT); 81 82 write_to_file(file, "An existing file."); 83 84 client_close(file); 85 86 printf("Opening %s...\n", filename); 87 88 file = client_open(filename, O_RDONLY); 89 90 if (!client_opened(file)) 91 { 92 client_close(file); 93 printf("Could not open file: %s\n", filename); 94 return 1; 95 } 96 97 /* Read from the file, then remove the file, then read again. */ 98 99 read_from_file(file); 100 101 printf("Removing...\n"); 102 103 long err = client_remove(filename); 104 105 if (err) 106 { 107 printf("Could not remove file: %s\n", filename); 108 return 1; 109 } 110 111 printf("Reading again...\n"); 112 113 read_from_file(file); 114 115 /* Open the file again. */ 116 117 file_t *file_new = client_open(filename, O_RDONLY); 118 119 if (client_opened(file_new)) 120 { 121 printf("File should be absent: %s\n", filename); 122 return 1; 123 } 124 125 file_new = client_open(filename, O_RDWR | O_CREAT); 126 127 if (!client_opened(file_new)) 128 { 129 client_close(file_new); 130 printf("File should be present: %s\n", filename); 131 return 1; 132 } 133 134 write_to_file(file_new, "New file!"); 135 136 /* Read from the initial file and new file. */ 137 138 read_from_file(file); 139 read_from_file(file_new); 140 141 /* Close the files. */ 142 143 client_close(file); 144 client_close(file_new); 145 146 printf("End of test.\n"); 147 return 0; 148 } 149 150 // vim: tabstop=2 expandtab shiftwidth=2