paul@50 | 1 | /* |
paul@50 | 2 | * Test dataspace operations. |
paul@50 | 3 | * |
paul@330 | 4 | * Copyright (C) 2020, 2021, 2022 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@85 | 25 | #include <systypes/fcntl.h> |
paul@85 | 26 | |
paul@50 | 27 | #include <stdio.h> |
paul@50 | 28 | #include <string.h> |
paul@50 | 29 | #include <stdlib.h> |
paul@50 | 30 | |
paul@94 | 31 | #include <fsclient/file.h> |
paul@94 | 32 | #include <mem/memory_utils.h> |
paul@50 | 33 | |
paul@50 | 34 | |
paul@50 | 35 | |
paul@402 | 36 | static void show(file_t *file, offset_t step, offset_t 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@402 | 42 | for (offset_t offset = 0; offset < file_populated_span(file); offset += step) |
paul@55 | 43 | { |
paul@402 | 44 | offset_t remaining = file_populated_span(file) - offset; |
paul@402 | 45 | offset_t sample_remaining = remaining < sample ? remaining : sample; |
paul@55 | 46 | |
paul@55 | 47 | printf("%ld bytes from %p...\n", sample_remaining, (file->memory + offset)); |
paul@332 | 48 | memcpy(buf, (file->memory + offset), sample_remaining); |
paul@55 | 49 | buf[sample_remaining] = '\0'; |
paul@332 | 50 | |
paul@402 | 51 | offset_t leading = 0; |
paul@332 | 52 | char *outbuf = buf; |
paul@332 | 53 | |
paul@402 | 54 | /* Skip leading zero bytes. */ |
paul@402 | 55 | |
paul@332 | 56 | while ((*outbuf == '\0') && (leading < sample_remaining)) |
paul@332 | 57 | { |
paul@332 | 58 | outbuf++; |
paul@332 | 59 | leading++; |
paul@332 | 60 | } |
paul@332 | 61 | |
paul@402 | 62 | offset_t trailing = 0; |
paul@402 | 63 | char *outbuf_end = outbuf + strlen(outbuf), *end = buf + sample_remaining - 1; |
paul@332 | 64 | |
paul@402 | 65 | while ((*end == '\0') && (end >= outbuf_end)) |
paul@402 | 66 | { |
paul@402 | 67 | end--; |
paul@402 | 68 | trailing++; |
paul@402 | 69 | } |
paul@332 | 70 | |
paul@402 | 71 | printf("----\n%s\n----\n", outbuf); |
paul@402 | 72 | |
paul@402 | 73 | printf("[%ld zero bytes at start]\n", leading); |
paul@402 | 74 | printf("[%ld bytes shown]\n", strlen(outbuf)); |
paul@332 | 75 | printf("[%ld bytes after string]\n", sample_remaining - leading - strlen(outbuf)); |
paul@402 | 76 | printf("[%ld zero bytes at end]\n", trailing); |
paul@55 | 77 | } |
paul@55 | 78 | } |
paul@55 | 79 | |
paul@50 | 80 | int main(int argc, char *argv[]) |
paul@50 | 81 | { |
paul@50 | 82 | if (argc < 4) |
paul@50 | 83 | { |
paul@50 | 84 | printf("Need filename, step and sample size.\n"); |
paul@50 | 85 | return 1; |
paul@50 | 86 | } |
paul@50 | 87 | |
paul@50 | 88 | /* Obtain filename and access parameters. */ |
paul@50 | 89 | |
paul@50 | 90 | char *filename = argv[1]; |
paul@402 | 91 | offset_t step = atoi(argv[2]); |
paul@402 | 92 | offset_t sample = atoi(argv[3]); |
paul@50 | 93 | |
paul@50 | 94 | /* Obtain access to the filesystem. */ |
paul@50 | 95 | |
paul@50 | 96 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@50 | 97 | |
paul@50 | 98 | /* Invoke the open method to receive the file reference. */ |
paul@50 | 99 | |
paul@50 | 100 | file_t file; |
paul@85 | 101 | long err = file_open(&file, filename, O_RDWR, server); |
paul@50 | 102 | |
paul@50 | 103 | if (err) |
paul@50 | 104 | { |
paul@50 | 105 | printf("Could not obtain file: %s\n", l4sys_errtostr(err)); |
paul@50 | 106 | return 1; |
paul@50 | 107 | } |
paul@50 | 108 | |
paul@402 | 109 | offset_t original_size = file.size; |
paul@50 | 110 | |
paul@402 | 111 | /* A region of the file is mapped. Here, the start and length is not provided |
paul@402 | 112 | as page-aligned offsets, but the region is nevertheless not masked, and the |
paul@402 | 113 | region limits will be page-aligned in any case. */ |
paul@402 | 114 | |
paul@402 | 115 | err = file_mmap(&file, 10, original_size - 20, 0, 0, file_region_flags(file.flags)); |
paul@50 | 116 | |
paul@50 | 117 | if (err) |
paul@50 | 118 | { |
paul@50 | 119 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@50 | 120 | return 1; |
paul@50 | 121 | } |
paul@50 | 122 | |
paul@402 | 123 | printf("Region %ld...%ld populated size %ld size %ld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); |
paul@332 | 124 | printf("File contents:\n"); |
paul@332 | 125 | |
paul@55 | 126 | show(&file, step, sample); |
paul@55 | 127 | |
paul@332 | 128 | printf("File shown.\n"); |
paul@402 | 129 | printf("Expected...\n" |
paul@402 | 130 | "[0 zero bytes at start]\n" |
paul@402 | 131 | "[%ld bytes shown]\n" |
paul@402 | 132 | "[0 bytes after string]\n" |
paul@402 | 133 | "[0 zero bytes at end]\n\n", |
paul@402 | 134 | original_size); |
paul@332 | 135 | |
paul@332 | 136 | /* A region of the file is mapped. Here, the resulting offsets will be used to |
paul@332 | 137 | define a masked region. */ |
paul@332 | 138 | |
paul@402 | 139 | err = file_mmap(&file, 10, original_size - 20, 10, original_size - 10, file_region_flags(file.flags)); |
paul@332 | 140 | |
paul@332 | 141 | if (err) |
paul@332 | 142 | { |
paul@332 | 143 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@332 | 144 | return 1; |
paul@332 | 145 | } |
paul@332 | 146 | |
paul@402 | 147 | printf("Region %ld...%ld populated size %ld size %ld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); |
paul@332 | 148 | printf("File contents:\n"); |
paul@332 | 149 | |
paul@332 | 150 | show(&file, step, sample); |
paul@332 | 151 | |
paul@332 | 152 | printf("File shown.\n"); |
paul@402 | 153 | printf("Expected...\n" |
paul@402 | 154 | "[10 zero bytes at start]\n" |
paul@402 | 155 | "[%ld bytes shown]\n" |
paul@402 | 156 | "[%ld bytes after string]\n" |
paul@402 | 157 | "[%ld zero bytes at end]\n\n", |
paul@402 | 158 | original_size - 20, 10UL, 10UL); |
paul@332 | 159 | |
paul@55 | 160 | /* Resizing must occur before writing beyond the end of file. Otherwise, the |
paul@55 | 161 | data may get discarded if the supporting flexpage needs to be flushed. */ |
paul@55 | 162 | |
paul@402 | 163 | offset_t old_populated_size = file_populated_span(&file); |
paul@402 | 164 | offset_t old_size = file.size; |
paul@402 | 165 | offset_t old_populated_size_rounded = round(old_populated_size, page(1)); |
paul@402 | 166 | offset_t new_size = old_populated_size_rounded + old_populated_size; |
paul@402 | 167 | offset_t new_region = round(new_size, page(1)); |
paul@55 | 168 | |
paul@332 | 169 | printf("Resize to %ld...\n", new_size); |
paul@55 | 170 | |
paul@332 | 171 | err = file_resize(&file, new_size); |
paul@55 | 172 | |
paul@55 | 173 | if (err) |
paul@55 | 174 | { |
paul@55 | 175 | printf("Could not resize file: %s\n", l4sys_errtostr(err)); |
paul@55 | 176 | return 1; |
paul@55 | 177 | } |
paul@55 | 178 | |
paul@55 | 179 | printf("Resized file...\n"); |
paul@402 | 180 | printf("Region %ld...%ld populated size %ld size %ld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); |
paul@402 | 181 | printf("Expected %ld...%ld populated size %ld size %ld.\n\n", 0UL, old_populated_size_rounded, old_populated_size_rounded, new_size); |
paul@55 | 182 | |
paul@332 | 183 | /* Re-map to avoid masking the region. */ |
paul@332 | 184 | |
paul@402 | 185 | err = file_mmap(&file, 0, new_size, 0, 0, file_region_flags(file.flags)); |
paul@332 | 186 | |
paul@332 | 187 | if (err) |
paul@332 | 188 | { |
paul@332 | 189 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@332 | 190 | return 1; |
paul@332 | 191 | } |
paul@332 | 192 | |
paul@402 | 193 | printf("Remapped file without masking...\n"); |
paul@402 | 194 | printf("Region %ld...%ld populated size %ld size %ld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); |
paul@402 | 195 | printf("Expected %ld...%ld populated size %ld size %ld.\n\n", 0UL, new_region, old_populated_size, old_size); |
paul@55 | 196 | |
paul@402 | 197 | printf("Resize to %ld...\n", new_size); |
paul@55 | 198 | |
paul@402 | 199 | err = file_resize(&file, new_size); |
paul@402 | 200 | |
paul@402 | 201 | if (err) |
paul@50 | 202 | { |
paul@402 | 203 | printf("Could not resize file: %s\n", l4sys_errtostr(err)); |
paul@402 | 204 | return 1; |
paul@55 | 205 | } |
paul@50 | 206 | |
paul@402 | 207 | printf("Resized file...\n"); |
paul@402 | 208 | printf("Region %ld...%ld populated size %ld size %ld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); |
paul@402 | 209 | printf("Expected %ld...%ld populated size %ld size %ld.\n\n", 0UL, round(new_size, page(1)), new_size, new_size); |
paul@402 | 210 | |
paul@402 | 211 | /* Copy the file data to another file region. */ |
paul@402 | 212 | |
paul@402 | 213 | printf("Copy data to %ld...\n", old_populated_size_rounded); |
paul@402 | 214 | |
paul@402 | 215 | memcpy(file.memory + old_populated_size_rounded, file.memory, old_size); |
paul@402 | 216 | |
paul@402 | 217 | printf("Region %ld...%ld populated size %ld size %ld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); |
paul@332 | 218 | printf("File contents:\n"); |
paul@332 | 219 | |
paul@332 | 220 | show(&file, step, sample); |
paul@332 | 221 | |
paul@332 | 222 | printf("File shown.\n"); |
paul@402 | 223 | printf("Expected...\n" |
paul@402 | 224 | "[0 zero bytes at start]\n" |
paul@402 | 225 | "[%ld bytes shown]\n" |
paul@402 | 226 | "[%ld bytes after string]\n" |
paul@402 | 227 | "[%ld zero bytes at end]\n" |
paul@402 | 228 | "...\n" |
paul@402 | 229 | "[0 zero bytes at start]\n" |
paul@402 | 230 | "[%ld bytes shown]\n" |
paul@402 | 231 | "[0 bytes after string]\n" |
paul@402 | 232 | "[0 zero bytes at end]\n\n", |
paul@402 | 233 | original_size, sample - original_size, sample - original_size, |
paul@402 | 234 | original_size); |
paul@332 | 235 | |
paul@332 | 236 | /* Re-map to mask the region again. */ |
paul@332 | 237 | |
paul@402 | 238 | err = file_mmap(&file, 0, new_size, 10, new_size - 10, file_region_flags(file.flags)); |
paul@332 | 239 | |
paul@332 | 240 | if (err) |
paul@332 | 241 | { |
paul@332 | 242 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@332 | 243 | return 1; |
paul@332 | 244 | } |
paul@332 | 245 | |
paul@402 | 246 | printf("Region %ld...%ld populated size %ld size %ld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); |
paul@332 | 247 | printf("File contents:\n"); |
paul@332 | 248 | |
paul@55 | 249 | show(&file, step, sample); |
paul@55 | 250 | |
paul@55 | 251 | printf("File shown.\n"); |
paul@402 | 252 | printf("Expected...\n" |
paul@402 | 253 | "[10 zero bytes at start]\n" |
paul@402 | 254 | "[%ld bytes shown]\n" |
paul@402 | 255 | "[%ld bytes after string]\n" |
paul@402 | 256 | "[%ld zero bytes at end]\n" |
paul@402 | 257 | "...\n" |
paul@402 | 258 | "[0 zero bytes at start]\n" |
paul@402 | 259 | "[%ld bytes shown]\n" |
paul@402 | 260 | "[10 bytes after string]\n" |
paul@402 | 261 | "[10 zero bytes at end]\n", |
paul@402 | 262 | original_size - 10, sample - original_size, sample - original_size, |
paul@402 | 263 | original_size - 10); |
paul@50 | 264 | |
paul@402 | 265 | printf("End of test.\n"); |
paul@50 | 266 | return 0; |
paul@50 | 267 | } |
paul@50 | 268 | |
paul@50 | 269 | // vim: tabstop=2 expandtab shiftwidth=2 |