paul@388 | 1 | /* |
paul@388 | 2 | * Test removal operations. |
paul@388 | 3 | * |
paul@388 | 4 | * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@388 | 5 | * |
paul@388 | 6 | * This program is free software; you can redistribute it and/or |
paul@388 | 7 | * modify it under the terms of the GNU General Public License as |
paul@388 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@388 | 9 | * the License, or (at your option) any later version. |
paul@388 | 10 | * |
paul@388 | 11 | * This program is distributed in the hope that it will be useful, |
paul@388 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@388 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@388 | 14 | * GNU General Public License for more details. |
paul@388 | 15 | * |
paul@388 | 16 | * You should have received a copy of the GNU General Public License |
paul@388 | 17 | * along with this program; if not, write to the Free Software |
paul@388 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@388 | 19 | * Boston, MA 02110-1301, USA |
paul@388 | 20 | */ |
paul@388 | 21 | |
paul@388 | 22 | #include <l4/re/env.h> |
paul@388 | 23 | #include <l4/sys/err.h> |
paul@388 | 24 | |
paul@388 | 25 | #include <stdio.h> |
paul@388 | 26 | #include <string.h> |
paul@388 | 27 | #include <stdlib.h> |
paul@388 | 28 | |
paul@388 | 29 | #include <fsclient/client.h> |
paul@388 | 30 | #include <systypes/fcntl.h> |
paul@455 | 31 | #include <systypes/format.h> |
paul@455 | 32 | |
paul@455 | 33 | #define FMToffsetd "%" pFMToffset "d" |
paul@388 | 34 | |
paul@388 | 35 | |
paul@388 | 36 | |
paul@388 | 37 | static void read_from_file(file_t *file) |
paul@388 | 38 | { |
paul@388 | 39 | char buf[20]; |
paul@388 | 40 | offset_t nread; |
paul@388 | 41 | |
paul@388 | 42 | client_seek(file, 0, SEEK_SET); |
paul@388 | 43 | |
paul@388 | 44 | printf("Reading...\n"); |
paul@388 | 45 | |
paul@388 | 46 | nread = client_read(file, buf, 20); |
paul@388 | 47 | |
paul@455 | 48 | printf("Read " FMToffsetd " bytes...\n", nread); |
paul@388 | 49 | fwrite(buf, sizeof(char), nread, stdout); |
paul@388 | 50 | fputs("\n", stdout); |
paul@388 | 51 | } |
paul@388 | 52 | |
paul@388 | 53 | static void write_to_file(file_t *file, const char *buf) |
paul@388 | 54 | { |
paul@388 | 55 | offset_t nwritten; |
paul@388 | 56 | |
paul@388 | 57 | client_seek(file, 0, SEEK_SET); |
paul@388 | 58 | |
paul@388 | 59 | printf("Writing...\n"); |
paul@388 | 60 | |
paul@388 | 61 | nwritten = client_write(file, buf, strlen(buf)); |
paul@388 | 62 | |
paul@455 | 63 | printf("Wrote " FMToffsetd " bytes: ", nwritten); |
paul@388 | 64 | fwrite(buf, sizeof(char), nwritten, stdout); |
paul@388 | 65 | fputs("\n", stdout); |
paul@388 | 66 | } |
paul@388 | 67 | |
paul@388 | 68 | int main(int argc, char *argv[]) |
paul@388 | 69 | { |
paul@388 | 70 | if (argc < 2) |
paul@388 | 71 | { |
paul@388 | 72 | printf("Need a file to remove.\n"); |
paul@388 | 73 | return 1; |
paul@388 | 74 | } |
paul@388 | 75 | |
paul@388 | 76 | char *filename = argv[1]; |
paul@388 | 77 | |
paul@388 | 78 | printf("Creating %s...\n", filename); |
paul@388 | 79 | |
paul@388 | 80 | file_t *file = client_open(filename, O_WRONLY | O_CREAT); |
paul@388 | 81 | |
paul@388 | 82 | write_to_file(file, "An existing file."); |
paul@388 | 83 | |
paul@388 | 84 | client_close(file); |
paul@388 | 85 | |
paul@388 | 86 | printf("Opening %s...\n", filename); |
paul@388 | 87 | |
paul@388 | 88 | file = client_open(filename, O_RDONLY); |
paul@388 | 89 | |
paul@402 | 90 | if (!client_opened(file)) |
paul@388 | 91 | { |
paul@402 | 92 | client_close(file); |
paul@388 | 93 | printf("Could not open file: %s\n", filename); |
paul@388 | 94 | return 1; |
paul@388 | 95 | } |
paul@388 | 96 | |
paul@388 | 97 | /* Read from the file, then remove the file, then read again. */ |
paul@388 | 98 | |
paul@388 | 99 | read_from_file(file); |
paul@388 | 100 | |
paul@388 | 101 | printf("Removing...\n"); |
paul@388 | 102 | |
paul@388 | 103 | long err = client_remove(filename); |
paul@388 | 104 | |
paul@388 | 105 | if (err) |
paul@388 | 106 | { |
paul@388 | 107 | printf("Could not remove file: %s\n", filename); |
paul@388 | 108 | return 1; |
paul@388 | 109 | } |
paul@388 | 110 | |
paul@388 | 111 | printf("Reading again...\n"); |
paul@388 | 112 | |
paul@388 | 113 | read_from_file(file); |
paul@388 | 114 | |
paul@388 | 115 | /* Open the file again. */ |
paul@388 | 116 | |
paul@388 | 117 | file_t *file_new = client_open(filename, O_RDONLY); |
paul@388 | 118 | |
paul@402 | 119 | if (client_opened(file_new)) |
paul@388 | 120 | { |
paul@388 | 121 | printf("File should be absent: %s\n", filename); |
paul@388 | 122 | return 1; |
paul@388 | 123 | } |
paul@388 | 124 | |
paul@388 | 125 | file_new = client_open(filename, O_RDWR | O_CREAT); |
paul@388 | 126 | |
paul@402 | 127 | if (!client_opened(file_new)) |
paul@388 | 128 | { |
paul@402 | 129 | client_close(file_new); |
paul@388 | 130 | printf("File should be present: %s\n", filename); |
paul@388 | 131 | return 1; |
paul@388 | 132 | } |
paul@388 | 133 | |
paul@388 | 134 | write_to_file(file_new, "New file!"); |
paul@388 | 135 | |
paul@388 | 136 | /* Read from the initial file and new file. */ |
paul@388 | 137 | |
paul@388 | 138 | read_from_file(file); |
paul@388 | 139 | read_from_file(file_new); |
paul@388 | 140 | |
paul@388 | 141 | /* Close the files. */ |
paul@388 | 142 | |
paul@388 | 143 | client_close(file); |
paul@388 | 144 | client_close(file_new); |
paul@388 | 145 | |
paul@402 | 146 | printf("End of test.\n"); |
paul@388 | 147 | return 0; |
paul@388 | 148 | } |
paul@388 | 149 | |
paul@388 | 150 | // vim: tabstop=2 expandtab shiftwidth=2 |