refactor: more tr_torrent housekeeping (#2320)

This commit is contained in:
Charles Kerr 2021-12-16 03:43:51 -06:00 committed by GitHub
parent 4ea2429217
commit b93f3f0463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 49 additions and 97 deletions

View File

@ -14,7 +14,8 @@
#error only libtransmission should #include this header.
#endif
#include <inttypes.h>
#include <cstddef>
#include <cinttypes> // uintX_t
#include "crypto-utils.h"
#include "tr-macros.h"

View File

@ -8,7 +8,8 @@
#pragma once
#include <cinttypes>
#include <cstddef> // size_t
#include <cinttypes> // uintX_t
#include <ctime>
#include <string_view>

View File

@ -16,6 +16,8 @@
***
**/
#include <cstddef> // size_t
#include <cstdint> // uintX_t
#include <ctime>
#include <event2/buffer.h>

View File

@ -785,7 +785,7 @@ static void peerCallbackFunc(tr_peer* peer, tr_peer_event const* e, void* vs)
tor->uploadedCur += e->length;
tr_announcerAddBytes(tor, TR_ANN_UP, e->length);
tr_torrentSetDateActive(tor, now);
tor->setDateActive(now);
tor->setDirty();
tr_statsAddUploaded(tor->session, e->length);
@ -803,7 +803,7 @@ static void peerCallbackFunc(tr_peer* peer, tr_peer_event const* e, void* vs)
tr_torrent* tor = s->tor;
tor->downloadedCur += e->length;
tr_torrentSetDateActive(tor, now);
tor->setDateActive(now);
tor->setDirty();
tr_statsAddDownloaded(tor->session, e->length);

View File

@ -13,6 +13,7 @@
#endif
#include <cinttypes>
#include <cstddef>
#include <ctime>
#include "peer-common.h"

View File

@ -843,7 +843,7 @@ static uint64_t loadFromFile(tr_torrent* tor, uint64_t fieldsToLoad, bool* didRe
if ((fieldsToLoad & TR_FR_ACTIVITY_DATE) != 0 && tr_variantDictFindInt(&top, TR_KEY_activity_date, &i))
{
tr_torrentSetDateActive(tor, i);
tor->setDateActive(i);
fieldsLoaded |= TR_FR_ACTIVITY_DATE;
}

View File

@ -706,7 +706,7 @@ static void initField(
break;
case TR_KEY_primary_mime_type:
tr_variantInitStrView(initme, tr_torrentPrimaryMimeType(tor));
tr_variantInitStrView(initme, tor->primaryMimeType());
break;
case TR_KEY_priorities:

View File

@ -15,12 +15,14 @@
#define TR_NAME "Transmission"
#include <array>
#include <cstddef> // size_t
#include <cstdint> // uintX_t
#include <cstring> // memcmp()
#include <ctime>
#include <list>
#include <mutex>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include <unordered_set>

View File

@ -12,9 +12,12 @@
#error only libtransmission should #include this header.
#endif
#include <cinttypes>
#include <cinttypes> // intX_t
#include <cstddef> // size_t
#include <ctime>
struct tr_torrent;
// defined by BEP #9
inline constexpr int METADATA_PIECE_SIZE = 1024 * 16;

View File

@ -707,7 +707,9 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor)
TR_ASSERT(tor->downloadedCur == 0);
TR_ASSERT(tor->uploadedCur == 0);
tr_torrentSetDateAdded(tor, tr_time()); /* this is a default value to be overwritten by the resume file */
auto const now = tr_time();
tor->addedDate = now; // this is a default that will be overwritten by the resume file
tor->anyDate = now;
// tr_torrentLoadResume() calls a lot of tr_torrentSetFoo() methods
// that set things as dirty, but... these settings being loaded are
@ -937,13 +939,12 @@ tr_stat const* tr_torrentStatCached(tr_torrent* tor)
return (tr_isTorrent(tor) && now == tor->lastStatTime) ? &tor->stats : tr_torrentStat(tor);
}
void tr_torrentSetVerifyState(tr_torrent* tor, tr_verify_state state)
void tr_torrent::setVerifyState(tr_verify_state state)
{
TR_ASSERT(tr_isTorrent(tor));
TR_ASSERT(state == TR_VERIFY_NONE || state == TR_VERIFY_WAIT || state == TR_VERIFY_NOW);
tor->verifyState = state;
tor->markChanged();
this->verifyState = state;
this->markChanged();
}
tr_torrent_activity tr_torrentGetActivity(tr_torrent const* tor)
@ -2181,44 +2182,6 @@ bool tr_torrentSetAnnounceList(tr_torrent* tor, char const* const* announce_urls
***
**/
#define BACK_COMPAT_FUNC(oldname, newname) \
void oldname(tr_torrent* tor, time_t t) \
{ \
newname(tor, t); \
}
BACK_COMPAT_FUNC(tr_torrentSetAddedDate, tr_torrentSetDateAdded)
BACK_COMPAT_FUNC(tr_torrentSetActivityDate, tr_torrentSetDateActive)
BACK_COMPAT_FUNC(tr_torrentSetDoneDate, tr_torrentSetDateDone)
#undef BACK_COMPAT_FUNC
void tr_torrentSetDateAdded(tr_torrent* tor, time_t t)
{
TR_ASSERT(tr_isTorrent(tor));
tor->addedDate = t;
tor->anyDate = std::max(tor->anyDate, tor->addedDate);
}
void tr_torrentSetDateActive(tr_torrent* tor, time_t t)
{
TR_ASSERT(tr_isTorrent(tor));
tor->activityDate = t;
tor->anyDate = std::max(tor->anyDate, tor->activityDate);
}
void tr_torrentSetDateDone(tr_torrent* tor, time_t t)
{
TR_ASSERT(tr_isTorrent(tor));
tor->doneDate = t;
tor->anyDate = std::max(tor->anyDate, tor->doneDate);
}
/**
***
**/
uint64_t tr_torrentGetBytesLeftToAllocate(tr_torrent const* tor)
{
TR_ASSERT(tr_isTorrent(tor));
@ -2631,15 +2594,15 @@ void tr_torrentSetLocation(
return tor->setLocation(location ? location : "", move_from_old_location, setme_progress, setme_state);
}
std::string_view tr_torrentPrimaryMimeType(tr_torrent const* tor)
std::string_view tr_torrent::primaryMimeType() const
{
// count up how many bytes there are for each mime-type in the torrent
// NB: get_mime_type_for_filename() always returns the same ptr for a
// mime_type, so its raw pointer can be used as a key.
auto size_per_mime_type = std::unordered_map<std::string_view, size_t>{};
for (tr_file_index_t i = 0, n = tor->fileCount(); i < n; ++i)
for (tr_file_index_t i = 0, n = this->fileCount(); i < n; ++i)
{
auto const& file = tor->file(i);
auto const& file = this->file(i);
auto const mime_type = tr_get_mime_type_for_filename(file.name);
size_per_mime_type[mime_type] += file.length;
}

View File

@ -12,6 +12,7 @@
#error only libtransmission should #include this header.
#endif
#include <cstddef> // size_t
#include <ctime>
#include <optional>
#include <string>
@ -103,16 +104,6 @@ void tr_torrentSave(tr_torrent* tor);
void tr_torrentSetLocalError(tr_torrent* tor, char const* fmt, ...) TR_GNUC_PRINTF(2, 3);
void tr_torrentSetDateAdded(tr_torrent* torrent, time_t addedDate);
void tr_torrentSetDateActive(tr_torrent* torrent, time_t activityDate);
void tr_torrentSetDateDone(tr_torrent* torrent, time_t doneDate);
/** Return the mime-type (e.g. "audio/x-flac") that matches more of the
torrent's content than any other mime-type. */
std::string_view tr_torrentPrimaryMimeType(tr_torrent const* tor);
enum tr_verify_state
{
TR_VERIFY_NONE,
@ -120,8 +111,6 @@ enum tr_verify_state
TR_VERIFY_NOW
};
void tr_torrentSetVerifyState(tr_torrent* tor, tr_verify_state state);
tr_torrent_activity tr_torrentGetActivity(tr_torrent const* tor);
struct tr_incomplete_metadata;
@ -504,6 +493,18 @@ public:
return this->isPublic() && tr_sessionAllowsLPD(this->session);
}
void setVerifyState(tr_verify_state state);
void setDateActive(time_t t)
{
this->activityDate = t;
this->anyDate = std::max(this->anyDate, this->activityDate);
}
/** Return the mime-type (e.g. "audio/x-flac") that matches more of the
torrent's content than any other mime-type. */
std::string_view primaryMimeType() const;
tr_info info = {};
tr_bitfield checked_pieces_ = tr_bitfield{ 0 };

View File

@ -27,6 +27,11 @@ THE SOFTWARE.
#error only libtransmission should #include this header.
#endif
#include <cstddef> // size_t
#include <cstdint> // uintX_t
struct tr_session;
void tr_udpInit(tr_session*);
void tr_udpUninit(tr_session*);
void tr_udpSetSocketBuffers(tr_session*);

View File

@ -1821,18 +1821,6 @@ tr_stat const* tr_torrentStat(tr_torrent* torrent);
reduce the CPU load if you're calling tr_torrentStat() frequently. */
tr_stat const* tr_torrentStatCached(tr_torrent* torrent);
/** @deprecated because this should only be accessible to libtransmission.
private code, use tr_torentSetDateAdded() instead */
TR_DEPRECATED void tr_torrentSetAddedDate(tr_torrent* torrent, time_t addedDate);
/** @deprecated because this should only be accessible to libtransmission.
private code, use tr_torentSetDateActive() instead */
TR_DEPRECATED void tr_torrentSetActivityDate(tr_torrent* torrent, time_t activityDate);
/** @deprecated because this should only be accessible to libtransmission.
private code, use tr_torentSetDateDone() instead */
TR_DEPRECATED void tr_torrentSetDoneDate(tr_torrent* torrent, time_t doneDate);
/** @} */
/** @brief Sanity checker to test that the direction is TR_UP or TR_DOWN */

View File

@ -226,9 +226,9 @@ static void verifyThreadFunc(void* /*user_data*/)
tr_torrent* tor = currentNode.torrent;
tr_logAddTorInfo(tor, "%s", _("Verifying torrent"));
tr_torrentSetVerifyState(tor, TR_VERIFY_NOW);
tor->setVerifyState(TR_VERIFY_NOW);
changed = verifyTorrent(tor, &stopCurrent);
tr_torrentSetVerifyState(tor, TR_VERIFY_NONE);
tor->setVerifyState(TR_VERIFY_NONE);
TR_ASSERT(tr_isTorrent(tor));
if (!stopCurrent && changed)
@ -257,7 +257,7 @@ void tr_verifyAdd(tr_torrent* tor, tr_verify_done_func callback_func, void* call
node.current_size = tor->hasTotal();
auto const lock = std::lock_guard(verify_mutex_);
tr_torrentSetVerifyState(tor, TR_VERIFY_WAIT);
tor->setVerifyState(TR_VERIFY_WAIT);
verifyList.insert(node);
if (verifyThread == nullptr)
@ -290,7 +290,7 @@ void tr_verifyRemove(tr_torrent* tor)
std::end(verifyList),
[tor](auto const& task) { return tor == task.torrent; });
tr_torrentSetVerifyState(tor, TR_VERIFY_NONE);
tor->setVerifyState(TR_VERIFY_NONE);
if (it != std::end(verifyList))
{

View File

@ -236,21 +236,6 @@ bool trashDataFile(char const* filename, tr_error** error)
[self startTransferNoQueue];
}
//upgrading from versions < 1.30: get old added, activity, and done dates
NSDate* date;
if ((date = history[@"Date"]))
{
tr_torrentSetAddedDate(fHandle, date.timeIntervalSince1970);
}
if ((date = history[@"DateActivity"]))
{
tr_torrentSetActivityDate(fHandle, date.timeIntervalSince1970);
}
if ((date = history[@"DateCompleted"]))
{
tr_torrentSetDoneDate(fHandle, date.timeIntervalSince1970);
}
//upgrading from versions < 1.60: get old stop ratio settings
NSNumber* ratioSetting;
if ((ratioSetting = history[@"RatioSetting"]))