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