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@31 | 25 | #include "dataspace_client.h" |
paul@31 | 26 | #include "opener_client.h" |
paul@31 | 27 | #include "opener_context_client.h" |
paul@31 | 28 | #include "mapped_file_client.h" |
paul@31 | 29 | |
paul@31 | 30 | #include "file.h" |
paul@31 | 31 | |
paul@31 | 32 | |
paul@31 | 33 | |
paul@33 | 34 | /* Release resources for the given file. */ |
paul@31 | 35 | |
paul@31 | 36 | void file_close(file_t *file) |
paul@31 | 37 | { |
paul@31 | 38 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 39 | ipc_cap_free_um(file->ref); |
paul@31 | 40 | |
paul@31 | 41 | if (file->memory != NULL) |
paul@31 | 42 | ipc_detach_dataspace(file->memory); |
paul@31 | 43 | |
paul@31 | 44 | file_init(file); |
paul@31 | 45 | } |
paul@31 | 46 | |
paul@33 | 47 | /* Initialise a file structure for a context obtained from the given server. */ |
paul@31 | 48 | |
paul@33 | 49 | long file_context(file_t *file, l4_cap_idx_t server) |
paul@31 | 50 | { |
paul@31 | 51 | client_Opener opener(server); |
paul@31 | 52 | unsigned long size, flags; |
paul@31 | 53 | long err; |
paul@31 | 54 | |
paul@31 | 55 | file_init(file); |
paul@31 | 56 | err = opener.context(&file->ref); |
paul@31 | 57 | |
paul@31 | 58 | if (err) |
paul@31 | 59 | return err; |
paul@31 | 60 | |
paul@31 | 61 | client_Dataspace context_ds(file->ref); |
paul@31 | 62 | |
paul@31 | 63 | err = context_ds.info(&size, &flags); |
paul@31 | 64 | |
paul@31 | 65 | if (err) |
paul@31 | 66 | return err; |
paul@31 | 67 | |
paul@31 | 68 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 69 | } |
paul@31 | 70 | |
paul@33 | 71 | /* Initialise the given file structure. */ |
paul@33 | 72 | |
paul@33 | 73 | void file_init(file_t *file) |
paul@33 | 74 | { |
paul@33 | 75 | file->memory = NULL; |
paul@33 | 76 | file->ref = L4_INVALID_CAP; |
paul@33 | 77 | } |
paul@33 | 78 | |
paul@33 | 79 | /* Map a region of the given file to a memory region. */ |
paul@33 | 80 | |
paul@33 | 81 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@33 | 82 | { |
paul@33 | 83 | client_MappedFile mapped_file(file->ref); |
paul@33 | 84 | long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); |
paul@33 | 85 | |
paul@33 | 86 | if (err) |
paul@33 | 87 | return err; |
paul@33 | 88 | |
paul@33 | 89 | return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory); |
paul@33 | 90 | } |
paul@33 | 91 | |
paul@35 | 92 | /* Open a file using the given structure and context. */ |
paul@33 | 93 | |
paul@35 | 94 | long file_open(file_t *file, file_t *context) |
paul@33 | 95 | { |
paul@35 | 96 | client_OpenerContext openercontext(context->ref); |
paul@33 | 97 | file_init(file); |
paul@35 | 98 | return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); |
paul@33 | 99 | } |
paul@33 | 100 | |
paul@33 | 101 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 102 | |
paul@33 | 103 | offset_t file_span(file_t *file) |
paul@33 | 104 | { |
paul@33 | 105 | offset_t size = file->end_pos - file->start_pos; |
paul@33 | 106 | |
paul@33 | 107 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 108 | } |
paul@33 | 109 | |
paul@31 | 110 | // vim: tabstop=2 expandtab shiftwidth=2 |