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