1 /* 2 * An object providing access to file 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 "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, map_flags_t flags, 30 ProviderRegistry *registry, PageMapper *mapper) 31 : Provider(fileid, registry), _flags(flags), _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. In general, the page mapper may not have 41 exclusive ownership of an accessor. */ 42 43 Accessor *accessor = _mapper->accessor(); 44 delete _mapper; 45 delete accessor; 46 } 47 48 /* Return the page mapper provided. */ 49 50 PageMapper *FileProvider::mapper() 51 { 52 return _mapper; 53 } 54 55 /* Return a file pager initialised with a provider, page mapper and accessor. */ 56 57 long FileProvider::make_resource(offset_t *size, object_flags_t *object_flags, 58 Resource **resource) 59 { 60 /* Initialise the pager with the provider and a reference to this object for 61 detaching from the provider. */ 62 63 FilePager *pager = new FilePager(_fileid, this, _flags); 64 65 /* Obtain the size details from the pager, also providing appropriate 66 flags. */ 67 68 *size = pager->get_data_size(); 69 *object_flags = OBJECT_SUPPORTS_MMAP | OBJECT_HAS_SIZE; 70 71 this->attach(); 72 *resource = pager; 73 return L4_EOK; 74 } 75 76 /* Lifecycle methods. */ 77 78 /* Remove the file using the accessor. */ 79 80 void FileProvider::remove() 81 { 82 _mapper->accessor()->remove(); 83 } 84 85 // vim: tabstop=4 expandtab shiftwidth=4