2016-07-23 22:14:43 -04:00
|
|
|
#include "util/folderiterator.h"
|
|
|
|
|
2016-07-20 16:11:26 -04:00
|
|
|
#include "directory_storage.h"
|
|
|
|
#include "directory_updater.h"
|
|
|
|
|
2016-07-23 22:14:43 -04:00
|
|
|
#define DEBUG_LOCAL_DIR_UPDATER 1
|
|
|
|
|
2016-07-20 16:11:26 -04:00
|
|
|
void RemoteDirectoryUpdater::tick()
|
|
|
|
{
|
|
|
|
// use the stored iterator
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
LocalDirectoryUpdater::LocalDirectoryUpdater(HashStorage *hc)
|
2016-07-23 22:14:43 -04:00
|
|
|
: mHashCache(hc)
|
2016-07-21 00:16:12 -04:00
|
|
|
{
|
2016-07-27 15:22:59 -04:00
|
|
|
mSharedDirectories = new LocalDirectoryStorage("local_storage.txt");
|
2016-07-21 00:16:12 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 16:11:26 -04:00
|
|
|
void LocalDirectoryUpdater::tick()
|
|
|
|
{
|
2016-07-23 22:14:43 -04:00
|
|
|
std::cerr << "LocalDirectoryUpdater::tick()" << std::endl;
|
|
|
|
|
2016-07-20 16:11:26 -04:00
|
|
|
// recursive update algorithm works that way:
|
|
|
|
// - the external loop starts on the shared directory list and goes through sub-directories
|
2016-07-21 00:16:12 -04:00
|
|
|
// - at the same time, it updates the local list of shared directories. A single sweep is performed over the whole directory structure.
|
2016-07-20 16:11:26 -04:00
|
|
|
// - the information that is costly to compute (the hash) is store externally into a separate structure.
|
|
|
|
// - doing so, changing directory names or moving files between directories does not cause a re-hash of the content.
|
|
|
|
//
|
2016-07-21 00:16:12 -04:00
|
|
|
std::list<SharedDirInfo> shared_directory_list ;
|
2016-07-24 23:48:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
std::set<std::string> sub_dir_list ;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-23 22:14:43 -04:00
|
|
|
for(std::list<SharedDirInfo>::const_iterator real_dir_it(shared_directory_list.begin());real_dir_it!=shared_directory_list.end();++real_dir_it)
|
2016-07-24 23:48:22 -04:00
|
|
|
sub_dir_list.insert( (*real_dir_it).filename ) ;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
|
|
|
// make sure that entries in stored_dir_it are the same than paths in real_dir_it, and in the same order.
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
mSharedDirectories->updateSubDirectoryList(mSharedDirectories->root(),sub_dir_list) ;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
|
|
|
// now for each of them, go recursively and match both files and dirs
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
DirectoryStorage::DirIterator stored_dir_it(mSharedDirectories,mSharedDirectories->root()) ;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-23 22:14:43 -04:00
|
|
|
for(std::list<SharedDirInfo>::const_iterator real_dir_it(shared_directory_list.begin());real_dir_it!=shared_directory_list.end();++real_dir_it, ++stored_dir_it)
|
|
|
|
recursUpdateSharedDir(real_dir_it->filename, *stored_dir_it) ;
|
2016-07-21 00:16:12 -04:00
|
|
|
}
|
|
|
|
|
2016-07-23 22:14:43 -04:00
|
|
|
void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx)
|
2016-07-21 00:16:12 -04:00
|
|
|
{
|
2016-07-23 22:14:43 -04:00
|
|
|
std::cerr << " parsing directory " << cumulated_path << ", index=" << indx << std::endl;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-23 22:14:43 -04:00
|
|
|
// make sure list of subdirs is the same
|
2016-07-21 00:16:12 -04:00
|
|
|
// make sure list of subfiles is the same
|
|
|
|
// request all hashes to the hashcache
|
|
|
|
|
|
|
|
librs::util::FolderIterator dirIt(cumulated_path);
|
|
|
|
|
|
|
|
if(!dirIt.isValid())
|
|
|
|
{
|
|
|
|
mSharedDirectories->removeDirectory(indx) ; // this is a complex operation since it needs to *update* it so that it is kept consistent.
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect subdirs and subfiles
|
|
|
|
|
2016-07-24 23:48:22 -04:00
|
|
|
std::set<std::string> subfiles ;
|
|
|
|
std::set<std::string> subdirs ;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
for(;dirIt.isValid();dirIt.next())
|
2016-07-21 00:16:12 -04:00
|
|
|
{
|
2016-07-23 22:14:43 -04:00
|
|
|
switch(dirIt.file_type())
|
2016-07-21 00:16:12 -04:00
|
|
|
{
|
2016-07-24 23:48:22 -04:00
|
|
|
case librs::util::FolderIterator::TYPE_FILE: subfiles.insert(dirIt.file_name()) ;
|
2016-07-23 22:14:43 -04:00
|
|
|
std::cerr << " adding sub-file \"" << dirIt.file_name() << "\"" << std::endl;
|
|
|
|
break;
|
|
|
|
|
2016-07-24 23:48:22 -04:00
|
|
|
case librs::util::FolderIterator::TYPE_DIR: subdirs.insert(dirIt.file_name()) ;
|
2016-07-23 22:14:43 -04:00
|
|
|
std::cerr << " adding sub-dir \"" << dirIt.file_name() << "\"" << std::endl;
|
2016-07-21 00:16:12 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-07-23 22:14:43 -04:00
|
|
|
std::cerr << "(EE) Dir entry of unknown type with path \"" << cumulated_path << "/" << dirIt.file_name() << "\"" << std::endl;
|
2016-07-21 00:16:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// update file and dir lists for current directory.
|
|
|
|
|
|
|
|
mSharedDirectories->updateSubDirectoryList(indx,subdirs) ;
|
2016-07-24 23:48:22 -04:00
|
|
|
|
|
|
|
std::set<std::string> new_files ;
|
|
|
|
mSharedDirectories->updateSubFilesList(indx,subfiles,new_files) ;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
|
|
|
// now go through list of subfiles and request the hash to hashcache
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
for(DirectoryStorage::FileIterator dit(mSharedDirectories,indx);dit;++dit)
|
2016-07-21 00:16:12 -04:00
|
|
|
{
|
2016-07-23 22:14:43 -04:00
|
|
|
// ask about the hash. If not present, ask HashCache. If not present, or different, the callback will update it.
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
RsFileHash hash ;
|
|
|
|
|
|
|
|
if(mHashCache->requestHash(dit.fullpath(),dit.size(),dit.modtime(),hash,this,*dit) && dit.hash() != hash)
|
|
|
|
mSharedDirectories->updateHash(*dit,hash);
|
2016-07-21 00:16:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// go through the list of sub-dirs and recursively update
|
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
DirectoryStorage::DirIterator stored_dir_it(mSharedDirectories,indx) ;
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-24 23:48:22 -04:00
|
|
|
for(std::set<std::string>::const_iterator real_dir_it(subdirs.begin());real_dir_it!=subdirs.end();++real_dir_it, ++stored_dir_it)
|
2016-07-21 00:16:12 -04:00
|
|
|
recursUpdateSharedDir(*real_dir_it, *stored_dir_it) ;
|
2016-07-20 16:11:26 -04:00
|
|
|
}
|
2016-07-21 00:16:12 -04:00
|
|
|
|
2016-07-27 15:22:59 -04:00
|
|
|
void LocalDirectoryUpdater::hash_callback(uint32_t client_param, const std::string& name, const RsFileHash& hash, uint64_t size)
|
|
|
|
{
|
|
|
|
if(!mSharedDirectories->updateHash(DirectoryStorage::EntryIndex(client_param),hash))
|
|
|
|
std::cerr << "(EE) Cannot update file. Something's wrong." << std::endl;
|
|
|
|
}
|
|
|
|
|