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