Thread-safe pointer operations
This commit is contained in:
@@ -96,12 +96,12 @@ public:
|
||||
virtual ~_CSiaFileTree();
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<_CSiaFile>> _fileList;
|
||||
std::shared_ptr<std::vector<std::shared_ptr<_CSiaFile>>> _fileList;
|
||||
|
||||
public:
|
||||
void BuildTree(const json& result);
|
||||
|
||||
std::vector<std::shared_ptr<_CSiaFile>> GetFileList() const;
|
||||
std::shared_ptr<std::vector<std::shared_ptr<_CSiaFile>>> GetFileList() const;
|
||||
|
||||
std::vector<std::shared_ptr<_CSiaFile>> Query(SString query) const;
|
||||
|
||||
@@ -247,6 +247,7 @@ typedef std::shared_ptr<CSiaConsensus> CSiaConsensusPtr;
|
||||
typedef CSiaApi::_CSiaFile CSiaFile;
|
||||
typedef std::shared_ptr<CSiaFile> CSiaFilePtr;
|
||||
typedef std::vector<CSiaFilePtr> CSiaFileCollection;
|
||||
typedef std::shared_ptr<CSiaFileCollection> CSiaFileCollectionPtr;
|
||||
typedef CSiaApi::_CSiaFileTree CSiaFileTree;
|
||||
typedef std::shared_ptr<CSiaFileTree> CSiaFileTreePtr;
|
||||
NS_END(2)
|
||||
|
@@ -16,46 +16,51 @@ CSiaApi::_CSiaFileTree::~_CSiaFileTree()
|
||||
|
||||
void CSiaApi::_CSiaFileTree::BuildTree(const json& result)
|
||||
{
|
||||
_fileList.clear();
|
||||
CSiaFileCollectionPtr fileList(new CSiaFileCollection());
|
||||
for (const auto& file : result["files"])
|
||||
{
|
||||
_fileList.push_back(CSiaFilePtr(new CSiaFile(GetSiaCurl(), &GetSiaDriveConfig(), file)));
|
||||
fileList->push_back(CSiaFilePtr(new CSiaFile(GetSiaCurl(), &GetSiaDriveConfig(), file)));
|
||||
}
|
||||
|
||||
_fileList = fileList;
|
||||
}
|
||||
|
||||
bool CSiaApi::_CSiaFileTree::FileExists(const SString& siaPath) const
|
||||
{
|
||||
auto result = std::find_if(_fileList.begin(), _fileList.end(), [&](const CSiaFilePtr& item)->bool
|
||||
auto fileList = GetFileList();
|
||||
auto result = std::find_if(fileList->begin(), fileList->end(), [&](const CSiaFilePtr& item)->bool
|
||||
{
|
||||
return (item->GetSiaPath() == siaPath);
|
||||
});
|
||||
|
||||
return (result != _fileList.end());
|
||||
return (result != fileList->end());
|
||||
}
|
||||
|
||||
CSiaFileCollection CSiaApi::_CSiaFileTree::GetFileList() const
|
||||
CSiaFileCollectionPtr CSiaApi::_CSiaFileTree::GetFileList() const
|
||||
{
|
||||
return _fileList;
|
||||
}
|
||||
|
||||
CSiaFilePtr CSiaApi::_CSiaFileTree::GetFile(const SString& siaPath) const
|
||||
{
|
||||
auto result = std::find_if(_fileList.begin(), _fileList.end(), [&](const CSiaFilePtr& item)->bool
|
||||
auto fileList = GetFileList();
|
||||
auto result = std::find_if(fileList->begin(), fileList->end(), [&](const CSiaFilePtr& item)->bool
|
||||
{
|
||||
return (item->GetSiaPath() == siaPath);
|
||||
});
|
||||
|
||||
return ((result != _fileList.end()) ? *result : nullptr);
|
||||
return ((result != fileList->end()) ? *result : nullptr);
|
||||
}
|
||||
|
||||
CSiaFileCollection CSiaApi::_CSiaFileTree::Query(SString query) const
|
||||
{
|
||||
auto fileList = GetFileList();
|
||||
query = CSiaApi::FormatToSiaPath(query);
|
||||
query.Replace(".", "\\.").Replace("*", "[^/]+").Replace("?", "[^/]?");
|
||||
std::wregex r(query.str());
|
||||
|
||||
CSiaFileCollection ret;
|
||||
std::copy_if(_fileList.begin(), _fileList.end(), std::back_inserter(ret), [&](const CSiaFilePtr& v) -> bool
|
||||
std::copy_if(fileList->begin(), fileList->end(), std::back_inserter(ret), [&](const CSiaFilePtr& v) -> bool
|
||||
{
|
||||
return std::regex_match(v->GetSiaPath().str(), r);
|
||||
});
|
||||
@@ -65,6 +70,7 @@ CSiaFileCollection CSiaApi::_CSiaFileTree::Query(SString query) const
|
||||
|
||||
std::vector<SString> CSiaApi::_CSiaFileTree::QueryDirectories(SString rootFolder) const
|
||||
{
|
||||
auto fileList = GetFileList();
|
||||
CSiaFileCollection col;
|
||||
rootFolder.Replace("/", "\\");
|
||||
if (rootFolder[0] == '\\')
|
||||
@@ -73,7 +79,7 @@ std::vector<SString> CSiaApi::_CSiaFileTree::QueryDirectories(SString rootFolder
|
||||
}
|
||||
|
||||
std::vector<SString> ret;
|
||||
std::for_each(_fileList.begin(), _fileList.end(), [&](const CSiaFilePtr& v)
|
||||
std::for_each(fileList->begin(), fileList->end(), [&](const CSiaFilePtr& v)
|
||||
{
|
||||
SString dir = v->GetSiaPath();
|
||||
dir.Replace("/", "\\");
|
||||
|
@@ -93,17 +93,17 @@ void CSiaApi::_CSiaRenter::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* sia
|
||||
GetFileTree(fileTree);
|
||||
|
||||
auto fileList = fileTree->GetFileList();
|
||||
if (fileList.size())
|
||||
if (fileList->size())
|
||||
{
|
||||
std::uint64_t total = std::accumulate(std::next(fileList.begin()), fileList.end(), fileList[0]->GetFileSize(), [](const std::uint64_t& sz, const CSiaFilePtr& file)
|
||||
std::uint64_t total = std::accumulate(std::next(fileList->begin()), fileList->end(), fileList->at(0)->GetFileSize(), [](const std::uint64_t& sz, const CSiaFilePtr& file)
|
||||
{
|
||||
return sz + file->GetFileSize();
|
||||
});
|
||||
|
||||
std::uint32_t totalProgress = std::accumulate(std::next(fileList.begin()), fileList.end(), fileList[0]->GetUploadProgress(), [](const std::uint32_t& progress, const CSiaFilePtr& file)
|
||||
std::uint32_t totalProgress = std::accumulate(std::next(fileList->begin()), fileList->end(), fileList->at(0)->GetUploadProgress(), [](const std::uint32_t& progress, const CSiaFilePtr& file)
|
||||
{
|
||||
return progress + min(100, file->GetUploadProgress());
|
||||
}) / static_cast<std::uint32_t>(fileList.size());
|
||||
}) / static_cast<std::uint32_t>(fileList->size());
|
||||
|
||||
SetTotalUsedBytes(total);
|
||||
SetTotalUploadProgress(totalProgress);
|
||||
|
@@ -176,13 +176,13 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
|
||||
UploadStatus uploadStatus = static_cast<UploadStatus>(query.getColumn(query.getColumnIndex("status")).getUInt());
|
||||
|
||||
auto fileList = fileTree->GetFileList();
|
||||
auto it = std::find_if(fileList.begin(), fileList.end(), [&](const CSiaFilePtr& ptr)
|
||||
auto it = std::find_if(fileList->begin(), fileList->end(), [&](const CSiaFilePtr& ptr)
|
||||
{
|
||||
return ptr->GetSiaPath() == siaPath;
|
||||
});
|
||||
|
||||
// Removed by another client
|
||||
if (it == fileList.end())
|
||||
if (it == fileList->end())
|
||||
{
|
||||
HandleFileRemove(siaCurl, siaPath);
|
||||
}
|
||||
|
Reference in New Issue
Block a user