1 /* 2 * File registry and opening functionality. 3 * 4 * Copyright (C) 2021 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_object_registry.h" 24 #include "file_pager.h" 25 26 #include <systypes/fcntl.h> 27 28 29 30 /* The file object registry provides a memory page collection to the page 31 mappers it creates. */ 32 33 FileObjectRegistry::FileObjectRegistry(Pages *pages) 34 : _pages(pages) 35 { 36 } 37 38 39 40 /* Convert opening flags to map-compatible paging flags. */ 41 42 map_flags_t FileObjectRegistry::get_flags(flags_t flags) 43 { 44 return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; 45 } 46 47 48 49 /* Make a directory provider. */ 50 51 long FileObjectRegistry::make_directory_provider(FileOpening *opening, 52 const char *path, flags_t flags, 53 fileid_t fileid, 54 Provider **provider) 55 { 56 /* Make an accessor and a provider to encapsulate it. */ 57 58 DirectoryAccessor *accessor; 59 long err = opening->make_directory_accessor(path, flags, fileid, &accessor); 60 61 if (err) 62 return err; 63 64 *provider = new DirectoryProvider(fileid, this, accessor); 65 return L4_EOK; 66 } 67 68 /* Make a file provider. */ 69 70 long FileObjectRegistry::make_file_provider(FileOpening *opening, 71 const char *path, flags_t flags, 72 fileid_t fileid, 73 Provider **provider) 74 { 75 /* Make an accessor, page mapper, and a provider to encapsulate them. */ 76 77 Accessor *accessor; 78 long err = opening->make_accessor(path, flags, fileid, &accessor); 79 80 if (err) 81 return err; 82 83 PageMapper *mapper = new PageMapper(accessor, _pages); 84 *provider = new FileProvider(fileid, get_flags(flags), this, mapper); 85 return L4_EOK; 86 } 87 88 /* Make a provider of the appropriate type. */ 89 90 long FileObjectRegistry::make_provider(FileOpening *opening, 91 const char *path, flags_t flags, 92 fileid_t fileid, 93 Provider **provider) 94 { 95 long err = -L4_EIO; 96 97 if (opening->accessing_directory(path, flags, fileid)) 98 err = make_directory_provider(opening, path, flags, fileid, provider); 99 else if (opening->accessing_file(path, flags, fileid)) 100 err = make_file_provider(opening, path, flags, fileid, provider); 101 102 if (err) 103 return err; 104 105 /* Register the provider. */ 106 107 set(fileid, *provider); 108 return L4_EOK; 109 } 110 111 112 113 /* Return a resource for a filesystem object. */ 114 115 long FileObjectRegistry::get_resource(FileOpening *opening, const char *path, 116 flags_t flags, offset_t *size, 117 object_flags_t *object_flags, 118 Resource **resource) 119 { 120 std::lock_guard<std::mutex> guard(_lock); 121 122 /* Obtain an identifier for the object, even for new files (subject to use 123 of the appropriate flag). */ 124 125 fileid_t fileid; 126 long err = opening->get_fileid(path, flags, &fileid); 127 128 if (err) 129 return err; 130 131 /* Obtain a provider for the object. */ 132 133 Provider *provider; 134 err = find_provider(fileid, &provider); 135 136 /* Make a new provider if necessary. */ 137 138 if (err == -L4_ENOENT) 139 err = make_provider(opening, path, flags, fileid, &provider); 140 141 if (err) 142 return err; 143 144 /* Make a resource for the provider. */ 145 146 return provider->make_resource(size, object_flags, resource); 147 } 148 149 150 151 /* Obtain any active provider for the given 'fileid'. */ 152 153 long FileObjectRegistry::find_provider(fileid_t fileid, Provider **provider) 154 { 155 /* Obtain any registered provider. */ 156 157 Accountable *accountable = get(fileid); 158 159 if (accountable != NULL) 160 { 161 *provider = dynamic_cast<Provider *>(accountable); 162 163 if ((*provider) != NULL) 164 return L4_EOK; 165 else 166 return -L4_EIO; 167 } 168 169 return -L4_ENOENT; 170 } 171 172 // vim: tabstop=4 expandtab shiftwidth=4