paul@0 | 1 | /* |
paul@0 | 2 | * Test dataspace operations. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) 2020, 2021 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@0 | 25 | #include <stdio.h> |
paul@0 | 26 | #include <string.h> |
paul@0 | 27 | #include <stdlib.h> |
paul@0 | 28 | |
paul@34 | 29 | #include <ipc/thread.h> |
paul@0 | 30 | |
paul@34 | 31 | #include "file.h" |
paul@7 | 32 | #include "memory_utils.h" |
paul@7 | 33 | |
paul@0 | 34 | |
paul@0 | 35 | |
paul@12 | 36 | int main(int argc, char *argv[]) |
paul@0 | 37 | { |
paul@12 | 38 | if (argc < 4) |
paul@12 | 39 | { |
paul@12 | 40 | printf("Need filename, step and sample size.\n"); |
paul@12 | 41 | return 1; |
paul@12 | 42 | } |
paul@12 | 43 | |
paul@12 | 44 | /* Obtain filename and access parameters. */ |
paul@12 | 45 | |
paul@12 | 46 | char *filename = argv[1]; |
paul@12 | 47 | unsigned long step = atoi(argv[2]); |
paul@12 | 48 | unsigned long sample = atoi(argv[3]); |
paul@12 | 49 | |
paul@12 | 50 | /* Allocate a buffer for sampling from the file. */ |
paul@12 | 51 | |
paul@12 | 52 | char buf[sample + 1]; |
paul@12 | 53 | |
paul@12 | 54 | /* Obtain access to the filesystem. */ |
paul@12 | 55 | |
paul@0 | 56 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@34 | 57 | file_t context; |
paul@0 | 58 | |
paul@34 | 59 | long err = file_context(&context, server); |
paul@10 | 60 | |
paul@10 | 61 | if (err) |
paul@10 | 62 | { |
paul@10 | 63 | printf("Could not obtain context: %s\n", l4sys_errtostr(err)); |
paul@10 | 64 | return 1; |
paul@10 | 65 | } |
paul@10 | 66 | |
paul@10 | 67 | /* Write the filename. */ |
paul@10 | 68 | |
paul@34 | 69 | strcpy(context.memory, filename); |
paul@10 | 70 | |
paul@10 | 71 | /* Invoke the open method to receive the file reference. */ |
paul@10 | 72 | |
paul@34 | 73 | file_t file; |
paul@10 | 74 | |
paul@35 | 75 | err = file_open(&file, &context); |
paul@10 | 76 | |
paul@10 | 77 | if (err) |
paul@10 | 78 | { |
paul@10 | 79 | printf("Could not obtain file: %s\n", l4sys_errtostr(err)); |
paul@10 | 80 | return 1; |
paul@10 | 81 | } |
paul@10 | 82 | |
paul@34 | 83 | /* A region of the file is mapped. */ |
paul@0 | 84 | |
paul@34 | 85 | err = file_mmap(&file, 0, page(10)); |
paul@7 | 86 | |
paul@7 | 87 | if (err) |
paul@7 | 88 | { |
paul@7 | 89 | printf("Could not map file region: %s\n", l4sys_errtostr(err)); |
paul@7 | 90 | return 1; |
paul@7 | 91 | } |
paul@7 | 92 | |
paul@45 | 93 | for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) |
paul@0 | 94 | { |
paul@45 | 95 | unsigned long remaining = file_populated_span(&file) - offset; |
paul@12 | 96 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@12 | 97 | |
paul@34 | 98 | printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); |
paul@34 | 99 | strncpy(buf, (file.memory + offset), sample_remaining); |
paul@12 | 100 | buf[sample_remaining] = '\0'; |
paul@7 | 101 | printf("%s\n", buf); |
paul@0 | 102 | } |
paul@0 | 103 | |
paul@0 | 104 | return 0; |
paul@0 | 105 | } |
paul@34 | 106 | |
paul@34 | 107 | // vim: tabstop=2 expandtab shiftwidth=2 |