paul@102 | 1 | /* |
paul@106 | 2 | * A file accessor employing a file provided by an Ext2-compatible filesystem. |
paul@102 | 3 | * |
paul@102 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@102 | 5 | * |
paul@102 | 6 | * This program is free software; you can redistribute it and/or |
paul@102 | 7 | * modify it under the terms of the GNU General Public License as |
paul@102 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@102 | 9 | * the License, or (at your option) any later version. |
paul@102 | 10 | * |
paul@102 | 11 | * This program is distributed in the hope that it will be useful, |
paul@102 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@102 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@102 | 14 | * GNU General Public License for more details. |
paul@102 | 15 | * |
paul@102 | 16 | * You should have received a copy of the GNU General Public License |
paul@102 | 17 | * along with this program; if not, write to the Free Software |
paul@102 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@102 | 19 | * Boston, MA 02110-1301, USA |
paul@102 | 20 | */ |
paul@102 | 21 | |
paul@102 | 22 | #include <stdio.h> |
paul@102 | 23 | #include <string.h> |
paul@102 | 24 | |
paul@102 | 25 | #include <algorithm> |
paul@102 | 26 | |
paul@102 | 27 | #include <systypes/fcntl.h> |
paul@102 | 28 | |
paul@106 | 29 | #include "ext2_file_accessor.h" |
paul@102 | 30 | |
paul@102 | 31 | |
paul@102 | 32 | |
paul@148 | 33 | Ext2FileAccessor::Ext2FileAccessor(Ext2FileOperations *ops, ext2_file_t file, |
paul@148 | 34 | fileid_t fileid) |
paul@148 | 35 | : Accessor(fileid), _ops(ops), _file(file) |
paul@102 | 36 | { |
paul@102 | 37 | } |
paul@102 | 38 | |
paul@102 | 39 | /* Return the size of the file. */ |
paul@102 | 40 | |
paul@106 | 41 | offset_t Ext2FileAccessor::get_size() |
paul@102 | 42 | { |
paul@148 | 43 | return _ops->get_size(_file); |
paul@102 | 44 | } |
paul@102 | 45 | |
paul@102 | 46 | /* Update the size of the file. */ |
paul@102 | 47 | |
paul@106 | 48 | void Ext2FileAccessor::set_size(offset_t size) |
paul@102 | 49 | { |
paul@148 | 50 | _ops->set_size(_file, size); |
paul@102 | 51 | } |
paul@102 | 52 | |
paul@102 | 53 | /* Perform any closing operation on the file. */ |
paul@102 | 54 | |
paul@106 | 55 | void Ext2FileAccessor::close() |
paul@102 | 56 | { |
paul@148 | 57 | _ops->close_file(_file); |
paul@102 | 58 | } |
paul@102 | 59 | |
paul@102 | 60 | /* Data transfer helper methods. */ |
paul@102 | 61 | |
paul@102 | 62 | /* Fill the populated portion of a flexpage. */ |
paul@102 | 63 | |
paul@106 | 64 | void Ext2FileAccessor::fill_populated(Flexpage *flexpage) |
paul@102 | 65 | { |
paul@102 | 66 | offset_t filepos = flexpage->base_offset; |
paul@102 | 67 | offset_t addr = flexpage->base_addr; |
paul@102 | 68 | |
paul@102 | 69 | /* Tag the region with file state. */ |
paul@102 | 70 | |
paul@102 | 71 | flexpage->region->fill(fileid, filepos); |
paul@102 | 72 | |
paul@102 | 73 | /* Fill the region with file content. */ |
paul@102 | 74 | |
paul@148 | 75 | offset_t nread = _ops->read_file(_file, filepos, (void *) addr, flexpage->size); |
paul@102 | 76 | |
paul@102 | 77 | /* Pad the flexpage with zero. */ |
paul@102 | 78 | |
paul@102 | 79 | memset((void *) (addr + nread), 0, flexpage->size - nread); |
paul@102 | 80 | } |
paul@102 | 81 | |
paul@102 | 82 | /* Flush the populated portion of a flexpage. */ |
paul@102 | 83 | |
paul@106 | 84 | void Ext2FileAccessor::flush_populated(Flexpage *flexpage) |
paul@102 | 85 | { |
paul@102 | 86 | offset_t filepos = flexpage->base_offset; |
paul@102 | 87 | offset_t addr = flexpage->base_addr; |
paul@102 | 88 | offset_t populated_size = std::min(flexpage->size, get_size() - filepos); |
paul@102 | 89 | |
paul@102 | 90 | /* Remove the file state tag from the region. */ |
paul@102 | 91 | |
paul@102 | 92 | flexpage->region->flush(); |
paul@102 | 93 | |
paul@102 | 94 | /* Copy the populated region to the file. */ |
paul@102 | 95 | |
paul@148 | 96 | _ops->write_file(_file, filepos, (const void *) addr, populated_size); |
paul@102 | 97 | } |
paul@102 | 98 | |
paul@102 | 99 | // vim: tabstop=4 expandtab shiftwidth=4 |