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