1 /* 2 * A file accessor employing a file provided by an Ext2-compatible filesystem. 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 <stdio.h> 23 #include <string.h> 24 25 #include <algorithm> 26 27 #include <systypes/fcntl.h> 28 29 #include "ext2_file_accessor.h" 30 31 32 33 Ext2FileAccessor::Ext2FileAccessor(Ext2FileOperations *ops, ext2_file_t file, 34 fileid_t fileid) 35 : Accessor(fileid), _ops(ops), _file(file) 36 { 37 } 38 39 /* Return the size of the file. */ 40 41 offset_t Ext2FileAccessor::get_size() 42 { 43 return _ops->get_size(_file); 44 } 45 46 /* Update the size of the file. */ 47 48 void Ext2FileAccessor::set_size(offset_t size) 49 { 50 _ops->set_size(_file, size); 51 } 52 53 /* Perform any closing operation on the file. */ 54 55 void Ext2FileAccessor::close() 56 { 57 if (_closed) 58 return; 59 60 _ops->close_file(_file); 61 _closed = true; 62 } 63 64 /* Data transfer helper methods. */ 65 66 /* Fill the populated portion of a flexpage. */ 67 68 void Ext2FileAccessor::fill_populated(Flexpage *flexpage) 69 { 70 offset_t filepos = flexpage->base_offset; 71 offset_t addr = flexpage->base_addr; 72 73 /* Tag the region with file state. */ 74 75 flexpage->region->fill(fileid, filepos); 76 77 /* Fill the region with file content. */ 78 79 offset_t nread = _ops->read_file(_file, filepos, (void *) addr, flexpage->size); 80 81 /* Pad the flexpage with zero. */ 82 83 memset((void *) (addr + nread), 0, flexpage->size - nread); 84 } 85 86 /* Flush the populated portion of a flexpage. */ 87 88 void Ext2FileAccessor::flush_populated(Flexpage *flexpage) 89 { 90 offset_t filepos = flexpage->base_offset; 91 offset_t addr = flexpage->base_addr; 92 offset_t populated_size = std::min(flexpage->size, get_size() - filepos); 93 94 /* Remove the file state tag from the region. */ 95 96 flexpage->region->flush(); 97 98 /* Copy the populated region to the file. */ 99 100 _ops->write_file(_file, filepos, (const void *) addr, populated_size); 101 } 102 103 104 105 /* Lifecycle methods. */ 106 107 /* Remove the underlying filesystem object. */ 108 109 void Ext2FileAccessor::remove() 110 { 111 _ops->remove(fileid); 112 } 113 114 // vim: tabstop=4 expandtab shiftwidth=4