diff --git a/libretroshare/src/file_sharing/dir_hierarchy.cc b/libretroshare/src/file_sharing/dir_hierarchy.cc index f7e8fe61a..94fa8eaff 100644 --- a/libretroshare/src/file_sharing/dir_hierarchy.cc +++ b/libretroshare/src/file_sharing/dir_hierarchy.cc @@ -227,7 +227,7 @@ bool InternalFileHierarchyStorage::updateFile(const DirectoryStorage::EntryIndex return true; } -bool InternalFileHierarchyStorage::updateDirEntry(const DirectoryStorage::EntryIndex& indx,const std::string& dir_name,time_t most_recent_time,time_t dir_modtime,const std::vector& subdirs_array,const std::vector& subfiles_array) +bool InternalFileHierarchyStorage::updateDirEntry(const DirectoryStorage::EntryIndex& indx,const std::string& dir_name,time_t most_recent_time,time_t dir_modtime,const std::vector& subdirs_hash,const std::vector& subfiles_hash) { if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) { @@ -241,47 +241,103 @@ bool InternalFileHierarchyStorage::updateDirEntry(const DirectoryStorage::EntryI d.dir_update_time = time(NULL); d.dir_name = dir_name; - d.subfiles = subfiles_array ; - d.subdirs = subdirs_array ; + d.subdirs.clear(); + d.subfiles.clear(); // check that all subdirs already exist. If not, create. - for(uint32_t i=0;i= mNodes.size() ) - mNodes.resize(subdirs_array[i]+1,NULL); + DirectoryStorage::EntryIndex dir_index = 0; + std::map::const_iterator it = mDirHashes.find(subdirs_hash[i]) ; - FileStorageNode *& node(mNodes[subdirs_array[i]]); - - if(node != NULL && node->type() != FileStorageNode::TYPE_DIR) + if(it == mDirHashes.end() || it->second >= mNodes.size()) { - delete node ; - node = NULL ; - } + // find an epty slot + int found = -1 ; - if(node == NULL) + for(uint32_t j=0;jsecond; + + if(mNodes[dir_index] != NULL && mNodes[dir_index]->type() != FileStorageNode::TYPE_DIR) + { + delete mNodes[dir_index] ; + mNodes[dir_index] = NULL ; + } + } + FileStorageNode *& node(mNodes[dir_index]); + if(!node) node = new DirEntry(""); + d.subdirs.push_back(dir_index) ; + ((DirEntry*&)node)->dir_parent_path = d.dir_parent_path + "/" + dir_name ; node->row = i ; node->parent_index = indx ; } - for(uint32_t i=0;i= mNodes.size() ) - mNodes.resize(subfiles_array[i]+1,NULL); + DirectoryStorage::EntryIndex file_index = 0; + std::map::const_iterator it = mFileHashes.find(subfiles_hash[i]) ; - FileStorageNode *& node(mNodes[subfiles_array[i]]); - - if(node != NULL && node->type() != FileStorageNode::TYPE_FILE) + if(it == mFileHashes.end()) { - delete node ; - node = NULL ; + // find an epty slot + int found = -1; + + for(uint32_t j=0;jsecond ; - if(node == NULL) - node = new FileEntry("",0,0); + if(mNodes[file_index] != NULL && mNodes[file_index]->type() != FileStorageNode::TYPE_FILE) + { + delete mNodes[file_index] ; + mNodes[file_index] = NULL ; + } - node->row = subdirs_array.size()+i ; + file_index = it->second; + } + FileStorageNode *& node(mNodes[file_index]); + if(!node) + node = new FileEntry("",0,0,subfiles_hash[i]); + + d.subfiles.push_back(file_index) ; + + node->row = subdirs_hash.size()+i ; node->parent_index = indx ; } diff --git a/libretroshare/src/file_sharing/dir_hierarchy.h b/libretroshare/src/file_sharing/dir_hierarchy.h index 3115543d5..13a3f5c3e 100644 --- a/libretroshare/src/file_sharing/dir_hierarchy.h +++ b/libretroshare/src/file_sharing/dir_hierarchy.h @@ -73,7 +73,7 @@ public: bool updateSubFilesList(const DirectoryStorage::EntryIndex& indx,const std::map& subfiles,std::map& new_files); bool updateHash(const DirectoryStorage::EntryIndex& file_index,const RsFileHash& hash); bool updateFile(const DirectoryStorage::EntryIndex& file_index,const RsFileHash& hash, const std::string& fname,uint64_t size, const time_t modf_time); - bool updateDirEntry(const DirectoryStorage::EntryIndex& indx,const std::string& dir_name,time_t most_recent_time,time_t dir_modtime,const std::vector& subdirs_array,const std::vector& subfiles_array); + bool updateDirEntry(const DirectoryStorage::EntryIndex& indx, const std::string& dir_name, time_t most_recent_time, time_t dir_modtime, const std::vector &subdirs_hash, const std::vector &subfiles_hash); bool getDirUpdateTS(const DirectoryStorage::EntryIndex& index,time_t& recurs_max_modf_TS,time_t& local_update_TS); bool setDirUpdateTS(const DirectoryStorage::EntryIndex& index,time_t& recurs_max_modf_TS,time_t& local_update_TS); diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index 94e5f74ed..bdcd24c84 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -466,7 +466,7 @@ bool LocalDirectoryStorage::serialiseDirEntry(const EntryIndex& indx,RsTlvBinary } // compute list of allowed subdirs - std::vector allowed_subdirs ; + std::vector allowed_subdirs ; FileStorageFlags node_flags ; std::list node_groups ; @@ -474,7 +474,16 @@ bool LocalDirectoryStorage::serialiseDirEntry(const EntryIndex& indx,RsTlvBinary for(uint32_t i=0;isubdirs.size();++i) if(indx != 0 || (locked_getFileSharingPermissions(dir->subdirs[i],node_flags,node_groups) && (rsPeers->computePeerPermissionFlags(client_id,node_flags,node_groups) & RS_FILE_HINTS_BROWSABLE))) - allowed_subdirs.push_back(dir->subdirs[i]) ; + { + RsFileHash hash ; + + if(!getHashFromIndex(dir->subdirs[i],hash)) + { + std::cerr << "(EE) cannot get hash from index for subdir " << dir->subdirs[i] << " at position " << i << " in subdirs list. Weird." << std::endl; + continue ; + } + allowed_subdirs.push_back(hash) ; + } unsigned char *section_data = NULL; uint32_t section_size = 0; @@ -498,7 +507,7 @@ bool LocalDirectoryStorage::serialiseDirEntry(const EntryIndex& indx,RsTlvBinary // serialise subdirs entry indexes for(uint32_t i=0;isubfiles[i] )) return false ; if(!FileListIO::writeField(file_section_data,file_section_size,file_section_offset,FILE_LIST_IO_TAG_FILE_NAME ,file->file_name )) return false ; if(!FileListIO::writeField(file_section_data,file_section_size,file_section_offset,FILE_LIST_IO_TAG_FILE_SIZE ,file->file_size )) return false ; if(!FileListIO::writeField(file_section_data,file_section_size,file_section_offset,FILE_LIST_IO_TAG_FILE_SHA1_HASH,file->file_hash )) return false ; @@ -594,14 +602,14 @@ bool RemoteDirectoryStorage::deserialiseUpdateDirEntry(const EntryIndex& indx,co // serialise subdirs entry indexes - std::vector subdirs_array ; - uint32_t subdir_index ; + std::vector subdirs_hashes ; + RsFileHash subdir_hash ; for(uint32_t i=0;iupdateDirEntry(indx,dir_name,most_recent_time,dir_modtime,subdirs_array,subfiles_array)) + // First create the entries for each subdir and each subfile, if needed. + if(!mFileHierarchy->updateDirEntry(indx,dir_name,most_recent_time,dir_modtime,subdirs_hashes,subfiles_hash)) { std::cerr << "(EE) Cannot update dir entry with index " << indx << ": entry does not exist." << std::endl; return false ; } - // then update the subfiles - for(uint32_t i=0;iupdateFile(subfiles_array[i],subfiles_hash[i],subfiles_name[i],subfiles_size[i],subfiles_modtime[i])) - std::cerr << "(EE) Cannot update file with index " << subfiles_array[i] << ". This is very weird. Entry should have just been created and therefore should exist. Skipping." << std::endl; + if(!mFileHierarchy->updateFile(file_index,subfiles_hash[i],subfiles_name[i],subfiles_size[i],subfiles_modtime[i])) + std::cerr << "(EE) Cannot update file with index " << file_index <<" and hash " << subfiles_hash[i] << ". This is very weird. Entry should have just been created and therefore should exist. Skipping." << std::endl; } mChanged = true ;