paul@93 | 1 | /* |
paul@202 | 2 | * File registry and opening functionality. |
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@202 | 22 | #include "directory_resource.h" |
paul@209 | 23 | #include "file_object_registry.h" |
paul@79 | 24 | #include "file_pager.h" |
paul@9 | 25 | |
paul@85 | 26 | #include <systypes/fcntl.h> |
paul@85 | 27 | |
paul@79 | 28 | |
paul@79 | 29 | |
paul@209 | 30 | /* The file object registry provides a memory page collection to the page |
paul@200 | 31 | mappers it creates. */ |
paul@200 | 32 | |
paul@209 | 33 | FileObjectRegistry::FileObjectRegistry(Pages *pages) |
paul@200 | 34 | : _pages(pages) |
paul@94 | 35 | { |
paul@94 | 36 | } |
paul@94 | 37 | |
paul@85 | 38 | |
paul@85 | 39 | |
paul@87 | 40 | /* Convert opening flags to map-compatible paging flags. */ |
paul@85 | 41 | |
paul@209 | 42 | map_flags_t FileObjectRegistry::get_flags(flags_t flags) |
paul@85 | 43 | { |
paul@87 | 44 | return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; |
paul@85 | 45 | } |
paul@85 | 46 | |
paul@85 | 47 | |
paul@85 | 48 | |
paul@208 | 49 | /* Obtain any active provider for 'fileid'. */ |
paul@79 | 50 | |
paul@209 | 51 | long FileObjectRegistry::find_directory_provider(fileid_t fileid, |
paul@209 | 52 | DirectoryProvider **directory_provider) |
paul@79 | 53 | { |
paul@200 | 54 | /* Obtain any registered provider. */ |
paul@200 | 55 | |
paul@200 | 56 | Accountable *provider = get(fileid); |
paul@79 | 57 | |
paul@200 | 58 | if (provider != NULL) |
paul@200 | 59 | { |
paul@202 | 60 | *directory_provider = dynamic_cast<DirectoryProvider *>(provider); |
paul@202 | 61 | |
paul@202 | 62 | if ((*directory_provider) != NULL) |
paul@202 | 63 | return L4_EOK; |
paul@202 | 64 | else |
paul@202 | 65 | return -L4_EIO; |
paul@202 | 66 | } |
paul@202 | 67 | |
paul@208 | 68 | return -L4_ENOENT; |
paul@208 | 69 | } |
paul@208 | 70 | |
paul@208 | 71 | /* Obtain a provider for the 'fileid' or register a new one. */ |
paul@208 | 72 | |
paul@209 | 73 | long FileObjectRegistry::get_directory_provider(FileOpening *opening, |
paul@209 | 74 | const char *path, flags_t flags, |
paul@209 | 75 | fileid_t fileid, |
paul@209 | 76 | DirectoryProvider **directory_provider) |
paul@208 | 77 | { |
paul@208 | 78 | long err = find_directory_provider(fileid, directory_provider); |
paul@208 | 79 | |
paul@208 | 80 | if (err != -L4_ENOENT) |
paul@208 | 81 | return err; |
paul@208 | 82 | |
paul@202 | 83 | /* Make an accessor and a provider to encapsulate it. */ |
paul@202 | 84 | |
paul@202 | 85 | DirectoryAccessor *accessor; |
paul@208 | 86 | err = opening->make_directory_accessor(path, flags, fileid, &accessor); |
paul@202 | 87 | |
paul@202 | 88 | if (err) |
paul@202 | 89 | return err; |
paul@202 | 90 | |
paul@202 | 91 | *directory_provider = new DirectoryProvider(accessor); |
paul@200 | 92 | |
paul@202 | 93 | /* Register the provider. */ |
paul@202 | 94 | |
paul@202 | 95 | set(fileid, *directory_provider); |
paul@202 | 96 | return L4_EOK; |
paul@202 | 97 | } |
paul@202 | 98 | |
paul@202 | 99 | /* Obtain a provider for the 'fileid' or register a new one. */ |
paul@202 | 100 | |
paul@209 | 101 | long FileObjectRegistry::get_file_provider(FileOpening *opening, |
paul@209 | 102 | const char *path, flags_t flags, |
paul@209 | 103 | fileid_t fileid, |
paul@209 | 104 | FileProvider **file_provider) |
paul@202 | 105 | { |
paul@202 | 106 | /* Obtain any registered provider. */ |
paul@202 | 107 | |
paul@202 | 108 | Accountable *provider = get(fileid); |
paul@202 | 109 | |
paul@202 | 110 | if (provider != NULL) |
paul@202 | 111 | { |
paul@200 | 112 | *file_provider = dynamic_cast<FileProvider *>(provider); |
paul@79 | 113 | |
paul@200 | 114 | if ((*file_provider) != NULL) |
paul@200 | 115 | return L4_EOK; |
paul@200 | 116 | else |
paul@200 | 117 | return -L4_EIO; |
paul@200 | 118 | } |
paul@79 | 119 | |
paul@200 | 120 | /* Make an accessor, page mapper, and a provider to encapsulate them. */ |
paul@79 | 121 | |
paul@143 | 122 | Accessor *accessor; |
paul@144 | 123 | long err = opening->make_accessor(path, flags, fileid, &accessor); |
paul@106 | 124 | |
paul@143 | 125 | if (err) |
paul@143 | 126 | return err; |
paul@106 | 127 | |
paul@200 | 128 | PageMapper *mapper = new PageMapper(accessor, _pages); |
paul@200 | 129 | *file_provider = new FileProvider(mapper); |
paul@79 | 130 | |
paul@200 | 131 | /* Register the provider. */ |
paul@79 | 132 | |
paul@200 | 133 | set(fileid, *file_provider); |
paul@143 | 134 | return L4_EOK; |
paul@79 | 135 | } |
paul@79 | 136 | |
paul@79 | 137 | |
paul@79 | 138 | |
paul@202 | 139 | /* Return a directory resource initialised with a provider. */ |
paul@202 | 140 | |
paul@209 | 141 | long FileObjectRegistry::get_directory(FileOpening *opening, const char *path, |
paul@209 | 142 | flags_t flags, fileid_t fileid, |
paul@209 | 143 | Resource **resource) |
paul@202 | 144 | { |
paul@202 | 145 | std::lock_guard<std::mutex> guard(_lock); |
paul@202 | 146 | |
paul@202 | 147 | /* Obtain any existing provider registered for the object, or make a new |
paul@202 | 148 | provider. */ |
paul@202 | 149 | |
paul@202 | 150 | DirectoryProvider *provider; |
paul@202 | 151 | long err = get_directory_provider(opening, path, flags, fileid, &provider); |
paul@202 | 152 | |
paul@202 | 153 | if (err) |
paul@202 | 154 | return err; |
paul@202 | 155 | |
paul@202 | 156 | /* Initialise the resource with the provider and a reference to this object |
paul@202 | 157 | for detaching from the provider. */ |
paul@202 | 158 | |
paul@202 | 159 | provider->attach(); |
paul@202 | 160 | *resource = new DirectoryResource(fileid, provider, this); |
paul@202 | 161 | return L4_EOK; |
paul@202 | 162 | } |
paul@202 | 163 | |
paul@200 | 164 | /* Return a pager initialised with a provider, page mapper and accessor. */ |
paul@79 | 165 | |
paul@209 | 166 | long FileObjectRegistry::get_pager(FileOpening *opening, const char *path, |
paul@209 | 167 | flags_t flags, fileid_t fileid, |
paul@209 | 168 | Pager **pager) |
paul@79 | 169 | { |
paul@79 | 170 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 171 | |
paul@200 | 172 | /* Obtain any existing provider registered for the file, or make a new |
paul@200 | 173 | provider. */ |
paul@143 | 174 | |
paul@200 | 175 | FileProvider *provider; |
paul@202 | 176 | long err = get_file_provider(opening, path, flags, fileid, &provider); |
paul@143 | 177 | |
paul@143 | 178 | if (err) |
paul@143 | 179 | return err; |
paul@143 | 180 | |
paul@200 | 181 | /* Initialise the pager with the provider and a reference to this object for |
paul@202 | 182 | detaching from the provider. */ |
paul@79 | 183 | |
paul@200 | 184 | provider->attach(); |
paul@200 | 185 | *pager = new FilePager(fileid, provider, get_flags(flags), this); |
paul@143 | 186 | return L4_EOK; |
paul@79 | 187 | } |
paul@79 | 188 | |
paul@9 | 189 | // vim: tabstop=4 expandtab shiftwidth=4 |