BUGFIX: File Codec on Windows#507
Merged
Merged
Conversation
zymelaii
commented
Aug 29, 2023
- fix: file/dir name codec error (fix crash with some filenames (windows) #354)
- fix: deprecated usage
current method to resolve path is not corret due to encoding issues, simply using QDir/QFileInfo to replace std::filesystem and std::string.
1. reserve parent dir entry 2. exclude current dir entry 3. exclude hidden entry on non-windows os
Owner
|
Hi master branch sorting: name your PR sorting: name I'll poke it around more once I get home |
Author
|
This is indeed a problem, let me see if I can find a better solution. Thanks for your profiling, my dear easymodo. |
Author
I run a benchmark on the two cases as below, with 10000 empty files named after random uuids under #include <benchmark/benchmark.h>
#include <QRegularExpressionMatch>
#include <QDateTime>
#include <QDir>
#include <QCollator>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
struct FSEntryOld {
QString path, name;
std::uintmax_t size;
fs::file_time_type modifyTime;
bool isDirectory;
};
struct FSEntryNew {
QString path, name;
std::uintmax_t size;
QDateTime modifyTime;
bool isDirectory;
};
void loadEntryList__old(
std::vector<FSEntryOld>& fileEntryVec,
std::vector<FSEntryOld>& dirEntryVec,
QString directoryPath) {
QRegularExpressionMatch match;
for (const auto& entry :
fs::directory_iterator(directoryPath.toStdWString())) {
QString name =
QString::fromStdString(entry.path().filename().generic_string());
QString path = QString::fromStdString(entry.path().generic_string());
if (entry.is_directory()) { // this can still throw std::bad_alloc ..
FSEntryOld newEntry;
try {
newEntry.name = name;
newEntry.path = path;
newEntry.isDirectory = true;
// newEntry.size = entry.file_size();
// newEntry.modifyTime = entry.last_write_time();
} catch (const std::filesystem::filesystem_error& err) {
qDebug() << "[DirectoryManager]" << err.what();
continue;
}
dirEntryVec.emplace_back(newEntry);
} else if (match.hasMatch()) {
FSEntryOld newEntry;
try {
newEntry.name = name;
newEntry.path = path;
newEntry.isDirectory = false;
newEntry.size = entry.file_size();
newEntry.modifyTime = entry.last_write_time();
} catch (const std::filesystem::filesystem_error& err) {
qDebug() << "[DirectoryManager]" << err.what();
continue;
}
fileEntryVec.emplace_back(newEntry);
}
}
}
void loadEntryList__new(
std::vector<FSEntryNew>& fileEntryVec,
std::vector<FSEntryNew>& dirEntryVec,
QString directoryPath) {
QDir root(directoryPath);
root.setFilter(QDir::Dirs | QDir::Files | QDir::NoDot);
QRegularExpressionMatch match{};
for (const auto& entry : root.entryInfoList()) {
if (!entry.isDir() && !match.hasMatch()) { continue; }
FSEntryNew newEntry{};
newEntry.name = entry.fileName();
newEntry.path = entry.absoluteFilePath();
newEntry.isDirectory = entry.isDir();
if (newEntry.isDirectory) {
dirEntryVec.emplace_back(newEntry);
} else {
newEntry.size = entry.size();
newEntry.modifyTime = entry.lastModified();
fileEntryVec.emplace_back(newEntry);
}
}
}
static void OLD_addEntriesFromDirectory(benchmark::State& state) {
for (auto _ : state) {
std::vector<FSEntryOld> fileEntryVec;
std::vector<FSEntryOld> dirEntryVec;
loadEntryList__old(
fileEntryVec, dirEntryVec, QStringLiteral("D:\\foobar.dir"));
}
}
static void NEW_addEntriesFromDirectory(benchmark::State& state) {
for (auto _ : state) {
std::vector<FSEntryNew> fileEntryVec;
std::vector<FSEntryNew> dirEntryVec;
loadEntryList__new(
fileEntryVec, dirEntryVec, QStringLiteral("D:\\foobar.dir"));
}
}
static void OLD_sortByName(benchmark::State& state) {
QCollator collator;
for (auto _ : state) {
std::vector<FSEntryOld> fileEntryVec;
std::vector<FSEntryOld> dirEntryVec;
loadEntryList__old(
fileEntryVec, dirEntryVec, QStringLiteral("D:\\foobar.dir"));
std::sort(
fileEntryVec.begin(),
fileEntryVec.end(),
[&collator](const auto& lhs, const auto& rhs) {
return collator.compare(lhs.path, lhs.path) < 0;
});
}
}
static void NEW_sortByName(benchmark::State& state) {
QCollator collator;
for (auto _ : state) {
std::vector<FSEntryNew> fileEntryVec;
std::vector<FSEntryNew> dirEntryVec;
loadEntryList__new(
fileEntryVec, dirEntryVec, QStringLiteral("D:\\foobar.dir"));
std::sort(
fileEntryVec.begin(),
fileEntryVec.end(),
[&collator](const auto& lhs, const auto& rhs) {
return collator.compare(lhs.path, lhs.path) < 0;
});
}
}
static void OLD_sortByDate(benchmark::State& state) {
QCollator collator;
for (auto _ : state) {
std::vector<FSEntryOld> fileEntryVec;
std::vector<FSEntryOld> dirEntryVec;
loadEntryList__old(
fileEntryVec, dirEntryVec, QStringLiteral("D:\\foobar.dir"));
std::sort(
fileEntryVec.begin(),
fileEntryVec.end(),
[&collator](const auto& lhs, const auto& rhs) {
return lhs.modifyTime < rhs.modifyTime;
});
}
}
static void NEW_sortByDate(benchmark::State& state) {
QCollator collator;
for (auto _ : state) {
std::vector<FSEntryNew> fileEntryVec;
std::vector<FSEntryNew> dirEntryVec;
loadEntryList__new(
fileEntryVec, dirEntryVec, QStringLiteral("D:\\foobar.dir"));
std::sort(
fileEntryVec.begin(),
fileEntryVec.end(),
[&collator](const auto& lhs, const auto& rhs) {
return lhs.modifyTime < rhs.modifyTime;
});
}
}
BENCHMARK(OLD_addEntriesFromDirectory);
BENCHMARK(NEW_addEntriesFromDirectory);
BENCHMARK(OLD_sortByName);
BENCHMARK(NEW_sortByName);
BENCHMARK(OLD_sortByDate);
BENCHMARK(NEW_sortByDate);
BENCHMARK_MAIN(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.