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