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 (file == NULL) 49 { 50 printf("Could not obtain file.\n"); 51 return 1; 52 } 53 54 /* Copy the sampled data to another file region. */ 55 56 offset_t size = file->size; 57 char buffer[size]; 58 offset_t nread = client_read(file, buffer, size); 59 60 if (nread != size) 61 { 62 printf("Could not read entire file: %ld out of %ld bytes.\n", nread, size); 63 return 1; 64 } 65 66 printf("File contents...\n"); 67 68 fwrite(buffer, sizeof(char), nread, stdout); 69 70 printf("Copy %ld bytes to end...\n", nread); 71 72 for (int times = 0; times < repetition; times++) 73 { 74 printf("Copy #%d...\n", times); 75 76 offset_t nwritten = client_write(file, buffer, nread); 77 78 if (nwritten != nread) 79 { 80 printf("Could not write file section: %ld instead of %ld bytes.\n", 81 nwritten, nread); 82 return 1; 83 } 84 } 85 86 printf("File is now %ld in size.\n", file->size); 87 88 printf("Seek to start...\n"); 89 90 client_seek(file, 0, SEEK_SET); 91 92 printf("At mapped region from %ld to %ld with data at %ld to %ld.\n", 93 file->start_pos, file->end_pos, file->data_current, file->data_end); 94 95 printf("File contents...\n"); 96 97 offset_t position = 0; 98 99 while (position < file->size) 100 { 101 offset_t nread = client_read(file, buffer, size); 102 103 if ((nread != size) && (nread != file->size - position)) 104 { 105 printf("Could not read file section: %ld instead of %ld or remaining %ld bytes.\n", 106 nread, size, file->size - position); 107 return 1; 108 } 109 110 fwrite(buffer, sizeof(char), nread, stdout); 111 position += nread; 112 } 113 114 printf("File shown.\n"); 115 116 return 0; 117 } 118 119 // vim: tabstop=2 expandtab shiftwidth=2