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@47 | 58 | |
paul@31 | 59 | err = opener.context(&file->ref); |
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 | if (err) |
paul@31 | 67 | return err; |
paul@31 | 68 | |
paul@47 | 69 | file->start_pos = 0; |
paul@47 | 70 | file->end_pos = size; |
paul@47 | 71 | |
paul@31 | 72 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 73 | } |
paul@31 | 74 | |
paul@47 | 75 | /* Open a file using the given structure and context. */ |
paul@47 | 76 | |
paul@47 | 77 | long file_context_open(file_t *file, file_t *context) |
paul@47 | 78 | { |
paul@47 | 79 | client_OpenerContext openercontext(context->ref); |
paul@47 | 80 | file_init(file); |
paul@47 | 81 | return openercontext.open(L4_FPAGE_RW, &file->size, &file->ref); |
paul@47 | 82 | } |
paul@47 | 83 | |
paul@33 | 84 | /* Initialise the given file structure. */ |
paul@33 | 85 | |
paul@33 | 86 | void file_init(file_t *file) |
paul@33 | 87 | { |
paul@33 | 88 | file->memory = NULL; |
paul@33 | 89 | file->ref = L4_INVALID_CAP; |
paul@47 | 90 | file->start_pos = 0; |
paul@47 | 91 | file->end_pos = 0; |
paul@47 | 92 | file->data_end = 0; |
paul@33 | 93 | } |
paul@33 | 94 | |
paul@47 | 95 | /* Open a file using the given structure, indicating the filename and |
paul@47 | 96 | filesystem server. This is a convenience function invoking file_context and |
paul@47 | 97 | file_context_open. */ |
paul@46 | 98 | |
paul@47 | 99 | long file_open(file_t *file, const char *filename, l4_cap_idx_t server) |
paul@46 | 100 | { |
paul@47 | 101 | file_t context; |
paul@47 | 102 | long err; |
paul@47 | 103 | |
paul@47 | 104 | err = file_context(&context, server); |
paul@47 | 105 | if (err) |
paul@47 | 106 | return err; |
paul@47 | 107 | |
paul@47 | 108 | if (!file_string_set(&context, filename, 0, NULL)) |
paul@47 | 109 | return -L4_ENOMEM; |
paul@47 | 110 | |
paul@47 | 111 | err = file_context_open(file, &context); |
paul@47 | 112 | file_close(&context); |
paul@47 | 113 | return err; |
paul@46 | 114 | } |
paul@46 | 115 | |
paul@46 | 116 | |
paul@46 | 117 | |
paul@33 | 118 | /* Map a region of the given file to a memory region. */ |
paul@33 | 119 | |
paul@33 | 120 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@33 | 121 | { |
paul@33 | 122 | client_MappedFile mapped_file(file->ref); |
paul@33 | 123 | long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); |
paul@33 | 124 | |
paul@33 | 125 | if (err) |
paul@33 | 126 | return err; |
paul@33 | 127 | |
paul@46 | 128 | return ipc_attach_dataspace(file->ref, file_span(file), (void **) &file->memory); |
paul@33 | 129 | } |
paul@33 | 130 | |
paul@33 | 131 | /* Return the amount of data in the mapped region for the given file. */ |
paul@33 | 132 | |
paul@45 | 133 | offset_t file_populated_span(file_t *file) |
paul@33 | 134 | { |
paul@45 | 135 | offset_t size = file_span(file); |
paul@33 | 136 | return (file->data_end < size) ? file->data_end : size; |
paul@33 | 137 | } |
paul@33 | 138 | |
paul@45 | 139 | /* Return the size of the mapped region for the given file. */ |
paul@45 | 140 | |
paul@45 | 141 | offset_t file_span(file_t *file) |
paul@45 | 142 | { |
paul@45 | 143 | return file->end_pos - file->start_pos; |
paul@45 | 144 | } |
paul@45 | 145 | |
paul@46 | 146 | |
paul@46 | 147 | |
paul@46 | 148 | /* Get a pointer to any terminated string at the given offset or NULL if the |
paul@46 | 149 | data from offset is not terminated. */ |
paul@46 | 150 | |
paul@46 | 151 | char *file_string_get(file_t *file, offset_t offset) |
paul@46 | 152 | { |
paul@46 | 153 | offset_t limit = file_span(file) - offset; |
paul@46 | 154 | |
paul@46 | 155 | if (strnlen(file->memory + offset, limit) < limit) |
paul@46 | 156 | return file->memory + offset; |
paul@46 | 157 | else |
paul@46 | 158 | return NULL; |
paul@46 | 159 | } |
paul@46 | 160 | |
paul@47 | 161 | /* Copy a string to the mapped region at the given offset, returning 1 (true) |
paul@47 | 162 | where all characters were copied, 0 (false) otherwise. The precise number of |
paul@47 | 163 | characters copied, excluding the zero terminator is provided via the written |
paul@47 | 164 | parameter if it is not specified as NULL. */ |
paul@46 | 165 | |
paul@47 | 166 | int file_string_set(file_t *file, const char *data, offset_t offset, |
paul@47 | 167 | offset_t *written) |
paul@46 | 168 | { |
paul@47 | 169 | offset_t i, pos, limit = file_span(file); |
paul@46 | 170 | |
paul@46 | 171 | /* Do not attempt to copy data with an invalid offset. */ |
paul@46 | 172 | |
paul@47 | 173 | if (offset >= limit) |
paul@47 | 174 | { |
paul@47 | 175 | if (written != NULL) |
paul@47 | 176 | *written = 0; |
paul@46 | 177 | return 0; |
paul@47 | 178 | } |
paul@46 | 179 | |
paul@47 | 180 | /* Copy the data to the given offset, stopping at the end of the region. */ |
paul@46 | 181 | |
paul@46 | 182 | for (i = 0, pos = offset; pos < limit; i++, pos++) |
paul@46 | 183 | { |
paul@46 | 184 | file->memory[pos] = data[i]; |
paul@46 | 185 | |
paul@46 | 186 | /* Terminator written, can return immediately. */ |
paul@46 | 187 | |
paul@46 | 188 | if (!data[i]) |
paul@47 | 189 | { |
paul@47 | 190 | if (written != NULL) |
paul@47 | 191 | *written = pos - offset; |
paul@47 | 192 | return 1; |
paul@47 | 193 | } |
paul@46 | 194 | } |
paul@46 | 195 | |
paul@46 | 196 | /* Terminate the incomplete string at the end of the region. */ |
paul@46 | 197 | |
paul@47 | 198 | file->memory[limit - 1] = '\0'; |
paul@47 | 199 | if (written != NULL) |
paul@47 | 200 | *written = limit - 1 - offset; |
paul@47 | 201 | return 0; |
paul@46 | 202 | } |
paul@46 | 203 | |
paul@31 | 204 | // vim: tabstop=2 expandtab shiftwidth=2 |