From 2aac88464df96de1a73bf4f9079be2ad8b09ea7e Mon Sep 17 00:00:00 2001 From: mr-alice Date: Thu, 28 Jul 2016 09:29:15 +0200 Subject: [PATCH] fixed a few bugs in directory parsing --- .../src/file_sharing/directory_storage.cc | 41 ++++++++++++------- .../src/file_sharing/directory_storage.h | 8 +++- .../src/file_sharing/directory_updater.cc | 16 ++++---- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index ee89d572c..b178daf85 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -32,7 +32,7 @@ class InternalFileHierarchyStorage class FileEntry: public FileStorageNode { public: - FileEntry(const std::string& name) : file_name(name) {} + FileEntry(const std::string& name,uint64_t size,time_t modtime) : file_name(name),file_size(size),file_modtime(modtime) {} virtual uint32_t type() const { return FileStorageNode::TYPE_FILE ; } virtual ~FileEntry() {} @@ -131,7 +131,7 @@ class InternalFileHierarchyStorage return true; } - bool updateSubFilesList(const DirectoryStorage::EntryIndex& indx,const std::set& subfiles,std::set& new_files) + bool updateSubFilesList(const DirectoryStorage::EntryIndex& indx,const std::map& subfiles,std::map& new_files) { if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) return false; @@ -139,22 +139,33 @@ class InternalFileHierarchyStorage DirEntry& d(*static_cast(mNodes[indx])) ; new_files = subfiles ; + // remove from new_files the ones that already exist and have a modf time that is not older. + for(uint32_t i=0;i(mNodes[d.subfiles[i]])->file_name) == subfiles.end()) + { + FileEntry& f(*static_cast(mNodes[d.subfiles[i]])) ; + std::map::const_iterator it = subfiles.find(f.file_name) ; + + if(it == subfiles.end()) // file does not exist anymore => delete { d.subfiles[i] = d.subfiles[d.subfiles.size()-1] ; d.subfiles.pop_back(); - } - else - { - new_files.erase(static_cast(mNodes[d.subfiles[i]])->file_name) ; - ++i; + + continue; } - for(std::set::const_iterator it(new_files.begin());it!=new_files.end();++it) + 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 + else + new_files.erase(f.file_name) ; + + ++i; + } + + for(std::map::const_iterator it(new_files.begin());it!=new_files.end();++it) { d.subfiles.push_back(mNodes.size()) ; - mNodes.push_back(new FileEntry(*it)); + mNodes.push_back(new FileEntry(it->first,it->second.size,it->second.modtime)); } return true; } @@ -166,7 +177,7 @@ class InternalFileHierarchyStorage static_cast(mNodes[file_index])->file_hash = hash ; return true; } - bool updateFile(const DirectoryStorage::EntryIndex& file_index,const RsFileHash& hash, const std::string& fname,uint64_t size, const uint32_t modf_time) + bool updateFile(const DirectoryStorage::EntryIndex& file_index,const RsFileHash& hash, const std::string& fname,uint64_t size, const time_t modf_time) { if(!checkIndex(file_index,FileStorageNode::TYPE_FILE)) return false; @@ -273,15 +284,15 @@ private: std::string indent(2*depth,' '); DirEntry& d(*static_cast(mNodes[node])); - std::cerr << indent << d.dir_name << std::endl; + std::cerr << indent << "dir:" << d.dir_name << std::endl; for(int i=0;i(mNodes[i])); - std::cerr << indent << " " << f.file_hash << " " << f.file_modtime << " " << f.file_name << std::endl; + FileEntry& f(*static_cast(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; } } @@ -369,7 +380,7 @@ bool DirectoryStorage::updateSubDirectoryList(const EntryIndex& indx,const std:: RS_STACK_MUTEX(mDirStorageMtx) ; return mFileHierarchy->updateSubDirectoryList(indx,subdirs) ; } -bool DirectoryStorage::updateSubFilesList(const EntryIndex& indx,const std::set& subfiles,std::set& new_files) +bool DirectoryStorage::updateSubFilesList(const EntryIndex& indx,const std::map& subfiles,std::map& new_files) { RS_STACK_MUTEX(mDirStorageMtx) ; return mFileHierarchy->updateSubFilesList(indx,subfiles,new_files) ; diff --git a/libretroshare/src/file_sharing/directory_storage.h b/libretroshare/src/file_sharing/directory_storage.h index 9f8293963..bd0f5b8ce 100644 --- a/libretroshare/src/file_sharing/directory_storage.h +++ b/libretroshare/src/file_sharing/directory_storage.h @@ -83,10 +83,16 @@ class DirectoryStorage InternalFileHierarchyStorage *mStorage ; }; + struct FileTS + { + uint64_t size ; + time_t modtime; + }; + EntryIndex root() const ; // returns the index of the root directory entry. bool updateSubDirectoryList(const EntryIndex& indx,const std::set& subdirs) ; - bool updateSubFilesList(const EntryIndex& indx,const std::set& subfiles,std::set& new_files) ; + bool updateSubFilesList(const EntryIndex& indx, const std::map &subfiles, std::map &new_files) ; bool removeDirectory(const EntryIndex& indx) ; bool updateFile(const EntryIndex& index,const RsFileHash& hash, const std::string& fname, uint64_t size, time_t modf_time) ; diff --git a/libretroshare/src/file_sharing/directory_updater.cc b/libretroshare/src/file_sharing/directory_updater.cc index 9367e826e..f9a228e1b 100644 --- a/libretroshare/src/file_sharing/directory_updater.cc +++ b/libretroshare/src/file_sharing/directory_updater.cc @@ -66,21 +66,21 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p // collect subdirs and subfiles - std::set subfiles ; + std::map subfiles ; std::set subdirs ; for(;dirIt.isValid();dirIt.next()) { switch(dirIt.file_type()) { - case librs::util::FolderIterator::TYPE_FILE: subfiles.insert(dirIt.file_name()) ; - std::cerr << " adding sub-file \"" << dirIt.file_name() << "\"" << std::endl; - break; + case librs::util::FolderIterator::TYPE_FILE: subfiles[dirIt.file_name()].modtime = dirIt.file_modtime() ; + subfiles[dirIt.file_name()].size = dirIt.file_size(); + std::cerr << " adding sub-file \"" << dirIt.file_name() << "\"" << std::endl; + break; - case librs::util::FolderIterator::TYPE_DIR: subdirs.insert(dirIt.file_name()) ; - std::cerr << " adding sub-dir \"" << dirIt.file_name() << "\"" << std::endl; + case librs::util::FolderIterator::TYPE_DIR: subdirs.insert(dirIt.file_name()) ; + std::cerr << " adding sub-dir \"" << dirIt.file_name() << "\"" << std::endl; break; - default: std::cerr << "(EE) Dir entry of unknown type with path \"" << cumulated_path << "/" << dirIt.file_name() << "\"" << std::endl; } @@ -89,7 +89,7 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p mSharedDirectories->updateSubDirectoryList(indx,subdirs) ; - std::set new_files ; + std::map new_files ; mSharedDirectories->updateSubFilesList(indx,subfiles,new_files) ; // now go through list of subfiles and request the hash to hashcache