1 /* 2 * A file accessor using the filesystem client interface. 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 <fsclient/client.h> 28 #include <systypes/fcntl.h> 29 30 #include "client_file_accessor.h" 31 32 33 34 ClientFileAccessor::ClientFileAccessor(const char *path, fileid_t fileid) 35 : Accessor(fileid), _path(path) 36 { 37 } 38 39 /* Return the size of the file. */ 40 41 offset_t ClientFileAccessor::get_size() 42 { 43 return _file->size; 44 } 45 46 /* Update the size of the file. */ 47 48 void ClientFileAccessor::set_size(offset_t size) 49 { 50 file_resize(_file, size); 51 } 52 53 /* Perform any closing operation on the file. */ 54 55 void ClientFileAccessor::close() 56 { 57 client_close(_file); 58 } 59 60 /* Perform any opening operation on the file. */ 61 62 void ClientFileAccessor::open() 63 { 64 _file = client_open(_path, O_RDWR); 65 } 66 67 /* Data transfer helper methods. */ 68 69 /* Fill the populated portion of a flexpage. */ 70 71 void ClientFileAccessor::fill_populated(Flexpage *flexpage) 72 { 73 offset_t filepos = flexpage->base_offset; 74 offset_t addr = flexpage->base_addr; 75 76 /* Tag the region with file state. */ 77 78 flexpage->region->fill(fileid, filepos); 79 80 /* Fill the region with file content. */ 81 82 client_seek(_file, filepos, SEEK_SET); 83 offset_t nread = client_read(_file, (char *) addr, flexpage->size); 84 85 /* Pad the flexpage with zero. */ 86 87 memset((void *) (addr + nread), 0, flexpage->size - nread); 88 } 89 90 /* Flush the populated portion of a flexpage. */ 91 92 void ClientFileAccessor::flush_populated(Flexpage *flexpage) 93 { 94 offset_t filepos = flexpage->base_offset; 95 offset_t addr = flexpage->base_addr; 96 offset_t populated_size = std::min(flexpage->size, get_size() - filepos); 97 98 /* Remove the file state tag from the region. */ 99 100 flexpage->region->flush(); 101 102 /* Copy the populated region to the file. */ 103 104 client_seek(_file, filepos, SEEK_SET); 105 client_write(_file, (char *) addr, populated_size); 106 } 107 108 // vim: tabstop=4 expandtab shiftwidth=4