2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* RetroShare FileCache Module: fimonitor.h
|
2009-08-25 16:49:50 -04:00
|
|
|
*
|
2007-11-14 22:18:48 -05:00
|
|
|
* Copyright 2004-2007 by Robert Fernie.
|
2009-08-25 16:49:50 -04:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2007-11-14 22:18:48 -05:00
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
2009-08-25 16:49:50 -04:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
2007-11-14 22:18:48 -05:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FILE_INDEX_MONITOR_H
|
|
|
|
#define FILE_INDEX_MONITOR_H
|
|
|
|
|
|
|
|
#include "dbase/cachestrapper.h"
|
2008-03-03 09:41:15 -05:00
|
|
|
#include "dbase/findex.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
#include "util/rsthreads.h"
|
2010-08-06 05:40:23 -04:00
|
|
|
#include "retroshare/rsfiles.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
* The Local Monitoring Class: FileIndexMonitor.
|
|
|
|
*
|
|
|
|
* This periodically scans the directory tree, and updates any modified directories/files.
|
|
|
|
*
|
|
|
|
*****************************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
STILL TODO:
|
|
|
|
|
|
|
|
(1) Implement Hash function.
|
|
|
|
|
|
|
|
bool FileIndexMonitor::hashFile(std::string path, FileEntry &fi);
|
|
|
|
|
|
|
|
(2) Add Shared directory controls to Monitor.
|
|
|
|
|
|
|
|
int FileIndexMonitor::addSharedDirectory(std::path);
|
|
|
|
int FileIndexMonitor::removeSharedDirectory(std::path);
|
|
|
|
std::string FileIndexMonitor::findRealRoot(std::string base);
|
|
|
|
|
|
|
|
These must be split into <base>/<top> and the mapping saved.
|
|
|
|
eg: addSharedDirectory("c:/home/stuff/dir1") --> "c:/home/stuff" <-> "dir1"
|
|
|
|
This code has been written already, and can just be moved over.
|
|
|
|
|
|
|
|
FOR LATER:
|
|
|
|
(2) Port File/Directory lookup code to windoze. (or compile dirent.c under windoze)
|
|
|
|
(3) Add Load/Store interface to FileIndexMonitor. (later)
|
|
|
|
(4) Integrate with real Thread/Mutex code (last thing to do)
|
|
|
|
|
|
|
|
******************************************************************************************/
|
|
|
|
|
2009-01-22 16:06:54 -05:00
|
|
|
class NotifyBase ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-02-26 18:05:07 -05:00
|
|
|
class DirContentToHash
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::vector<FileEntry> fentries ;
|
|
|
|
|
|
|
|
std::string realpath ;
|
|
|
|
std::string dirpath ;
|
|
|
|
};
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-10-31 16:35:31 -04:00
|
|
|
class HashCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HashCache(const std::string& save_file_name) ;
|
|
|
|
|
|
|
|
void save() ;
|
|
|
|
void insert(const std::string& full_path,uint64_t size,time_t time_stamp,const std::string& hash) ;
|
|
|
|
bool find(const std::string& full_path,uint64_t size,time_t time_stamp,std::string& hash) ;
|
|
|
|
void clean() ;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint64_t size ;
|
|
|
|
time_t time_stamp ;
|
|
|
|
time_t modf_stamp ;
|
|
|
|
std::string hash ;
|
|
|
|
} HashCacheInfo ;
|
|
|
|
|
|
|
|
void setRememberHashFilesDuration(uint32_t days) { _max_cache_duration_days = days ; }
|
|
|
|
uint32_t rememberHashFilesDuration() const { return _max_cache_duration_days ; }
|
|
|
|
void clear() { _files.clear(); }
|
2010-10-31 17:35:15 -04:00
|
|
|
bool empty() const { return _files.empty() ; }
|
2010-10-31 16:35:31 -04:00
|
|
|
private:
|
|
|
|
uint32_t _max_cache_duration_days ; // maximum duration of un-requested cache entries
|
|
|
|
std::map<std::string, HashCacheInfo> _files ;
|
|
|
|
std::string _path ;
|
|
|
|
bool _changed ;
|
|
|
|
};
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/******************************************************************************************
|
|
|
|
* FileIndexMonitor
|
|
|
|
*****************************************************************************************/
|
|
|
|
|
|
|
|
class FileIndexMonitor: public CacheSource, public RsThread
|
|
|
|
{
|
|
|
|
public:
|
2011-06-15 16:03:26 -04:00
|
|
|
FileIndexMonitor(CacheStrapper *cs, NotifyBase *cb_in, std::string cachedir, std::string pid, const std::string& config_dir);
|
2009-08-03 15:43:52 -04:00
|
|
|
virtual ~FileIndexMonitor();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* external interface for filetransfer */
|
2012-11-01 06:06:12 -04:00
|
|
|
bool findLocalFile(std::string hash,uint32_t flags,const std::string& peer_id, std::string &fullpath, uint64_t &size) const;
|
|
|
|
|
|
|
|
int SearchKeywords(std::list<std::string> keywords, std::list<DirDetails> &results,uint32_t flags,const std::string& peer_id) ;
|
|
|
|
int SearchBoolExp(Expression *exp, std::list<DirDetails> &results,uint32_t flags,const std::string& peer_id) const ;
|
|
|
|
|
|
|
|
int filterResults(std::list<FileEntry*>& firesults,std::list<DirDetails>& results,TransferInfoFlags flags,const std::string& peer_id) const ;
|
2009-08-25 08:04:43 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* external interface for local access to files */
|
|
|
|
bool convertSharedFilePath(std::string path, std::string &fullpath);
|
2008-03-31 10:06:59 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* Interacting with CacheSource */
|
|
|
|
/* overloaded from CacheSource */
|
|
|
|
virtual bool loadLocalCache(const CacheData &data); /* called with stored data */
|
2012-11-01 06:06:12 -04:00
|
|
|
bool updateCache(const CacheData &data,const std::list<std::string>& dest_peers); /* we call when we have a new cache for others */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* the FileIndexMonitor inner workings */
|
|
|
|
//virtual void run(std::string& currentJob); /* overloaded from RsThread */
|
|
|
|
//void updateCycle(std::string& currentJob);
|
|
|
|
virtual void run(); /* overloaded from RsThread */
|
|
|
|
void updateCycle();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-09 16:00:25 -04:00
|
|
|
// Interface for browsing dir hirarchy
|
|
|
|
int RequestDirDetails(void*, DirDetails&, uint32_t) const ;
|
2011-04-03 15:59:12 -04:00
|
|
|
uint32_t getType(void*) const ;
|
2012-01-17 15:38:25 -05:00
|
|
|
int RequestDirDetails(const std::string& path, DirDetails &details) const ;
|
2009-08-09 16:00:25 -04:00
|
|
|
|
|
|
|
// set/update shared directories
|
2010-09-17 15:21:14 -04:00
|
|
|
virtual void setSharedDirectories(const std::list<SharedDirInfo>& dirs);
|
2009-08-03 15:43:52 -04:00
|
|
|
void getSharedDirectories(std::list<SharedDirInfo>& dirs);
|
|
|
|
void updateShareFlags(const SharedDirInfo& info) ;
|
2008-08-09 12:06:01 -04:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
void forceDirectoryCheck();
|
|
|
|
bool inDirectoryCheck();
|
2009-01-22 16:06:54 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* util fns */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-10-31 16:35:31 -04:00
|
|
|
protected:
|
|
|
|
// Sets/gets the duration period within which already hashed files are remembered.
|
|
|
|
//
|
|
|
|
void setRememberHashFilesDuration(uint32_t days) ;
|
|
|
|
uint32_t rememberHashFilesDuration() const ;
|
|
|
|
void setRememberHashFiles(bool) ;
|
|
|
|
bool rememberHashFiles() ;
|
|
|
|
// Remove any memory of formerly hashed files that are not shared anymore
|
|
|
|
void clearHashFiles() ;
|
2011-03-31 15:59:56 -04:00
|
|
|
void setPeriod(int insecs);
|
|
|
|
int getPeriod() const;
|
2011-03-28 17:52:21 -04:00
|
|
|
|
|
|
|
bool autoCheckEnabled() const ;
|
2010-10-31 16:35:31 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
private:
|
2010-05-29 09:17:09 -04:00
|
|
|
/* the mutex should be locked before calling these 3. */
|
|
|
|
|
2009-08-09 12:32:31 -04:00
|
|
|
// saves file indexs and update the cache.
|
2009-08-09 16:00:25 -04:00
|
|
|
void locked_saveFileIndexes() ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-05-29 09:17:09 -04:00
|
|
|
// Finds the share flags associated with this file entry.
|
2012-11-01 06:06:12 -04:00
|
|
|
void locked_findShareFlagsAndParentGroups(FileEntry *fe,FileStorageFlags& shareflags,std::list<std::string>& parent_groups) const ;
|
2010-05-29 09:17:09 -04:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
std::string locked_findRealRoot(std::string base) const;
|
2010-05-29 09:17:09 -04:00
|
|
|
|
2010-02-26 18:05:07 -05:00
|
|
|
void hashFiles(const std::vector<DirContentToHash>& to_hash) ;
|
2009-08-03 15:43:52 -04:00
|
|
|
bool hashFile(std::string path, FileEntry &fi); /* To Implement */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* data */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
mutable RsMutex fiMutex;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
FileIndex fi;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
int updatePeriod;
|
|
|
|
std::map<std::string, SharedDirInfo> directoryMap; /* used by findRealRoot */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* flags to kick - if we were busy or sleeping */
|
|
|
|
bool pendingDirs;
|
|
|
|
bool pendingForceCacheWrite;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
/* flags to force Check, to tell if we're in check */
|
|
|
|
bool mForceCheck;
|
|
|
|
bool mInCheck;
|
2008-03-31 10:06:59 -04:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
std::list<SharedDirInfo> pendingDirList;
|
|
|
|
bool internal_setSharedDirectories();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-08-03 15:43:52 -04:00
|
|
|
NotifyBase *cb ;
|
2010-10-31 16:35:31 -04:00
|
|
|
|
|
|
|
HashCache hashCache ;
|
|
|
|
bool useHashCache ;
|
2007-11-14 22:18:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|