2008-07-01 22:36:39 -04:00
|
|
|
#include "ftfileprovider.h"
|
|
|
|
|
|
|
|
ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string
|
|
|
|
hash) : total_size(size), hash(hash), file_name(path), fd(NULL) {
|
2008-07-02 00:05:58 -04:00
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
2008-07-02 00:05:58 -04:00
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
ftFileProvider::~ftFileProvider(){
|
|
|
|
if (fd!=NULL) {
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string ftFileProvider::getHash()
|
|
|
|
{
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ftFileProvider::getFileSize()
|
|
|
|
{
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *data)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ftPMutex); /********** STACK LOCKED MTX ******/
|
2008-07-02 00:05:58 -04:00
|
|
|
if (fd==NULL)
|
|
|
|
{
|
2008-07-01 22:36:39 -04:00
|
|
|
int init = initializeFileAttrs();
|
2008-07-02 00:05:58 -04:00
|
|
|
if (init ==0)
|
|
|
|
{
|
2008-07-01 22:36:39 -04:00
|
|
|
std::cerr <<"Initialization Failed" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-07-02 00:05:58 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use private data, which has a pointer to file, total size etc
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* FIXME: Warning of comparison between unsigned and signed int?
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
int data_size = chunk_size;
|
|
|
|
long base_loc = offset;
|
|
|
|
|
|
|
|
if (base_loc + data_size > total_size)
|
|
|
|
{
|
|
|
|
data_size = total_size - base_loc;
|
|
|
|
std::cerr <<"Chunk Size greater than total file size, adjusting chunk size " << data_size << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data_size > 0)
|
2008-07-02 00:05:58 -04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* seek for base_loc
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
fseek(fd, base_loc, SEEK_SET);
|
|
|
|
|
|
|
|
void *data = malloc(chunk_size);
|
2008-07-02 00:05:58 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* read the data
|
|
|
|
*/
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
if (1 != fread(data, data_size, 1, fd))
|
|
|
|
{
|
|
|
|
std::cerr << "ftFileProvider::getFileData() Failed to get data!";
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* Update status of ftFileStatus to reflect last usage (for GUI display)
|
2008-07-01 22:36:39 -04:00
|
|
|
* We need to store.
|
|
|
|
* (a) Id,
|
|
|
|
* (b) Offset,
|
|
|
|
* (c) Size,
|
|
|
|
* (d) timestamp
|
|
|
|
*/
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
time_t now = time(NULL);
|
2008-07-01 22:36:39 -04:00
|
|
|
req_loc = offset;
|
|
|
|
req_size = data_size;
|
|
|
|
lastTS = now;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::cerr << "No data to read" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ftFileProvider::initializeFileAttrs()
|
|
|
|
{
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* check if the file exists
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
std::cout <<
|
|
|
|
"ftFileProvider::initializeFileAttrs() Filename: " << file_name;
|
|
|
|
}
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* attempt to open file
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
|
|
|
|
fd = fopen(file_name.c_str(), "r+b");
|
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
std::cout <<
|
2008-07-02 00:05:58 -04:00
|
|
|
"ftFileProvider::initializeFileAttrs() Failed to open (r+b): "<< file_name << std::endl;
|
2008-07-01 22:36:39 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* if it opened, find it's length
|
|
|
|
* move to the end
|
|
|
|
*/
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
if (0 != fseek(fd, 0L, SEEK_END))
|
|
|
|
{
|
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() Seek Failed" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_size = ftell(fd);
|
|
|
|
return 1;
|
|
|
|
}
|