1 /* 2 * A resource offering support for creating contexts and opening files. 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 <string.h> 23 24 #include "opener_server.h" 25 #include "opener_resource.h" 26 #include "resource_server.h" 27 28 /* Support for providing access to files. */ 29 30 OpenerResource::OpenerResource(FileObjectRegistry *registry) 31 : _registry(registry) 32 { 33 } 34 35 OpenerResource::~OpenerResource() 36 { 37 } 38 39 int OpenerResource::expected_items() 40 { 41 return Opener_expected_items; 42 } 43 44 ipc_server_handler_type OpenerResource::handler() 45 { 46 return (ipc_server_handler_type) handle_Opener; 47 } 48 49 50 51 /* Obtain any active parent directory for a path, notifying its subscribers of 52 the file opening event. */ 53 54 long OpenerResource::notify_parent(const char *path) 55 { 56 char *sep = strrchr(path, (int) '/'); 57 DirectoryProvider *provider; 58 fileid_t fileid; 59 long err; 60 61 /* For top-level paths, use the empty string to get the root directory. 62 Otherwise, obtain the parent directory path to obtain the file 63 identifier. */ 64 65 if (sep == NULL) 66 err = get_fileid("", 0, &fileid); 67 else 68 { 69 char *parent = strndup(path, sep - path + 1); 70 71 parent[sep - path] = '\0'; 72 err = get_fileid(parent, 0, &fileid); 73 free(parent); 74 } 75 76 if (err) 77 return err; 78 79 /* Obtain the provider of the parent directory. */ 80 81 err = _registry->find_directory_provider(fileid, &provider); 82 83 if (err) 84 return err; 85 86 /* With any active provider, send a notification. 87 NOTE: This should also communicate which file was involved, probably 88 NOTE: using the file identifier of the opened file. */ 89 90 provider->notify_all(NOTIFY_FILE_OPENED); 91 return L4_EOK; 92 } 93 94 95 96 /* Return an object for the given path and flags. */ 97 98 long OpenerResource::open(const char *path, flags_t flags, offset_t *size, 99 l4_cap_idx_t *cap, object_flags_t *object_flags) 100 { 101 /* Obtain an identifier for the file, even for new files. */ 102 103 fileid_t fileid; 104 long err = get_fileid(path, flags, &fileid); 105 106 if (err) 107 return err; 108 109 /* Notify listeners subscribed to the parent of the opened object. */ 110 111 notify_parent(path); 112 113 /* Test for file and directory access. */ 114 115 if (accessing_directory(path, flags, fileid)) 116 return get_directory(path, flags, fileid, size, cap, object_flags); 117 else if (accessing_file(path, flags, fileid)) 118 return get_file(path, flags, fileid, size, cap, object_flags); 119 else 120 return -L4_EIO; 121 } 122 123 124 125 /* Return a directory object reference for the given file identifier. */ 126 127 long OpenerResource::get_directory(const char *path, flags_t flags, 128 fileid_t fileid, offset_t *size, 129 l4_cap_idx_t *cap, 130 object_flags_t *object_flags) 131 { 132 Resource *directory; 133 long err = _registry->get_directory(this, path, flags, fileid, &directory); 134 135 if (err) 136 return err; 137 138 /* Provide non-file values for certain outputs. */ 139 140 *size = 0; 141 *object_flags = 0; 142 143 return ResourceServer(directory).start_thread(cap); 144 } 145 146 /* Return a file pager. */ 147 148 long OpenerResource::get_file(const char *path, flags_t flags, fileid_t fileid, 149 offset_t *size, l4_cap_idx_t *cap, 150 object_flags_t *object_flags) 151 { 152 Pager *pager; 153 long err = _registry->get_pager(this, path, flags, fileid, &pager); 154 155 if (err) 156 return err; 157 158 /* Obtain the size details from the pager, also providing appropriate 159 flags. */ 160 161 *size = pager->get_data_size(); 162 *object_flags = OBJECT_SUPPORTS_MMAP | OBJECT_HAS_SIZE; 163 164 return ResourceServer(pager).start_thread(cap); 165 } 166 167 168 169 /* Opener interface methods. */ 170 171 long OpenerResource::context(l4_cap_idx_t *context) 172 { 173 OpenerContextResource *resource = new OpenerContextResource(this); 174 175 /* Complete the initialisation and start a server in a new thread. 176 If the thread does not start, the resource will be finalised. */ 177 178 return ResourceServer(resource).start_thread(context); 179 } 180 181 // vim: tabstop=4 expandtab shiftwidth=4