From aeb05953015d786997303abe55686f3255dc8cd0 Mon Sep 17 00:00:00 2001 From: mr-alice Date: Thu, 28 Jul 2016 10:49:49 +0200 Subject: [PATCH] fixed updated ts in hash cache files --- .../src/file_sharing/directory_storage.cc | 6 +++++- .../src/file_sharing/directory_updater.cc | 18 +++++++++++++++-- .../src/file_sharing/directory_updater.h | 14 +++++-------- libretroshare/src/file_sharing/hash_cache.cc | 20 ++++++++++++++++++- libretroshare/src/file_sharing/hash_cache.h | 1 + 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index b178daf85..d94cb64f0 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -155,7 +155,11 @@ class InternalFileHierarchyStorage } if(it->second.modtime != f.file_modtime || it->second.size != f.file_size) // file is newer and/or has different size + { f.file_hash.clear(); // hash needs recomputing + f.file_modtime = it->second.modtime; + f.file_size = it->second.size; + } else new_files.erase(f.file_name) ; @@ -292,7 +296,7 @@ private: for(int i=0;i(mNodes[d.subfiles[i]])); - std::cerr << indent << " hash:" << f.file_hash << " ts:" << std::fill(8) << (uint64_t)f.file_modtime << " " << f.file_size << " " << f.file_name << std::endl; + std::cerr << indent << " hash:" << f.file_hash << " ts:" << (uint64_t)f.file_modtime << " " << f.file_size << " " << f.file_name << std::endl; } } diff --git a/libretroshare/src/file_sharing/directory_updater.cc b/libretroshare/src/file_sharing/directory_updater.cc index f9a228e1b..b4ebfb0f5 100644 --- a/libretroshare/src/file_sharing/directory_updater.cc +++ b/libretroshare/src/file_sharing/directory_updater.cc @@ -5,6 +5,8 @@ #define DEBUG_LOCAL_DIR_UPDATER 1 +static const uint32_t DELAY_BETWEEN_DIRECTORY_UPDATES = 10 ; // 10 seconds for testing. Should be much more!! + void RemoteDirectoryUpdater::tick() { // use the stored iterator @@ -13,11 +15,23 @@ void RemoteDirectoryUpdater::tick() LocalDirectoryUpdater::LocalDirectoryUpdater(HashStorage *hc,LocalDirectoryStorage *lds) : mHashCache(hc),mSharedDirectories(lds) { + mLastSweepTime = 0; } -void LocalDirectoryUpdater::tick() +void LocalDirectoryUpdater::data_tick() { - std::cerr << "LocalDirectoryUpdater::tick()" << std::endl; + time_t now = time(NULL) ; + + if(now > DELAY_BETWEEN_DIRECTORY_UPDATES + mLastSweepTime) + { + sweepSharedDirectories() ; + mLastSweepTime = now; + } +} + +void LocalDirectoryUpdater::sweepSharedDirectories() +{ + std::cerr << "LocalDirectoryUpdater::sweep()" << std::endl; // recursive update algorithm works that way: // - the external loop starts on the shared directory list and goes through sub-directories diff --git a/libretroshare/src/file_sharing/directory_updater.h b/libretroshare/src/file_sharing/directory_updater.h index f0317b36d..2d30793cd 100644 --- a/libretroshare/src/file_sharing/directory_updater.h +++ b/libretroshare/src/file_sharing/directory_updater.h @@ -11,30 +11,26 @@ class DirectoryUpdater public: DirectoryUpdater() {} virtual ~DirectoryUpdater(){} - - // Does some updating job. Crawls the existing directories and checks wether it has been updated - // recently enough. If not, calls the directry source. - // - virtual void tick() =0; - - // }; -class LocalDirectoryUpdater: public DirectoryUpdater, public HashStorageClient +class LocalDirectoryUpdater: public DirectoryUpdater, public HashStorageClient, public RsTickingThread { public: LocalDirectoryUpdater(HashStorage *hash_cache,LocalDirectoryStorage *lds) ; virtual ~LocalDirectoryUpdater() {} - virtual void tick() ; + virtual void data_tick() ; protected: virtual void hash_callback(uint32_t client_param, const std::string& name, const RsFileHash& hash, uint64_t size); void recursUpdateSharedDir(const std::string& cumulated_path,DirectoryStorage::EntryIndex indx); + void sweepSharedDirectories(); private: HashStorage *mHashCache ; LocalDirectoryStorage *mSharedDirectories ; + + time_t mLastSweepTime; }; class RemoteDirectoryUpdater: public DirectoryUpdater diff --git a/libretroshare/src/file_sharing/hash_cache.cc b/libretroshare/src/file_sharing/hash_cache.cc index 0acd323c2..eb86813b3 100644 --- a/libretroshare/src/file_sharing/hash_cache.cc +++ b/libretroshare/src/file_sharing/hash_cache.cc @@ -40,6 +40,16 @@ void HashStorage::data_tick() mRunning = false ; std::cerr << "done." << std::endl; } + + // 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; } // call the client @@ -51,6 +61,9 @@ bool HashStorage::requestHash(const std::string& full_path,uint64_t size,time_t { // check if the hash is up to date w.r.t. cache. +#ifdef HASHSTORAGE_DEBUG + std::cerr << "HASH Requested for file " << full_path << ": "; +#endif RS_STACK_MUTEX(mHashMtx) ; time_t now = time(NULL) ; @@ -59,11 +72,15 @@ bool HashStorage::requestHash(const std::string& full_path,uint64_t size,time_t if(it != mFiles.end() && (uint64_t)mod_time == it->second.modf_stamp && size == it->second.size) { it->second.time_stamp = now ; -#ifdef HASHCACHE_DEBUG + known_hash = it->second.hash; +#ifdef HASHSTORAGE_DEBUG std::cerr << "Found in cache." << std::endl ; #endif return true ; } +#ifdef HASHSTORAGE_DEBUG + std::cerr << "Not in cache. Sceduling for re-hash." << std::endl ; +#endif // we need to schedule a re-hashing @@ -75,6 +92,7 @@ bool HashStorage::requestHash(const std::string& full_path,uint64_t size,time_t job.client = c ; job.client_param = client_param ; job.full_path = full_path ; + job.ts = mod_time ; mFilesToHash[full_path] = job; diff --git a/libretroshare/src/file_sharing/hash_cache.h b/libretroshare/src/file_sharing/hash_cache.h index 88b4206f9..2d10c09ca 100644 --- a/libretroshare/src/file_sharing/hash_cache.h +++ b/libretroshare/src/file_sharing/hash_cache.h @@ -68,6 +68,7 @@ private: std::string full_path; HashStorageClient *client; uint32_t client_param ; + time_t ts; }; // current work