paul@0 | 1 | /* |
paul@0 | 2 | * Test dataspace operations. |
paul@0 | 3 | * |
paul@507 | 4 | * Copyright (C) 2020, 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/re/env.h> |
paul@0 | 23 | #include <l4/sys/err.h> |
paul@0 | 24 | |
paul@507 | 25 | #include <systypes/env.h> |
paul@85 | 26 | #include <systypes/fcntl.h> |
paul@85 | 27 | |
paul@0 | 28 | #include <stdio.h> |
paul@0 | 29 | #include <string.h> |
paul@0 | 30 | #include <stdlib.h> |
paul@0 | 31 | |
paul@94 | 32 | #include <fsclient/file.h> |
paul@94 | 33 | #include <mem/memory_utils.h> |
paul@7 | 34 | |
paul@0 | 35 | |
paul@0 | 36 | |
paul@12 | 37 | int main(int argc, char *argv[]) |
paul@0 | 38 | { |
paul@12 | 39 | if (argc < 4) |
paul@12 | 40 | { |
paul@12 | 41 | printf("Need filename, step and sample size.\n"); |
paul@12 | 42 | return 1; |
paul@12 | 43 | } |
paul@12 | 44 | |
paul@12 | 45 | /* Obtain filename and access parameters. */ |
paul@12 | 46 | |
paul@12 | 47 | char *filename = argv[1]; |
paul@12 | 48 | unsigned long step = atoi(argv[2]); |
paul@12 | 49 | unsigned long sample = atoi(argv[3]); |
paul@12 | 50 | |
paul@12 | 51 | /* Allocate a buffer for sampling from the file. */ |
paul@12 | 52 | |
paul@12 | 53 | char buf[sample + 1]; |
paul@12 | 54 | |
paul@12 | 55 | /* Obtain access to the filesystem. */ |
paul@12 | 56 | |
paul@507 | 57 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@10 | 58 | |
paul@10 | 59 | /* Invoke the open method to receive the file reference. */ |
paul@10 | 60 | |
paul@34 | 61 | file_t file; |
paul@85 | 62 | long err = file_open(&file, filename, O_RDWR, server); |
paul@10 | 63 | |
paul@10 | 64 | if (err) |
paul@10 | 65 | { |
paul@10 | 66 | printf("Could not obtain file: %s\n", l4sys_errtostr(err)); |
paul@10 | 67 | return 1; |
paul@10 | 68 | } |
paul@10 | 69 | |
paul@34 | 70 | /* A region of the file is mapped. */ |
paul@0 | 71 | |
paul@339 | 72 | err = file_mmap(&file, 0, page(10), 0, 0, file_region_flags(file.flags)); |
paul@7 | 73 | |
paul@7 | 74 | if (err) |
paul@7 | 75 | { |
paul@7 | 76 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@7 | 77 | return 1; |
paul@7 | 78 | } |
paul@7 | 79 | |
paul@45 | 80 | for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) |
paul@0 | 81 | { |
paul@45 | 82 | unsigned long remaining = file_populated_span(&file) - offset; |
paul@12 | 83 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@12 | 84 | |
paul@34 | 85 | printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); |
paul@34 | 86 | strncpy(buf, (file.memory + offset), sample_remaining); |
paul@12 | 87 | buf[sample_remaining] = '\0'; |
paul@7 | 88 | printf("%s\n", buf); |
paul@0 | 89 | } |
paul@0 | 90 | |
paul@68 | 91 | printf("File shown.\n"); |
paul@407 | 92 | printf("End of test.\n"); |
paul@0 | 93 | return 0; |
paul@0 | 94 | } |
paul@34 | 95 | |
paul@34 | 96 | // vim: tabstop=2 expandtab shiftwidth=2 |