1 /* 2 * An object providing access to file 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 "file_pager.h" 23 #include "file_provider.h" 24 25 26 27 /* Initialise the provider with a page 'mapper' for the file's contents. */ 28 29 FileProvider::FileProvider(fileid_t fileid, ProviderRegistry *registry, 30 PageMapper *mapper) 31 : Provider(fileid, registry), _mapper(mapper) 32 { 33 } 34 35 /* Deallocate the provider's resources. */ 36 37 FileProvider::~FileProvider() 38 { 39 /* Accessors are allocated exclusively for page mappers for files, and 40 so can be deleted here. */ 41 42 Accessor *accessor = _mapper->accessor(); 43 delete _mapper; 44 45 accessor->close(); 46 47 if (_remove) 48 accessor->remove(); 49 50 delete accessor; 51 } 52 53 /* Return the page mapper provided. */ 54 55 PageMapper *FileProvider::mapper() 56 { 57 return _mapper; 58 } 59 60 /* Return a file pager initialised with a provider, page mapper and accessor. */ 61 62 long FileProvider::make_resource(flags_t flags, offset_t *size, 63 object_flags_t *object_flags, 64 Resource **resource) 65 { 66 /* Initialise the pager with the provider and a reference to this object for 67 detaching from the provider. */ 68 69 FilePager *pager = new FilePager(_fileid, this, flags); 70 71 /* Obtain the size details from the pager, also providing appropriate 72 flags. */ 73 74 *size = pager->get_data_size(); 75 *object_flags = OBJECT_SUPPORTS_MMAP | OBJECT_HAS_SIZE; 76 77 this->attach(); 78 *resource = pager; 79 return L4_EOK; 80 } 81 82 // vim: tabstop=4 expandtab shiftwidth=4