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@65 | 31 | #include "pipe_client.h" |
paul@65 | 32 | #include "pipe_opener_client.h" |
paul@31 | 33 | #include "mapped_file_client.h" |
paul@31 | 34 | |
paul@31 | 35 | #include "file.h" |
paul@31 | 36 | |
paul@31 | 37 | |
paul@31 | 38 | |
paul@33 | 39 | /* Release resources for the given file. */ |
paul@31 | 40 | |
paul@31 | 41 | void file_close(file_t *file) |
paul@31 | 42 | { |
paul@31 | 43 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 44 | ipc_cap_free_um(file->ref); |
paul@31 | 45 | |
paul@31 | 46 | if (file->memory != NULL) |
paul@31 | 47 | ipc_detach_dataspace(file->memory); |
paul@31 | 48 | |
paul@31 | 49 | file_init(file); |
paul@31 | 50 | } |
paul@31 | 51 | |
paul@33 | 52 | /* Initialise a file structure for a context obtained from the given server. */ |
paul@31 | 53 | |
paul@33 | 54 | long file_context(file_t *file, l4_cap_idx_t server) |
paul@31 | 55 | { |
paul@85 | 56 | if (l4_is_invalid_cap(server)) |
paul@85 | 57 | return -L4_EINVAL; |
paul@85 | 58 | |
paul@31 | 59 | client_Opener opener(server); |
paul@31 | 60 | unsigned long size, flags; |
paul@31 | 61 | long err; |
paul@31 | 62 | |
paul@31 | 63 | file_init(file); |
paul@47 | 64 | |
paul@31 | 65 | err = opener.context(&file->ref); |
paul@31 | 66 | if (err) |
paul@31 | 67 | return err; |
paul@31 | 68 | |
paul@31 | 69 | client_Dataspace context_ds(file->ref); |
paul@31 | 70 | |
paul@31 | 71 | err = context_ds.info(&size, &flags); |
paul@31 | 72 | if (err) |
paul@31 | 73 | return err; |
paul@31 | 74 | |
paul@47 | 75 | file->start_pos = 0; |
paul@47 | 76 | file->end_pos = size; |
paul@47 | 77 | |
paul@31 | 78 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 79 | } |
paul@31 | 80 | |
paul@47 | 81 | /* Open a file using the given structure and context. */ |
paul@47 | 82 | |
paul@85 | 83 | long file_context_open(file_t *file, flags_t flags, file_t *context) |
paul@47 | 84 | { |
paul@47 | 85 | client_OpenerContext openercontext(context->ref); |
paul@47 | 86 | file_init(file); |
paul@85 | 87 | return openercontext.open(flags, &file->size, &file->ref); |
paul@47 | 88 | } |
paul@47 | 89 | |
paul@33 | 90 | /* Initialise the given file structure. */ |
paul@33 | 91 | |
paul@33 | 92 | void file_init(file_t *file) |
paul@33 | 93 | { |
paul@33 | 94 | file->memory = NULL; |
paul@33 | 95 | file->ref = L4_INVALID_CAP; |
paul@47 | 96 | file->start_pos = 0; |
paul@47 | 97 | file->end_pos = 0; |
paul@47 | 98 | file->data_end = 0; |
paul@33 | 99 | } |
paul@33 | 100 | |
paul@47 | 101 | /* Open a file using the given structure, indicating the filename and |
paul@47 | 102 | filesystem server. This is a convenience function invoking file_context and |
paul@47 | 103 | file_context_open. */ |
paul@46 | 104 | |
paul@85 | 105 | long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server) |
paul@46 | 106 | { |
paul@47 | 107 | file_t context; |
paul@47 | 108 | long err; |
paul@47 | 109 | |
paul@47 | 110 | err = file_context(&context, server); |
paul@47 | 111 | if (err) |
paul@47 | 112 | return err; |
paul@47 | 113 | |
paul@47 | 114 | if (!file_string_set(&context, filename, 0, NULL)) |
paul@47 | 115 | return -L4_ENOMEM; |
paul@47 | 116 | |
paul@85 | 117 | err = file_context_open(file, flags, &context); |
paul@47 | 118 | file_close(&context); |
paul@47 | 119 | return err; |
paul@46 | 120 | } |
paul@46 | 121 | |
paul@46 | 122 | |
paul@46 | 123 | |
paul@33 | 124 | /* Map a region of the given file to a memory region. */ |
paul@33 | 125 | |
paul@33 | 126 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@33 | 127 | { |
paul@33 | 128 | client_MappedFile mapped_file(file->ref); |
paul@33 | 129 | long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); |
paul@33 | 130 | |
paul@33 | 131 | if (err) |
paul@33 | 132 | return err; |
paul@33 | 133 | |
paul@46 | 134 | return ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); |
paul@33 | 135 | } |
paul@33 | 136 | |
paul@55 | 137 | /* Resize a file. */ |
paul@55 | 138 | |
paul@55 | 139 | long file_resize(file_t *file, offset_t size) |
paul@55 | 140 | { |
paul@55 | 141 | client_File _file(file->ref); |
paul@55 | 142 | offset_t file_size = size; |
paul@55 | 143 | long err = _file.resize(&file_size); |
paul@55 | 144 | |
paul@55 | 145 | if (!err) |
paul@55 | 146 | { |
paul@55 | 147 | /* Determine the extent of the file in this region. */ |
paul@55 | 148 | |
paul@55 | 149 | if (file_size > file->end_pos) |
paul@55 | 150 | file->data_end = file_span(file); |
paul@55 | 151 | else |
paul@55 | 152 | file->data_end = file_size - file->start_pos; |
paul@55 | 153 | |
paul@55 | 154 | /* Update the file size locally. */ |
paul@55 | 155 | |
paul@55 | 156 | file->size = file_size; |
paul@55 | 157 | } |
paul@55 | 158 | |
paul@55 | 159 | return err; |
paul@55 | 160 | } |
paul@55 | 161 | |
paul@55 | 162 | |
paul@55 | 163 | |
paul@33 | 164 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 165 | |
paul@45 | 166 | offset_t file_populated_span(file_t *file) |
paul@33 | 167 | { |
paul@45 | 168 | offset_t size = file_span(file); |
paul@33 | 169 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 170 | } |
paul@33 | 171 | |
paul@45 | 172 | /* Return the size of the mapped region for the given file. */ |
paul@45 | 173 | |
paul@45 | 174 | offset_t file_span(file_t *file) |
paul@45 | 175 | { |
paul@45 | 176 | return file->end_pos - file->start_pos; |
paul@45 | 177 | } |
paul@45 | 178 | |
paul@46 | 179 | |
paul@46 | 180 | |
paul@46 | 181 | /* Get a pointer to any terminated string at the given offset or NULL if the |
paul@46 | 182 | data from offset is not terminated. */ |
paul@46 | 183 | |
paul@46 | 184 | char *file_string_get(file_t *file, offset_t offset) |
paul@46 | 185 | { |
paul@46 | 186 | offset_t limit = file_span(file) - offset; |
paul@46 | 187 | |
paul@46 | 188 | if (strnlen(file->memory + offset, limit) < limit) |
paul@46 | 189 | return file->memory + offset; |
paul@46 | 190 | else |
paul@46 | 191 | return NULL; |
paul@46 | 192 | } |
paul@46 | 193 | |
paul@47 | 194 | /* Copy a string to the mapped region at the given offset, returning 1 (true) |
paul@47 | 195 | where all characters were copied, 0 (false) otherwise. The precise number of |
paul@47 | 196 | characters copied, excluding the zero terminator is provided via the written |
paul@47 | 197 | parameter if it is not specified as NULL. */ |
paul@46 | 198 | |
paul@47 | 199 | int file_string_set(file_t *file, const char *data, offset_t offset, |
paul@47 | 200 | offset_t *written) |
paul@46 | 201 | { |
paul@47 | 202 | offset_t i, pos, limit = file_span(file); |
paul@46 | 203 | |
paul@46 | 204 | /* Do not attempt to copy data with an invalid offset. */ |
paul@46 | 205 | |
paul@47 | 206 | if (offset >= limit) |
paul@47 | 207 | { |
paul@47 | 208 | if (written != NULL) |
paul@47 | 209 | *written = 0; |
paul@46 | 210 | return 0; |
paul@47 | 211 | } |
paul@46 | 212 | |
paul@47 | 213 | /* Copy the data to the given offset, stopping at the end of the region. */ |
paul@46 | 214 | |
paul@46 | 215 | for (i = 0, pos = offset; pos < limit; i++, pos++) |
paul@46 | 216 | { |
paul@46 | 217 | file->memory[pos] = data[i]; |
paul@46 | 218 | |
paul@46 | 219 | /* Terminator written, can return immediately. */ |
paul@46 | 220 | |
paul@46 | 221 | if (!data[i]) |
paul@47 | 222 | { |
paul@47 | 223 | if (written != NULL) |
paul@47 | 224 | *written = pos - offset; |
paul@47 | 225 | return 1; |
paul@47 | 226 | } |
paul@46 | 227 | } |
paul@46 | 228 | |
paul@46 | 229 | /* Terminate the incomplete string at the end of the region. */ |
paul@46 | 230 | |
paul@47 | 231 | file->memory[limit - 1] = '\0'; |
paul@47 | 232 | if (written != NULL) |
paul@47 | 233 | *written = limit - 1 - offset; |
paul@47 | 234 | return 0; |
paul@46 | 235 | } |
paul@46 | 236 | |
paul@65 | 237 | |
paul@65 | 238 | |
paul@65 | 239 | /* Open two pipe endpoints using the given pipe server. */ |
paul@65 | 240 | |
paul@66 | 241 | long pipe_open(offset_t size, file_t *reader, file_t *writer, l4_cap_idx_t server) |
paul@65 | 242 | { |
paul@85 | 243 | if (l4_is_invalid_cap(server)) |
paul@85 | 244 | return -L4_EINVAL; |
paul@85 | 245 | |
paul@65 | 246 | client_PipeOpener opener(server); |
paul@65 | 247 | |
paul@65 | 248 | file_init(reader); |
paul@65 | 249 | file_init(writer); |
paul@65 | 250 | |
paul@66 | 251 | long err = opener.pipe(size, &reader->ref, &writer->ref); |
paul@65 | 252 | if (err) |
paul@65 | 253 | return err; |
paul@65 | 254 | |
paul@65 | 255 | err = pipe_next(writer) || pipe_next(reader); |
paul@65 | 256 | |
paul@65 | 257 | if (err) |
paul@65 | 258 | { |
paul@65 | 259 | file_close(reader); |
paul@65 | 260 | file_close(writer); |
paul@65 | 261 | } |
paul@65 | 262 | |
paul@65 | 263 | return err; |
paul@65 | 264 | } |
paul@65 | 265 | |
paul@65 | 266 | /* Access the current region for a pipe endpoint. */ |
paul@65 | 267 | |
paul@65 | 268 | long pipe_current(file_t *pipe) |
paul@65 | 269 | { |
paul@65 | 270 | client_Pipe _pipe(pipe->ref); |
paul@65 | 271 | long err = _pipe.current_region(&pipe->data_end, &pipe->size); |
paul@65 | 272 | char *memory = pipe->memory; |
paul@65 | 273 | |
paul@65 | 274 | if (err) |
paul@65 | 275 | return err; |
paul@65 | 276 | |
paul@65 | 277 | pipe->end_pos = pipe->size; |
paul@65 | 278 | |
paul@65 | 279 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@65 | 280 | if (err) |
paul@65 | 281 | return err; |
paul@65 | 282 | |
paul@65 | 283 | if (memory != NULL) |
paul@65 | 284 | ipc_detach_dataspace(memory); |
paul@65 | 285 | |
paul@65 | 286 | return L4_EOK; |
paul@65 | 287 | } |
paul@65 | 288 | |
paul@65 | 289 | /* Access the next region for a pipe endpoint, updating the eventual size of |
paul@65 | 290 | the current region. */ |
paul@65 | 291 | |
paul@65 | 292 | long pipe_next(file_t *pipe) |
paul@65 | 293 | { |
paul@65 | 294 | client_Pipe _pipe(pipe->ref); |
paul@65 | 295 | long err = _pipe.next_region(&pipe->data_end, &pipe->size); |
paul@65 | 296 | char *memory = pipe->memory; |
paul@65 | 297 | |
paul@65 | 298 | if (err) |
paul@65 | 299 | return err; |
paul@65 | 300 | |
paul@65 | 301 | pipe->end_pos = pipe->size; |
paul@65 | 302 | |
paul@65 | 303 | err = ipc_attach_dataspace(pipe->ref, file_span(pipe), (void **) &pipe->memory); |
paul@65 | 304 | if (err) |
paul@65 | 305 | return err; |
paul@65 | 306 | |
paul@65 | 307 | if (memory != NULL) |
paul@65 | 308 | ipc_detach_dataspace(memory); |
paul@65 | 309 | |
paul@65 | 310 | return L4_EOK; |
paul@65 | 311 | } |
paul@65 | 312 | |
paul@65 | 313 | /* Set the size of the written region. */ |
paul@65 | 314 | |
paul@65 | 315 | long pipe_written(file_t *pipe, offset_t size) |
paul@65 | 316 | { |
paul@65 | 317 | if (size <= pipe->size) |
paul@65 | 318 | { |
paul@65 | 319 | pipe->data_end = size; |
paul@65 | 320 | return L4_EOK; |
paul@65 | 321 | } |
paul@65 | 322 | else |
paul@65 | 323 | return -L4_EINVAL; |
paul@65 | 324 | } |
paul@65 | 325 | |
paul@31 | 326 | // vim: tabstop=2 expandtab shiftwidth=2 |