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@170 | 22 | #include <ipc/cap_alloc.h> |
paul@170 | 23 | |
paul@10 | 24 | #include "opener_context_resource.h" |
paul@10 | 25 | #include "opener_context_object_server.h" |
paul@10 | 26 | #include "opener_resource.h" |
paul@9 | 27 | |
paul@32 | 28 | #include <string.h> |
paul@32 | 29 | |
paul@32 | 30 | |
paul@32 | 31 | |
paul@9 | 32 | /* Support for providing access to files. */ |
paul@9 | 33 | |
paul@10 | 34 | OpenerContextResource::OpenerContextResource(OpenerResource *opener, Memory *memory) |
paul@9 | 35 | : SimplePager(memory), _opener(opener) |
paul@9 | 36 | { |
paul@9 | 37 | } |
paul@9 | 38 | |
paul@10 | 39 | int OpenerContextResource::expected_items() |
paul@9 | 40 | { |
paul@10 | 41 | return OpenerContextObject_expected_items; |
paul@9 | 42 | } |
paul@9 | 43 | |
paul@10 | 44 | ipc_server_handler_type OpenerContextResource::handler() |
paul@10 | 45 | { |
paul@10 | 46 | return (ipc_server_handler_type) handle_OpenerContextObject; |
paul@10 | 47 | } |
paul@10 | 48 | |
paul@10 | 49 | |
paul@10 | 50 | |
paul@10 | 51 | /* Data access methods. */ |
paul@10 | 52 | |
paul@236 | 53 | char *OpenerContextResource::get_path(offset_t offset) |
paul@9 | 54 | { |
paul@236 | 55 | char *buffer = _region->read(offset); |
paul@32 | 56 | offset_t size = _region->size(); |
paul@32 | 57 | |
paul@32 | 58 | /* Confine the path to the limit of the buffer. */ |
paul@32 | 59 | |
paul@32 | 60 | if ((buffer != NULL) && (strnlen(buffer, size) < size)) |
paul@32 | 61 | return buffer; |
paul@32 | 62 | else |
paul@32 | 63 | return NULL; |
paul@9 | 64 | } |
paul@9 | 65 | |
paul@10 | 66 | |
paul@10 | 67 | |
paul@10 | 68 | /* Opener context interface methods. */ |
paul@10 | 69 | |
paul@171 | 70 | long OpenerContextResource::open(flags_t flags, offset_t *size, |
paul@171 | 71 | l4_cap_idx_t *file, object_flags_t *object_flags) |
paul@10 | 72 | { |
paul@32 | 73 | char *path = get_path(); |
paul@32 | 74 | |
paul@32 | 75 | if (path == NULL) |
paul@32 | 76 | return -L4_EINVAL; |
paul@32 | 77 | |
paul@202 | 78 | return _opener->open(path, flags, size, file, object_flags); |
paul@10 | 79 | } |
paul@10 | 80 | |
paul@232 | 81 | long OpenerContextResource::remove() |
paul@232 | 82 | { |
paul@232 | 83 | char *path = get_path(); |
paul@232 | 84 | |
paul@232 | 85 | if (path == NULL) |
paul@232 | 86 | return -L4_EINVAL; |
paul@232 | 87 | |
paul@232 | 88 | return _opener->remove(path); |
paul@232 | 89 | } |
paul@232 | 90 | |
paul@236 | 91 | long OpenerContextResource::rename() |
paul@236 | 92 | { |
paul@236 | 93 | char *source, *target; |
paul@236 | 94 | |
paul@236 | 95 | source = get_path(); |
paul@236 | 96 | |
paul@236 | 97 | if (source == NULL) |
paul@236 | 98 | return -L4_EINVAL; |
paul@236 | 99 | |
paul@236 | 100 | target = get_path(strlen(source) + 1); |
paul@236 | 101 | |
paul@236 | 102 | if (target == NULL) |
paul@236 | 103 | return -L4_EINVAL; |
paul@236 | 104 | |
paul@236 | 105 | return _opener->rename(source, target); |
paul@236 | 106 | } |
paul@236 | 107 | |
paul@9 | 108 | // vim: tabstop=4 expandtab shiftwidth=4 |