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