Merge pull request #2425 from Trial97/win_char
improve invalid path name
This commit is contained in:
commit
7d418a32e7
@ -815,25 +815,78 @@ QString NormalizePath(QString path)
|
||||
}
|
||||
}
|
||||
|
||||
static const QString BAD_PATH_CHARS = "\"?<>:;*|!+\r\n";
|
||||
static const QString BAD_FILENAME_CHARS = BAD_PATH_CHARS + "\\/";
|
||||
QString removeDuplicates(QString a)
|
||||
{
|
||||
auto b = a.split("");
|
||||
b.removeDuplicates();
|
||||
return b.join("");
|
||||
}
|
||||
|
||||
static const QString BAD_WIN_CHARS = "\"?<>:*|\r\n";
|
||||
|
||||
static const QString BAD_FAT_CHARS = "<>:\"|?*+.,;=[]!";
|
||||
static const QString BAD_NTFS_CHARS = "<>:\"|?*";
|
||||
static const QString BAD_HFS_CHARS = ":";
|
||||
|
||||
static const QString BAD_FILENAME_CHARS = removeDuplicates(BAD_WIN_CHARS + BAD_FAT_CHARS + BAD_NTFS_CHARS + BAD_HFS_CHARS) + "\\/";
|
||||
|
||||
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
|
||||
{
|
||||
for (int i = 0; i < string.length(); i++)
|
||||
if (string.at(i) < ' ' || BAD_FILENAME_CHARS.contains(string.at(i)))
|
||||
string[i] = replaceWith;
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
QString RemoveInvalidPathChars(QString string, QChar replaceWith)
|
||||
QString RemoveInvalidPathChars(QString path, QChar replaceWith)
|
||||
{
|
||||
for (int i = 0; i < string.length(); i++)
|
||||
if (string.at(i) < ' ' || BAD_PATH_CHARS.contains(string.at(i)))
|
||||
string[i] = replaceWith;
|
||||
QString invalidChars;
|
||||
#ifdef Q_OS_WIN
|
||||
invalidChars = BAD_WIN_CHARS;
|
||||
#endif
|
||||
|
||||
return string;
|
||||
// the null character is ignored in this check as it was not a problem until now
|
||||
switch (statFS(path).fsType) {
|
||||
case FilesystemType::FAT:
|
||||
invalidChars += BAD_FAT_CHARS;
|
||||
break;
|
||||
case FilesystemType::NTFS:
|
||||
/* fallthrough */
|
||||
case FilesystemType::REFS: // similar to NTFS(should be available only on windows)
|
||||
invalidChars += BAD_NTFS_CHARS;
|
||||
break;
|
||||
// case FilesystemType::EXT:
|
||||
// case FilesystemType::EXT_2_OLD:
|
||||
// case FilesystemType::EXT_2_3_4:
|
||||
// case FilesystemType::XFS:
|
||||
// case FilesystemType::BTRFS:
|
||||
// case FilesystemType::NFS:
|
||||
// case FilesystemType::ZFS:
|
||||
case FilesystemType::APFS:
|
||||
/* fallthrough */
|
||||
case FilesystemType::HFS:
|
||||
/* fallthrough */
|
||||
case FilesystemType::HFSPLUS:
|
||||
/* fallthrough */
|
||||
case FilesystemType::HFSX:
|
||||
invalidChars += BAD_HFS_CHARS;
|
||||
break;
|
||||
// case FilesystemType::FUSEBLK:
|
||||
// case FilesystemType::F2FS:
|
||||
// case FilesystemType::UNKNOWN:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (invalidChars.size() != 0) {
|
||||
for (int i = 0; i < path.length(); i++) {
|
||||
if (path.at(i) < ' ' || invalidChars.contains(path.at(i))) {
|
||||
path[i] = replaceWith;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
QString DirNameFromString(QString string, QString inDir)
|
||||
|
@ -289,9 +289,7 @@ std::optional<QStringList> extractSubDir(QuaZip* zip, const QString& subdir, con
|
||||
|
||||
do {
|
||||
QString file_name = zip->getCurrentFileName();
|
||||
#ifdef Q_OS_WIN
|
||||
file_name = FS::RemoveInvalidPathChars(file_name);
|
||||
#endif
|
||||
if (!file_name.startsWith(subdir))
|
||||
continue;
|
||||
|
||||
|
@ -51,6 +51,7 @@ void Library::getApplicableFiles(const RuntimeContext& runtimeContext,
|
||||
{
|
||||
bool local = isLocal();
|
||||
auto actualPath = [&](QString relPath) {
|
||||
relPath = FS::RemoveInvalidPathChars(relPath);
|
||||
QFileInfo out(FS::PathCombine(storagePrefix(), relPath));
|
||||
if (local && !overridePath.isEmpty()) {
|
||||
QString fileName = out.fileName();
|
||||
|
@ -538,9 +538,7 @@ void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
|
||||
}
|
||||
for (const auto& result : results) {
|
||||
auto fileName = result.fileName;
|
||||
#ifdef Q_OS_WIN
|
||||
fileName = FS::RemoveInvalidPathChars(fileName);
|
||||
#endif
|
||||
auto relpath = FS::PathCombine(result.targetFolder, fileName);
|
||||
|
||||
if (!result.required && !selectedOptionalMods.contains(relpath)) {
|
||||
|
@ -139,9 +139,7 @@ auto FlameMod::loadIndexedPackVersion(QJsonObject& obj, bool load_changelog) ->
|
||||
file.version = Json::requireString(obj, "displayName");
|
||||
file.downloadUrl = Json::ensureString(obj, "downloadUrl");
|
||||
file.fileName = Json::requireString(obj, "fileName");
|
||||
#ifdef Q_OS_WIN
|
||||
file.fileName = FS::RemoveInvalidPathChars(file.fileName);
|
||||
#endif
|
||||
|
||||
ModPlatform::IndexedVersionType::VersionType ver_type;
|
||||
switch (Json::requireInteger(obj, "releaseType")) {
|
||||
|
@ -241,9 +241,7 @@ bool ModrinthCreationTask::createInstance()
|
||||
|
||||
for (auto file : m_files) {
|
||||
auto fileName = file.path;
|
||||
#ifdef Q_OS_WIN
|
||||
fileName = FS::RemoveInvalidPathChars(fileName);
|
||||
#endif
|
||||
auto file_path = FS::PathCombine(root_modpack_path, fileName);
|
||||
if (!root_modpack_url.isParentOf(QUrl::fromLocalFile(file_path))) {
|
||||
// This means we somehow got out of the root folder, so abort here to prevent exploits
|
||||
|
@ -227,9 +227,7 @@ auto Modrinth::loadIndexedPackVersion(QJsonObject& obj, QString preferred_hash_t
|
||||
if (parent.contains("url")) {
|
||||
file.downloadUrl = Json::requireString(parent, "url");
|
||||
file.fileName = Json::requireString(parent, "filename");
|
||||
#ifdef Q_OS_WIN
|
||||
file.fileName = FS::RemoveInvalidPathChars(file.fileName);
|
||||
#endif
|
||||
file.is_preferred = Json::requireBoolean(parent, "primary") || (files.count() == 1);
|
||||
auto hash_list = Json::requireObject(parent, "hashes");
|
||||
|
||||
|
@ -84,9 +84,7 @@ auto HttpMetaCache::getEntry(QString base, QString resource_path) -> MetaEntryPt
|
||||
|
||||
auto HttpMetaCache::resolveEntry(QString base, QString resource_path, QString expected_etag) -> MetaEntryPtr
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
resource_path = FS::RemoveInvalidPathChars(resource_path);
|
||||
#endif
|
||||
auto entry = getEntry(base, resource_path);
|
||||
// it's not present? generate a default stale entry
|
||||
if (!entry) {
|
||||
|
Loading…
Reference in New Issue
Block a user