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 (file == NULL) 88 { 89 printf("Could not open file: %s\n", filename); 90 return 1; 91 } 92 93 /* Read from the file, then remove the file, then read again. */ 94 95 read_from_file(file); 96 97 printf("Removing...\n"); 98 99 long err = client_remove(filename); 100 101 if (err) 102 { 103 printf("Could not remove file: %s\n", filename); 104 return 1; 105 } 106 107 printf("Reading again...\n"); 108 109 read_from_file(file); 110 111 /* Open the file again. */ 112 113 file_t *file_new = client_open(filename, O_RDONLY); 114 115 if (file_new != NULL) 116 { 117 printf("File should be absent: %s\n", filename); 118 return 1; 119 } 120 121 file_new = client_open(filename, O_RDWR | O_CREAT); 122 123 if (file_new == NULL) 124 { 125 printf("File should be present: %s\n", filename); 126 return 1; 127 } 128 129 write_to_file(file_new, "New file!"); 130 131 /* Read from the initial file and new file. */ 132 133 read_from_file(file); 134 read_from_file(file_new); 135 136 /* Close the files. */ 137 138 client_close(file); 139 client_close(file_new); 140 141 printf("End of tests.\n"); 142 143 return 0; 144 } 145 146 // vim: tabstop=2 expandtab shiftwidth=2