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
This commit is contained in:
csoler 2009-08-09 16:32:31 +00:00
parent 6928b3c426
commit 4157321000
6 changed files with 168 additions and 111 deletions

View File

@ -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<std::string> forbidden_dirs ;
for(std::map<std::string,SharedDirInfo>::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<std::string>()); // 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<SharedDirInfo>::iterator it(pendingDirList.begin());it!=pendingDirList.end();++it)
for(std::list<SharedDirInfo>::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<std::string,SharedDirInfo>::iterator it(directoryMap.begin());it!=directoryMap.end();++it)
}
for(std::map<std::string,SharedDirInfo>::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<SharedDirInfo> dirs)

View File

@ -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;

View File

@ -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<std::string>& 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<std::string, DirEntry *>::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<std::string, DirEntry *>::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<std::string, FileEntry *>::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<std::string, DirEntry *>::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;

View File

@ -26,6 +26,7 @@
#include <string>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <stdint.h>
@ -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<std::string> inlist, time_t utime);
int getRootDirectories(std::list<std::string> &outlist);
/* control root entries */
int setRootDirectories(std::list<std::string> inlist, time_t utime);
int getRootDirectories(std::list<std::string> &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<std::string>& forbidden_roots);
/* search through this index */
int searchTerms(std::list<std::string> terms, std::list<FileEntry *> &results) const;
int searchHash(std::string hash, std::list<FileEntry *> &results) const;
int searchBoolExp(Expression * exp, std::list<FileEntry *> &results) const;
/* search through this index */
int searchTerms(std::list<std::string> terms, std::list<FileEntry *> &results) const;
int searchHash(std::string hash, std::list<FileEntry *> &results) const;
int searchBoolExp(Expression * exp, std::list<FileEntry *> &results) const;
PersonEntry *root;
PersonEntry *root;
};

View File

@ -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;

View File

@ -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;