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@31 | 34 | offset_t file_span(file_t *file) |
paul@31 | 35 | { |
paul@31 | 36 | offset_t size = file->end_pos - file->start_pos; |
paul@31 | 37 | |
paul@31 | 38 | return (file->data_end < size) ? file->data_end : size; |
paul@31 | 39 | } |
paul@31 | 40 | |
paul@31 | 41 | void file_init(file_t *file) |
paul@31 | 42 | { |
paul@31 | 43 | file->memory = NULL; |
paul@31 | 44 | file->ref = L4_INVALID_CAP; |
paul@31 | 45 | } |
paul@31 | 46 | |
paul@31 | 47 | long file_open(file_t *file, l4_cap_idx_t context_ref) |
paul@31 | 48 | { |
paul@31 | 49 | client_OpenerContext context(context_ref); |
paul@31 | 50 | file_init(file); |
paul@31 | 51 | return context.open(L4_FPAGE_RW, &file->size, &file->ref); |
paul@31 | 52 | } |
paul@31 | 53 | |
paul@31 | 54 | void file_close(file_t *file) |
paul@31 | 55 | { |
paul@31 | 56 | if (l4_is_valid_cap(file->ref)) |
paul@31 | 57 | ipc_cap_free_um(file->ref); |
paul@31 | 58 | |
paul@31 | 59 | if (file->memory != NULL) |
paul@31 | 60 | ipc_detach_dataspace(file->memory); |
paul@31 | 61 | |
paul@31 | 62 | file_init(file); |
paul@31 | 63 | } |
paul@31 | 64 | |
paul@31 | 65 | long file_mmap(file_t *file, offset_t position, offset_t length) |
paul@31 | 66 | { |
paul@31 | 67 | client_MappedFile mapped_file(file->ref); |
paul@31 | 68 | long err = mapped_file.mmap(position, length, &file->start_pos, &file->end_pos, &file->data_end); |
paul@31 | 69 | |
paul@31 | 70 | if (err) |
paul@31 | 71 | return err; |
paul@31 | 72 | |
paul@31 | 73 | return ipc_attach_dataspace(file->ref, file->end_pos - file->start_pos, (void **) &file->memory); |
paul@31 | 74 | } |
paul@31 | 75 | |
paul@31 | 76 | long context_open(file_t *file, l4_cap_idx_t server) |
paul@31 | 77 | { |
paul@31 | 78 | client_Opener opener(server); |
paul@31 | 79 | unsigned long size, flags; |
paul@31 | 80 | long err; |
paul@31 | 81 | |
paul@31 | 82 | file_init(file); |
paul@31 | 83 | err = opener.context(&file->ref); |
paul@31 | 84 | |
paul@31 | 85 | if (err) |
paul@31 | 86 | return err; |
paul@31 | 87 | |
paul@31 | 88 | client_Dataspace context_ds(file->ref); |
paul@31 | 89 | |
paul@31 | 90 | err = context_ds.info(&size, &flags); |
paul@31 | 91 | |
paul@31 | 92 | if (err) |
paul@31 | 93 | return err; |
paul@31 | 94 | |
paul@31 | 95 | return ipc_attach_dataspace(file->ref, size, (void **) &file->memory); |
paul@31 | 96 | } |
paul@31 | 97 | |
paul@31 | 98 | // vim: tabstop=2 expandtab shiftwidth=2 |