paul@93 | 1 | /* |
paul@93 | 2 | * A resource offering support for creating contexts and opening files. |
paul@93 | 3 | * |
paul@484 | 4 | * Copyright (C) 2021, 2022, 2023 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@224 | 28 | OpenerResource::OpenerResource(ResourceRegistry *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@459 | 37 | ipc_server_default_config_type OpenerResource::config() |
paul@9 | 38 | { |
paul@459 | 39 | return config_Opener; |
paul@10 | 40 | } |
paul@10 | 41 | |
paul@10 | 42 | |
paul@10 | 43 | |
paul@214 | 44 | /* Obtain any active parent directory for a path, notifying its subscribers of |
paul@232 | 45 | the file opening event. |
paul@232 | 46 | NOTE: This might need to happen in the registry due to concurrency issues. */ |
paul@214 | 47 | |
paul@214 | 48 | long OpenerResource::notify_parent(const char *path) |
paul@214 | 49 | { |
paul@220 | 50 | Provider *provider; |
paul@214 | 51 | fileid_t fileid; |
paul@214 | 52 | long err; |
paul@214 | 53 | |
paul@214 | 54 | err = get_parent(path, &fileid); |
paul@214 | 55 | |
paul@211 | 56 | if (err) |
paul@211 | 57 | return err; |
paul@211 | 58 | |
paul@211 | 59 | /* Obtain the provider of the parent directory. */ |
paul@211 | 60 | |
paul@220 | 61 | err = _registry->find_provider(fileid, &provider); |
paul@211 | 62 | |
paul@211 | 63 | if (err) |
paul@211 | 64 | return err; |
paul@211 | 65 | |
paul@211 | 66 | /* With any active provider, send a notification. |
paul@211 | 67 | NOTE: This should also communicate which file was involved, probably |
paul@211 | 68 | NOTE: using the file identifier of the opened file. */ |
paul@211 | 69 | |
paul@484 | 70 | provider->notify_all(NOTIFY_FILE_OPENED, NOTIFY_VALUES_NULL); |
paul@211 | 71 | return L4_EOK; |
paul@211 | 72 | } |
paul@211 | 73 | |
paul@211 | 74 | |
paul@211 | 75 | |
paul@271 | 76 | /* Make a directory. */ |
paul@271 | 77 | |
paul@271 | 78 | long OpenerResource::mkdir(const char *path, sys_mode_t mode) |
paul@271 | 79 | { |
paul@271 | 80 | return make_directory_object(path, mode); |
paul@271 | 81 | } |
paul@271 | 82 | |
paul@205 | 83 | /* Return an object for the given path and flags. */ |
paul@10 | 84 | |
paul@171 | 85 | long OpenerResource::open(const char *path, flags_t flags, offset_t *size, |
paul@475 | 86 | l4_cap_idx_t *file, object_flags_t *object_flags) |
paul@9 | 87 | { |
paul@218 | 88 | Resource *resource; |
paul@218 | 89 | long err = _registry->get_resource(this, path, flags, size, object_flags, &resource); |
paul@156 | 90 | |
paul@156 | 91 | if (err) |
paul@156 | 92 | return err; |
paul@156 | 93 | |
paul@211 | 94 | /* Notify listeners subscribed to the parent of the opened object. */ |
paul@211 | 95 | |
paul@211 | 96 | notify_parent(path); |
paul@211 | 97 | |
paul@218 | 98 | /* Start a server for the resource. */ |
paul@202 | 99 | |
paul@475 | 100 | return ResourceServer(resource).start_thread(file); |
paul@9 | 101 | } |
paul@9 | 102 | |
paul@232 | 103 | /* Request the removal of the named filesystem object. */ |
paul@232 | 104 | |
paul@232 | 105 | long OpenerResource::remove(const char *path) |
paul@232 | 106 | { |
paul@232 | 107 | return _registry->remove_provider(this, path); |
paul@232 | 108 | } |
paul@232 | 109 | |
paul@236 | 110 | /* Request the renaming of the named filesystem object. */ |
paul@236 | 111 | |
paul@236 | 112 | long OpenerResource::rename(const char *source, const char *target) |
paul@236 | 113 | { |
paul@236 | 114 | return _registry->rename_provider(this, source, target); |
paul@236 | 115 | } |
paul@236 | 116 | |
paul@266 | 117 | /* Obtain statistics metadata for the named filesystem object. */ |
paul@266 | 118 | |
paul@266 | 119 | long OpenerResource::stat(const char *path, void *base, offset_t size) |
paul@266 | 120 | { |
paul@266 | 121 | return stat_object(path, base, size); |
paul@266 | 122 | } |
paul@266 | 123 | |
paul@10 | 124 | |
paul@10 | 125 | |
paul@10 | 126 | /* Opener interface methods. */ |
paul@10 | 127 | |
paul@10 | 128 | long OpenerResource::context(l4_cap_idx_t *context) |
paul@10 | 129 | { |
paul@10 | 130 | OpenerContextResource *resource = new OpenerContextResource(this); |
paul@10 | 131 | |
paul@62 | 132 | /* Complete the initialisation and start a server in a new thread. |
paul@120 | 133 | If the thread does not start, the resource will be finalised. */ |
paul@10 | 134 | |
paul@120 | 135 | return ResourceServer(resource).start_thread(context); |
paul@10 | 136 | } |
paul@10 | 137 | |
paul@9 | 138 | // vim: tabstop=4 expandtab shiftwidth=4 |