1 /* 2 * Test dataspace 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 <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 and optional repetition.\n"); 37 return 1; 38 } 39 40 /* Obtain filename and access parameters. */ 41 42 char *filename = argv[1]; 43 int repetition = argc > 2 ? atoi(argv[2]) : 10; 44 file_t *file; 45 46 file = client_open(filename, O_RDWR); 47 48 if (!client_opened(file)) 49 { 50 client_close(file); 51 printf("Could not obtain file.\n"); 52 return 1; 53 } 54 55 /* Copy the sampled data to another file region. */ 56 57 offset_t size = file->size; 58 offset_t new_size = size * (repetition + 1); 59 60 char buffer[size]; 61 offset_t nread = client_read(file, buffer, size); 62 63 if (nread != size) 64 { 65 printf("Could not read entire file: %ld out of %ld bytes.\n", nread, size); 66 return 1; 67 } 68 69 printf("File contents...\n"); 70 71 fwrite(buffer, sizeof(char), nread, stdout); 72 73 printf("Copy %ld bytes to end...\n", nread); 74 75 for (int times = 0; times < repetition; times++) 76 { 77 printf("Copy #%d...\n", times); 78 79 offset_t nwritten = client_write(file, buffer, nread); 80 81 if (nwritten != nread) 82 { 83 printf("Could not write file section: %ld instead of %ld bytes.\n", 84 nwritten, nread); 85 return 1; 86 } 87 } 88 89 printf("File now has size %ld.\n", file->size); 90 printf("File has expected size %ld: %s\n", new_size, new_size == file->size ? "True" : "False"); 91 92 printf("Seek to start...\n"); 93 94 client_seek(file, 0, SEEK_SET); 95 96 printf("At mapped region from %ld to %ld with data at %ld to %ld.\n", 97 file->start_pos, file->end_pos, file->data_current, file->data_end); 98 99 printf("File contents...\n"); 100 101 offset_t position = 0; 102 103 while ((nread = client_read(file, buffer, size)) > 0) 104 { 105 if ((nread != size) && (nread != file->size - position)) 106 { 107 printf("Could not read file section: %ld instead of %ld or remaining %ld bytes.\n", 108 nread, size, file->size - position); 109 return 1; 110 } 111 112 fwrite(buffer, sizeof(char), nread, stdout); 113 position += nread; 114 } 115 116 printf("File shown in its entirety: %s\n", position == file->size ? "True" : "False"); 117 printf("End of test.\n"); 118 return 0; 119 } 120 121 // vim: tabstop=2 expandtab shiftwidth=2