2016-07-27 15:22:59 -04:00
|
|
|
#include "util/rsdir.h"
|
|
|
|
#include "hash_cache.h"
|
|
|
|
|
|
|
|
#define HASHSTORAGE_DEBUG 1
|
|
|
|
|
|
|
|
HashStorage::HashStorage(const std::string& save_file_name)
|
|
|
|
: mFilePath(save_file_name), mHashMtx("Hash Storage mutex")
|
|
|
|
{
|
2016-07-27 18:48:28 -04:00
|
|
|
mRunning = false ;
|
2016-07-27 15:22:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HashStorage::data_tick()
|
|
|
|
{
|
|
|
|
FileHashJob job;
|
2016-07-27 18:48:28 -04:00
|
|
|
RsFileHash hash;
|
|
|
|
uint64_t size ;
|
2016-07-27 15:22:59 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
RS_STACK_MUTEX(mHashMtx) ;
|
|
|
|
|
|
|
|
if(mFilesToHash.empty())
|
2016-08-05 16:37:40 -04:00
|
|
|
{
|
|
|
|
std::cerr << "Stopping hashing thread." << std::endl;
|
|
|
|
shutdown();
|
|
|
|
mRunning = false ;
|
|
|
|
std::cerr << "done." << std::endl;
|
|
|
|
|
|
|
|
usleep(2*1000*1000); // when no files to hash, just wait for 2 secs. This avoids a dramatic loop.
|
2016-07-27 15:22:59 -04:00
|
|
|
return ;
|
2016-08-05 16:37:40 -04:00
|
|
|
}
|
2016-07-27 15:22:59 -04:00
|
|
|
|
|
|
|
job = mFilesToHash.begin()->second ;
|
|
|
|
|
2016-07-27 18:48:28 -04:00
|
|
|
std::cerr << "Hashing file " << job.full_path << "..." ; std::cerr.flush();
|
2016-07-27 15:22:59 -04:00
|
|
|
|
|
|
|
|
2016-07-27 18:48:28 -04:00
|
|
|
if(!RsDirUtil::getFileHash(job.full_path, hash,size, this))
|
|
|
|
std::cerr << "ERROR: cannot hash file " << job.full_path << std::endl;
|
|
|
|
else
|
|
|
|
std::cerr << "done."<< std::endl;
|
2016-07-27 15:22:59 -04:00
|
|
|
|
2016-07-27 18:48:28 -04:00
|
|
|
mFilesToHash.erase(mFilesToHash.begin()) ;
|
2016-07-27 15:22:59 -04:00
|
|
|
|
2016-07-28 04:49:49 -04:00
|
|
|
// store the result
|
|
|
|
|
|
|
|
HashStorageInfo& info(mFiles[job.full_path]);
|
|
|
|
|
|
|
|
info.filename = job.full_path ;
|
|
|
|
info.size = size ;
|
|
|
|
info.modf_stamp = job.ts ;
|
|
|
|
info.time_stamp = time(NULL);
|
|
|
|
info.hash = hash;
|
2016-07-27 18:48:28 -04:00
|
|
|
}
|
2016-07-27 15:22:59 -04:00
|
|
|
// call the client
|
|
|
|
|
2016-07-27 18:48:28 -04:00
|
|
|
if(!hash.isNull())
|
|
|
|
job.client->hash_callback(job.client_param, job.full_path, hash, size);
|
2016-07-27 15:22:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HashStorage::requestHash(const std::string& full_path,uint64_t size,time_t mod_time,RsFileHash& known_hash,HashStorageClient *c,uint32_t client_param)
|
|
|
|
{
|
|
|
|
// check if the hash is up to date w.r.t. cache.
|
|
|
|
|
2016-07-28 04:49:49 -04:00
|
|
|
#ifdef HASHSTORAGE_DEBUG
|
|
|
|
std::cerr << "HASH Requested for file " << full_path << ": ";
|
|
|
|
#endif
|
2016-07-27 15:22:59 -04:00
|
|
|
RS_STACK_MUTEX(mHashMtx) ;
|
|
|
|
|
|
|
|
time_t now = time(NULL) ;
|
|
|
|
std::map<std::string,HashStorageInfo>::iterator it = mFiles.find(full_path) ;
|
|
|
|
|
|
|
|
if(it != mFiles.end() && (uint64_t)mod_time == it->second.modf_stamp && size == it->second.size)
|
|
|
|
{
|
|
|
|
it->second.time_stamp = now ;
|
2016-07-28 04:49:49 -04:00
|
|
|
known_hash = it->second.hash;
|
|
|
|
#ifdef HASHSTORAGE_DEBUG
|
2016-07-27 15:22:59 -04:00
|
|
|
std::cerr << "Found in cache." << std::endl ;
|
|
|
|
#endif
|
|
|
|
return true ;
|
|
|
|
}
|
2016-07-28 04:49:49 -04:00
|
|
|
#ifdef HASHSTORAGE_DEBUG
|
|
|
|
std::cerr << "Not in cache. Sceduling for re-hash." << std::endl ;
|
|
|
|
#endif
|
2016-07-27 15:22:59 -04:00
|
|
|
|
|
|
|
// we need to schedule a re-hashing
|
|
|
|
|
|
|
|
if(mFilesToHash.find(full_path) != mFilesToHash.end())
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
FileHashJob job ;
|
|
|
|
|
|
|
|
job.client = c ;
|
|
|
|
job.client_param = client_param ;
|
|
|
|
job.full_path = full_path ;
|
2016-07-28 04:49:49 -04:00
|
|
|
job.ts = mod_time ;
|
2016-07-27 15:22:59 -04:00
|
|
|
|
|
|
|
mFilesToHash[full_path] = job;
|
|
|
|
|
2016-07-27 18:48:28 -04:00
|
|
|
if(!mRunning)
|
2016-07-27 15:22:59 -04:00
|
|
|
{
|
2016-07-27 18:48:28 -04:00
|
|
|
mRunning = true ;
|
2016-07-27 15:22:59 -04:00
|
|
|
std::cerr << "Starting hashing thread." << std::endl;
|
|
|
|
start() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashStorage::clean()
|
|
|
|
{
|
|
|
|
#ifdef HASHSTORAGE_DEBUG
|
|
|
|
std::cerr << "Cleaning HashStorage..." << std::endl ;
|
|
|
|
#endif
|
|
|
|
time_t now = time(NULL) ;
|
|
|
|
time_t duration = mMaxStorageDurationDays * 24 * 3600 ; // seconds
|
|
|
|
|
|
|
|
#ifdef HASHSTORAGE_DEBUG
|
|
|
|
std::cerr << "cleaning hash cache." << std::endl ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for(std::map<std::string,HashStorageInfo>::iterator it(mFiles.begin());it!=mFiles.end();)
|
|
|
|
if(it->second.time_stamp + duration < (uint64_t)now)
|
|
|
|
{
|
|
|
|
#ifdef HASHSTORAGE_DEBUG
|
|
|
|
std::cerr << " Entry too old: " << it->first << ", ts=" << it->second.time_stamp << std::endl ;
|
|
|
|
#endif
|
|
|
|
std::map<std::string,HashStorageInfo>::iterator tmp(it) ;
|
|
|
|
++tmp ;
|
|
|
|
mFiles.erase(it) ;
|
|
|
|
it=tmp ;
|
|
|
|
mChanged = true ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it ;
|
|
|
|
|
|
|
|
#ifdef HASHSTORAGE_DEBUG
|
|
|
|
std::cerr << "Done." << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|