paul@93 | 1 | /* |
paul@93 | 2 | * A resource offering support for creating contexts and opening files. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@10 | 22 | #include "opener_server.h" |
paul@10 | 23 | #include "opener_resource.h" |
paul@10 | 24 | #include "resource_server.h" |
paul@9 | 25 | |
paul@9 | 26 | /* Support for providing access to files. */ |
paul@9 | 27 | |
paul@209 | 28 | OpenerResource::OpenerResource(FileObjectRegistry *registry) |
paul@209 | 29 | : _registry(registry) |
paul@9 | 30 | { |
paul@9 | 31 | } |
paul@9 | 32 | |
paul@156 | 33 | OpenerResource::~OpenerResource() |
paul@156 | 34 | { |
paul@156 | 35 | } |
paul@156 | 36 | |
paul@10 | 37 | int OpenerResource::expected_items() |
paul@9 | 38 | { |
paul@10 | 39 | return Opener_expected_items; |
paul@9 | 40 | } |
paul@9 | 41 | |
paul@10 | 42 | ipc_server_handler_type OpenerResource::handler() |
paul@10 | 43 | { |
paul@10 | 44 | return (ipc_server_handler_type) handle_Opener; |
paul@10 | 45 | } |
paul@10 | 46 | |
paul@10 | 47 | |
paul@10 | 48 | |
paul@205 | 49 | /* Return an object for the given path and flags. */ |
paul@10 | 50 | |
paul@171 | 51 | long OpenerResource::open(const char *path, flags_t flags, offset_t *size, |
paul@171 | 52 | l4_cap_idx_t *cap, object_flags_t *object_flags) |
paul@9 | 53 | { |
paul@156 | 54 | /* Obtain an identifier for the file, even for new files. */ |
paul@156 | 55 | |
paul@156 | 56 | fileid_t fileid; |
paul@156 | 57 | long err = get_fileid(path, flags, &fileid); |
paul@156 | 58 | |
paul@156 | 59 | if (err) |
paul@156 | 60 | return err; |
paul@156 | 61 | |
paul@156 | 62 | /* Test for file and directory access. */ |
paul@156 | 63 | |
paul@156 | 64 | if (accessing_directory(path, flags, fileid)) |
paul@171 | 65 | return get_directory(path, flags, fileid, size, cap, object_flags); |
paul@156 | 66 | else if (accessing_file(path, flags, fileid)) |
paul@171 | 67 | return get_file(path, flags, fileid, size, cap, object_flags); |
paul@156 | 68 | else |
paul@156 | 69 | return -L4_EIO; |
paul@156 | 70 | } |
paul@156 | 71 | |
paul@156 | 72 | |
paul@156 | 73 | |
paul@202 | 74 | /* Return a directory object reference for the given file identifier. */ |
paul@156 | 75 | |
paul@171 | 76 | long OpenerResource::get_directory(const char *path, flags_t flags, |
paul@171 | 77 | fileid_t fileid, offset_t *size, |
paul@202 | 78 | l4_cap_idx_t *cap, |
paul@202 | 79 | object_flags_t *object_flags) |
paul@156 | 80 | { |
paul@202 | 81 | Resource *directory; |
paul@209 | 82 | long err = _registry->get_directory(this, path, flags, fileid, &directory); |
paul@202 | 83 | |
paul@202 | 84 | if (err) |
paul@202 | 85 | return err; |
paul@202 | 86 | |
paul@202 | 87 | /* Provide non-file values for certain outputs. */ |
paul@202 | 88 | |
paul@202 | 89 | *size = 0; |
paul@202 | 90 | *object_flags = 0; |
paul@202 | 91 | |
paul@202 | 92 | return ResourceServer(directory).start_thread(cap); |
paul@156 | 93 | } |
paul@156 | 94 | |
paul@156 | 95 | /* Return a file pager. */ |
paul@156 | 96 | |
paul@171 | 97 | long OpenerResource::get_file(const char *path, flags_t flags, fileid_t fileid, |
paul@171 | 98 | offset_t *size, l4_cap_idx_t *cap, |
paul@171 | 99 | object_flags_t *object_flags) |
paul@156 | 100 | { |
paul@157 | 101 | Pager *pager; |
paul@209 | 102 | long err = _registry->get_pager(this, path, flags, fileid, &pager); |
paul@157 | 103 | |
paul@157 | 104 | if (err) |
paul@157 | 105 | return err; |
paul@157 | 106 | |
paul@202 | 107 | /* Obtain the size details from the pager, also providing appropriate |
paul@202 | 108 | flags. */ |
paul@157 | 109 | |
paul@157 | 110 | *size = pager->get_data_size(); |
paul@202 | 111 | *object_flags = OBJECT_SUPPORTS_MMAP | OBJECT_HAS_SIZE; |
paul@157 | 112 | |
paul@157 | 113 | return ResourceServer(pager).start_thread(cap); |
paul@9 | 114 | } |
paul@9 | 115 | |
paul@10 | 116 | |
paul@10 | 117 | |
paul@10 | 118 | /* Opener interface methods. */ |
paul@10 | 119 | |
paul@10 | 120 | long OpenerResource::context(l4_cap_idx_t *context) |
paul@10 | 121 | { |
paul@10 | 122 | OpenerContextResource *resource = new OpenerContextResource(this); |
paul@10 | 123 | |
paul@62 | 124 | /* Complete the initialisation and start a server in a new thread. |
paul@120 | 125 | If the thread does not start, the resource will be finalised. */ |
paul@10 | 126 | |
paul@120 | 127 | return ResourceServer(resource).start_thread(context); |
paul@10 | 128 | } |
paul@10 | 129 | |
paul@9 | 130 | // vim: tabstop=4 expandtab shiftwidth=4 |