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