Skip to content

Commit d73b18b

Browse files
committed
fix(file_sharing): handle NULL from canonicalize_file_name and separate cycle detection from duplicate filtering
1 parent f25f08a commit d73b18b

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

src/file_sharing/directory_updater.cc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#include "util/cxx17retrocompat.h"
2626
#include "util/folderiterator.h"
27-
#include <algorithm>
2827
#include "util/rstime.h"
2928
#include "rsserver/p3face.h"
3029
#include "directory_storage.h"
@@ -221,9 +220,11 @@ bool LocalDirectoryUpdater::sweepSharedDirectories(bool& some_files_not_ready)
221220
RS_DBG4("recursing into \"", stored_dir_it.name());
222221

223222
std::string canonical = RsDirUtil::removeSymLinks(stored_dir_it.name());
224-
existing_dirs.insert(canonical);
225-
std::vector<std::string> current_branch_real_paths;
226-
current_branch_real_paths.push_back(canonical);
223+
if(!canonical.empty())
224+
existing_dirs.insert(canonical);
225+
std::set<std::string> current_branch_real_paths;
226+
if(!canonical.empty())
227+
current_branch_real_paths.insert(canonical);
227228
recursUpdateSharedDir(
228229
stored_dir_it.name(), *stored_dir_it,
229230
existing_dirs, current_branch_real_paths, 1, some_files_not_ready );
@@ -244,7 +245,7 @@ bool LocalDirectoryUpdater::sweepSharedDirectories(bool& some_files_not_ready)
244245

245246
void LocalDirectoryUpdater::recursUpdateSharedDir(
246247
const std::string& cumulated_path, DirectoryStorage::EntryIndex indx,
247-
std::set<std::string>& existing_directories, std::vector<std::string>& current_branch_real_paths, uint32_t current_depth,
248+
std::set<std::string>& existing_directories, std::set<std::string>& current_branch_real_paths, uint32_t current_depth,
248249
bool& some_files_not_ready )
249250
{
250251
RS_DBG4("parsing directory \"", cumulated_path, "\" index: ", indx);
@@ -309,7 +310,13 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(
309310
std::string real_path = RsDirUtil::removeSymLinks(
310311
RsDirUtil::makePath(cumulated_path, dirIt.file_name()) );
311312

312-
if (std::find(current_branch_real_paths.begin(), current_branch_real_paths.end(), real_path) != current_branch_real_paths.end())
313+
if(real_path.empty())
314+
{
315+
RS_WARN( "Broken/circular symlink: \"", cumulated_path,
316+
"/", dirIt.file_name(), "\". Ignoring." );
317+
dir_is_accepted = false;
318+
}
319+
else if(current_branch_real_paths.end() != current_branch_real_paths.find(real_path))
313320
{
314321
RS_WARN( "Circular symlink detected: \"", cumulated_path,
315322
"\" points to ancestor \"", real_path,
@@ -381,13 +388,15 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(
381388
{
382389
std::string next_path = RsDirUtil::makePath(cumulated_path, stored_dir_it.name());
383390
std::string canonical = RsDirUtil::removeSymLinks(next_path);
384-
current_branch_real_paths.push_back(canonical);
391+
if(!canonical.empty())
392+
current_branch_real_paths.insert(canonical);
385393

386394
recursUpdateSharedDir( next_path,
387395
*stored_dir_it, existing_directories, current_branch_real_paths,
388396
current_depth+1, some_files_not_ready );
389397

390-
current_branch_real_paths.pop_back();
398+
if(!canonical.empty())
399+
current_branch_real_paths.erase(canonical);
391400
}
392401
}
393402

src/file_sharing/directory_updater.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "file_sharing/hash_cache.h"
2929
#include "file_sharing/directory_storage.h"
3030
#include "util/rstime.h"
31-
#include <vector>
3231

3332
class LocalDirectoryUpdater: public HashStorageClient, public RsTickingThread
3433
{
@@ -68,7 +67,7 @@ class LocalDirectoryUpdater: public HashStorageClient, public RsTickingThread
6867
virtual void hash_callback(uint32_t client_param, const std::string& name, const RsFileHash& hash, uint64_t size);
6968
virtual bool hash_confirm(uint32_t client_param) ;
7069

71-
void recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, std::set<std::string>& existing_directories, std::vector<std::string>& current_branch_real_paths, uint32_t current_depth,bool& files_not_ready);
70+
void recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, std::set<std::string>& existing_directories, std::set<std::string>& current_branch_real_paths, uint32_t current_depth,bool& files_not_ready);
7271
bool sweepSharedDirectories(bool &some_files_not_ready);
7372

7473
private:

src/util/rsdir.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,15 @@ std::string RsDirUtil::removeSymLinks(const std::string& path)
622622
return path ;
623623
#else
624624
char *tmp = canonicalize_file_name(path.c_str()) ;
625-
std::string result(tmp) ;
626625

626+
if(tmp == nullptr)
627+
{
628+
RS_WARN("removeSymLinks: cannot resolve \"", path,
629+
"\". Broken or circular symlink?");
630+
return std::string();
631+
}
632+
633+
std::string result(tmp) ;
627634
free(tmp);
628635
return result ;
629636
#endif

0 commit comments

Comments
 (0)