paul@93 | 1 | /* |
paul@224 | 2 | * Filesystem resource registry incorporating opening functionality. |
paul@93 | 3 | * |
paul@239 | 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@202 | 22 | #include "directory_resource.h" |
paul@79 | 23 | #include "file_pager.h" |
paul@224 | 24 | #include "resource_registry.h" |
paul@9 | 25 | |
paul@85 | 26 | #include <systypes/fcntl.h> |
paul@85 | 27 | |
paul@79 | 28 | |
paul@79 | 29 | |
paul@461 | 30 | /* The resource registry provides a memory page collection to the page mappers |
paul@461 | 31 | it creates. */ |
paul@200 | 32 | |
paul@224 | 33 | ResourceRegistry::ResourceRegistry(Pages *pages) |
paul@200 | 34 | : _pages(pages) |
paul@94 | 35 | { |
paul@94 | 36 | } |
paul@94 | 37 | |
paul@85 | 38 | |
paul@85 | 39 | |
paul@220 | 40 | /* Make a directory provider. */ |
paul@202 | 41 | |
paul@224 | 42 | long ResourceRegistry::make_directory_provider(FileOpening *opening, |
paul@240 | 43 | flags_t flags, |
paul@224 | 44 | fileid_t fileid, |
paul@224 | 45 | Provider **provider) |
paul@208 | 46 | { |
paul@202 | 47 | /* Make an accessor and a provider to encapsulate it. */ |
paul@202 | 48 | |
paul@202 | 49 | DirectoryAccessor *accessor; |
paul@240 | 50 | long err = opening->make_directory_accessor(flags, fileid, &accessor); |
paul@202 | 51 | |
paul@202 | 52 | if (err) |
paul@202 | 53 | return err; |
paul@202 | 54 | |
paul@221 | 55 | *provider = new DirectoryProvider(fileid, this, accessor); |
paul@202 | 56 | return L4_EOK; |
paul@202 | 57 | } |
paul@202 | 58 | |
paul@220 | 59 | /* Make a file provider. */ |
paul@202 | 60 | |
paul@224 | 61 | long ResourceRegistry::make_file_provider(FileOpening *opening, |
paul@240 | 62 | flags_t flags, |
paul@224 | 63 | fileid_t fileid, |
paul@224 | 64 | Provider **provider) |
paul@220 | 65 | { |
paul@200 | 66 | /* Make an accessor, page mapper, and a provider to encapsulate them. */ |
paul@79 | 67 | |
paul@143 | 68 | Accessor *accessor; |
paul@240 | 69 | long err = opening->make_accessor(flags, fileid, &accessor); |
paul@106 | 70 | |
paul@143 | 71 | if (err) |
paul@143 | 72 | return err; |
paul@106 | 73 | |
paul@200 | 74 | PageMapper *mapper = new PageMapper(accessor, _pages); |
paul@330 | 75 | *provider = new FileProvider(fileid, this, mapper); |
paul@143 | 76 | return L4_EOK; |
paul@79 | 77 | } |
paul@79 | 78 | |
paul@220 | 79 | /* Make a provider of the appropriate type. */ |
paul@220 | 80 | |
paul@224 | 81 | long ResourceRegistry::make_provider(FileOpening *opening, |
paul@240 | 82 | flags_t flags, |
paul@224 | 83 | fileid_t fileid, |
paul@224 | 84 | Provider **provider) |
paul@220 | 85 | { |
paul@223 | 86 | long err = -L4_EIO; |
paul@223 | 87 | |
paul@240 | 88 | if (opening->accessing_directory(flags, fileid)) |
paul@240 | 89 | err = make_directory_provider(opening, flags, fileid, provider); |
paul@240 | 90 | else if (opening->accessing_file(flags, fileid)) |
paul@240 | 91 | err = make_file_provider(opening, flags, fileid, provider); |
paul@223 | 92 | |
paul@223 | 93 | if (err) |
paul@223 | 94 | return err; |
paul@223 | 95 | |
paul@223 | 96 | /* Register the provider. */ |
paul@223 | 97 | |
paul@223 | 98 | set(fileid, *provider); |
paul@223 | 99 | return L4_EOK; |
paul@220 | 100 | } |
paul@220 | 101 | |
paul@79 | 102 | |
paul@79 | 103 | |
paul@224 | 104 | /* Obtain any active provider for the given 'fileid'. */ |
paul@224 | 105 | |
paul@224 | 106 | long ResourceRegistry::find_provider(fileid_t fileid, Provider **provider) |
paul@224 | 107 | { |
paul@224 | 108 | /* Obtain any registered provider. */ |
paul@224 | 109 | |
paul@224 | 110 | Accountable *accountable = get(fileid); |
paul@224 | 111 | |
paul@224 | 112 | if (accountable != NULL) |
paul@224 | 113 | { |
paul@224 | 114 | *provider = dynamic_cast<Provider *>(accountable); |
paul@224 | 115 | |
paul@224 | 116 | if ((*provider) != NULL) |
paul@224 | 117 | return L4_EOK; |
paul@224 | 118 | else |
paul@224 | 119 | return -L4_EIO; |
paul@224 | 120 | } |
paul@224 | 121 | |
paul@224 | 122 | return -L4_ENOENT; |
paul@224 | 123 | } |
paul@224 | 124 | |
paul@224 | 125 | |
paul@224 | 126 | |
paul@218 | 127 | /* Return a resource for a filesystem object. */ |
paul@218 | 128 | |
paul@224 | 129 | long ResourceRegistry::get_resource(FileOpening *opening, const char *path, |
paul@224 | 130 | flags_t flags, offset_t *size, |
paul@224 | 131 | object_flags_t *object_flags, |
paul@224 | 132 | Resource **resource) |
paul@218 | 133 | { |
paul@218 | 134 | std::lock_guard<std::mutex> guard(_lock); |
paul@218 | 135 | |
paul@220 | 136 | /* Obtain an identifier for the object, even for new files (subject to use |
paul@239 | 137 | of the appropriate flag). This also checks the access rights for any |
paul@239 | 138 | configured user. */ |
paul@218 | 139 | |
paul@218 | 140 | fileid_t fileid; |
paul@218 | 141 | long err = opening->get_fileid(path, flags, &fileid); |
paul@218 | 142 | |
paul@218 | 143 | if (err) |
paul@218 | 144 | return err; |
paul@218 | 145 | |
paul@220 | 146 | /* Obtain a provider for the object. */ |
paul@220 | 147 | |
paul@220 | 148 | Provider *provider; |
paul@220 | 149 | err = find_provider(fileid, &provider); |
paul@220 | 150 | |
paul@220 | 151 | /* Make a new provider if necessary. */ |
paul@220 | 152 | |
paul@220 | 153 | if (err == -L4_ENOENT) |
paul@240 | 154 | err = make_provider(opening, flags, fileid, &provider); |
paul@220 | 155 | |
paul@220 | 156 | if (err) |
paul@220 | 157 | return err; |
paul@220 | 158 | |
paul@220 | 159 | /* Make a resource for the provider. */ |
paul@220 | 160 | |
paul@346 | 161 | return provider->make_resource(flags, size, object_flags, resource); |
paul@220 | 162 | } |
paul@220 | 163 | |
paul@234 | 164 | /* Request the removal of a filesystem object through any active provider or |
paul@234 | 165 | perform the direct removal of any inactive object. */ |
paul@232 | 166 | |
paul@232 | 167 | long ResourceRegistry::remove_provider(FileOpening *opening, const char *path) |
paul@232 | 168 | { |
paul@232 | 169 | std::lock_guard<std::mutex> guard(_lock); |
paul@232 | 170 | |
paul@239 | 171 | /* Obtain an identifier for any recognised object. This also checks the |
paul@239 | 172 | access rights for any configured user. */ |
paul@232 | 173 | |
paul@232 | 174 | fileid_t fileid; |
paul@232 | 175 | long err = opening->get_fileid(path, 0, &fileid); |
paul@232 | 176 | |
paul@232 | 177 | if (err) |
paul@232 | 178 | return err; |
paul@232 | 179 | |
paul@236 | 180 | return _remove_provider(opening, path, fileid); |
paul@236 | 181 | } |
paul@236 | 182 | |
paul@236 | 183 | long ResourceRegistry::_remove_provider(FileOpening *opening, const char *path, |
paul@236 | 184 | fileid_t fileid) |
paul@236 | 185 | { |
paul@232 | 186 | /* Obtain a provider for the object. */ |
paul@232 | 187 | |
paul@232 | 188 | Provider *provider; |
paul@247 | 189 | long err = find_provider(fileid, &provider), lookup_err = err; |
paul@232 | 190 | |
paul@232 | 191 | if (err && (err != -L4_ENOENT)) |
paul@232 | 192 | return err; |
paul@232 | 193 | |
paul@240 | 194 | fileid_t parent_fileid; |
paul@240 | 195 | |
paul@240 | 196 | err = opening->get_parent(path, &parent_fileid); |
paul@240 | 197 | |
paul@240 | 198 | if (err) |
paul@240 | 199 | return err; |
paul@240 | 200 | |
paul@256 | 201 | /* Test for an empty directory. |
paul@256 | 202 | NOTE: A descriptive error should be communicated. */ |
paul@256 | 203 | |
paul@256 | 204 | if (opening->accessing_directory(0, fileid) && !opening->directory_is_empty(fileid)) |
paul@256 | 205 | return -L4_EIO; |
paul@256 | 206 | |
paul@247 | 207 | /* Unlink the object regardless of whether it will be removed. */ |
paul@232 | 208 | |
paul@240 | 209 | err = opening->unlink_object(parent_fileid, fileid); |
paul@232 | 210 | |
paul@232 | 211 | if (err) |
paul@232 | 212 | return err; |
paul@232 | 213 | |
paul@232 | 214 | /* Without a provider being active, remove the object directly. */ |
paul@232 | 215 | |
paul@247 | 216 | if (lookup_err) |
paul@240 | 217 | return opening->remove_object(fileid); |
paul@232 | 218 | |
paul@232 | 219 | /* With a provider active, request the object's removal upon closure. */ |
paul@232 | 220 | |
paul@232 | 221 | provider->remove_pending(true); |
paul@232 | 222 | return L4_EOK; |
paul@232 | 223 | } |
paul@232 | 224 | |
paul@236 | 225 | /* Request the renaming of a filesystem object. */ |
paul@236 | 226 | |
paul@236 | 227 | long ResourceRegistry::rename_provider(FileOpening *opening, const char *source, |
paul@236 | 228 | const char *target) |
paul@236 | 229 | { |
paul@236 | 230 | std::lock_guard<std::mutex> guard(_lock); |
paul@236 | 231 | |
paul@236 | 232 | long err; |
paul@236 | 233 | fileid_t source_fileid, target_fileid; |
paul@236 | 234 | |
paul@239 | 235 | /* Obtain an identifier for any recognised object. This also checks the |
paul@239 | 236 | access rights for any configured user. */ |
paul@236 | 237 | |
paul@236 | 238 | err = opening->get_fileid(source, 0, &source_fileid); |
paul@236 | 239 | |
paul@236 | 240 | if (err) |
paul@236 | 241 | return err; |
paul@236 | 242 | |
paul@236 | 243 | /* If source is a directory, the target must be a new directory or an empty |
paul@236 | 244 | directory. An empty directory will be replaced by the source |
paul@236 | 245 | directory. */ |
paul@236 | 246 | |
paul@240 | 247 | if (opening->accessing_directory(0, source_fileid)) |
paul@236 | 248 | { |
paul@236 | 249 | err = opening->get_fileid(target, 0, &target_fileid); |
paul@236 | 250 | |
paul@236 | 251 | if (err && (err != -L4_ENOENT)) |
paul@236 | 252 | return err; |
paul@236 | 253 | |
paul@236 | 254 | /* Any existing directory must be empty. */ |
paul@236 | 255 | |
paul@236 | 256 | if (!err) |
paul@236 | 257 | { |
paul@236 | 258 | /* NOTE: Should really use the equivalent of ENOTDIR. */ |
paul@236 | 259 | |
paul@240 | 260 | if (!opening->accessing_directory(0, target_fileid)) |
paul@236 | 261 | return -L4_EIO; |
paul@236 | 262 | |
paul@236 | 263 | /* NOTE: Test for empty directory. */ |
paul@236 | 264 | |
paul@236 | 265 | /* The existing directory will be removed. */ |
paul@236 | 266 | |
paul@236 | 267 | err = _remove_provider(opening, target, target_fileid); |
paul@236 | 268 | |
paul@236 | 269 | if (err) |
paul@236 | 270 | return err; |
paul@236 | 271 | } |
paul@236 | 272 | |
paul@236 | 273 | /* The source directory is moved within the filesystem to the parent of |
paul@236 | 274 | the indicated target. */ |
paul@236 | 275 | |
paul@240 | 276 | return opening->rename_object(source, target); |
paul@236 | 277 | } |
paul@236 | 278 | |
paul@236 | 279 | /* If source is a file, the target must be a new or existing file, not a |
paul@236 | 280 | directory. An existing file will be replaced. */ |
paul@236 | 281 | |
paul@240 | 282 | else if (opening->accessing_file(0, source_fileid)) |
paul@236 | 283 | { |
paul@236 | 284 | err = opening->get_fileid(target, 0, &target_fileid); |
paul@236 | 285 | |
paul@236 | 286 | if (err && (err != -L4_ENOENT)) |
paul@236 | 287 | return err; |
paul@236 | 288 | |
paul@236 | 289 | if (!err) |
paul@236 | 290 | { |
paul@236 | 291 | /* NOTE: Should really use the equivalent of EISDIR. */ |
paul@236 | 292 | |
paul@240 | 293 | if (!opening->accessing_file(0, target_fileid)) |
paul@236 | 294 | return -L4_EIO; |
paul@236 | 295 | |
paul@236 | 296 | /* The existing file will be removed. */ |
paul@236 | 297 | |
paul@236 | 298 | err = _remove_provider(opening, target, target_fileid); |
paul@236 | 299 | |
paul@236 | 300 | if (err) |
paul@236 | 301 | return err; |
paul@236 | 302 | } |
paul@236 | 303 | |
paul@236 | 304 | /* The source file is moved within the filesystem to the parent of the |
paul@236 | 305 | indicated target. */ |
paul@236 | 306 | |
paul@240 | 307 | return opening->rename_object(source, target); |
paul@236 | 308 | } |
paul@236 | 309 | |
paul@236 | 310 | /* NOTE: Other object types are to be supported. */ |
paul@236 | 311 | |
paul@236 | 312 | else |
paul@236 | 313 | return -L4_EIO; |
paul@236 | 314 | } |
paul@236 | 315 | |
paul@9 | 316 | // vim: tabstop=4 expandtab shiftwidth=4 |