1 /* 2 * Test dataspace operations. 3 * 4 * Copyright (C) 2020, 2021, 2022, 2023 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 <l4/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <systypes/env.h> 26 #include <systypes/fcntl.h> 27 28 #include <stdio.h> 29 #include <string.h> 30 #include <stdlib.h> 31 32 #include <fsclient/file.h> 33 #include <mem/memory_utils.h> 34 35 36 37 int main(int argc, char *argv[]) 38 { 39 if (argc < 4) 40 { 41 printf("Need filename, step and sample size.\n"); 42 return 1; 43 } 44 45 /* Obtain filename and access parameters. */ 46 47 char *filename = argv[1]; 48 unsigned long step = atoi(argv[2]); 49 unsigned long sample = atoi(argv[3]); 50 51 /* Allocate a buffer for sampling from the file. */ 52 53 char buf[sample + 1]; 54 55 /* Obtain access to the filesystem. */ 56 57 l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); 58 59 /* Invoke the open method to receive the file reference. */ 60 61 file_t file; 62 long err = file_open(&file, filename, O_RDWR, server); 63 64 if (err) 65 { 66 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 67 return 1; 68 } 69 70 /* A region of the file is mapped. */ 71 72 err = file_mmap(&file, 0, page(10), 0, 0, file_region_flags(file.flags)); 73 74 if (err) 75 { 76 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 77 return 1; 78 } 79 80 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) 81 { 82 unsigned long remaining = file_populated_span(&file) - offset; 83 unsigned long sample_remaining = remaining < sample ? remaining : sample; 84 85 printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); 86 strncpy(buf, (file.memory + offset), sample_remaining); 87 buf[sample_remaining] = '\0'; 88 printf("%s\n", buf); 89 } 90 91 printf("File shown.\n"); 92 printf("End of test.\n"); 93 return 0; 94 } 95 96 // vim: tabstop=2 expandtab shiftwidth=2