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