Merge pull request #11785 from Tester798/fix-portable-mode

Fix portable mode
This commit is contained in:
Mike Tzou 2020-02-14 11:30:32 +08:00 committed by GitHub
commit 195889a854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 15 deletions

View file

@ -82,6 +82,7 @@
#include "base/torrentfileguard.h"
#include "base/torrentfilter.h"
#include "base/unicodestrings.h"
#include "base/utils/bytearray.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/utils/net.h"
@ -2088,6 +2089,23 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
patchedFastresumeData.replace("6:pausedi0e", "6:pausedi1e");
patchedFastresumeData.replace("12:auto_managedi0e", "12:auto_managedi1e");
// converting relative save_path to absolute
int start = patchedFastresumeData.indexOf("9:save_path");
if (start > -1) {
start += 11;
const int end = patchedFastresumeData.indexOf(':', start);
const int len = Utils::ByteArray::midView(patchedFastresumeData, start, (end - start)).toInt();
if (len > 0) {
const QByteArray relativePath = Utils::ByteArray::midView(patchedFastresumeData, (end + 1), len);
const QByteArray absolutePath = Profile::instance()->fromPortablePath(Utils::Fs::toUniformPath(QString::fromUtf8(relativePath))).toUtf8();
if (relativePath != absolutePath) {
const QByteArray replaceBefore = "9:save_path" + QByteArray::number(len) + ':' + relativePath;
const QByteArray replaceAfter = "9:save_path" + QByteArray::number(absolutePath.size()) + ':' + absolutePath;
patchedFastresumeData.replace(replaceBefore, replaceAfter);
}
}
}
p.resume_data = std::vector<char> {patchedFastresumeData.constData()
, (patchedFastresumeData.constData() + patchedFastresumeData.size())};
p.flags |= lt::add_torrent_params::flag_use_resume_save_path;
@ -2251,6 +2269,7 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
if (params.restored) { // load from existing fastresume
lt::error_code ec;
p = lt::read_resume_data(fastresumeData, ec);
p.save_path = Profile::instance()->fromPortablePath(Utils::Fs::toUniformPath(fromLTString(p.save_path))).toStdString();
}
else { // new torrent
if (!params.hasRootFolder)

View file

@ -179,7 +179,7 @@ QString Private::Converter::toPortablePath(const QString &path) const
QString Private::Converter::fromPortablePath(const QString &portablePath) const
{
if (QDir::isAbsolutePath(portablePath))
if (portablePath.isEmpty() || QDir::isAbsolutePath(portablePath))
return portablePath;
return QDir::cleanPath(m_baseDir.absoluteFilePath(portablePath));

View file

@ -33,13 +33,21 @@
Profile *Profile::m_instance = nullptr;
Profile::Profile(Private::Profile *impl, Private::PathConverter *pathConverter)
: m_profileImpl(impl)
, m_pathConverterImpl(pathConverter)
Profile::Profile(const QString &rootProfilePath, const QString &configurationName, const bool convertPathsToProfileRelative)
{
if (rootProfilePath.isEmpty())
m_profileImpl = std::make_unique<Private::DefaultProfile>(configurationName);
else
m_profileImpl = std::make_unique<Private::CustomProfile>(rootProfilePath, configurationName);
ensureDirectoryExists(SpecialFolder::Cache);
ensureDirectoryExists(SpecialFolder::Config);
ensureDirectoryExists(SpecialFolder::Data);
if (convertPathsToProfileRelative)
m_pathConverterImpl = std::make_unique<Private::Converter>(m_profileImpl->baseDirectory());
else
m_pathConverterImpl = std::make_unique<Private::NoConvertConverter>();
}
void Profile::initInstance(const QString &rootProfilePath, const QString &configurationName,
@ -47,14 +55,7 @@ void Profile::initInstance(const QString &rootProfilePath, const QString &config
{
if (m_instance)
return;
std::unique_ptr<Private::Profile> profile(rootProfilePath.isEmpty()
? static_cast<Private::Profile *>(new Private::DefaultProfile(configurationName))
: static_cast<Private::Profile *>(new Private::CustomProfile(rootProfilePath, configurationName)));
std::unique_ptr<Private::PathConverter> converter(convertPathsToProfileRelative
? static_cast<Private::PathConverter *>(new Private::Converter(profile->baseDirectory()))
: static_cast<Private::PathConverter *>(new Private::NoConvertConverter()));
m_instance = new Profile(profile.release(), converter.release());
m_instance = new Profile(rootProfilePath, configurationName, convertPathsToProfileRelative);
}
void Profile::freeInstance()

View file

@ -71,13 +71,13 @@ public:
QString fromPortablePath(const QString &portablePath) const;
private:
Profile(Private::Profile *impl, Private::PathConverter *pathConverter);
Profile(const QString &rootProfilePath, const QString &configurationName, bool convertPathsToProfileRelative);
~Profile() = default; // to generate correct call to ProfilePrivate::~ProfileImpl()
void ensureDirectoryExists(SpecialFolder folder) const;
const std::unique_ptr<Private::Profile> m_profileImpl;
const std::unique_ptr<Private::PathConverter> m_pathConverterImpl;
std::unique_ptr<Private::Profile> m_profileImpl;
std::unique_ptr<Private::PathConverter> m_pathConverterImpl;
static Profile *m_instance;
};