2008-07-01 22:36:39 -04:00
|
|
|
#include "ftfileprovider.h"
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
#include "util/rsdir.h"
|
2008-11-02 08:53:17 -05:00
|
|
|
#include <stdlib.h>
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string
|
2008-10-22 14:12:58 -04:00
|
|
|
hash) : mSize(size), hash(hash), file_name(path), fd(NULL)
|
|
|
|
{
|
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-10-22 14:12:58 -04:00
|
|
|
bool ftFileProvider::fileOk()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
return (fd != NULL);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string ftFileProvider::getHash()
|
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2008-08-03 08:45:53 -04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ftFileProvider::getFileSize()
|
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
return mSize;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftFileProvider::FileDetails(FileInfo &info)
|
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2008-08-09 13:03:24 -04:00
|
|
|
info.hash = hash;
|
2008-10-22 14:12:58 -04:00
|
|
|
info.size = mSize;
|
2008-08-09 13:03:24 -04:00
|
|
|
info.path = file_name;
|
|
|
|
info.fname = RsDirUtil::getTopDir(file_name);
|
|
|
|
|
|
|
|
/* Use req_loc / req_size to estimate data rate */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *data)
|
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
/* dodgey checking outside of mutex...
|
|
|
|
* much check again inside FileAttrs().
|
|
|
|
*/
|
|
|
|
if (fd == NULL)
|
|
|
|
if (!initializeFileAttrs())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
|
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-11-13 18:03:46 -05:00
|
|
|
|
|
|
|
uint32_t data_size = chunk_size;
|
|
|
|
uint64_t base_loc = offset;
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-10-22 14:12:58 -04:00
|
|
|
if (base_loc + data_size > mSize)
|
2008-07-01 22:36:39 -04:00
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
data_size = mSize - base_loc;
|
2008-07-01 22:36:39 -04:00
|
|
|
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);
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
// Data space allocated by caller.
|
|
|
|
//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-10-22 14:12:58 -04:00
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() Filename: ";
|
|
|
|
std::cerr << file_name;
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << std::endl;
|
2008-10-22 14:12:58 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
if (fd)
|
|
|
|
return 1;
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* check if the file exists
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
|
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() trying (r+b) ";
|
|
|
|
std::cerr << std::endl;
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* attempt to open file
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-11-13 18:03:46 -05:00
|
|
|
fd = fopen(file_name.c_str(), "rb");
|
2008-07-01 22:36:39 -04:00
|
|
|
if (!fd)
|
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (r+b): ";
|
|
|
|
std::cerr << file_name << std::endl;
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-11-13 18:03:46 -05:00
|
|
|
/* try opening read only */
|
|
|
|
fd = fopen(file_name.c_str(), "rb");
|
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (rb): ";
|
|
|
|
std::cerr << file_name << std::endl;
|
|
|
|
|
|
|
|
/* try opening read only */
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-22 14:12:58 -04:00
|
|
|
uint64_t recvdsize = ftell(fd);
|
|
|
|
|
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() File Expected Size: " << mSize << " RecvdSize: " << recvdsize << std::endl;
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
return 1;
|
|
|
|
}
|