Browse Source

Add support for quarantine on macOS

adaptive-webui-19844
Chocobo1 1 year ago
parent
commit
98576dacae
  1. 4
      src/base/CMakeLists.txt
  2. 8
      src/base/bittorrent/torrentimpl.cpp
  3. 8
      src/base/net/downloadhandlerimpl.cpp
  4. 56
      src/base/utils/misc.cpp
  5. 5
      src/base/utils/misc.h

4
src/base/CMakeLists.txt

@ -205,9 +205,9 @@ target_link_libraries(qbt_base
) )
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
find_library(IOKit_LIBRARY IOKit)
find_library(Carbon_LIBRARY Carbon)
find_library(AppKit_LIBRARY AppKit) find_library(AppKit_LIBRARY AppKit)
find_library(Carbon_LIBRARY Carbon)
find_library(IOKit_LIBRARY IOKit)
target_link_libraries(qbt_base PRIVATE target_link_libraries(qbt_base PRIVATE
${AppKit_LIBRARY} ${AppKit_LIBRARY}

8
src/base/bittorrent/torrentimpl.cpp

@ -68,9 +68,9 @@
#include "peerinfo.h" #include "peerinfo.h"
#include "sessionimpl.h" #include "sessionimpl.h"
#ifdef Q_OS_WIN #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
#include "base/utils/misc.h" #include "base/utils/misc.h"
#endif #endif // Q_OS_MACOS || Q_OS_WIN
using namespace BitTorrent; using namespace BitTorrent;
@ -2200,14 +2200,14 @@ void TorrentImpl::handleFileCompletedAlert(const lt::file_completed_alert *p)
const Path actualPath = actualFilePath(fileIndex); const Path actualPath = actualFilePath(fileIndex);
#ifdef Q_OS_WIN #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
// only apply Mark-of-the-Web to new download files // only apply Mark-of-the-Web to new download files
if (isDownloading()) if (isDownloading())
{ {
const Path fullpath = actualStorageLocation() / actualPath; const Path fullpath = actualStorageLocation() / actualPath;
Utils::Misc::applyMarkOfTheWeb(fullpath); Utils::Misc::applyMarkOfTheWeb(fullpath);
} }
#endif #endif // Q_OS_MACOS || Q_OS_WIN
if (m_session->isAppendExtensionEnabled()) if (m_session->isAppendExtensionEnabled())
{ {

8
src/base/net/downloadhandlerimpl.cpp

@ -150,9 +150,9 @@ void Net::DownloadHandlerImpl::processFinishedDownload()
{ {
m_result.filePath = result.value(); m_result.filePath = result.value();
#ifdef Q_OS_WIN #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
Utils::Misc::applyMarkOfTheWeb(m_result.filePath, m_result.url); Utils::Misc::applyMarkOfTheWeb(m_result.filePath, m_result.url);
#endif #endif // Q_OS_MACOS || Q_OS_WIN
} }
else else
{ {
@ -166,9 +166,9 @@ void Net::DownloadHandlerImpl::processFinishedDownload()
{ {
m_result.filePath = destinationPath; m_result.filePath = destinationPath;
#ifdef Q_OS_WIN #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
Utils::Misc::applyMarkOfTheWeb(m_result.filePath, m_result.url); Utils::Misc::applyMarkOfTheWeb(m_result.filePath, m_result.url);
#endif #endif // Q_OS_MACOS || Q_OS_WIN
} }
else else
{ {

56
src/base/utils/misc.cpp

@ -43,6 +43,7 @@
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
#endif #endif
@ -52,6 +53,7 @@
#include <openssl/opensslv.h> #include <openssl/opensslv.h>
#include <zlib.h> #include <zlib.h>
#include <QtAssert>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QLocale> #include <QLocale>
@ -620,10 +622,50 @@ QString Utils::Misc::zlibVersionString()
} }
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
Path Utils::Misc::windowsSystemPath()
{
static const Path path = []() -> Path
{
WCHAR systemPath[MAX_PATH] = {0};
GetSystemDirectoryW(systemPath, sizeof(systemPath) / sizeof(WCHAR));
return Path(QString::fromWCharArray(systemPath));
}();
return path;
}
#endif // Q_OS_WIN
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
bool Utils::Misc::applyMarkOfTheWeb(const Path &file, const QString &url) bool Utils::Misc::applyMarkOfTheWeb(const Path &file, const QString &url)
{ {
Q_ASSERT(url.isEmpty() || url.startsWith(u"http:") || url.startsWith(u"https:")); Q_ASSERT(url.isEmpty() || url.startsWith(u"http:") || url.startsWith(u"https:"));
#ifdef Q_OS_MACOS
// References:
// https://searchfox.org/mozilla-central/rev/ffdc4971dc18e1141cb2a90c2b0b776365650270/xpcom/io/CocoaFileUtils.mm#230
// https://github.com/transmission/transmission/blob/f62f7427edb1fd5c430e0ef6956bbaa4f03ae597/macosx/Torrent.mm#L1945-L1955
CFMutableDictionaryRef properties = ::CFDictionaryCreateMutable(kCFAllocatorDefault, 0
, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (properties == NULL)
return false;
::CFDictionarySetValue(properties, kLSQuarantineTypeKey, kLSQuarantineTypeOtherDownload);
if (!url.isEmpty())
::CFDictionarySetValue(properties, kLSQuarantineDataURLKey, url.toCFString());
const CFStringRef fileString = file.toString().toCFString();
const CFURLRef fileURL = ::CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, fileString, kCFURLPOSIXPathStyle, false);
const Boolean success = ::CFURLSetResourcePropertyForKey(fileURL, kCFURLQuarantinePropertiesKey
, properties, NULL);
::CFRelease(fileURL);
::CFRelease(fileString);
::CFRelease(properties);
return success;
#elif defined(Q_OS_WIN)
const QString zoneIDStream = file.toString() + u":Zone.Identifier"; const QString zoneIDStream = file.toString() + u":Zone.Identifier";
HANDLE handle = ::CreateFileW(zoneIDStream.toStdWString().c_str(), GENERIC_WRITE HANDLE handle = ::CreateFileW(zoneIDStream.toStdWString().c_str(), GENERIC_WRITE
, (FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE) , (FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE)
@ -642,16 +684,6 @@ bool Utils::Misc::applyMarkOfTheWeb(const Path &file, const QString &url)
::CloseHandle(handle); ::CloseHandle(handle);
return writeResult && (written == zoneID.size()); return writeResult && (written == zoneID.size());
#endif
} }
#endif // Q_OS_MACOS || Q_OS_WIN
Path Utils::Misc::windowsSystemPath()
{
static const Path path = []() -> Path
{
WCHAR systemPath[MAX_PATH] = {0};
GetSystemDirectoryW(systemPath, sizeof(systemPath) / sizeof(WCHAR));
return Path(QString::fromWCharArray(systemPath));
}();
return path;
}
#endif // Q_OS_WIN

5
src/base/utils/misc.h

@ -95,7 +95,6 @@ namespace Utils::Misc
QString languageToLocalizedString(const QString &localeStr); QString languageToLocalizedString(const QString &localeStr);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
bool applyMarkOfTheWeb(const Path &file, const QString &url = {});
Path windowsSystemPath(); Path windowsSystemPath();
template <typename T> template <typename T>
@ -105,4 +104,8 @@ namespace Utils::Misc
return reinterpret_cast<T>(::GetProcAddress(::LoadLibraryW(path.c_str()), funcName)); return reinterpret_cast<T>(::GetProcAddress(::LoadLibraryW(path.c_str()), funcName));
} }
#endif // Q_OS_WIN #endif // Q_OS_WIN
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
bool applyMarkOfTheWeb(const Path &file, const QString &url = {});
#endif // Q_OS_MACOS || Q_OS_WIN
} }

Loading…
Cancel
Save