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