From 4157321000a2455ef37126fa8a4640381d95ff98 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 9 Aug 2009 16:32:31 +0000 Subject: [PATCH] Share Flags - now only the directories with flag "browsable" on are dispatched to friends - when switching flags, directories are re-sent to friends Still to do: - turtle search only files with "Network wide" flag on - test what happens when 2 directories have the same base name - display own directories by calling FileIndexMonitor instead of FileIndexStore Warning: at this time, directories that are not browsable do not appear in the Files dialog under "my directories". git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1506 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dbase/fimonitor.cc | 175 ++++++++++++++++----------- libretroshare/src/dbase/fimonitor.h | 2 + libretroshare/src/dbase/findex.cc | 49 ++++++-- libretroshare/src/dbase/findex.h | 45 +++---- libretroshare/src/rsiface/rsfiles.h | 4 - retroshare-gui/src/rsiface/rsfiles.h | 4 - 6 files changed, 168 insertions(+), 111 deletions(-) diff --git a/libretroshare/src/dbase/fimonitor.cc b/libretroshare/src/dbase/fimonitor.cc index 2ed1072d4..0e000f252 100644 --- a/libretroshare/src/dbase/fimonitor.cc +++ b/libretroshare/src/dbase/fimonitor.cc @@ -150,8 +150,11 @@ bool FileIndexMonitor::loadLocalCache(const CacheData &data) /* called with sto //fi.root->name = data.pid; /* More error checking needed here! */ - if ((ok = fi.loadIndex(data.path + '/' + data.name, - data.hash, data.size))) + + std::string name = data.name ; // this trick allows to load the complete file. Not the one being shared. + name[name.length()-1] = 'c' ; + + if ((ok = fi.loadIndex(data.path + '/' + data.name, data.hash, data.size))) { #ifdef FIM_DEBUG std::cerr << "FileIndexMonitor::loadCache() Success!"; @@ -544,56 +547,8 @@ void FileIndexMonitor::updateCycle() if (fiMods) - { - /* store to the cacheDirectory */ - fiMutex.lock(); { /* LOCKED DIRS */ - - std::string path = getCacheDir(); - std::ostringstream out; - out << "fc-own-" << time(NULL) << ".rsfc"; - - std::string tmpname = out.str(); - std::string fname = path + "/" + tmpname; - -#ifdef FIM_DEBUG - std::cerr << "FileIndexMonitor::updateCycle() FileIndex modified ... updating"; - std::cerr << std::endl; - std::cerr << "FileIndexMonitor::updateCycle() saving to: " << fname; - std::cerr << std::endl; -#endif - - std::string calchash; - uint64_t size; - - fi.saveIndex(fname, calchash, size); - -#ifdef FIM_DEBUG - std::cerr << "FileIndexMonitor::updateCycle() saved with hash:" << calchash; - std::cerr << std::endl; -#endif - - /* should clean up the previous cache.... */ - - /* flag as new info */ - CacheData data; - data.pid = fi.root->id; - data.cid.type = getCacheType(); - data.cid.subid = 0; - data.path = path; - data.name = tmpname; - data.hash = calchash; - data.size = size; - data.recvd = time(NULL); - - updateCache(data); - -#ifdef FIM_DEBUG - std::cerr << "FileIndexMonitor::updateCycle() called updateCache()"; - std::cerr << std::endl; -#endif - - } fiMutex.unlock(); /* UNLOCKED DIRS */ - } + saveFileIndexes() ; + { RsStackMutex stack(fiMutex); /**** LOCKED DIRS ****/ @@ -602,37 +557,115 @@ void FileIndexMonitor::updateCycle() cb->notifyHashingInfo("") ; } +void FileIndexMonitor::saveFileIndexes() +{ + /* store to the cacheDirectory */ + RsStackMutex mutex(fiMutex) ; /* LOCKED DIRS */ + + std::string path = getCacheDir(); + + // Two files are saved: one with only browsable dirs, which will be shared by the cache system, + // and one with the complete file collection. + // + std::ostringstream out; + out << "fc-own-" << time(NULL) << ".rsfb"; + std::string tmpname_browsable = out.str(); + std::string fname_browsable = path + "/" + tmpname_browsable; + + std::ostringstream out2; + out2 << "fc-own-" << time(NULL) << ".rsfc"; + std::string tmpname_total = out2.str(); + std::string fname_total = path + "/" + tmpname_total; + +#ifdef FIM_DEBUG + std::cerr << "FileIndexMonitor::updateCycle() FileIndex modified ... updating"; + std::cerr << std::endl; + std::cerr << "FileIndexMonitor::updateCycle() saving browsable file list to: " << fname_browsable << std::endl ; + std::cerr << "FileIndexMonitor::updateCycle() saving total file list to to: " << fname_total << std::endl ; +#endif + + std::string calchash; + uint64_t size; + + std::cerr << "About to save, with the following restrictions:" << std::endl ; + std::set forbidden_dirs ; + for(std::map::const_iterator it(directoryMap.begin());it!=directoryMap.end();++it) + { + std::cerr << " dir=" << it->first << " : " ; + if((it->second.shareflags & RS_FILE_HINTS_BROWSABLE) == 0) + { + std::cerr << "forbidden" << std::endl; + forbidden_dirs.insert(it->first) ; + } + else + std::cerr << "autorized" << std::endl; + } + + uint64_t sizetmp ; + + fi.saveIndex(fname_total, calchash, sizetmp,std::set()); // save all files + fi.saveIndex(fname_browsable, calchash, size,forbidden_dirs); // save only browsable files + +#ifdef FIM_DEBUG + std::cerr << "FileIndexMonitor::updateCycle() saved with hash:" << calchash; + std::cerr << std::endl; +#endif + + /* should clean up the previous cache.... */ + + /* flag as new info */ + CacheData data; + data.pid = fi.root->id; + data.cid.type = getCacheType(); + data.cid.subid = 0; + data.path = path; + data.name = tmpname_browsable; + data.hash = calchash; + data.size = size; + data.recvd = time(NULL); + + updateCache(data); + +#ifdef FIM_DEBUG + std::cerr << "FileIndexMonitor::updateCycle() called updateCache()"; + std::cerr << std::endl; +#endif +} + void FileIndexMonitor::updateShareFlags(const SharedDirInfo& dir) { + bool fimods = false ; #ifdef FIM_DEBUG std::cerr << "*** FileIndexMonitor: Updating flags for " << dir.filename << " to " << dir.shareflags << std::endl ; #endif { RsStackMutex stack(fiMutex) ; /* LOCKED DIRS */ - - if(pendingDirs) - for(std::list::iterator it(pendingDirList.begin());it!=pendingDirList.end();++it) + + for(std::list::iterator it(pendingDirList.begin());it!=pendingDirList.end();++it) + { + std::cerr << "** testing pending dir " << (*it).filename << std::endl ; + if((*it).filename == dir.filename) { - std::cerr << "** testing pending dir " << (*it).filename << std::endl ; - if((*it).filename == dir.filename) - { - std::cerr << "** Updating to " << (*it).shareflags << "!!" << std::endl ; - (*it).shareflags = dir.shareflags ; - break ; - } + std::cerr << "** Updating to " << (*it).shareflags << "!!" << std::endl ; + (*it).shareflags = dir.shareflags ; + break ; } - else - for(std::map::iterator it(directoryMap.begin());it!=directoryMap.end();++it) + } + + for(std::map::iterator it(directoryMap.begin());it!=directoryMap.end();++it) + { + std::cerr << "** testing " << (*it).second.filename << std::endl ; + if((*it).second.filename == dir.filename) { - std::cerr << "** testing " << (*it).second.filename << std::endl ; - if((*it).second.filename == dir.filename) - { - std::cerr << "** Updating from " << it->second.shareflags << "!!" << std::endl ; - (*it).second.shareflags = dir.shareflags ; - break ; - } + std::cerr << "** Updating from " << it->second.shareflags << "!!" << std::endl ; + (*it).second.shareflags = dir.shareflags ; + fimods = true ; + break ; } + } } + if(fimods) + saveFileIndexes() ; } /* interface */ void FileIndexMonitor::setSharedDirectories(std::list dirs) diff --git a/libretroshare/src/dbase/fimonitor.h b/libretroshare/src/dbase/fimonitor.h index b106c68c6..d625156ac 100644 --- a/libretroshare/src/dbase/fimonitor.h +++ b/libretroshare/src/dbase/fimonitor.h @@ -106,6 +106,8 @@ class FileIndexMonitor: public CacheSource, public RsThread /* util fns */ private: + // saves file indexs and update the cache. + void saveFileIndexes() ; /* the mutex should be locked before calling... these. */ std::string locked_findRealRoot(std::string base) const; diff --git a/libretroshare/src/dbase/findex.cc b/libretroshare/src/dbase/findex.cc index 6f526b3c7..b86636999 100644 --- a/libretroshare/src/dbase/findex.cc +++ b/libretroshare/src/dbase/findex.cc @@ -859,7 +859,7 @@ error: } -int FileIndex::saveIndex(std::string filename, std::string &fileHash, uint64_t &size) +int FileIndex::saveIndex(std::string filename, std::string &fileHash, uint64_t &size,const std::set& forbidden_dirs) { unsigned char sha_buf[SHA_DIGEST_LENGTH]; std::ofstream file (filename.c_str(), std::ofstream::binary); @@ -881,7 +881,25 @@ int FileIndex::saveIndex(std::string filename, std::string &fileHash, uint64_t & oss << "#" << std::endl; /* begin recusion */ - root->saveEntry(oss); + root->writeDirInfo(oss) ; + + std::map::iterator it; + for(it = root->subdirs.begin(); it != root->subdirs.end(); it++) + { + std::cout << "writting root directory: name=" << it->second->name << ", path=" << it->second->path << std::endl ; + if(forbidden_dirs.find(it->second->name) != forbidden_dirs.end()) + std::cerr << " will be suppressed." << std::endl ; + else + { + std::cerr << " will be saved." << std::endl ; + (it->second)->saveEntry(oss); + } + } + + root->writeFileInfo(oss) ; // this should never do anything + + /* signal to pop directory from stack in loadIndex() */ + oss << "-" << std::endl; /* calculate sha1 hash */ SHA_CTX *sha_ctx = new SHA_CTX; @@ -920,9 +938,7 @@ std::string FixName(std::string in) return in; } - -/* recusive function for traversing the dir tree in preorder */ -int DirEntry::saveEntry(std::ostringstream &oss) +void DirEntry::writeDirInfo(std::ostringstream& oss) { /* print node info */ oss << "d"; @@ -932,13 +948,10 @@ int DirEntry::saveEntry(std::ostringstream &oss) oss << modtime << ","; oss << pop << ","; oss << updtime << "," << std::endl; +} - std::map::iterator it; - for(it = subdirs.begin(); it != subdirs.end(); it++) - { - (it->second)->saveEntry(oss); - } - +void DirEntry::writeFileInfo(std::ostringstream& oss) +{ /* print file info */ std::map::iterator fit; for(fit = files.begin(); fit != files.end(); fit++) @@ -951,6 +964,20 @@ int DirEntry::saveEntry(std::ostringstream &oss) oss << (fit->second)->pop << ","; oss << (fit->second)->updtime << "," << std::endl; } +} + +/* recusive function for traversing the dir tree in preorder */ +int DirEntry::saveEntry(std::ostringstream &oss) +{ + writeDirInfo(oss) ; + + std::map::iterator it; + for(it = subdirs.begin(); it != subdirs.end(); it++) + { + (it->second)->saveEntry(oss); + } + + writeFileInfo(oss) ; /* signal to pop directory from stack in loadIndex() */ oss << "-" << std::endl; diff --git a/libretroshare/src/dbase/findex.h b/libretroshare/src/dbase/findex.h index 92aac6e6f..f83235199 100644 --- a/libretroshare/src/dbase/findex.h +++ b/libretroshare/src/dbase/findex.h @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -134,6 +135,8 @@ int updateDirectories(std::string path, int pop, int modtime); int print(std::ostream &out); int saveEntry(std::ostringstream &out); +void writeDirInfo(std::ostringstream&); +void writeFileInfo(std::ostringstream&); /* Data */ std::string path; /* full path (includes name) */ @@ -194,35 +197,35 @@ class Expression; class FileIndex { public: - FileIndex(std::string pid); - ~FileIndex(); + FileIndex(std::string pid); + ~FileIndex(); - /* control root entries */ -int setRootDirectories(std::list inlist, time_t utime); -int getRootDirectories(std::list &outlist); + /* control root entries */ + int setRootDirectories(std::list inlist, time_t utime); + int getRootDirectories(std::list &outlist); - /* update (index building) */ -DirEntry * updateDirEntry(std::string path, FileEntry fe, time_t utime); -FileEntry * updateFileEntry(std::string path, FileEntry fe, time_t utime); + /* update (index building) */ + DirEntry * updateDirEntry(std::string path, FileEntry fe, time_t utime); + FileEntry * updateFileEntry(std::string path, FileEntry fe, time_t utime); -DirEntry * findOldDirectory(time_t old); /* finds directories older than old */ -int removeOldDirectory(std::string fpath, std::string name, time_t old); + DirEntry * findOldDirectory(time_t old); /* finds directories older than old */ + int removeOldDirectory(std::string fpath, std::string name, time_t old); -int cleanOldEntries(time_t old); /* removes entries older than old */ + int cleanOldEntries(time_t old); /* removes entries older than old */ - /* debug */ -int printFileIndex(std::ostream &out); + /* debug */ + int printFileIndex(std::ostream &out); - /* load/save to file */ -int loadIndex(std::string filename, std::string expectedHash, uint64_t size); -int saveIndex(std::string filename, std::string &fileHash, uint64_t &size); + /* load/save to file */ + int loadIndex(std::string filename, std::string expectedHash, uint64_t size); + int saveIndex(std::string filename, std::string &fileHash, uint64_t &size, const std::set& forbidden_roots); - /* search through this index */ -int searchTerms(std::list terms, std::list &results) const; -int searchHash(std::string hash, std::list &results) const; -int searchBoolExp(Expression * exp, std::list &results) const; + /* search through this index */ + int searchTerms(std::list terms, std::list &results) const; + int searchHash(std::string hash, std::list &results) const; + int searchBoolExp(Expression * exp, std::list &results) const; - PersonEntry *root; + PersonEntry *root; }; diff --git a/libretroshare/src/rsiface/rsfiles.h b/libretroshare/src/rsiface/rsfiles.h index 71cc47f73..33640fa9f 100644 --- a/libretroshare/src/rsiface/rsfiles.h +++ b/libretroshare/src/rsiface/rsfiles.h @@ -72,10 +72,6 @@ const uint32_t RS_FILE_HINTS_TURTLE = 0x00000040; const uint32_t RS_FILE_HINTS_NETWORK_WIDE = 0x00000080; // anonymously shared over network const uint32_t RS_FILE_HINTS_BROWSABLE = 0x00000100; // browsable by friends -//const uint32_t RS_SHARED_DIR_ANONYMOUS = 0x01 ; -//const uint32_t RS_SHARED_DIR_BROWSABLE = 0x02 ; -//const uint32_t RS_SHARED_DIR_UNIVERSAL = 0x03 ; - const uint32_t RS_FILE_HINTS_SPEC_ONLY = 0x01000000; const uint32_t RS_FILE_HINTS_NO_SEARCH = 0x02000000; diff --git a/retroshare-gui/src/rsiface/rsfiles.h b/retroshare-gui/src/rsiface/rsfiles.h index 71cc47f73..33640fa9f 100644 --- a/retroshare-gui/src/rsiface/rsfiles.h +++ b/retroshare-gui/src/rsiface/rsfiles.h @@ -72,10 +72,6 @@ const uint32_t RS_FILE_HINTS_TURTLE = 0x00000040; const uint32_t RS_FILE_HINTS_NETWORK_WIDE = 0x00000080; // anonymously shared over network const uint32_t RS_FILE_HINTS_BROWSABLE = 0x00000100; // browsable by friends -//const uint32_t RS_SHARED_DIR_ANONYMOUS = 0x01 ; -//const uint32_t RS_SHARED_DIR_BROWSABLE = 0x02 ; -//const uint32_t RS_SHARED_DIR_UNIVERSAL = 0x03 ; - const uint32_t RS_FILE_HINTS_SPEC_ONLY = 0x01000000; const uint32_t RS_FILE_HINTS_NO_SEARCH = 0x02000000;