paul@141 | 1 | /* |
paul@141 | 2 | * Test file operations. |
paul@141 | 3 | * |
paul@479 | 4 | * Copyright (C) 2020, 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@141 | 5 | * |
paul@141 | 6 | * This program is free software; you can redistribute it and/or |
paul@141 | 7 | * modify it under the terms of the GNU General Public License as |
paul@141 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@141 | 9 | * the License, or (at your option) any later version. |
paul@141 | 10 | * |
paul@141 | 11 | * This program is distributed in the hope that it will be useful, |
paul@141 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@141 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@141 | 14 | * GNU General Public License for more details. |
paul@141 | 15 | * |
paul@141 | 16 | * You should have received a copy of the GNU General Public License |
paul@141 | 17 | * along with this program; if not, write to the Free Software |
paul@141 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@141 | 19 | * Boston, MA 02110-1301, USA |
paul@141 | 20 | */ |
paul@141 | 21 | |
paul@141 | 22 | #include <l4/re/env.h> |
paul@141 | 23 | #include <l4/sys/err.h> |
paul@141 | 24 | |
paul@141 | 25 | #include <thread> |
paul@141 | 26 | |
paul@141 | 27 | #include <stdio.h> |
paul@141 | 28 | #include <string.h> |
paul@141 | 29 | #include <stdlib.h> |
paul@141 | 30 | |
paul@141 | 31 | #include <fsclient/client.h> |
paul@141 | 32 | #include <systypes/fcntl.h> |
paul@455 | 33 | #include <systypes/format.h> |
paul@455 | 34 | |
paul@455 | 35 | #define FMToffsetd "%" pFMToffset "d" |
paul@141 | 36 | |
paul@141 | 37 | |
paul@141 | 38 | |
paul@141 | 39 | /* Write data to the file. */ |
paul@141 | 40 | |
paul@141 | 41 | static void write_data(file_t *file, int region) |
paul@141 | 42 | { |
paul@141 | 43 | offset_t size = 600; |
paul@141 | 44 | char buffer[size]; |
paul@141 | 45 | |
paul@141 | 46 | memset(buffer, (int) 'a' + region, size); |
paul@141 | 47 | |
paul@141 | 48 | offset_t nwritten = client_write(file, buffer, size); |
paul@141 | 49 | |
paul@455 | 50 | printf("Written " FMToffsetd "/" FMToffsetd " in #%d to file with first %c, last %c...\n", nwritten, size, region, *buffer, *(buffer + nwritten - 1)); |
paul@141 | 51 | |
paul@141 | 52 | /* Flush to make the output available. */ |
paul@141 | 53 | |
paul@141 | 54 | client_flush(file); |
paul@141 | 55 | } |
paul@141 | 56 | |
paul@141 | 57 | /* Read and write data to the file. */ |
paul@141 | 58 | |
paul@141 | 59 | static void read_and_write(file_t *file, bool creator) |
paul@141 | 60 | { |
paul@141 | 61 | offset_t size = 600, total = 0; |
paul@141 | 62 | long err; |
paul@141 | 63 | int region = 0; |
paul@141 | 64 | |
paul@141 | 65 | /* Register for notifications. */ |
paul@141 | 66 | |
paul@180 | 67 | if ((err = client_subscribe(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED, client_notifier_task()))) |
paul@141 | 68 | { |
paul@141 | 69 | printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); |
paul@141 | 70 | return; |
paul@141 | 71 | } |
paul@141 | 72 | |
paul@141 | 73 | /* Seek to the end of the file, if this is not actually a new file. */ |
paul@141 | 74 | |
paul@141 | 75 | client_seek(file, 0, SEEK_END); |
paul@141 | 76 | |
paul@455 | 77 | printf("File %s at position " FMToffsetd "...\n", creator ? "creator" : "follower", client_tell(file)); |
paul@141 | 78 | |
paul@141 | 79 | /* Write initial data as the file's creator. */ |
paul@141 | 80 | |
paul@141 | 81 | if (creator) |
paul@141 | 82 | write_data(file, region++); |
paul@141 | 83 | |
paul@141 | 84 | while (region < 26) |
paul@141 | 85 | { |
paul@141 | 86 | char buffer[size]; |
paul@141 | 87 | offset_t nread; |
paul@141 | 88 | |
paul@141 | 89 | /* Wait for notification of content. */ |
paul@141 | 90 | |
paul@180 | 91 | err = client_wait_file(file, client_notifier_task()); |
paul@141 | 92 | |
paul@141 | 93 | if (err) |
paul@141 | 94 | { |
paul@141 | 95 | printf("Error waiting for notifications: %s\n", l4sys_errtostr(err)); |
paul@141 | 96 | return; |
paul@141 | 97 | } |
paul@141 | 98 | |
paul@141 | 99 | printf("File %s notified with conditions:%s%s\n", creator ? "creator" : "follower", |
paul@479 | 100 | file_notifications(file) & NOTIFY_PEER_CLOSED ? " closed" : "", |
paul@479 | 101 | file_notifications(file) & NOTIFY_CONTENT_AVAILABLE ? " content" : ""); |
paul@141 | 102 | |
paul@141 | 103 | /* Attempt to read. */ |
paul@141 | 104 | |
paul@141 | 105 | nread = client_read(file, buffer, size); |
paul@141 | 106 | |
paul@141 | 107 | while (nread) |
paul@141 | 108 | { |
paul@141 | 109 | total += nread; |
paul@141 | 110 | |
paul@455 | 111 | printf("Read as %s " FMToffsetd "/" FMToffsetd ", total " FMToffsetd ", first %c, last %c, from file...\n", creator ? "creator" : "follower", |
paul@141 | 112 | nread, size, total, *buffer, *(buffer + nread - 1)); |
paul@141 | 113 | |
paul@141 | 114 | if (!(*(buffer + nread - 1))) |
paul@425 | 115 | printf("Warning: length before zero region is %zd\n", strlen(buffer)); |
paul@141 | 116 | |
paul@141 | 117 | nread = client_read(file, buffer, size); |
paul@141 | 118 | } |
paul@141 | 119 | |
paul@141 | 120 | /* Without any more content, a peer closed event should terminate reading |
paul@402 | 121 | from the file. */ |
paul@141 | 122 | |
paul@479 | 123 | if (file_notifications(file) & NOTIFY_PEER_CLOSED) |
paul@141 | 124 | break; |
paul@141 | 125 | |
paul@402 | 126 | /* Otherwise, write some data to the file for the other party. */ |
paul@402 | 127 | |
paul@141 | 128 | write_data(file, region++); |
paul@141 | 129 | } |
paul@141 | 130 | |
paul@402 | 131 | printf("Data written: %s\n", region == 26 ? "True" : "False"); |
paul@141 | 132 | } |
paul@141 | 133 | |
paul@141 | 134 | int main(int argc, char *argv[]) |
paul@141 | 135 | { |
paul@141 | 136 | if (argc < 2) |
paul@141 | 137 | { |
paul@237 | 138 | printf("Need a filename.\n"); |
paul@141 | 139 | return 1; |
paul@141 | 140 | } |
paul@141 | 141 | |
paul@141 | 142 | char *filename = argv[1]; |
paul@147 | 143 | file_t *file1, *file2; |
paul@141 | 144 | |
paul@237 | 145 | file1 = client_open(filename, O_RDWR | O_CREAT); |
paul@237 | 146 | file2 = client_open(filename, O_RDWR); |
paul@141 | 147 | |
paul@402 | 148 | if (!client_opened(file1) || !client_opened(file2)) |
paul@141 | 149 | { |
paul@402 | 150 | client_close(file1); |
paul@402 | 151 | client_close(file2); |
paul@141 | 152 | printf("Could not obtain files.\n"); |
paul@141 | 153 | return 1; |
paul@141 | 154 | } |
paul@141 | 155 | |
paul@141 | 156 | /* Schedule threads. */ |
paul@141 | 157 | |
paul@141 | 158 | std::thread *activities[2]; |
paul@141 | 159 | |
paul@141 | 160 | activities[0] = new std::thread(read_and_write, file1, true); |
paul@141 | 161 | activities[1] = new std::thread(read_and_write, file2, false); |
paul@141 | 162 | |
paul@141 | 163 | for (int i = 0; i < 2; i++) |
paul@141 | 164 | activities[i]->join(); |
paul@402 | 165 | |
paul@402 | 166 | printf("End of test.\n"); |
paul@141 | 167 | } |
paul@141 | 168 | |
paul@141 | 169 | // vim: tabstop=2 expandtab shiftwidth=2 |