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