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 methods. */ 36 37 void HostFileAccessor::fill(Flexpage *flexpage) 38 { 39 /* Obtain the offset and seek in the file. */ 40 41 offset_t filepos = flexpage->base_offset; 42 43 fseek(_fp, filepos, SEEK_SET); 44 45 /* Tag the region with file state. */ 46 47 flexpage->region->fill(fileid, filepos); 48 49 /* Fill the region with file content. */ 50 51 offset_t addr = flexpage->base_addr; 52 53 size_t nread = fread((void *) addr, sizeof(char), flexpage->size, _fp); 54 55 /* Pad the flexpage with zero. */ 56 57 memset((void *) (addr + nread), 0, flexpage->size - nread); 58 } 59 60 void HostFileAccessor::flush(Flexpage *flexpage) 61 { 62 /* NOTE: A real flush operation would store the memory content and update 63 the file size, if appropriate. */ 64 65 flexpage->region->flush(); 66 } 67 68 // vim: tabstop=4 expandtab shiftwidth=4