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 <l4/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <systypes/fcntl.h> 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <stdlib.h> 30 31 #include <fsclient/file.h> 32 #include <mem/memory_utils.h> 33 34 35 36 static void show(file_t *file, unsigned long step, unsigned long sample) 37 { 38 /* Allocate a buffer for sampling from the file. */ 39 40 char buf[sample + 1]; 41 42 for (unsigned long offset = 0; offset < file_populated_span(file); offset += step) 43 { 44 unsigned long remaining = file_populated_span(file) - offset; 45 unsigned long sample_remaining = remaining < sample ? remaining : sample; 46 47 printf("%ld bytes from %p...\n", sample_remaining, (file->memory + offset)); 48 strncpy(buf, (file->memory + offset), sample_remaining); 49 buf[sample_remaining] = '\0'; 50 printf("%s\n", buf); 51 } 52 } 53 54 int main(int argc, char *argv[]) 55 { 56 if (argc < 4) 57 { 58 printf("Need filename, step and sample size.\n"); 59 return 1; 60 } 61 62 /* Obtain filename and access parameters. */ 63 64 char *filename = argv[1]; 65 unsigned long step = atoi(argv[2]); 66 unsigned long sample = atoi(argv[3]); 67 68 /* Obtain access to the filesystem. */ 69 70 l4_cap_idx_t server = l4re_env_get_cap("server"); 71 72 /* Invoke the open method to receive the file reference. */ 73 74 file_t file; 75 long err = file_open(&file, filename, O_RDWR, server); 76 77 if (err) 78 { 79 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 80 return 1; 81 } 82 83 /* A region of the file is mapped. */ 84 85 err = file_mmap(&file, 0, page(10)); 86 87 if (err) 88 { 89 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 90 return 1; 91 } 92 93 show(&file, step, sample); 94 95 /* Resizing must occur before writing beyond the end of file. Otherwise, the 96 data may get discarded if the supporting flexpage needs to be flushed. */ 97 98 offset_t new_region = round(file_populated_span(&file), page(1)); 99 100 printf("Resize to %ld...\n", new_region + file_populated_span(&file)); 101 102 err = file_resize(&file, new_region + file_populated_span(&file)); 103 104 if (err) 105 { 106 printf("Could not resize file: %s\n", l4sys_errtostr(err)); 107 return 1; 108 } 109 110 printf("Resized file...\n"); 111 112 /* Copy the sampled data to another file region. */ 113 114 printf("Copy data to %ld...\n", new_region); 115 116 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) 117 { 118 memcpy(file.memory + new_region + offset, file.memory + offset, sample); 119 if (step > sample) 120 memset(file.memory + new_region + offset + sample, 0, step - sample); 121 } 122 123 show(&file, step, sample); 124 125 printf("File shown.\n"); 126 127 return 0; 128 } 129 130 // vim: tabstop=2 expandtab shiftwidth=2