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