paul@50 | 1 | /* |
paul@50 | 2 | * Test dataspace operations. |
paul@50 | 3 | * |
paul@50 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@50 | 5 | * |
paul@50 | 6 | * This program is free software; you can redistribute it and/or |
paul@50 | 7 | * modify it under the terms of the GNU General Public License as |
paul@50 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@50 | 9 | * the License, or (at your option) any later version. |
paul@50 | 10 | * |
paul@50 | 11 | * This program is distributed in the hope that it will be useful, |
paul@50 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@50 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@50 | 14 | * GNU General Public License for more details. |
paul@50 | 15 | * |
paul@50 | 16 | * You should have received a copy of the GNU General Public License |
paul@50 | 17 | * along with this program; if not, write to the Free Software |
paul@50 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@50 | 19 | * Boston, MA 02110-1301, USA |
paul@50 | 20 | */ |
paul@50 | 21 | |
paul@50 | 22 | #include <l4/re/env.h> |
paul@50 | 23 | #include <l4/sys/err.h> |
paul@50 | 24 | |
paul@50 | 25 | #include <stdio.h> |
paul@50 | 26 | #include <string.h> |
paul@50 | 27 | #include <stdlib.h> |
paul@50 | 28 | |
paul@50 | 29 | #include <ipc/thread.h> |
paul@50 | 30 | |
paul@50 | 31 | #include "file.h" |
paul@50 | 32 | #include "memory_utils.h" |
paul@50 | 33 | |
paul@50 | 34 | |
paul@50 | 35 | |
paul@55 | 36 | static void show(file_t *file, unsigned long step, unsigned long sample) |
paul@55 | 37 | { |
paul@55 | 38 | /* Allocate a buffer for sampling from the file. */ |
paul@55 | 39 | |
paul@55 | 40 | char buf[sample + 1]; |
paul@55 | 41 | |
paul@55 | 42 | for (unsigned long offset = 0; offset < file_populated_span(file); offset += step) |
paul@55 | 43 | { |
paul@55 | 44 | unsigned long remaining = file_populated_span(file) - offset; |
paul@55 | 45 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@55 | 46 | |
paul@55 | 47 | printf("%ld bytes from %p...\n", sample_remaining, (file->memory + offset)); |
paul@55 | 48 | strncpy(buf, (file->memory + offset), sample_remaining); |
paul@55 | 49 | buf[sample_remaining] = '\0'; |
paul@55 | 50 | printf("%s\n", buf); |
paul@55 | 51 | } |
paul@55 | 52 | } |
paul@55 | 53 | |
paul@50 | 54 | int main(int argc, char *argv[]) |
paul@50 | 55 | { |
paul@50 | 56 | if (argc < 4) |
paul@50 | 57 | { |
paul@50 | 58 | printf("Need filename, step and sample size.\n"); |
paul@50 | 59 | return 1; |
paul@50 | 60 | } |
paul@50 | 61 | |
paul@50 | 62 | /* Obtain filename and access parameters. */ |
paul@50 | 63 | |
paul@50 | 64 | char *filename = argv[1]; |
paul@50 | 65 | unsigned long step = atoi(argv[2]); |
paul@50 | 66 | unsigned long sample = atoi(argv[3]); |
paul@50 | 67 | |
paul@50 | 68 | /* Obtain access to the filesystem. */ |
paul@50 | 69 | |
paul@50 | 70 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@50 | 71 | |
paul@50 | 72 | /* Invoke the open method to receive the file reference. */ |
paul@50 | 73 | |
paul@50 | 74 | file_t file; |
paul@50 | 75 | long err = file_open(&file, filename, server); |
paul@50 | 76 | |
paul@50 | 77 | if (err) |
paul@50 | 78 | { |
paul@50 | 79 | printf("Could not obtain file: %s\n", l4sys_errtostr(err)); |
paul@50 | 80 | return 1; |
paul@50 | 81 | } |
paul@50 | 82 | |
paul@50 | 83 | /* A region of the file is mapped. */ |
paul@50 | 84 | |
paul@50 | 85 | err = file_mmap(&file, 0, page(10)); |
paul@50 | 86 | |
paul@50 | 87 | if (err) |
paul@50 | 88 | { |
paul@50 | 89 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@50 | 90 | return 1; |
paul@50 | 91 | } |
paul@50 | 92 | |
paul@55 | 93 | show(&file, step, sample); |
paul@55 | 94 | |
paul@55 | 95 | /* Resizing must occur before writing beyond the end of file. Otherwise, the |
paul@55 | 96 | data may get discarded if the supporting flexpage needs to be flushed. */ |
paul@55 | 97 | |
paul@55 | 98 | offset_t new_region = round(file_populated_span(&file), page(1)); |
paul@55 | 99 | |
paul@55 | 100 | printf("Resize to %ld...\n", new_region + file_populated_span(&file)); |
paul@55 | 101 | |
paul@55 | 102 | err = file_resize(&file, new_region + file_populated_span(&file)); |
paul@55 | 103 | |
paul@55 | 104 | if (err) |
paul@55 | 105 | { |
paul@55 | 106 | printf("Could not resize file: %s\n", l4sys_errtostr(err)); |
paul@55 | 107 | return 1; |
paul@55 | 108 | } |
paul@55 | 109 | |
paul@55 | 110 | printf("Resized file...\n"); |
paul@55 | 111 | |
paul@55 | 112 | /* Copy the sampled data to another file region. */ |
paul@55 | 113 | |
paul@55 | 114 | printf("Copy data to %ld...\n", new_region); |
paul@55 | 115 | |
paul@50 | 116 | for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) |
paul@50 | 117 | { |
paul@55 | 118 | memcpy(file.memory + new_region + offset, file.memory + offset, sample); |
paul@55 | 119 | if (step > sample) |
paul@55 | 120 | memset(file.memory + new_region + offset + sample, 0, step - sample); |
paul@55 | 121 | } |
paul@50 | 122 | |
paul@55 | 123 | show(&file, step, sample); |
paul@55 | 124 | |
paul@55 | 125 | printf("File shown.\n"); |
paul@50 | 126 | |
paul@50 | 127 | return 0; |
paul@50 | 128 | } |
paul@50 | 129 | |
paul@50 | 130 | // vim: tabstop=2 expandtab shiftwidth=2 |