2016-07-21 00:16:12 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include "util/rsthreads.h"
|
2016-07-23 22:14:43 -04:00
|
|
|
#include "retroshare/rsfiles.h"
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
class HashStorageClient
|
2016-07-23 22:14:43 -04:00
|
|
|
{
|
|
|
|
public:
|
2016-07-27 15:22:59 -04:00
|
|
|
HashStorageClient() {}
|
|
|
|
virtual ~HashStorageClient() {}
|
2016-07-23 22:14:43 -04:00
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
virtual void hash_callback(uint32_t client_param, const std::string& name, const RsFileHash& hash, uint64_t size)=0;
|
2016-07-23 22:14:43 -04:00
|
|
|
};
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
class HashStorage: public RsTickingThread
|
2016-07-18 21:52:44 -04:00
|
|
|
{
|
2016-07-21 00:16:12 -04:00
|
|
|
public:
|
2016-07-27 15:22:59 -04:00
|
|
|
HashStorage(const std::string& save_file_name) ;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief requestHash Requests the hash for the given file, assuming size and mod_time are the same.
|
|
|
|
*
|
|
|
|
* \param full_path Full path to reach the file
|
|
|
|
* \param size Actual file size
|
|
|
|
* \param mod_time Actual file modification time
|
|
|
|
* \param known_hash Returned hash for the file.
|
|
|
|
* \param c Hash cache client to which the hash should be sent once calculated
|
|
|
|
* \param client_param Param to be passed to the client callback
|
|
|
|
*
|
|
|
|
* \return true if the supplied hash info is up to date.
|
|
|
|
*/
|
|
|
|
bool requestHash(const std::string& full_path, uint64_t size, time_t mod_time, RsFileHash& known_hash, HashStorageClient *c, uint32_t client_param) ;
|
|
|
|
|
|
|
|
struct HashStorageInfo
|
2016-07-21 00:16:12 -04:00
|
|
|
{
|
|
|
|
std::string filename ;
|
|
|
|
uint64_t size ;
|
2016-07-27 15:22:59 -04:00
|
|
|
uint32_t time_stamp ; // last time the hash was tested/requested
|
2016-07-21 00:16:12 -04:00
|
|
|
uint32_t modf_stamp ;
|
|
|
|
RsFileHash hash ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
// interaction with GUI, called from p3FileLists
|
2016-07-27 15:22:59 -04:00
|
|
|
void setRememberHashFilesDuration(uint32_t days) { mMaxStorageDurationDays = days ; }
|
|
|
|
uint32_t rememberHashFilesDuration() const { return mMaxStorageDurationDays ; }
|
2016-07-23 22:14:43 -04:00
|
|
|
void clear() { mFiles.clear(); }
|
|
|
|
bool empty() const { return mFiles.empty() ; }
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
// Functions called by the thread
|
|
|
|
|
|
|
|
virtual void data_tick() ;
|
2016-07-21 00:16:12 -04:00
|
|
|
private:
|
|
|
|
void clean() ;
|
|
|
|
|
|
|
|
void save() ;
|
|
|
|
void load() ;
|
|
|
|
|
|
|
|
// Local configuration and storage
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
uint32_t mMaxStorageDurationDays ; // maximum duration of un-requested cache entries
|
|
|
|
std::map<std::string, HashStorageInfo> mFiles ; // stored as (full_path, hash_info)
|
2016-07-21 00:16:12 -04:00
|
|
|
std::string mFilePath ;
|
|
|
|
bool mChanged ;
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
struct FileHashJob
|
|
|
|
{
|
|
|
|
std::string full_path;
|
|
|
|
HashStorageClient *client;
|
|
|
|
uint32_t client_param ;
|
2016-07-28 04:49:49 -04:00
|
|
|
time_t ts;
|
2016-07-27 15:22:59 -04:00
|
|
|
};
|
|
|
|
|
2016-07-21 00:16:12 -04:00
|
|
|
// current work
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
std::map<std::string,FileHashJob> mFilesToHash ;
|
|
|
|
|
|
|
|
// thread/mutex stuff
|
|
|
|
|
|
|
|
RsMutex mHashMtx ;
|
2016-07-27 18:48:28 -04:00
|
|
|
bool mRunning;
|
2016-07-18 21:52:44 -04:00
|
|
|
};
|
|
|
|
|