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@106 | 24 | #include <sys/stat.h> |
paul@106 | 25 | #include <sys/types.h> |
paul@102 | 26 | |
paul@102 | 27 | #include <algorithm> |
paul@102 | 28 | |
paul@102 | 29 | #include <systypes/fcntl.h> |
paul@102 | 30 | |
paul@106 | 31 | #include "ext2_file_accessor.h" |
paul@102 | 32 | |
paul@102 | 33 | |
paul@102 | 34 | |
paul@106 | 35 | Ext2FileAccessor::Ext2FileAccessor(ext2_file_t file, fileid_t fileid) |
paul@106 | 36 | : Accessor(fileid), _file(file) |
paul@102 | 37 | { |
paul@102 | 38 | } |
paul@102 | 39 | |
paul@102 | 40 | /* Return the size of the file. */ |
paul@102 | 41 | |
paul@106 | 42 | offset_t Ext2FileAccessor::get_size() |
paul@102 | 43 | { |
paul@106 | 44 | return ext2fs_file_get_size(_file); |
paul@102 | 45 | } |
paul@102 | 46 | |
paul@102 | 47 | /* Update the size of the file. */ |
paul@102 | 48 | |
paul@106 | 49 | void Ext2FileAccessor::set_size(offset_t size) |
paul@102 | 50 | { |
paul@106 | 51 | ext2fs_file_set_size(_file, size); |
paul@102 | 52 | } |
paul@102 | 53 | |
paul@102 | 54 | /* Perform any closing operation on the file. */ |
paul@102 | 55 | |
paul@106 | 56 | void Ext2FileAccessor::close() |
paul@102 | 57 | { |
paul@106 | 58 | ext2fs_file_flush(_file); |
paul@106 | 59 | ext2fs_file_close(_file); |
paul@102 | 60 | } |
paul@102 | 61 | |
paul@102 | 62 | /* Data transfer helper methods. */ |
paul@102 | 63 | |
paul@102 | 64 | /* Fill the populated portion of a flexpage. */ |
paul@102 | 65 | |
paul@106 | 66 | void Ext2FileAccessor::fill_populated(Flexpage *flexpage) |
paul@102 | 67 | { |
paul@102 | 68 | offset_t filepos = flexpage->base_offset; |
paul@102 | 69 | offset_t addr = flexpage->base_addr; |
paul@102 | 70 | |
paul@102 | 71 | /* Tag the region with file state. */ |
paul@102 | 72 | |
paul@102 | 73 | flexpage->region->fill(fileid, filepos); |
paul@102 | 74 | |
paul@102 | 75 | /* Fill the region with file content. */ |
paul@102 | 76 | |
paul@106 | 77 | ext2fs_file_llseek(_file, filepos, SEEK_SET, NULL); |
paul@106 | 78 | |
paul@106 | 79 | unsigned int nread; |
paul@106 | 80 | ext2fs_file_read(_file, (void *) addr, flexpage->size, &nread); |
paul@102 | 81 | |
paul@102 | 82 | /* Pad the flexpage with zero. */ |
paul@102 | 83 | |
paul@102 | 84 | memset((void *) (addr + nread), 0, flexpage->size - nread); |
paul@102 | 85 | } |
paul@102 | 86 | |
paul@102 | 87 | /* Flush the populated portion of a flexpage. */ |
paul@102 | 88 | |
paul@106 | 89 | void Ext2FileAccessor::flush_populated(Flexpage *flexpage) |
paul@102 | 90 | { |
paul@102 | 91 | offset_t filepos = flexpage->base_offset; |
paul@102 | 92 | offset_t addr = flexpage->base_addr; |
paul@102 | 93 | offset_t populated_size = std::min(flexpage->size, get_size() - filepos); |
paul@102 | 94 | |
paul@102 | 95 | /* Remove the file state tag from the region. */ |
paul@102 | 96 | |
paul@102 | 97 | flexpage->region->flush(); |
paul@102 | 98 | |
paul@102 | 99 | /* Copy the populated region to the file. */ |
paul@102 | 100 | |
paul@106 | 101 | ext2fs_file_llseek(_file, filepos, SEEK_SET, NULL); |
paul@106 | 102 | ext2fs_file_write(_file, (const void *) addr, populated_size, NULL); |
paul@102 | 103 | } |
paul@102 | 104 | |
paul@102 | 105 | // vim: tabstop=4 expandtab shiftwidth=4 |