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 "opener_server.h" 23 #include "opener_resource.h" 24 #include "resource_server.h" 25 26 /* Support for providing access to files. */ 27 28 OpenerResource::OpenerResource(FilePaging *paging) 29 : _paging(paging) 30 { 31 } 32 33 OpenerResource::~OpenerResource() 34 { 35 } 36 37 int OpenerResource::expected_items() 38 { 39 return Opener_expected_items; 40 } 41 42 ipc_server_handler_type OpenerResource::handler() 43 { 44 return (ipc_server_handler_type) handle_Opener; 45 } 46 47 48 49 /* Return a pager object for the given path and flags. */ 50 51 long OpenerResource::open(const char *path, flags_t flags, Pager **pager) 52 { 53 /* Obtain an identifier for the file, even for new files. */ 54 55 fileid_t fileid; 56 long err = get_fileid(path, flags, &fileid); 57 58 if (err) 59 return err; 60 61 /* Test for file and directory access. */ 62 63 if (accessing_directory(path, flags, fileid)) 64 return get_directory(path, flags, fileid, pager); 65 else if (accessing_file(path, flags, fileid)) 66 return get_file(path, flags, fileid, pager); 67 else 68 return -L4_EIO; 69 } 70 71 72 73 /* Return a directory pager. */ 74 75 long OpenerResource::get_directory(const char *path, flags_t flags, fileid_t fileid, Pager **pager) 76 { 77 (void) path; (void) flags; (void) fileid; *pager = NULL; 78 return -L4_EIO; 79 } 80 81 /* Return a file pager. */ 82 83 long OpenerResource::get_file(const char *path, flags_t flags, fileid_t fileid, Pager **pager) 84 { 85 return _paging->get_pager(this, path, flags, fileid, pager); 86 } 87 88 89 90 /* Opener interface methods. */ 91 92 long OpenerResource::context(l4_cap_idx_t *context) 93 { 94 OpenerContextResource *resource = new OpenerContextResource(this); 95 96 /* Complete the initialisation and start a server in a new thread. 97 If the thread does not start, the resource will be finalised. */ 98 99 return ResourceServer(resource).start_thread(context); 100 } 101 102 // vim: tabstop=4 expandtab shiftwidth=4