Merge pull request #584 from mr-alice/v0.6-FileLists

added an option to follow symbolic links or not, and anti-loop system…
This commit is contained in:
Cyril Soler 2016-11-25 22:30:47 +01:00 committed by GitHub
commit c92c270a4e
13 changed files with 230 additions and 128 deletions

View File

@ -43,6 +43,8 @@ LocalDirectoryUpdater::LocalDirectoryUpdater(HashStorage *hc,LocalDirectoryStora
mDelayBetweenDirectoryUpdates = DELAY_BETWEEN_DIRECTORY_UPDATES;
mIsEnabled = false ;
mFollowSymLinks = FOLLOW_SYMLINKS_DEFAULT ;
mNeedsFullRebuild = false ;
}
bool LocalDirectoryUpdater::isEnabled() const
@ -69,6 +71,8 @@ void LocalDirectoryUpdater::data_tick()
if(now > mDelayBetweenDirectoryUpdates + mLastSweepTime)
{
sweepSharedDirectories() ;
mNeedsFullRebuild = false ;
mLastSweepTime = now;
mSharedDirectories->notifyTSChanged() ;
}
@ -119,29 +123,43 @@ void LocalDirectoryUpdater::sweepSharedDirectories()
// now for each of them, go recursively and match both files and dirs
std::set<std::string> existing_dirs ;
for(DirectoryStorage::DirIterator stored_dir_it(mSharedDirectories,mSharedDirectories->root()) ; stored_dir_it;++stored_dir_it)
{
#ifdef DEBUG_LOCAL_DIR_UPDATER
std::cerr << "[directory storage] recursing into " << stored_dir_it.name() << std::endl;
#endif
recursUpdateSharedDir(stored_dir_it.name(), *stored_dir_it) ; // here we need to use the list that was stored, instead of the shared dir list, because the two
recursUpdateSharedDir(stored_dir_it.name(), *stored_dir_it,existing_dirs) ; // here we need to use the list that was stored, instead of the shared dir list, because the two
// are not necessarily in the same order.
}
RsServer::notify()->notifyListChange(NOTIFY_LIST_DIRLIST_LOCAL, 0);
}
void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx)
void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx,std::set<std::string>& existing_directories)
{
#ifdef DEBUG_LOCAL_DIR_UPDATER
std::cerr << "[directory storage] parsing directory " << cumulated_path << ", index=" << indx << std::endl;
#endif
if(mFollowSymLinks)
{
std::string real_path = RsDirUtil::removeSymLinks(cumulated_path) ;
if(existing_directories.end() != existing_directories.find(real_path))
{
std::cerr << "(WW) Directory " << cumulated_path << " has real path " << real_path << " which already belongs to another shared directory. Ignoring" << std::endl;
return ;
}
existing_directories.insert(real_path) ;
}
// make sure list of subdirs is the same
// make sure list of subfiles is the same
// request all hashes to the hashcache
librs::util::FolderIterator dirIt(cumulated_path,false,false); // disallow symbolic links and files from the future.
librs::util::FolderIterator dirIt(cumulated_path,mFollowSymLinks,false); // disallow symbolic links and files from the future.
time_t dir_local_mod_time ;
if(!mSharedDirectories->getDirectoryLocalModTime(indx,dir_local_mod_time))
@ -150,8 +168,8 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p
return;
}
if(dirIt.dir_modtime() > dir_local_mod_time) // the > is because we may have changed the virtual name, and therefore the TS wont match.
// we only want to detect when the directory has changed on the disk
if(mNeedsFullRebuild || dirIt.dir_modtime() > dir_local_mod_time) // the > is because we may have changed the virtual name, and therefore the TS wont match.
// we only want to detect when the directory has changed on the disk
{
// collect subdirs and subfiles
@ -213,7 +231,7 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p
#ifdef DEBUG_LOCAL_DIR_UPDATER
std::cerr << " recursing into " << stored_dir_it.name() << std::endl;
#endif
recursUpdateSharedDir(cumulated_path + "/" + stored_dir_it.name(), *stored_dir_it) ;
recursUpdateSharedDir(cumulated_path + "/" + stored_dir_it.name(), *stored_dir_it,existing_directories) ;
}
}
@ -244,6 +262,20 @@ uint32_t LocalDirectoryUpdater::fileWatchPeriod() const
return mDelayBetweenDirectoryUpdates ;
}
void LocalDirectoryUpdater::setFollowSymLinks(bool b)
{
if(b != mFollowSymLinks)
mNeedsFullRebuild = true ;
mFollowSymLinks = b ;
forceUpdate();
}
bool LocalDirectoryUpdater::followSymLinks() const
{
return mFollowSymLinks ;
}

View File

@ -47,6 +47,9 @@ public:
void setFileWatchPeriod(int seconds) ;
uint32_t fileWatchPeriod() const ;
void setFollowSymLinks(bool b) ;
bool followSymLinks() const ;
void setEnabled(bool b) ;
bool isEnabled() const ;
@ -56,7 +59,7 @@ protected:
virtual void hash_callback(uint32_t client_param, const std::string& name, const RsFileHash& hash, uint64_t size);
virtual bool hash_confirm(uint32_t client_param) ;
void recursUpdateSharedDir(const std::string& cumulated_path,DirectoryStorage::EntryIndex indx);
void recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, std::set<std::string>& existing_directories);
void sweepSharedDirectories();
private:
@ -70,5 +73,7 @@ private:
uint32_t mDelayBetweenDirectoryUpdates;
bool mIsEnabled ;
bool mFollowSymLinks;
bool mNeedsFullRebuild ;
};

View File

@ -37,6 +37,7 @@ static const uint32_t DELAY_BEFORE_DELETE_EMPTY_REMOTE_DIR = 5*24*86400 ;
static const std::string HASH_CACHE_DURATION_SS = "HASH_CACHE_DURATION" ; // key string to store hash remembering time
static const std::string WATCH_FILE_DURATION_SS = "WATCH_FILES_DELAY" ; // key to store delay before re-checking for new files
static const std::string WATCH_FILE_ENABLED_SS = "WATCH_FILES_ENABLED"; // key to store ON/OFF flags for file whatch
static const std::string FOLLOW_SYMLINKS_SS = "FOLLOW_SYMLINKS"; // dereference symbolic links, or just ignore them.
static const std::string WATCH_HASH_SALT_SS = "WATCH_HASH_SALT"; // Salt that is used to hash directory names
static const std::string FILE_SHARING_DIR_NAME = "file_sharing" ; // hard-coded directory name to store friend file lists, hash cache, etc.
@ -54,4 +55,6 @@ static const uint32_t NB_ENTRY_INDEX_BITS = 22 ; // Do not
static const uint32_t ENTRY_INDEX_BIT_MASK = 0x003fffff ; // used for storing (EntryIndex,Friend) couples into a 32bits pointer. Depends on the two values just before. Dont change!
static const uint32_t DELAY_BEFORE_DROP_REQUEST = 600; // every 10 min
static const bool FOLLOW_SYMLINKS_DEFAULT = true;
static const uint32_t FL_BASE_TMP_SECTION_SIZE = 4096 ;

View File

@ -273,19 +273,22 @@ cleanup = true;
mLocalSharedDirs->getSharedDirectoryList(dirList);
}
for(std::list<SharedDirInfo>::iterator it = dirList.begin(); it != dirList.end(); ++it)
{
RsFileConfigItem *fi = new RsFileConfigItem();
{
RS_STACK_MUTEX(mFLSMtx) ;
for(std::list<SharedDirInfo>::iterator it = dirList.begin(); it != dirList.end(); ++it)
{
RsFileConfigItem *fi = new RsFileConfigItem();
fi->file.path = (*it).filename ;
fi->file.name = (*it).virtualname ;
fi->flags = (*it).shareflags.toUInt32() ;
fi->file.path = (*it).filename ;
fi->file.name = (*it).virtualname ;
fi->flags = (*it).shareflags.toUInt32() ;
for(std::list<RsNodeGroupId>::const_iterator it2( (*it).parent_groups.begin());it2!=(*it).parent_groups.end();++it2)
fi->parent_groups.ids.insert(*it2) ;
for(std::list<RsNodeGroupId>::const_iterator it2( (*it).parent_groups.begin());it2!=(*it).parent_groups.end();++it2)
fi->parent_groups.ids.insert(*it2) ;
sList.push_back(fi);
}
sList.push_back(fi);
}
}
RsConfigKeyValueSet *rskv = new RsConfigKeyValueSet();
@ -314,7 +317,14 @@ cleanup = true;
rskv->tlvkvs.pairs.push_back(kv);
}
{
RsTlvKeyValue kv;
kv.key = FOLLOW_SYMLINKS_SS;
kv.value = followSymLinks()?"YES":"NO" ;
rskv->tlvkvs.pairs.push_back(kv);
}
{
RsTlvKeyValue kv;
@ -373,6 +383,10 @@ bool p3FileDatabase::loadList(std::list<RsItem *>& load)
if(sscanf(kit->value.c_str(),"%d",&t) == 1)
setWatchPeriod(t);
}
else if(kit->key == FOLLOW_SYMLINKS_SS)
{
setFollowSymLinks(kit->value == "YES") ;
}
else if(kit->key == WATCH_FILE_ENABLED_SS)
{
setWatchEnabled(kit->value == "YES") ;
@ -891,6 +905,17 @@ bool p3FileDatabase::inDirectoryCheck()
RS_STACK_MUTEX(mFLSMtx) ;
return mLocalDirWatcher->inDirectoryCheck();
}
void p3FileDatabase::setFollowSymLinks(bool b)
{
RS_STACK_MUTEX(mFLSMtx) ;
mLocalDirWatcher->setFollowSymLinks(b) ;
IndicateConfigChanged();
}
bool p3FileDatabase::followSymLinks() const
{
RS_STACK_MUTEX(mFLSMtx) ;
return mLocalDirWatcher->followSymLinks() ;
}
void p3FileDatabase::setWatchEnabled(bool b)
{
RS_STACK_MUTEX(mFLSMtx) ;

View File

@ -138,6 +138,9 @@ class p3FileDatabase: public p3Service, public p3Config, public ftSearch //, pub
void setWatchEnabled(bool b) ;
bool watchEnabled() ;
bool followSymLinks() const;
void setFollowSymLinks(bool b) ;
// interfact for directory parsing
void forceDirectoryCheck(); // Force re-sweep the directories and see what's changed

View File

@ -804,11 +804,13 @@ bool ftServer::removeSharedDirectory(std::string dir)
return true;
}
bool ftServer::watchEnabled() { return mFileDatabase->watchEnabled() ; }
int ftServer::watchPeriod() const { return mFileDatabase->watchPeriod()/60 ; }
bool ftServer::watchEnabled() { return mFileDatabase->watchEnabled() ; }
int ftServer::watchPeriod() const { return mFileDatabase->watchPeriod()/60 ; }
bool ftServer::followSymLinks() const { return mFileDatabase->followSymLinks() ; }
void ftServer::setWatchEnabled(bool b) { mFileDatabase->setWatchEnabled(b) ; }
void ftServer::setWatchPeriod(int minutes) { mFileDatabase->setWatchPeriod(minutes*60) ; }
void ftServer::setWatchEnabled(bool b) { mFileDatabase->setWatchEnabled(b) ; }
void ftServer::setWatchPeriod(int minutes) { mFileDatabase->setWatchPeriod(minutes*60) ; }
void ftServer::setFollowSymLinks(bool b) { mFileDatabase->setFollowSymLinks(b) ; }
bool ftServer::getShareDownloadDirectory()
{

View File

@ -215,6 +215,8 @@ public:
virtual int watchPeriod() const ;
virtual void setWatchEnabled(bool b) ;
virtual bool watchEnabled() ;
virtual bool followSymLinks() const;
virtual void setFollowSymLinks(bool b);
/***************************************************************/
/*************** Data Transfer Interface ***********************/

View File

@ -247,6 +247,8 @@ class RsFiles
virtual void setWatchEnabled(bool b) =0;
virtual int watchPeriod() const =0;
virtual bool watchEnabled() =0;
virtual bool followSymLinks() const=0;
virtual void setFollowSymLinks(bool b)=0 ;
virtual bool getShareDownloadDirectory() = 0;
virtual bool shareDownloadDirectory(bool share) = 0;

View File

@ -480,6 +480,21 @@ bool RsDirUtil::checkCreateDirectory(const std::string& dir)
}
std::string RsDirUtil::removeSymLinks(const std::string& path)
{
#if defined(WINDOWS_SYS) || defined(__APPLE__)
#warning (Mr.Alice): I don't know how to do this on windows/MacOS. See https://msdn.microsoft.com/en-us/library/windows/desktop/hh707084(v=vs.85).aspx
//if(!S_OK == PathCchCanonicalizeEx(tmp,...) ;
return path ;
#else
char *tmp = canonicalize_file_name(path.c_str()) ;
std::string result(tmp) ;
free(tmp);
return result ;
#endif
}
bool RsDirUtil::cleanupDirectory(const std::string& cleandir, const std::set<std::string> &keepFiles)
{
for(librs::util::FolderIterator it(cleandir,false);it.isValid();it.next())

View File

@ -91,6 +91,10 @@ bool checkFile(const std::string& filename,uint64_t& file_size,bool disallow
bool checkDirectory(const std::string& dir);
bool checkCreateDirectory(const std::string& dir);
// Removes all symbolic links along the path and computes the actual location of the file/dir passed as argument.
std::string removeSymLinks(const std::string& path) ;
bool cleanupDirectory(const std::string& dir, const std::set<std::string> &keepFiles);
bool cleanupDirectoryFaster(const std::string& dir, const std::set<std::string> &keepFiles);

View File

@ -68,6 +68,7 @@ bool DirectoriesPage::save(QString &/*errmsg*/)
rsFiles->setWatchPeriod(ui.autoCheckDirectoriesDelay_SB->value());
rsFiles->shareDownloadDirectory(ui.shareDownloadDirectoryCB->isChecked());
rsFiles->setFollowSymLinks(ui.followSymLinks_CB->isChecked());
return true;
}
@ -83,6 +84,7 @@ void DirectoriesPage::load()
ui.incomingDir->setText(QString::fromUtf8(rsFiles->getDownloadDirectory().c_str()));
ui.partialsDir->setText(QString::fromUtf8(rsFiles->getPartialsDirectory().c_str()));
ui.followSymLinks_CB->setChecked(rsFiles->followSymLinks());
}
void DirectoriesPage::setIncomingDirectory()

View File

@ -6,114 +6,12 @@
<rect>
<x>0</x>
<y>0</y>
<width>895</width>
<width>929</width>
<height>549</height>
</rect>
</property>
<layout class="QGridLayout">
<property name="margin">
<number>6</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="1" column="0">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Incoming Directory</string>
</property>
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLineEdit" name="incomingDir">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="incomingButton">
<property name="minimumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="toolTip">
<string>Browse</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/directoryselect_24x24_shadow.png</normaloff>:/images/directoryselect_24x24_shadow.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Partials Directory</string>
</property>
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLineEdit" name="partialsDir">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="partialButton">
<property name="minimumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="toolTip">
<string>Browse</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/directoryselect_24x24_shadow.png</normaloff>:/images/directoryselect_24x24_shadow.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Shared Directories</string>
@ -176,10 +74,119 @@
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="followSymLinks_CB">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Tells Retroshare to follow the links. Loops and duplicate directories are automatically taken care of. If unchecked, Retroshare will just ignore symbolic links to both files and directories.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>follow symbolic links</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Incoming Directory</string>
</property>
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLineEdit" name="incomingDir">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="incomingButton">
<property name="minimumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="toolTip">
<string>Browse</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/directoryselect_24x24_shadow.png</normaloff>:/images/directoryselect_24x24_shadow.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Partials Directory</string>
</property>
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLineEdit" name="partialsDir">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="partialButton">
<property name="minimumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="toolTip">
<string>Browse</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/directoryselect_24x24_shadow.png</normaloff>:/images/directoryselect_24x24_shadow.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>741</width>
<height>372</height>
<height>422</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">