1 #include <stdio.h> 2 #include <string.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 6 #include "host_file_accessor.h" 7 8 HostFileAccessor::HostFileAccessor(const char *path, fileid_t fileid) 9 : Accessor(fileid), _path(path) 10 { 11 /* Initialise the size of the file. */ 12 13 struct stat buf; 14 15 if (!stat(_path, &buf)) 16 _size = buf.st_size; 17 else 18 _size = 0; 19 } 20 21 /* Perform any closing operation on the file. */ 22 23 void HostFileAccessor::close() 24 { 25 fclose(_fp); 26 } 27 28 /* Perform any opening operation on the file. */ 29 30 void HostFileAccessor::open() 31 { 32 _fp = fopen(_path, "r"); 33 } 34 35 /* Data transfer helper methods. */ 36 37 void HostFileAccessor::fill_populated(Flexpage *flexpage) 38 { 39 offset_t filepos = flexpage->base_offset; 40 offset_t addr = flexpage->base_addr; 41 42 /* Seek to the offset in the file. */ 43 44 fseek(_fp, filepos, SEEK_SET); 45 46 /* Tag the region with file state. */ 47 48 flexpage->region->fill(fileid, filepos); 49 50 /* Fill the region with file content. */ 51 52 size_t nread = fread((void *) addr, sizeof(char), flexpage->size, _fp); 53 54 /* Pad the flexpage with zero. */ 55 56 memset((void *) (addr + nread), 0, flexpage->size - nread); 57 } 58 59 void HostFileAccessor::flush_populated(Flexpage *flexpage) 60 { 61 flexpage->region->flush(); 62 } 63 64 // vim: tabstop=4 expandtab shiftwidth=4