paul@31 | 1 | /* |
paul@31 | 2 | * File access convenience functions. |
paul@31 | 3 | * |
paul@31 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@31 | 5 | * |
paul@31 | 6 | * This program is free software; you can redistribute it and/or |
paul@31 | 7 | * modify it under the terms of the GNU General Public License as |
paul@31 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@31 | 9 | * the License, or (at your option) any later version. |
paul@31 | 10 | * |
paul@31 | 11 | * This program is distributed in the hope that it will be useful, |
paul@31 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@31 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@31 | 14 | * GNU General Public License for more details. |
paul@31 | 15 | * |
paul@31 | 16 | * You should have received a copy of the GNU General Public License |
paul@31 | 17 | * along with this program; if not, write to the Free Software |
paul@31 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@31 | 19 | * Boston, MA 02110-1301, USA |
paul@31 | 20 | */ |
paul@31 | 21 | |
paul@31 | 22 | #include <ipc/cap_alloc.h> |
paul@31 | 23 | #include <ipc/mem_ipc.h> |
paul@31 | 24 | |
paul@46 | 25 | #include <string.h> |
paul@46 | 26 | |
paul@31 | 27 | #include "dataspace_client.h" |
paul@31 | 28 | #include "opener_client.h" |
paul@31 | 29 | #include "opener_context_client.h" |
paul@31 | 30 | #include "mapped_file_client.h" |
paul@31 | 31 | |
paul@31 | 32 | #include "file.h" |
paul@31 | 33 | |
paul@31 | 34 | |
paul@31 | 35 | |
paul@33 | 36 | /* Release resources for the given file. */ |
paul@31 | 37 | |
paul@31 | 38 | void file_close(file_t *file) |
paul@31 | 39 | { |
paul@31 | 40 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 41 | ipc_cap_free_um(file->ref); |
paul@31 | 42 | |
paul@31 | 43 | if (file->memory != NULL) |
paul@31 | 44 | ipc_detach_dataspace(file->memory); |
paul@31 | 45 | |
paul@31 | 46 | file_init(file); |
paul@31 | 47 | } |
paul@31 | 48 | |
paul@33 | 49 | /* Initialise a file structure for a context obtained from the given server. */ |
paul@31 | 50 | |
paul@33 | 51 | long file_context(file_t *file, l4_cap_idx_t server) |
paul@31 | 52 | { |
paul@31 | 53 | client_Opener opener(server); |
paul@31 | 54 | unsigned long size, flags; |
paul@31 | 55 | long err; |
paul@31 | 56 | |
paul@31 | 57 | file_init(file); |
paul@31 | 58 | err = opener.context(&file->ref); |
paul@31 | 59 | |
paul@31 | 60 | if (err) |
paul@31 | 61 | return err; |
paul@31 | 62 | |
paul@31 | 63 | client_Dataspace context_ds(file->ref); |
paul@31 | 64 | |
paul@31 | 65 | err = context_ds.info(&size, &flags); |
paul@31 | 66 | |
paul@31 | 67 | if (err) |
paul@31 | 68 | return err; |
paul@31 | 69 | |
paul@31 | 70 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 71 | } |
paul@31 | 72 | |
paul@33 | 73 | /* Initialise the given file structure. */ |
paul@33 | 74 | |
paul@33 | 75 | void file_init(file_t *file) |
paul@33 | 76 | { |
paul@33 | 77 | file->memory = NULL; |
paul@33 | 78 | file->ref = L4_INVALID_CAP; |
paul@33 | 79 | } |
paul@33 | 80 | |
paul@46 | 81 | /* Open a file using the given structure and context. */ |
paul@46 | 82 | |
paul@46 | 83 | long file_open(file_t *file, file_t *context) |
paul@46 | 84 | { |
paul@46 | 85 | client_OpenerContext openercontext(context->ref); |
paul@46 | 86 | file_init(file); |
paul@46 | 87 | return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); |
paul@46 | 88 | } |
paul@46 | 89 | |
paul@46 | 90 | |
paul@46 | 91 | |
paul@33 | 92 | /* Map a region of the given file to a memory region. */ |
paul@33 | 93 | |
paul@33 | 94 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@33 | 95 | { |
paul@33 | 96 | client_MappedFile mapped_file(file->ref); |
paul@33 | 97 | long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); |
paul@33 | 98 | |
paul@33 | 99 | if (err) |
paul@33 | 100 | return err; |
paul@33 | 101 | |
paul@46 | 102 | return ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); |
paul@33 | 103 | } |
paul@33 | 104 | |
paul@33 | 105 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 106 | |
paul@45 | 107 | offset_t file_populated_span(file_t *file) |
paul@33 | 108 | { |
paul@45 | 109 | offset_t size = file_span(file); |
paul@33 | 110 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 111 | } |
paul@33 | 112 | |
paul@45 | 113 | /* Return the size of the mapped region for the given file. */ |
paul@45 | 114 | |
paul@45 | 115 | offset_t file_span(file_t *file) |
paul@45 | 116 | { |
paul@45 | 117 | return file->end_pos - file->start_pos; |
paul@45 | 118 | } |
paul@45 | 119 | |
paul@46 | 120 | |
paul@46 | 121 | |
paul@46 | 122 | /* Get a pointer to any terminated string at the given offset or NULL if the |
paul@46 | 123 | data from offset is not terminated. */ |
paul@46 | 124 | |
paul@46 | 125 | char *file_string_get(file_t *file, offset_t offset) |
paul@46 | 126 | { |
paul@46 | 127 | offset_t limit = file_span(file) - offset; |
paul@46 | 128 | |
paul@46 | 129 | if (strnlen(file->memory + offset, limit) < limit) |
paul@46 | 130 | return file->memory + offset; |
paul@46 | 131 | else |
paul@46 | 132 | return NULL; |
paul@46 | 133 | } |
paul@46 | 134 | |
paul@46 | 135 | /* Copy a string to the mapped region at the given offset, returning the number |
paul@46 | 136 | of copied characters excluding the zero terminator. */ |
paul@46 | 137 | |
paul@46 | 138 | offset_t file_string_set(file_t *file, const char *data, offset_t offset) |
paul@46 | 139 | { |
paul@46 | 140 | offset_t i, pos, limit = file_span(file) - 1; |
paul@46 | 141 | |
paul@46 | 142 | /* Do not attempt to copy data with an invalid offset. */ |
paul@46 | 143 | |
paul@46 | 144 | if (offset > limit) |
paul@46 | 145 | return 0; |
paul@46 | 146 | |
paul@46 | 147 | /* Copy the data to the given offset, stopping before the final byte of the |
paul@46 | 148 | region. */ |
paul@46 | 149 | |
paul@46 | 150 | for (i = 0, pos = offset; pos < limit; i++, pos++) |
paul@46 | 151 | { |
paul@46 | 152 | file->memory[pos] = data[i]; |
paul@46 | 153 | |
paul@46 | 154 | /* Terminator written, can return immediately. */ |
paul@46 | 155 | |
paul@46 | 156 | if (!data[i]) |
paul@46 | 157 | return pos - offset - 1; |
paul@46 | 158 | } |
paul@46 | 159 | |
paul@46 | 160 | /* Terminate the incomplete string at the end of the region. */ |
paul@46 | 161 | |
paul@46 | 162 | file->memory[limit] = '\0'; |
paul@46 | 163 | return limit - offset; |
paul@46 | 164 | } |
paul@46 | 165 | |
paul@31 | 166 | // vim: tabstop=2 expandtab shiftwidth=2 |