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@55 | 28 | #include "file_client.h" |
paul@31 | 29 | #include "opener_client.h" |
paul@31 | 30 | #include "opener_context_client.h" |
paul@31 | 31 | #include "mapped_file_client.h" |
paul@31 | 32 | |
paul@31 | 33 | #include "file.h" |
paul@31 | 34 | |
paul@31 | 35 | |
paul@31 | 36 | |
paul@33 | 37 | /* Release resources for the given file. */ |
paul@31 | 38 | |
paul@31 | 39 | void file_close(file_t *file) |
paul@31 | 40 | { |
paul@31 | 41 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 42 | ipc_cap_free_um(file->ref); |
paul@31 | 43 | |
paul@31 | 44 | if (file->memory != NULL) |
paul@31 | 45 | ipc_detach_dataspace(file->memory); |
paul@31 | 46 | |
paul@31 | 47 | file_init(file); |
paul@31 | 48 | } |
paul@31 | 49 | |
paul@33 | 50 | /* Initialise a file structure for a context obtained from the given server. */ |
paul@31 | 51 | |
paul@33 | 52 | long file_context(file_t *file, l4_cap_idx_t server) |
paul@31 | 53 | { |
paul@31 | 54 | client_Opener opener(server); |
paul@31 | 55 | unsigned long size, flags; |
paul@31 | 56 | long err; |
paul@31 | 57 | |
paul@31 | 58 | file_init(file); |
paul@47 | 59 | |
paul@31 | 60 | err = opener.context(&file->ref); |
paul@31 | 61 | if (err) |
paul@31 | 62 | return err; |
paul@31 | 63 | |
paul@31 | 64 | client_Dataspace context_ds(file->ref); |
paul@31 | 65 | |
paul@31 | 66 | err = context_ds.info(&size, &flags); |
paul@31 | 67 | if (err) |
paul@31 | 68 | return err; |
paul@31 | 69 | |
paul@47 | 70 | file->start_pos = 0; |
paul@47 | 71 | file->end_pos = size; |
paul@47 | 72 | |
paul@31 | 73 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 74 | } |
paul@31 | 75 | |
paul@47 | 76 | /* Open a file using the given structure and context. */ |
paul@47 | 77 | |
paul@47 | 78 | long file_context_open(file_t *file, file_t *context) |
paul@47 | 79 | { |
paul@47 | 80 | client_OpenerContext openercontext(context->ref); |
paul@47 | 81 | file_init(file); |
paul@47 | 82 | return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); |
paul@47 | 83 | } |
paul@47 | 84 | |
paul@33 | 85 | /* Initialise the given file structure. */ |
paul@33 | 86 | |
paul@33 | 87 | void file_init(file_t *file) |
paul@33 | 88 | { |
paul@33 | 89 | file->memory = NULL; |
paul@33 | 90 | file->ref = L4_INVALID_CAP; |
paul@47 | 91 | file->start_pos = 0; |
paul@47 | 92 | file->end_pos = 0; |
paul@47 | 93 | file->data_end = 0; |
paul@33 | 94 | } |
paul@33 | 95 | |
paul@47 | 96 | /* Open a file using the given structure, indicating the filename and |
paul@47 | 97 | filesystem server. This is a convenience function invoking file_context and |
paul@47 | 98 | file_context_open. */ |
paul@46 | 99 | |
paul@47 | 100 | long file_open(file_t *file, const char *filename, l4_cap_idx_t server) |
paul@46 | 101 | { |
paul@47 | 102 | file_t context; |
paul@47 | 103 | long err; |
paul@47 | 104 | |
paul@47 | 105 | err = file_context(&context, server); |
paul@47 | 106 | if (err) |
paul@47 | 107 | return err; |
paul@47 | 108 | |
paul@47 | 109 | if (!file_string_set(&context, filename, 0, NULL)) |
paul@47 | 110 | return -L4_ENOMEM; |
paul@47 | 111 | |
paul@47 | 112 | err = file_context_open(file, &context); |
paul@47 | 113 | file_close(&context); |
paul@47 | 114 | return err; |
paul@46 | 115 | } |
paul@46 | 116 | |
paul@46 | 117 | |
paul@46 | 118 | |
paul@33 | 119 | /* Map a region of the given file to a memory region. */ |
paul@33 | 120 | |
paul@33 | 121 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@33 | 122 | { |
paul@33 | 123 | client_MappedFile mapped_file(file->ref); |
paul@33 | 124 | long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); |
paul@33 | 125 | |
paul@33 | 126 | if (err) |
paul@33 | 127 | return err; |
paul@33 | 128 | |
paul@46 | 129 | return ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); |
paul@33 | 130 | } |
paul@33 | 131 | |
paul@55 | 132 | /* Resize a file. */ |
paul@55 | 133 | |
paul@55 | 134 | long file_resize(file_t *file, offset_t size) |
paul@55 | 135 | { |
paul@55 | 136 | client_File _file(file->ref); |
paul@55 | 137 | offset_t file_size = size; |
paul@55 | 138 | long err = _file.resize(&file_size); |
paul@55 | 139 | |
paul@55 | 140 | if (!err) |
paul@55 | 141 | { |
paul@55 | 142 | /* Determine the extent of the file in this region. */ |
paul@55 | 143 | |
paul@55 | 144 | if (file_size > file->end_pos) |
paul@55 | 145 | file->data_end = file_span(file); |
paul@55 | 146 | else |
paul@55 | 147 | file->data_end = file_size - file->start_pos; |
paul@55 | 148 | |
paul@55 | 149 | /* Update the file size locally. */ |
paul@55 | 150 | |
paul@55 | 151 | file->size = file_size; |
paul@55 | 152 | } |
paul@55 | 153 | |
paul@55 | 154 | return err; |
paul@55 | 155 | } |
paul@55 | 156 | |
paul@55 | 157 | |
paul@55 | 158 | |
paul@33 | 159 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 160 | |
paul@45 | 161 | offset_t file_populated_span(file_t *file) |
paul@33 | 162 | { |
paul@45 | 163 | offset_t size = file_span(file); |
paul@33 | 164 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 165 | } |
paul@33 | 166 | |
paul@45 | 167 | /* Return the size of the mapped region for the given file. */ |
paul@45 | 168 | |
paul@45 | 169 | offset_t file_span(file_t *file) |
paul@45 | 170 | { |
paul@45 | 171 | return file->end_pos - file->start_pos; |
paul@45 | 172 | } |
paul@45 | 173 | |
paul@46 | 174 | |
paul@46 | 175 | |
paul@46 | 176 | /* Get a pointer to any terminated string at the given offset or NULL if the |
paul@46 | 177 | data from offset is not terminated. */ |
paul@46 | 178 | |
paul@46 | 179 | char *file_string_get(file_t *file, offset_t offset) |
paul@46 | 180 | { |
paul@46 | 181 | offset_t limit = file_span(file) - offset; |
paul@46 | 182 | |
paul@46 | 183 | if (strnlen(file->memory + offset, limit) < limit) |
paul@46 | 184 | return file->memory + offset; |
paul@46 | 185 | else |
paul@46 | 186 | return NULL; |
paul@46 | 187 | } |
paul@46 | 188 | |
paul@47 | 189 | /* Copy a string to the mapped region at the given offset, returning 1 (true) |
paul@47 | 190 | where all characters were copied, 0 (false) otherwise. The precise number of |
paul@47 | 191 | characters copied, excluding the zero terminator is provided via the written |
paul@47 | 192 | parameter if it is not specified as NULL. */ |
paul@46 | 193 | |
paul@47 | 194 | int file_string_set(file_t *file, const char *data, offset_t offset, |
paul@47 | 195 | offset_t *written) |
paul@46 | 196 | { |
paul@47 | 197 | offset_t i, pos, limit = file_span(file); |
paul@46 | 198 | |
paul@46 | 199 | /* Do not attempt to copy data with an invalid offset. */ |
paul@46 | 200 | |
paul@47 | 201 | if (offset >= limit) |
paul@47 | 202 | { |
paul@47 | 203 | if (written != NULL) |
paul@47 | 204 | *written = 0; |
paul@46 | 205 | return 0; |
paul@47 | 206 | } |
paul@46 | 207 | |
paul@47 | 208 | /* Copy the data to the given offset, stopping at the end of the region. */ |
paul@46 | 209 | |
paul@46 | 210 | for (i = 0, pos = offset; pos < limit; i++, pos++) |
paul@46 | 211 | { |
paul@46 | 212 | file->memory[pos] = data[i]; |
paul@46 | 213 | |
paul@46 | 214 | /* Terminator written, can return immediately. */ |
paul@46 | 215 | |
paul@46 | 216 | if (!data[i]) |
paul@47 | 217 | { |
paul@47 | 218 | if (written != NULL) |
paul@47 | 219 | *written = pos - offset; |
paul@47 | 220 | return 1; |
paul@47 | 221 | } |
paul@46 | 222 | } |
paul@46 | 223 | |
paul@46 | 224 | /* Terminate the incomplete string at the end of the region. */ |
paul@46 | 225 | |
paul@47 | 226 | file->memory[limit - 1] = '\0'; |
paul@47 | 227 | if (written != NULL) |
paul@47 | 228 | *written = limit - 1 - offset; |
paul@47 | 229 | return 0; |
paul@46 | 230 | } |
paul@46 | 231 | |
paul@31 | 232 | // vim: tabstop=2 expandtab shiftwidth=2 |