diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2020-02-14 08:01:27 -0500 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2020-02-18 17:25:04 -0500 |
| commit | 4a74102736a20a6fa5bf9df9cc7716cbecb5b777 (patch) | |
| tree | 5ecde6b6a1c2bea7e07058e730f5b3191f7d7262 /src | |
| parent | 75359bc9c2bd9f593c59fb156186b3491d0b7c47 (diff) | |
moved dump() to DirectoryEntry
static functions instead of lambdas
Diffstat (limited to 'src')
| -rw-r--r-- | src/filetree.cpp | 25 | ||||
| -rw-r--r-- | src/shared/directoryentry.cpp | 131 | ||||
| -rw-r--r-- | src/shared/directoryentry.h | 15 |
3 files changed, 111 insertions, 60 deletions
diff --git a/src/filetree.cpp b/src/filetree.cpp index e5ea4e9d..45c21c7f 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -438,30 +438,7 @@ void FileTree::dumpToFile() const return; } - QFile out(file); - - if (!out.open(QIODevice::WriteOnly)) { - QMessageBox::critical( - m_tree->window(), tr("Error"), tr("Failed to open file '%1': %2") - .arg(file) - .arg(out.errorString())); - - return; - } - - try - { - dumpToFile(out, "Data", *m_core.directoryStructure()); - } - catch(DumpFailed&) - { - // try to remove it silently - if (out.exists()) { - if (!out.remove()) { - log::error("failed to remove '{}', ignoring", file); - } - } - } + m_core.directoryStructure()->dump(file.toStdWString()); } void FileTree::dumpToFile( diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 2605d083..394eda7e 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -1380,61 +1380,80 @@ FileEntry::Ptr DirectoryEntry::insert( return fe;
}
+struct DirectoryEntry::Context
+{
+ FilesOrigin& origin;
+ DirectoryStats& stats;
+ std::stack<DirectoryEntry*> current;
+};
+
void DirectoryEntry::addFiles(
env::DirectoryWalker& walker, FilesOrigin &origin,
const std::wstring& path, DirectoryStats& stats)
{
- struct Context
- {
- FilesOrigin& origin;
- DirectoryStats& stats;
- std::stack<DirectoryEntry*> current;
- };
-
Context cx = {origin, stats};
cx.current.push(this);
walker.forEachEntry(path, &cx,
[](void* pcx, std::wstring_view path)
{
- Context* cx = (Context*)pcx;
- elapsed(cx->stats.dirTimes, [&] {
- auto* sd = cx->current.top()->getSubDirectory(
- path, true, cx->stats, cx->origin.getID());
-
- cx->current.push(sd);
- });
+ onDirectoryStart((Context*)pcx, path);
},
[](void* pcx, std::wstring_view path)
{
- Context* cx = (Context*)pcx;
+ onDirectoryEnd((Context*)pcx, path);
+ },
- elapsed(cx->stats.dirTimes, [&] {
- auto* current= cx->current.top();
+ [](void* pcx, std::wstring_view path, FILETIME ft)
+ {
+ onFile((Context*)pcx, path, ft);
+ }
+ );
- {
- std::scoped_lock lock(current->m_SubDirMutex);
+ {
+ std::scoped_lock lock(m_SubDirMutex);
- std::sort(
- current->m_SubDirectories.begin(),
- current->m_SubDirectories.end(),
- &DirCompareByName);
- }
+ std::sort(
+ m_SubDirectories.begin(),
+ m_SubDirectories.end(),
+ &DirCompareByName);
+ }
+}
- cx->current.pop();
- });
- },
+void DirectoryEntry::onDirectoryStart(Context* cx, std::wstring_view path)
+{
+ elapsed(cx->stats.dirTimes, [&] {
+ auto* sd = cx->current.top()->getSubDirectory(
+ path, true, cx->stats, cx->origin.getID());
+
+ cx->current.push(sd);
+ });
+}
+
+void DirectoryEntry::onDirectoryEnd(Context* cx, std::wstring_view path)
+{
+ elapsed(cx->stats.dirTimes, [&] {
+ auto* current = cx->current.top();
- [](void* pcx, std::wstring_view path, FILETIME ft)
{
- Context* cx = (Context*)pcx;
+ std::scoped_lock lock(current->m_SubDirMutex);
- elapsed(cx->stats.fileTimes, [&]{
- cx->current.top()->insert(path, cx->origin, ft, L"", -1, cx->stats);
- });
+ std::sort(
+ current->m_SubDirectories.begin(),
+ current->m_SubDirectories.end(),
+ &DirCompareByName);
}
- );
+
+ cx->current.pop();
+ });
+}
+
+void DirectoryEntry::onFile(Context* cx, std::wstring_view path, FILETIME ft)
+{
+ elapsed(cx->stats.fileTimes, [&]{
+ cx->current.top()->insert(path, cx->origin, ft, L"", -1, cx->stats);
+ });
}
void DirectoryEntry::addFiles(
@@ -1510,6 +1529,8 @@ DirectoryEntry *DirectoryEntry::getSubDirectory( {
SubDirectoriesLookup::iterator itor;
+ std::scoped_lock lock(m_SubDirMutex);
+
elapsed(stats.subdirLookupTimes, [&] {
itor = m_SubDirectoriesLookup.find(dir.lcname);
});
@@ -1664,4 +1685,48 @@ void DirectoryEntry::addFileToList( // fileNameLower has been moved from this point
}
+void DirectoryEntry::dump(const std::wstring& file) const
+{
+ std::FILE* f = nullptr;
+ auto e = _wfopen_s(&f, file.c_str(), L"wb");
+
+ dump(f, L"Data");
+
+ std::fclose(f);
+}
+
+void DirectoryEntry::dump(std::FILE* f, const std::wstring& parentPath) const
+{
+ {
+ std::scoped_lock lock(m_FilesMutex);
+
+ for (auto&& index : m_Files) {
+ const auto file = m_FileRegister->getFile(index.second);
+ if (!file) {
+ continue;
+ }
+
+ if (file->isFromArchive()) {
+ // TODO: don't list files from archives. maybe make this an option?
+ continue;
+ }
+
+ const auto o = m_OriginConnection->getByID(file->getOrigin());
+ const auto path = parentPath + L"\\" + file->getName();
+ const auto line = path + L"\t(" + o.getName() + L")\r\n";
+
+ const auto lineu8 = MOShared::ToString(line, true);
+ std::fwrite(lineu8.data(), lineu8.size(), 1, f);
+ }
+ }
+
+ {
+ std::scoped_lock lock(m_SubDirMutex);
+ for (auto&& d : m_SubDirectories) {
+ const auto path = parentPath + L"\\" + d->m_Name;
+ d->dump(f, path);
+ }
+ }
+}
+
} // namespace MOShared
diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index f406ced6..ce50da44 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -537,6 +537,8 @@ public: void removeFiles(const std::set<FileEntry::Index> &indices);
+ void dump(const std::wstring& file) const;
+
private:
using FilesMap = std::map<std::wstring, FileEntry::Index>;
using FilesLookup = std::unordered_map<FileKey, FileEntry::Index>;
@@ -556,9 +558,9 @@ private: std::set<int> m_Origins;
bool m_Populated;
bool m_TopLevel;
- std::mutex m_SubDirMutex;
- std::mutex m_FilesMutex;
- std::mutex m_OriginsMutex;
+ mutable std::mutex m_SubDirMutex;
+ mutable std::mutex m_FilesMutex;
+ mutable std::mutex m_OriginsMutex;
DirectoryEntry(const DirectoryEntry &reference);
@@ -600,6 +602,13 @@ private: void addFileToList(std::wstring fileNameLower, FileEntry::Index index);
void removeFileFromList(FileEntry::Index index);
void removeFilesFromList(const std::set<FileEntry::Index>& indices);
+
+ struct Context;
+ static void onDirectoryStart(Context* cx, std::wstring_view path);
+ static void onDirectoryEnd(Context* cx, std::wstring_view path);
+ static void onFile(Context* cx, std::wstring_view path, FILETIME ft);
+
+ void dump(std::FILE* f, const std::wstring& parentPath) const;
};
} // namespace MOShared
|
