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