paul@93 | 1 | /* |
paul@93 | 2 | * A context resource offering support for 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_context_resource.h" |
paul@10 | 23 | #include "opener_context_object_server.h" |
paul@10 | 24 | #include "opener_resource.h" |
paul@10 | 25 | #include "resource_server.h" |
paul@9 | 26 | |
paul@32 | 27 | #include <string.h> |
paul@32 | 28 | |
paul@32 | 29 | |
paul@32 | 30 | |
paul@9 | 31 | /* Support for providing access to files. */ |
paul@9 | 32 | |
paul@10 | 33 | OpenerContextResource::OpenerContextResource(OpenerResource *opener, Memory *memory) |
paul@9 | 34 | : SimplePager(memory), _opener(opener) |
paul@9 | 35 | { |
paul@9 | 36 | } |
paul@9 | 37 | |
paul@10 | 38 | int OpenerContextResource::expected_items() |
paul@9 | 39 | { |
paul@10 | 40 | return OpenerContextObject_expected_items; |
paul@9 | 41 | } |
paul@9 | 42 | |
paul@10 | 43 | ipc_server_handler_type OpenerContextResource::handler() |
paul@10 | 44 | { |
paul@10 | 45 | return (ipc_server_handler_type) handle_OpenerContextObject; |
paul@10 | 46 | } |
paul@10 | 47 | |
paul@10 | 48 | |
paul@10 | 49 | |
paul@10 | 50 | /* Data access methods. */ |
paul@10 | 51 | |
paul@10 | 52 | char *OpenerContextResource::get_path() |
paul@9 | 53 | { |
paul@32 | 54 | char *buffer = _region->read(); |
paul@32 | 55 | offset_t size = _region->size(); |
paul@32 | 56 | |
paul@32 | 57 | /* Confine the path to the limit of the buffer. */ |
paul@32 | 58 | |
paul@32 | 59 | if ((buffer != NULL) && (strnlen(buffer, size) < size)) |
paul@32 | 60 | return buffer; |
paul@32 | 61 | else |
paul@32 | 62 | return NULL; |
paul@9 | 63 | } |
paul@9 | 64 | |
paul@10 | 65 | |
paul@10 | 66 | |
paul@10 | 67 | /* Opener context interface methods. */ |
paul@10 | 68 | |
paul@92 | 69 | long OpenerContextResource::open(flags_t flags, offset_t *size, l4_cap_idx_t *file) |
paul@10 | 70 | { |
paul@32 | 71 | char *path = get_path(); |
paul@32 | 72 | |
paul@32 | 73 | if (path == NULL) |
paul@32 | 74 | return -L4_EINVAL; |
paul@32 | 75 | |
paul@79 | 76 | Pager *pager = _opener->open(path, flags); |
paul@10 | 77 | |
paul@62 | 78 | /* Complete the initialisation and start a server in a new thread. |
paul@62 | 79 | If the thread does not start, the resource should be finalised. */ |
paul@10 | 80 | |
paul@10 | 81 | ResourceServer server(pager); |
paul@10 | 82 | long err = server.start_thread(); |
paul@10 | 83 | |
paul@42 | 84 | if (!err) |
paul@42 | 85 | { |
paul@42 | 86 | /* Return the file size. */ |
paul@42 | 87 | /* Return the server capability to the caller. */ |
paul@10 | 88 | |
paul@42 | 89 | *size = pager->get_data_size(); |
paul@42 | 90 | *file = server.config()->server; |
paul@42 | 91 | } |
paul@42 | 92 | |
paul@10 | 93 | return err; |
paul@10 | 94 | } |
paul@10 | 95 | |
paul@9 | 96 | // vim: tabstop=4 expandtab shiftwidth=4 |