paul@93 | 1 | /* |
paul@93 | 2 | * A context resource offering support for opening files. |
paul@93 | 3 | * |
paul@266 | 4 | * Copyright (C) 2021, 2022 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@459 | 39 | ipc_server_default_config_type OpenerContextResource::config() |
paul@9 | 40 | { |
paul@459 | 41 | return config_OpenerContextObject; |
paul@10 | 42 | } |
paul@10 | 43 | |
paul@10 | 44 | |
paul@10 | 45 | |
paul@10 | 46 | /* Data access methods. */ |
paul@10 | 47 | |
paul@236 | 48 | char *OpenerContextResource::get_path(offset_t offset) |
paul@9 | 49 | { |
paul@236 | 50 | char *buffer = _region->read(offset); |
paul@32 | 51 | offset_t size = _region->size(); |
paul@32 | 52 | |
paul@32 | 53 | /* Confine the path to the limit of the buffer. */ |
paul@32 | 54 | |
paul@32 | 55 | if ((buffer != NULL) && (strnlen(buffer, size) < size)) |
paul@32 | 56 | return buffer; |
paul@32 | 57 | else |
paul@32 | 58 | return NULL; |
paul@9 | 59 | } |
paul@9 | 60 | |
paul@10 | 61 | |
paul@10 | 62 | |
paul@10 | 63 | /* Opener context interface methods. */ |
paul@10 | 64 | |
paul@271 | 65 | long OpenerContextResource::mkdir(sys_mode_t mode) |
paul@271 | 66 | { |
paul@271 | 67 | char *path = get_path(); |
paul@271 | 68 | |
paul@271 | 69 | if (path == NULL) |
paul@271 | 70 | return -L4_EINVAL; |
paul@271 | 71 | |
paul@271 | 72 | return _opener->mkdir(path, mode); |
paul@271 | 73 | } |
paul@271 | 74 | |
paul@171 | 75 | long OpenerContextResource::open(flags_t flags, offset_t *size, |
paul@171 | 76 | l4_cap_idx_t *file, object_flags_t *object_flags) |
paul@10 | 77 | { |
paul@32 | 78 | char *path = get_path(); |
paul@32 | 79 | |
paul@32 | 80 | if (path == NULL) |
paul@32 | 81 | return -L4_EINVAL; |
paul@32 | 82 | |
paul@202 | 83 | return _opener->open(path, flags, size, file, object_flags); |
paul@10 | 84 | } |
paul@10 | 85 | |
paul@232 | 86 | long OpenerContextResource::remove() |
paul@232 | 87 | { |
paul@232 | 88 | char *path = get_path(); |
paul@232 | 89 | |
paul@232 | 90 | if (path == NULL) |
paul@232 | 91 | return -L4_EINVAL; |
paul@232 | 92 | |
paul@232 | 93 | return _opener->remove(path); |
paul@232 | 94 | } |
paul@232 | 95 | |
paul@236 | 96 | long OpenerContextResource::rename() |
paul@236 | 97 | { |
paul@236 | 98 | char *source, *target; |
paul@236 | 99 | |
paul@236 | 100 | source = get_path(); |
paul@236 | 101 | |
paul@236 | 102 | if (source == NULL) |
paul@236 | 103 | return -L4_EINVAL; |
paul@236 | 104 | |
paul@236 | 105 | target = get_path(strlen(source) + 1); |
paul@236 | 106 | |
paul@236 | 107 | if (target == NULL) |
paul@236 | 108 | return -L4_EINVAL; |
paul@236 | 109 | |
paul@236 | 110 | return _opener->rename(source, target); |
paul@236 | 111 | } |
paul@236 | 112 | |
paul@266 | 113 | long OpenerContextResource::stat() |
paul@266 | 114 | { |
paul@266 | 115 | char *path = get_path(); |
paul@266 | 116 | |
paul@266 | 117 | if (path == NULL) |
paul@266 | 118 | return -L4_EINVAL; |
paul@266 | 119 | |
paul@266 | 120 | return _opener->stat(path, (void *) _region->start, _region->size()); |
paul@266 | 121 | } |
paul@266 | 122 | |
paul@9 | 123 | // vim: tabstop=4 expandtab shiftwidth=4 |