From e850e00a823ca9d7a9d7b351e5d7a3195ed95b3f Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 19 Jul 2021 16:40:13 +0200 Subject: [PATCH] Optimization, cleanup, compiler warning fix Chores I have made while working on single file share --- .../src/file_sharing/dir_hierarchy.cc | 157 ++++++---- .../src/file_sharing/directory_storage.cc | 20 +- .../src/file_sharing/directory_updater.cc | 5 +- libretroshare/src/file_sharing/p3filelists.cc | 278 ++++++++++-------- libretroshare/src/ft/ftserver.cc | 10 +- libretroshare/src/turtle/p3turtle.cc | 37 +-- libretroshare/src/util/folderiterator.cc | 7 +- libretroshare/src/util/folderiterator.h | 6 +- libretroshare/src/util/rsdir.h | 9 +- libretroshare/src/util/rstime.h | 3 +- 10 files changed, 309 insertions(+), 223 deletions(-) diff --git a/libretroshare/src/file_sharing/dir_hierarchy.cc b/libretroshare/src/file_sharing/dir_hierarchy.cc index 351212b12..6504da96f 100644 --- a/libretroshare/src/file_sharing/dir_hierarchy.cc +++ b/libretroshare/src/file_sharing/dir_hierarchy.cc @@ -73,7 +73,8 @@ InternalFileHierarchyStorage::InternalFileHierarchyStorage() : mRoot(0) mTotalFiles = 0 ; } -bool InternalFileHierarchyStorage::getDirHashFromIndex(const DirectoryStorage::EntryIndex& index,RsFileHash& hash) const +bool InternalFileHierarchyStorage::getDirHashFromIndex( + const DirectoryStorage::EntryIndex& index, RsFileHash& hash ) const { if(!checkIndex(index,FileStorageNode::TYPE_DIR)) return false ; @@ -82,6 +83,7 @@ bool InternalFileHierarchyStorage::getDirHashFromIndex(const DirectoryStorage::E return true; } + bool InternalFileHierarchyStorage::getIndexFromDirHash(const RsFileHash& hash,DirectoryStorage::EntryIndex& index) { std::map::iterator it = mDirHashes.find(hash) ; @@ -91,35 +93,39 @@ bool InternalFileHierarchyStorage::getIndexFromDirHash(const RsFileHash& hash,Di index = it->second; - // make sure the hash actually points to some existing file. If not, remove it. This is a lazy update of dir hashes: when we need them, we check them. - if(!checkIndex(index, FileStorageNode::TYPE_DIR) || static_cast(mNodes[index])->dir_hash != hash) - { - std::cerr << "(II) removing non existing hash from dir hash list: " << hash << std::endl; - - mDirHashes.erase(it) ; - return false ; - } + /* make sure the hash actually points to some existing directory. If not, + * remove it. This is an opportunistic update of dir hashes: when we need + * them, we check them. */ + if( !checkIndex(index, FileStorageNode::TYPE_DIR) || + static_cast(mNodes[index])->dir_hash != hash ) + { + RS_INFO("removing non existing dir hash: ", hash, " from dir hash list"); + mDirHashes.erase(it); + return false; + } return true; } -bool InternalFileHierarchyStorage::getIndexFromFileHash(const RsFileHash& hash,DirectoryStorage::EntryIndex& index) + +bool InternalFileHierarchyStorage::getIndexFromFileHash( + const RsFileHash& hash, DirectoryStorage::EntryIndex& index ) { - std::map::iterator it = mFileHashes.find(hash) ; + auto it = std::as_const(mFileHashes).find(hash); + if(it == mFileHashes.end()) return false; - if(it == mFileHashes.end()) - return false; + index = it->second; - index = it->second; + /* make sure the hash actually points to some existing file. If not, remove + * it. This is an opportunistic update of file hashes: when we need them, + * we check them. */ + if( !checkIndex(it->second, FileStorageNode::TYPE_FILE) || + static_cast(mNodes[index])->file_hash != hash ) + { + RS_INFO("removing non existing file hash: ", hash, " from file hash list"); + mFileHashes.erase(it); + return false; + } - // make sure the hash actually points to some existing file. If not, remove it. This is a lazy update of file hashes: when we need them, we check them. - if(!checkIndex(it->second, FileStorageNode::TYPE_FILE) || static_cast(mNodes[index])->file_hash != hash) - { - std::cerr << "(II) removing non existing hash from file hash list: " << hash << std::endl; - - mFileHashes.erase(it) ; - return false ; - } - - return true; + return true; } bool InternalFileHierarchyStorage::getChildIndex(DirectoryStorage::EntryIndex e,int row,DirectoryStorage::EntryIndex& c) const @@ -158,10 +164,13 @@ bool InternalFileHierarchyStorage::isIndexValid(DirectoryStorage::EntryIndex e) return e < mNodes.size() && mNodes[e] != NULL ; } -bool InternalFileHierarchyStorage::updateSubDirectoryList(const DirectoryStorage::EntryIndex& indx, const std::set& subdirs, const RsFileHash& random_hash_seed) +bool InternalFileHierarchyStorage::updateSubDirectoryList( + const DirectoryStorage::EntryIndex& indx, + const std::set& subdirs, + const RsFileHash& random_hash_seed ) { - if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) - return false; + if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) + return false; DirEntry& d(*static_cast(mNodes[indx])) ; @@ -287,10 +296,17 @@ bool InternalFileHierarchyStorage::checkIndex(DirectoryStorage::EntryIndex indx, return true; } -bool InternalFileHierarchyStorage::updateSubFilesList(const DirectoryStorage::EntryIndex& indx,const std::map& subfiles,std::map& new_files) +bool InternalFileHierarchyStorage::updateSubFilesList( + const DirectoryStorage::EntryIndex& indx, + const std::map& subfiles, + std::map& new_files ) { - if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) - return false; + if(!checkIndex(indx, FileStorageNode::TYPE_DIR)) + { + RS_ERR("indx: ", indx, std::errc::not_a_directory); + print_stacktrace(); + return false; + } DirEntry& d(*static_cast(mNodes[indx])) ; new_files = subfiles ; @@ -315,9 +331,11 @@ bool InternalFileHierarchyStorage::updateSubFilesList(const DirectoryStorage::En continue; } - if(it->second.modtime != f.file_modtime || it->second.size != f.file_size) // file is newer and/or has different size + // file is newer and/or has different size + if(it->second.modtime != f.file_modtime || it->second.size != f.file_size) { - f.file_hash.clear(); // hash needs recomputing + // hash needs recomputing + f.file_hash.clear(); f.file_modtime = it->second.modtime; f.file_size = it->second.size; @@ -345,13 +363,16 @@ bool InternalFileHierarchyStorage::updateSubFilesList(const DirectoryStorage::En } return true; } -bool InternalFileHierarchyStorage::updateHash(const DirectoryStorage::EntryIndex& file_index,const RsFileHash& hash) +bool InternalFileHierarchyStorage::updateHash( + const DirectoryStorage::EntryIndex& file_index, const RsFileHash& hash ) { - if(!checkIndex(file_index,FileStorageNode::TYPE_FILE)) - { - std::cerr << "[directory storage] (EE) cannot update file at index " << file_index << ". Not a valid index, or not a file." << std::endl; - return false; - } + if(!checkIndex(file_index, FileStorageNode::TYPE_FILE)) + { + RS_ERR( "Cannot update file at index ", file_index, + ". Not a valid index, or not a file." ); + print_stacktrace(); + return false; + } #ifdef DEBUG_DIRECTORY_STORAGE std::cerr << "[directory storage] updating hash at index " << file_index << ", hash=" << hash << std::endl; #endif @@ -439,14 +460,20 @@ DirectoryStorage::EntryIndex InternalFileHierarchyStorage::allocateNewIndex() return mNodes.size()-1 ; } -bool InternalFileHierarchyStorage::updateDirEntry(const DirectoryStorage::EntryIndex& indx,const std::string& dir_name,rstime_t most_recent_time,rstime_t dir_modtime,const std::vector& subdirs_hash,const std::vector& subfiles_array) +bool InternalFileHierarchyStorage::updateDirEntry( + const DirectoryStorage::EntryIndex& indx, const std::string& dir_name, + rstime_t most_recent_time, rstime_t dir_modtime, + const std::vector& subdirs_hash, + const std::vector& subfiles_array ) { - if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) - { - std::cerr << "[directory storage] (EE) cannot update dir at index " << indx << ". Not a valid index, or not an existing dir." << std::endl; - return false; - } - DirEntry& d(*static_cast(mNodes[indx])) ; + if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) + { + RS_ERR( "cannot update dir at index ", indx, ". Not a valid index, or " + "not an existing dir." ); + return false; + } + + DirEntry& d(*static_cast(mNodes[indx])); #ifdef DEBUG_DIRECTORY_STORAGE std::cerr << "Updating dir entry: name=\"" << dir_name << "\", most_recent_time=" << most_recent_time << ", modtime=" << dir_modtime << std::endl; @@ -706,14 +733,14 @@ const InternalFileHierarchyStorage::FileStorageNode *InternalFileHierarchyStorag return NULL ; } -const InternalFileHierarchyStorage::DirEntry *InternalFileHierarchyStorage::getDirEntry(DirectoryStorage::EntryIndex indx) const +const InternalFileHierarchyStorage::DirEntry* +InternalFileHierarchyStorage::getDirEntry(DirectoryStorage::EntryIndex indx) const { - if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) - return NULL ; - - return static_cast(mNodes[indx]) ; + if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) return nullptr; + return static_cast(mNodes[indx]); } -const InternalFileHierarchyStorage::FileEntry *InternalFileHierarchyStorage::getFileEntry(DirectoryStorage::EntryIndex indx) const +const InternalFileHierarchyStorage::FileEntry* +InternalFileHierarchyStorage::getFileEntry(DirectoryStorage::EntryIndex indx) const { if(!checkIndex(indx,FileStorageNode::TYPE_FILE)) return NULL ; @@ -757,7 +784,10 @@ bool InternalFileHierarchyStorage::searchHash(const RsFileHash& hash,DirectorySt class DirectoryStorageExprFileEntry: public RsRegularExpression::ExpFileEntry { public: - DirectoryStorageExprFileEntry(const InternalFileHierarchyStorage::FileEntry& fe,const InternalFileHierarchyStorage::DirEntry& parent) : mFe(fe),mDe(parent) {} + DirectoryStorageExprFileEntry( + const InternalFileHierarchyStorage::FileEntry& fe, + const InternalFileHierarchyStorage::DirEntry& parent ) : + mFe(fe), mDe(parent) {} inline virtual const std::string& file_name() const { return mFe.file_name ; } inline virtual uint64_t file_size() const { return mFe.file_size ; } @@ -771,14 +801,18 @@ private: const InternalFileHierarchyStorage::DirEntry& mDe ; }; -int InternalFileHierarchyStorage::searchBoolExp(RsRegularExpression::Expression * exp, std::list &results) const +int InternalFileHierarchyStorage::searchBoolExp( + RsRegularExpression::Expression* exp, + std::list& results ) const { - for(std::map::const_iterator it(mFileHashes.begin());it!=mFileHashes.end();++it) - if(mNodes[it->second] != NULL && exp->eval( - DirectoryStorageExprFileEntry(*static_cast(mNodes[it->second]), - *static_cast(mNodes[mNodes[it->second]->parent_index]) - ))) - results.push_back(it->second); + for(auto& it: std::as_const(mFileHashes)) + if(mNodes[it.second]) + if(exp->eval( + DirectoryStorageExprFileEntry( + *static_cast(mNodes[it.second]), + *static_cast(mNodes[mNodes[it.second]->parent_index]) + ) )) + results.push_back(it.second); return 0; } @@ -977,8 +1011,9 @@ void InternalFileHierarchyStorage::recursPrint(int depth,DirectoryStorage::Entry bool InternalFileHierarchyStorage::nodeAccessError(const std::string& s) { - std::cerr << "(EE) InternalDirectoryStructure: ERROR: " << s << std::endl; - return false ; + RS_ERR(s); + print_stacktrace(); + return false; } // Removes the given subdirectory from the parent node and all its pendign subdirs and files. diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index 0accb336e..402cae533 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -70,8 +70,11 @@ DirectoryStorage::FileIterator& DirectoryStorage::FileIterator::operator++() return *this; } -DirectoryStorage::EntryIndex DirectoryStorage::FileIterator::operator*() const { return mStorage->getSubFileIndex(mParentIndex,mFileTabIndex) ; } -DirectoryStorage::EntryIndex DirectoryStorage::DirIterator ::operator*() const { return mStorage->getSubDirIndex(mParentIndex,mDirTabIndex) ; } +DirectoryStorage::EntryIndex DirectoryStorage::FileIterator::operator*() const +{ return mStorage->getSubFileIndex(mParentIndex, mFileTabIndex); } + +DirectoryStorage::EntryIndex DirectoryStorage::DirIterator::operator*() const +{ return mStorage->getSubDirIndex(mParentIndex, mDirTabIndex); } DirectoryStorage::FileIterator::operator bool() const { return **this != DirectoryStorage::NO_INDEX; } DirectoryStorage::DirIterator ::operator bool() const { return **this != DirectoryStorage::NO_INDEX; } @@ -142,7 +145,9 @@ bool DirectoryStorage::updateSubDirectoryList(const EntryIndex& indx, const std: mChanged = true ; return res ; } -bool DirectoryStorage::updateSubFilesList(const EntryIndex& indx,const std::map& subfiles,std::map& new_files) +bool DirectoryStorage::updateSubFilesList( + const EntryIndex& indx, const std::map& subfiles, + std::map& new_files ) { RS_STACK_MUTEX(mDirStorageMtx) ; bool res = mFileHierarchy->updateSubFilesList(indx,subfiles,new_files) ; @@ -351,7 +356,8 @@ int LocalDirectoryStorage::searchHash(const RsFileHash& hash, RsFileHash& real_h return false ; } -void LocalDirectoryStorage::setSharedDirectoryList(const std::list& lst) +void LocalDirectoryStorage::setSharedDirectoryList( + const std::list& lst ) { std::set dirs_with_new_virtualname ; bool dirs_with_changed_flags = false ; @@ -382,7 +388,9 @@ void LocalDirectoryStorage::setSharedDirectoryList(const std::list new_dirs ; @@ -532,7 +540,7 @@ bool LocalDirectoryStorage::updateHash( #endif ret = (!update_internal_hierarchy) || - mFileHierarchy->updateHash(index,hash); + mFileHierarchy->updateHash(index, hash); } // RS_STACK_MUTEX(mDirStorageMtx); #ifdef RS_DEEP_FILES_INDEX diff --git a/libretroshare/src/file_sharing/directory_updater.cc b/libretroshare/src/file_sharing/directory_updater.cc index 16f09149c..7d6828712 100644 --- a/libretroshare/src/file_sharing/directory_updater.cc +++ b/libretroshare/src/file_sharing/directory_updater.cc @@ -3,7 +3,7 @@ * * * libretroshare: retroshare core library * * * - * Copyright (C) 2016 by Mr.Alice * + * Copyright (C) 2016 Mr.Alice * * Copyright (C) 2021 Gioacchino Mazzurco * * Copyright (C) 2021 Asociación Civil Altermundi * * * @@ -404,7 +404,8 @@ void LocalDirectoryUpdater::hash_callback(uint32_t client_param, const std::stri bool LocalDirectoryUpdater::hash_confirm(uint32_t client_param) { - return mSharedDirectories->getEntryType(DirectoryStorage::EntryIndex(client_param)) == DIR_TYPE_FILE ; + return mSharedDirectories->getEntryType( + DirectoryStorage::EntryIndex(client_param) ) == DIR_TYPE_FILE; } void LocalDirectoryUpdater::setFileWatchPeriod(int seconds) diff --git a/libretroshare/src/file_sharing/p3filelists.cc b/libretroshare/src/file_sharing/p3filelists.cc index 83523d0bf..419196275 100644 --- a/libretroshare/src/file_sharing/p3filelists.cc +++ b/libretroshare/src/file_sharing/p3filelists.cc @@ -3,7 +3,9 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2018 by Mr.Alice * + * Copyright (C) 2018 Mr.Alice * + * Copyright (C) 2021 Gioacchino Mazzurco * + * Copyright (C) 2021 Asociación Civil Altermundi * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -1044,31 +1046,36 @@ void p3FileDatabase::getExtraFilesDirDetails(void *ref,DirectoryStorage::EntryIn } // This function converts a pointer into directory details, to be used by the AbstractItemModel for browsing the files. -int p3FileDatabase::RequestDirDetails(void *ref, DirDetails& d, FileSearchFlags flags) const +int p3FileDatabase::RequestDirDetails( + void* ref, DirDetails& d, FileSearchFlags flags ) const { - RS_STACK_MUTEX(mFLSMtx) ; + RS_STACK_MUTEX(mFLSMtx); - d.children.clear(); + d.children.clear(); - // Case where the pointer is NULL, which means we're at the top of the list of shared directories for all friends (including us) - // or at the top of our own list of shared directories, depending on the flags. + /* Case where the pointer is NULL, which means we're at the top of the list + * of shared directories for all friends (including us) or at the top of our + * own list of shared directories, depending on the flags. + * + * Friend index is used as follows: + * 0 : own id + * 1...n : other friends + * + * entry_index: starts at 0. + * + * The point is: we cannot use (0,0) because it encodes to NULL. No existing + * combination should encode to NULL. + * So we need to properly convert the friend index into 0 or into a friend + * tab index in mRemoteDirectories. + * + * We should also check the consistency between flags and the content of ref. + */ - // Friend index is used as follows: - // 0 : own id - // 1...n : other friends - // - // entry_index: starts at 0. - // - // The point is: we cannot use (0,0) because it encodes to NULL. No existing combination should encode to NULL. - // So we need to properly convert the friend index into 0 or into a friend tab index in mRemoteDirectories. - // - // We should also check the consistency between flags and the content of ref. - - if (ref == NULL) - { - d.ref = NULL ; - d.type = DIR_TYPE_ROOT; - d.parent = NULL; + if (ref == nullptr) + { + d.ref = nullptr; + d.type = DIR_TYPE_ROOT; + d.parent = nullptr; d.prow = -1; d.name = "root"; d.hash.clear() ; @@ -1078,12 +1085,13 @@ int p3FileDatabase::RequestDirDetails(void *ref, DirDetails& d, FileSearchFlags d.max_mtime = 0 ; if(flags & RS_FILE_HINTS_LOCAL) - { - void *p; + { + void *p = nullptr; - { - convertEntryIndexToPointer(0,0,p); // root of own directories - DirStub stub; + { + // root of own directories + convertEntryIndexToPointer(0, 0, p); + DirStub stub; stub.type = DIR_TYPE_PERSON; stub.name = mServCtrl->getOwnId().toStdString(); stub.ref = p; @@ -1092,9 +1100,11 @@ int p3FileDatabase::RequestDirDetails(void *ref, DirDetails& d, FileSearchFlags if(mExtraFiles->size() > 0) { - convertEntryIndexToPointer(0,1,p); // local shared files from extra list - DirStub stub; - stub.type = DIR_TYPE_PERSON; // not totally exact, but used as a trick. + // local shared files from extra list + convertEntryIndexToPointer(0, 1, p); + DirStub stub; + // not totally exact, but used as a trick. + stub.type = DIR_TYPE_PERSON; stub.name = "[Extra List]"; stub.ref = p; @@ -1127,18 +1137,19 @@ int p3FileDatabase::RequestDirDetails(void *ref, DirDetails& d, FileSearchFlags } uint32_t fi; - DirectoryStorage::EntryIndex e ; + DirectoryStorage::EntryIndex e; convertPointerToEntryIndex(ref,e,fi); - // check consistency - if( (fi == 0 && !(flags & RS_FILE_HINTS_LOCAL)) || (fi > 1 && (flags & RS_FILE_HINTS_LOCAL))) - { - P3FILELISTS_ERROR() << "(EE) remote request on local index or local request on remote index. This should not happen." << std::endl; - return false ; - } + // check consistency + if( (fi == 0 && !(flags & RS_FILE_HINTS_LOCAL)) || + (fi > 1 && (flags & RS_FILE_HINTS_LOCAL))) + { + RS_ERR("Remote request on local index or local request on remote index"); + return false; + } - if((flags & RS_FILE_HINTS_LOCAL) && fi == 1) // extra list + if((flags & RS_FILE_HINTS_LOCAL) && fi == 1) // extra list { getExtraFilesDirDetails(ref,e,d); @@ -1150,25 +1161,28 @@ int p3FileDatabase::RequestDirDetails(void *ref, DirDetails& d, FileSearchFlags } - DirectoryStorage *storage = (flags & RS_FILE_HINTS_LOCAL)? ((DirectoryStorage*)mLocalSharedDirs) : ((DirectoryStorage*)mRemoteDirectories[fi-1]); + DirectoryStorage* storage = + (flags & RS_FILE_HINTS_LOCAL) ? + ((DirectoryStorage*) mLocalSharedDirs) : + ((DirectoryStorage*) mRemoteDirectories[fi-1]); - // Case where the index is the top of a single person. Can be us, or a friend. + /* Case where the index is the top of a single person. + * Can be us, or a friend. */ + if(!storage || !storage->extractData(e,d)) + { + RS_WARN( "request on index; ", e, ", for directory ID:", + ( (!storage)? ("[NULL]") : (storage->peerId().toStdString()) ), + " failed" ); + return false; + } - if(storage==NULL || !storage->extractData(e,d)) - { -#ifdef DEBUG_FILE_HIERARCHY - P3FILELISTS_DEBUG() << "(WW) request on index " << e << ", for directory ID=" << ((storage==NULL)?("[NULL]"):(storage->peerId().toStdString())) << " failed. This should not happen." << std::endl; -#endif - return false ; - } + /* update indexes. This is a bit hacky, but does the job. The cast to + * intptr_t is the proper way to convert a pointer into an int. */ + convertEntryIndexToPointer((intptr_t)d.ref,fi,d.ref); - // update indexes. This is a bit hacky, but does the job. The cast to intptr_t is the proper way to convert - // a pointer into an int. - - convertEntryIndexToPointer((intptr_t)d.ref,fi,d.ref) ; - - for(uint32_t i=0;i((intptr_t)d.children[i].ref,fi,d.children[i].ref); + for(uint32_t i=0; i( + (intptr_t) d.children[i].ref, fi, d.children[i].ref ); if(e == 0) // root { @@ -1178,9 +1192,9 @@ int p3FileDatabase::RequestDirDetails(void *ref, DirDetails& d, FileSearchFlags else { if(d.parent == 0) // child of root node - d.prow = (flags & RS_FILE_HINTS_LOCAL)?0:(fi-1); + d.prow = (flags & RS_FILE_HINTS_LOCAL) ? 0 : (fi-1); else - d.prow = storage->parentRow(e) ; + d.prow = storage->parentRow(e); convertEntryIndexToPointer((intptr_t)d.parent,fi,d.parent) ; } @@ -1307,7 +1321,9 @@ uint32_t p3FileDatabase::watchPeriod() return mLocalDirWatcher->fileWatchPeriod(); } -int p3FileDatabase::SearchKeywords(const std::list& keywords, std::list& results,FileSearchFlags flags,const RsPeerId& client_peer_id) +int p3FileDatabase::SearchKeywords( + const std::list& keywords, std::list& results, + FileSearchFlags flags, const RsPeerId& client_peer_id ) { if(flags & RS_FILE_HINTS_LOCAL) { @@ -1319,15 +1335,15 @@ int p3FileDatabase::SearchKeywords(const std::list& keywords, std:: mLocalSharedDirs->searchTerms(keywords,firesults) ; - for(std::list::iterator it(firesults.begin());it!=firesults.end();++it) - { - void *p=NULL; - convertEntryIndexToPointer(*it,0,p); - pointers.push_back(p) ; - } + for(auto& it: std::as_const(firesults)) + { + void *p = nullptr; + convertEntryIndexToPointer(it, 0, p); + pointers.push_back(p); + } } - filterResults(pointers,results,flags,client_peer_id) ; + filterResults(pointers, results, flags, client_peer_id); } if(flags & RS_FILE_HINTS_REMOTE) @@ -1610,78 +1626,90 @@ void p3FileDatabase::tickSend() void p3FileDatabase::handleDirSyncRequest(RsFileListsSyncRequestItem *item) { - RsFileListsSyncResponseItem *ritem = new RsFileListsSyncResponseItem; + RsFileListsSyncResponseItem* ritem = new RsFileListsSyncResponseItem; - // look at item TS. If local is newer, send the full directory content. - { - RS_STACK_MUTEX(mFLSMtx) ; + // look at item TS. If local is newer, send the full directory content. + { + RS_STACK_MUTEX(mFLSMtx); -#ifdef DEBUG_P3FILELISTS - P3FILELISTS_DEBUG() << "Received directory sync request from peer " << item->PeerId() << ". hash=" << item->entry_hash << ", flags=" << (void*)(intptr_t)item->flags << ", request id: " << std::hex << item->request_id << std::dec << ", last known TS: " << item->last_known_recurs_modf_TS << std::endl; -#endif + RS_DBG( "Received directory sync request from peer ", item->PeerId(), + ". hash=", item->entry_hash, ", flags=", item->flags, + ", request id: ", item->request_id, ", last known TS: ", + item->last_known_recurs_modf_TS ); - EntryIndex entry_index = DirectoryStorage::NO_INDEX; + EntryIndex entry_index = DirectoryStorage::NO_INDEX; + if(!mLocalSharedDirs->getIndexFromDirHash(item->entry_hash,entry_index)) + { + RS_DBG("Cannot find entry index for hash ", item->entry_hash, + " cannot respond to sync request." ); + return; + } - if(!mLocalSharedDirs->getIndexFromDirHash(item->entry_hash,entry_index)) - { -#ifdef DEBUG_P3FILELISTS - P3FILELISTS_DEBUG() << " (EE) Cannot find entry index for hash " << item->entry_hash << ": cannot respond to sync request." << std::endl; -#endif - return; - } + uint32_t entry_type = mLocalSharedDirs->getEntryType(entry_index); + ritem->PeerId(item->PeerId()); + ritem->request_id = item->request_id; + ritem->entry_hash = item->entry_hash; - uint32_t entry_type = mLocalSharedDirs->getEntryType(entry_index) ; - ritem->PeerId(item->PeerId()) ; - ritem->request_id = item->request_id; - ritem->entry_hash = item->entry_hash ; + std::list node_groups; + FileStorageFlags node_flags; - std::list node_groups; - FileStorageFlags node_flags; + if(entry_type != DIR_TYPE_DIR) + { + RS_DBG( "Directory does not exist anymore, or is not a directory, " + "or permission denied. Answering with proper flags." ); + ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | + RsFileListsItem::FLAGS_ENTRY_WAS_REMOVED; + } + else if( entry_index != 0 && + (!mLocalSharedDirs->getFileSharingPermissions( + entry_index, node_flags,node_groups ) || + !(rsPeers->computePeerPermissionFlags( + item->PeerId(), node_flags, node_groups ) & + RS_FILE_HINTS_BROWSABLE)) ) + { + RS_ERR("cannot get file permissions for entry index: ", entry_index, + ", or permission denied." ); + ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | + RsFileListsItem::FLAGS_ENTRY_WAS_REMOVED; + } + else + { + rstime_t local_recurs_max_time; + mLocalSharedDirs->getDirectoryRecursModTime( + entry_index, local_recurs_max_time ); - if(entry_type != DIR_TYPE_DIR) - { -#ifdef DEBUG_P3FILELISTS - P3FILELISTS_DEBUG() << " Directory does not exist anymore, or is not a directory, or permission denied. Answering with proper flags." << std::endl; -#endif - ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | RsFileListsItem::FLAGS_ENTRY_WAS_REMOVED ; - } - else if(entry_index != 0 && (!mLocalSharedDirs->getFileSharingPermissions(entry_index,node_flags,node_groups) || !(rsPeers->computePeerPermissionFlags(item->PeerId(),node_flags,node_groups) & RS_FILE_HINTS_BROWSABLE))) - { - P3FILELISTS_ERROR() << "(EE) cannot get file permissions for entry index " << (void*)(intptr_t)entry_index << ", or permission denied." << std::endl; - ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | RsFileListsItem::FLAGS_ENTRY_WAS_REMOVED ; - } - else - { - rstime_t local_recurs_max_time ; - mLocalSharedDirs->getDirectoryRecursModTime(entry_index,local_recurs_max_time) ; + /* normally, should be "<", but since we provided the TS it should + * be equal, so != is more robust. */ + if(item->last_known_recurs_modf_TS != local_recurs_max_time) + { + RS_DBG( "Directory is more recent than what the friend knows. " + "Sending full dir content as response."); - if(item->last_known_recurs_modf_TS != local_recurs_max_time) // normally, should be "<", but since we provided the TS it should be equal, so != is more robust. - { -#ifdef DEBUG_P3FILELISTS - P3FILELISTS_DEBUG() << " Directory is more recent than what the friend knows. Sending full dir content as response." << std::endl; -#endif + ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | + RsFileListsItem::FLAGS_SYNC_DIR_CONTENT; + ritem->last_known_recurs_modf_TS = local_recurs_max_time; - ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | RsFileListsItem::FLAGS_SYNC_DIR_CONTENT; - ritem->last_known_recurs_modf_TS = local_recurs_max_time; + /* We supply the peer id, in order to possibly remove some + * subdirs, if entries are not allowed to be seen by this peer. + */ + mLocalSharedDirs->serialiseDirEntry( + entry_index, ritem->directory_content_data, + item->PeerId() ); + } + else + { + RS_DBG3( "Directory is up to date w.r.t. what the friend knows." + " Sending ACK." ); - // We supply the peer id, in order to possibly remove some subdirs, if entries are not allowed to be seen by this peer. - mLocalSharedDirs->serialiseDirEntry(entry_index,ritem->directory_content_data,item->PeerId()) ; - } - else - { -#ifdef DEBUG_P3FILELISTS - P3FILELISTS_DEBUG() << " Directory is up to date w.r.t. what the friend knows. Sending ACK." << std::endl; -#endif + ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | + RsFileListsItem::FLAGS_ENTRY_UP_TO_DATE; + ritem->last_known_recurs_modf_TS = local_recurs_max_time; + } + } + } - ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | RsFileListsItem::FLAGS_ENTRY_UP_TO_DATE ; - ritem->last_known_recurs_modf_TS = local_recurs_max_time ; - } - } - } - - // sends the response. - - splitAndSendItem(ritem) ; + // sends the response. + splitAndSendItem(ritem); } void p3FileDatabase::splitAndSendItem(RsFileListsSyncResponseItem *ritem) diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 07a400acf..2da3c7398 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -3,7 +3,9 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2008 by Robert Fernie * + * Copyright (C) 2008 Robert Fernie * + * Copyright (C) 2018-2021 Gioacchino Mazzurco * + * Copyright (C) 2020-2021 Asociación Civil Altermundi * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -873,9 +875,11 @@ int ftServer::SearchKeywords(std::list keywords, std::listSearchKeywords(keywords, results,flags,RsPeerId()); } -int ftServer::SearchKeywords(std::list keywords, std::list &results,FileSearchFlags flags,const RsPeerId& peer_id) +int ftServer::SearchKeywords( + std::list keywords, std::list &results, + FileSearchFlags flags, const RsPeerId& peer_id ) { - return mFileDatabase->SearchKeywords(keywords, results,flags,peer_id); + return mFileDatabase->SearchKeywords(keywords, results, flags, peer_id); } int ftServer::SearchBoolExp(RsRegularExpression::Expression * exp, std::list &results,FileSearchFlags flags) diff --git a/libretroshare/src/turtle/p3turtle.cc b/libretroshare/src/turtle/p3turtle.cc index 10069056a..65f14048e 100644 --- a/libretroshare/src/turtle/p3turtle.cc +++ b/libretroshare/src/turtle/p3turtle.cc @@ -3,7 +3,9 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2009-2018 by Cyril Soler * + * Copyright (C) 2009-2018 Cyril Soler * + * Copyright (C) 2018-2021 Gioacchino Mazzurco * + * Copyright (C) 2021 Asociación Civil Altermundi * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -26,6 +28,10 @@ #include #include #include +#include +#include +#include +#include #include "rsserver/p3face.h" #include "crypto/rscrypto.h" @@ -39,13 +45,7 @@ #include "ft/ftcontroller.h" #include "p3turtle.h" - -#include -#include -#include - -#include - +#include "util/cxx17retrocompat.h" #include "util/rsdebug.h" #include "util/rsprint.h" #include "util/rsrandom.h" @@ -1975,7 +1975,8 @@ void p3turtle::handleTunnelResult(RsTurtleTunnelOkItem *item) // -void RsTurtleStringSearchRequestItem::search(std::list& result) const +void RsTurtleStringSearchRequestItem::search( + std::list& result ) const { /* call to core */ std::list initialResults; @@ -1988,17 +1989,19 @@ void RsTurtleStringSearchRequestItem::search(std::list& result) std::cerr << "Performing rsFiles->search()" << std::endl ; #endif // now, search! - rsFiles->SearchKeywords(words, initialResults,RS_FILE_HINTS_LOCAL | RS_FILE_HINTS_SEARCHABLE,PeerId()); + rsFiles->SearchKeywords( + words, initialResults, + RS_FILE_HINTS_LOCAL | RS_FILE_HINTS_SEARCHABLE, PeerId() ); #ifdef P3TURTLE_DEBUG std::cerr << initialResults.size() << " matches found." << std::endl ; #endif result.clear() ; - for(std::list::const_iterator it(initialResults.begin());it!=initialResults.end();++it) + for(auto& it: std::as_const(initialResults)) { // retain only file type - if (it->type == DIR_TYPE_DIR) + if (it.type == DIR_TYPE_DIR) { #ifdef P3TURTLE_DEBUG std::cerr << " Skipping directory " << it->name << std::endl ; @@ -2006,12 +2009,12 @@ void RsTurtleStringSearchRequestItem::search(std::list& result) continue; } - TurtleFileInfo i ; - i.hash = it->hash ; - i.size = it->size ; - i.name = it->name ; + TurtleFileInfo i; + i.hash = it.hash; + i.size = it.size; + i.name = it.name; - result.push_back(i) ; + result.push_back(i); } } void RsTurtleRegExpSearchRequestItem::search(std::list& result) const diff --git a/libretroshare/src/util/folderiterator.cc b/libretroshare/src/util/folderiterator.cc index c67204fc6..df3203ba2 100644 --- a/libretroshare/src/util/folderiterator.cc +++ b/libretroshare/src/util/folderiterator.cc @@ -38,8 +38,11 @@ namespace librs { namespace util { -FolderIterator::FolderIterator(const std::string& folderName, bool allow_symlinks, bool allow_files_from_the_future) - : mFolderName(folderName),mAllowSymLinks(allow_symlinks),mAllowFilesFromTheFuture(allow_files_from_the_future) +FolderIterator::FolderIterator( + const std::string& folderName, bool allow_symlinks, + bool allow_files_from_the_future ): + mFolderName(folderName), mAllowSymLinks(allow_symlinks), + mAllowFilesFromTheFuture(allow_files_from_the_future) { is_open = false ; validity = false ; diff --git a/libretroshare/src/util/folderiterator.h b/libretroshare/src/util/folderiterator.h index 19cbb8e9c..5a853eae3 100644 --- a/libretroshare/src/util/folderiterator.h +++ b/libretroshare/src/util/folderiterator.h @@ -44,8 +44,10 @@ namespace librs { namespace util { class FolderIterator { public: - FolderIterator(const std::string& folderName,bool allow_symlinks,bool allow_files_from_the_future = true); - ~FolderIterator(); + FolderIterator( + const std::string& folderName, bool allow_symlinks, + bool allow_files_from_the_future = true ); + ~FolderIterator(); enum { TYPE_UNKNOWN = 0x00, TYPE_FILE = 0x01, diff --git a/libretroshare/src/util/rsdir.h b/libretroshare/src/util/rsdir.h index 862e981db..9f4b2e6b5 100644 --- a/libretroshare/src/util/rsdir.h +++ b/libretroshare/src/util/rsdir.h @@ -81,10 +81,11 @@ const char *scanf_string_for_uint(int bytes) ; int breakupDirList(const std::string& path, std::list &subdirs); -// Splits the path into parent directory and file. File can be empty if full_path is a dir ending with '/' -// if full_path does not contain a directory, then dir will be "." and file will be full_path. - -bool splitDirFromFile(const std::string& full_path,std::string& dir, std::string& file); +/** Splits the path into parent directory and file. File can be empty if + * full_path is a dir ending with '/' if full_path does not contain a directory, + * then dir will be "." and file will be full_path */ +bool splitDirFromFile( const std::string& full_path, + std::string& dir, std::string& file ); bool copyFile(const std::string& source,const std::string& dest); diff --git a/libretroshare/src/util/rstime.h b/libretroshare/src/util/rstime.h index 6cc01e486..c17f2a5c7 100644 --- a/libretroshare/src/util/rstime.h +++ b/libretroshare/src/util/rstime.h @@ -3,7 +3,8 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2013-2013 by Cyril Soler * + * Copyright 2013 Cyril Soler * + * Copyright 2018 Gioacchino Mazzurco * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as *