1 /* 2 * Test dataspace operations. 3 * 4 * Copyright (C) 2020, 2021 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 <systypes/fcntl.h> 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include <fsclient/client.h> 29 30 31 32 int main(int argc, char *argv[]) 33 { 34 if (argc < 2) 35 { 36 printf("Need a filename, optional user identifier, and optional repetition.\n"); 37 return 1; 38 } 39 40 /* Obtain filename and access parameters. */ 41 42 char *filename = argv[1]; 43 bool have_uid = (argc > 2) && strlen(argv[2]); 44 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 45 int repetition = argc > 3 ? atoi(argv[3]) : 10; 46 file_t *file; 47 48 /* With a user, open a user-specific file opener. */ 49 50 if (have_uid) 51 { 52 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 53 54 if (l4_is_invalid_cap(opener)) 55 { 56 printf("Could not obtain opener for file.\n"); 57 return 1; 58 } 59 60 /* Invoke the open method to receive the file reference. */ 61 62 file = client_open_using(filename, O_RDWR, opener); 63 } 64 else 65 file = client_open(filename, O_RDWR); 66 67 if (file == NULL) 68 { 69 printf("Could not obtain file.\n"); 70 return 1; 71 } 72 73 /* Copy the sampled data to another file region. */ 74 75 offset_t size = file->size; 76 char buffer[size]; 77 offset_t nread = client_read(file, buffer, size); 78 79 if (nread != size) 80 { 81 printf("Could not read entire file: %ld out of %ld bytes.\n", nread, size); 82 return 1; 83 } 84 85 printf("File contents...\n"); 86 87 fwrite(buffer, sizeof(char), nread, stdout); 88 89 printf("Copy %ld bytes to end...\n", nread); 90 91 for (int times = 0; times < repetition; times++) 92 { 93 printf("Copy #%d...\n", times); 94 95 offset_t nwritten = client_write(file, buffer, nread); 96 97 if (nwritten != nread) 98 { 99 printf("Could not write file section: %ld instead of %ld bytes.\n", 100 nwritten, nread); 101 return 1; 102 } 103 } 104 105 printf("File is now %ld in size.\n", file->size); 106 107 printf("Seek to start...\n"); 108 109 client_seek(file, 0, SEEK_SET); 110 111 printf("At mapped region from %ld to %ld with data at %ld to %ld.\n", 112 file->start_pos, file->end_pos, file->data_current, file->data_end); 113 114 printf("File contents...\n"); 115 116 offset_t position = 0; 117 118 while (position < file->size) 119 { 120 offset_t nread = client_read(file, buffer, size); 121 122 if ((nread != size) && (nread != file->size - position)) 123 { 124 printf("Could not read file section: %ld instead of %ld or remaining %ld bytes.\n", 125 nread, size, file->size - position); 126 return 1; 127 } 128 129 fwrite(buffer, sizeof(char), nread, stdout); 130 position += nread; 131 } 132 133 printf("File shown.\n"); 134 135 return 0; 136 } 137 138 // vim: tabstop=2 expandtab shiftwidth=2