2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cerrno>
|
2021-12-21 22:14:15 +00:00
|
|
|
#include <cstdint>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cstdlib> /* qsort */
|
|
|
|
#include <cstring> /* strcmp, strlen */
|
2021-11-20 21:20:45 +00:00
|
|
|
#include <mutex>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <event2/util.h> /* evutil_ascii_strcasecmp() */
|
2011-03-13 08:21:55 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "transmission.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "error.h"
|
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "makemeta.h"
|
2011-07-13 03:23:37 +00:00
|
|
|
#include "platform.h" /* threads, locks */
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "session.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h" /* buildpath */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "version.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "web-utils.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct FileList
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t size;
|
|
|
|
char* filename;
|
|
|
|
struct FileList* next;
|
2007-06-18 03:40:41 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static struct FileList* getFiles(char const* dir, char const* base, struct FileList* list)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (dir == nullptr || base == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
auto buf = tr_strvPath(dir, base);
|
|
|
|
tr_sys_path_native_separators(std::data(buf));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_path_info info;
|
2021-12-17 05:47:51 +00:00
|
|
|
if (tr_error* error = nullptr; !tr_sys_path_get_info(buf.c_str(), 0, &info, &error))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-11-13 00:10:04 +00:00
|
|
|
tr_logAddError(_("Torrent Creator is skipping file \"%s\": %s"), buf.c_str(), error->message);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_free(error);
|
|
|
|
return list;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
if (tr_sys_dir_t odir = info.type == TR_SYS_PATH_IS_DIRECTORY ? tr_sys_dir_open(buf.c_str(), nullptr) : TR_BAD_SYS_DIR;
|
|
|
|
odir != TR_BAD_SYS_DIR)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
char const* name = nullptr;
|
2021-09-15 00:18:09 +00:00
|
|
|
while ((name = tr_sys_dir_read_name(odir, nullptr)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (name[0] != '.') /* skip dotfiles */
|
|
|
|
{
|
2021-11-13 00:10:04 +00:00
|
|
|
list = getFiles(buf.c_str(), name, list);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_dir_close(odir, nullptr);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (info.type == TR_SYS_PATH_IS_FILE && info.size > 0)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-12-17 05:47:51 +00:00
|
|
|
auto* const node = tr_new0(FileList, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
node->size = info.size;
|
2021-11-13 00:10:04 +00:00
|
|
|
node->filename = tr_strvDup(buf);
|
2017-04-19 12:04:45 +00:00
|
|
|
node->next = list;
|
|
|
|
list = node;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return list;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static uint32_t bestPieceSize(uint64_t totalSize)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
uint32_t const KiB = 1024;
|
|
|
|
uint32_t const MiB = 1048576;
|
|
|
|
uint32_t const GiB = 1073741824;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (totalSize >= 2 * GiB)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 2 * MiB;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (totalSize >= 1 * GiB)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 1 * MiB;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (totalSize >= 512 * MiB)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 512 * KiB;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (totalSize >= 350 * MiB)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 256 * KiB;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (totalSize >= 150 * MiB)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 128 * KiB;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (totalSize >= 50 * MiB)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 64 * KiB;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 32 * KiB; /* less than 50 meg */
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_metainfo_builder* tr_metaInfoBuilderCreate(char const* topFileArg)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char* const real_top = tr_sys_path_resolve(topFileArg, nullptr);
|
2018-01-24 20:17:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (real_top == nullptr)
|
2018-01-24 20:17:05 +00:00
|
|
|
{
|
|
|
|
/* TODO: Better error reporting */
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2018-01-24 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
auto* const ret = tr_new0(tr_metainfo_builder, 1);
|
2011-07-24 20:18:33 +00:00
|
|
|
|
2018-01-24 20:17:05 +00:00
|
|
|
ret->top = real_top;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sys_path_info info;
|
2021-09-15 00:18:09 +00:00
|
|
|
ret->isFolder = tr_sys_path_get_info(ret->top, 0, &info, nullptr) && info.type == TR_SYS_PATH_IS_DIRECTORY;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-25 06:01:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* build a list of files containing top file and,
|
|
|
|
if it's a directory, all of its children */
|
2021-10-24 17:40:15 +00:00
|
|
|
FileList* files = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char* dir = tr_sys_path_dirname(ret->top, nullptr);
|
|
|
|
char* base = tr_sys_path_basename(ret->top, nullptr);
|
|
|
|
files = getFiles(dir, base, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(base);
|
|
|
|
tr_free(dir);
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
for (struct FileList* walk = files; walk != nullptr; walk = walk->next)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
++ret->fileCount;
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret->files = tr_new0(tr_metainfo_builder_file, ret->fileCount);
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
int i = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
while (files != nullptr)
|
2007-06-19 00:08:39 +00:00
|
|
|
{
|
2020-11-08 19:54:40 +00:00
|
|
|
struct FileList* const tmp = files;
|
2017-05-13 22:38:31 +00:00
|
|
|
files = files->next;
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
tr_metainfo_builder_file* const file = &ret->files[i++];
|
2017-04-19 12:04:45 +00:00
|
|
|
file->filename = tmp->filename;
|
|
|
|
file->size = tmp->size;
|
2017-05-13 22:38:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret->totalSize += tmp->size;
|
2017-05-13 22:38:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tmp);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
std::sort(
|
|
|
|
ret->files,
|
|
|
|
ret->files + ret->fileCount,
|
|
|
|
[](auto const& a, auto const& b) { return evutil_ascii_strcasecmp(a.filename, b.filename) < 0; });
|
2007-06-19 00:08:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_metaInfoBuilderSetPieceSize(ret, bestPieceSize(ret->totalSize));
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool isValidPieceSize(uint32_t n)
|
2014-06-10 00:43:21 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
bool const isPowerOfTwo = n != 0 && (n & (n - 1)) == 0;
|
2014-06-10 00:43:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return isPowerOfTwo;
|
2014-06-10 00:43:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_metaInfoBuilderSetPieceSize(tr_metainfo_builder* b, uint32_t bytes)
|
2013-01-17 18:55:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!isValidPieceSize(bytes))
|
2014-06-10 00:43:21 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_logAddError(
|
|
|
|
_("Failed to set piece size to %s, leaving it at %s"),
|
|
|
|
tr_formatter_mem_B(bytes).c_str(),
|
|
|
|
tr_formatter_mem_B(b->pieceSize).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2014-06-10 00:43:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
b->pieceSize = bytes;
|
|
|
|
b->pieceCount = (int)(b->totalSize / b->pieceSize);
|
2013-01-17 18:55:51 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (b->totalSize % b->pieceSize != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
++b->pieceCount;
|
|
|
|
}
|
2014-06-10 00:43:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2013-01-17 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_metaInfoBuilderFree(tr_metainfo_builder* builder)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (builder != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (uint32_t i = 0; i < builder->fileCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
tr_free(builder->files[i].filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(builder->files);
|
|
|
|
tr_free(builder->top);
|
|
|
|
tr_free(builder->comment);
|
2021-10-18 23:05:39 +00:00
|
|
|
tr_free(builder->source);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < builder->trackerCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(builder->trackers[i].announce);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(builder->trackers);
|
|
|
|
tr_free(builder->outputFile);
|
|
|
|
tr_free(builder);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
static std::vector<std::byte> getHashInfo(tr_metainfo_builder* b)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
auto ret = std::vector<std::byte>(std::size(tr_sha1_digest_t{}) * b->pieceCount);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (b->totalSize == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b->pieceIndex = 0;
|
2021-09-12 17:41:49 +00:00
|
|
|
uint64_t totalRemain = b->totalSize;
|
2021-12-21 22:14:15 +00:00
|
|
|
uint32_t fileIndex = 0;
|
|
|
|
auto* walk = std::data(ret);
|
|
|
|
auto buf = std::vector<char>(b->pieceSize);
|
|
|
|
uint64_t off = 0;
|
|
|
|
tr_error* error = nullptr;
|
2013-01-25 06:01:45 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_sys_file_t fd = tr_sys_file_open(b->files[fileIndex].filename, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &error);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->my_errno = error->code;
|
|
|
|
tr_strlcpy(b->errfile, b->files[fileIndex].filename, sizeof(b->errfile));
|
|
|
|
b->result = TR_MAKEMETA_IO_READ;
|
|
|
|
tr_error_free(error);
|
2021-12-21 22:14:15 +00:00
|
|
|
return {};
|
2008-03-02 19:42:45 +00:00
|
|
|
}
|
2013-01-25 06:01:45 +00:00
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
while (totalRemain != 0)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(b->pieceIndex < b->pieceCount);
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
auto* bufptr = std::data(buf);
|
2021-09-19 20:41:35 +00:00
|
|
|
uint32_t const thisPieceSize = std::min(uint64_t{ b->pieceSize }, totalRemain);
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t leftInPiece = thisPieceSize;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (leftInPiece != 0)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
uint64_t const n_this_pass = std::min(b->files[fileIndex].size - off, leftInPiece);
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t n_read = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
(void)tr_sys_file_read(fd, bufptr, n_this_pass, &n_read, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
bufptr += n_read;
|
|
|
|
off += n_read;
|
|
|
|
leftInPiece -= n_read;
|
|
|
|
|
|
|
|
if (off == b->files[fileIndex].size)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
off = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_file_close(fd, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
fd = TR_BAD_SYS_FILE;
|
|
|
|
|
|
|
|
if (++fileIndex < b->fileCount)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fd = tr_sys_file_open(b->files[fileIndex].filename, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &error);
|
|
|
|
|
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->my_errno = error->code;
|
|
|
|
tr_strlcpy(b->errfile, b->files[fileIndex].filename, sizeof(b->errfile));
|
|
|
|
b->result = TR_MAKEMETA_IO_READ;
|
|
|
|
tr_error_free(error);
|
2021-12-21 22:14:15 +00:00
|
|
|
return {};
|
2008-03-02 19:42:45 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
TR_ASSERT(bufptr - std::data(buf) == (int)thisPieceSize);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(leftInPiece == 0);
|
2021-12-21 22:14:15 +00:00
|
|
|
auto const digest = tr_sha1(buf);
|
|
|
|
if (!digest)
|
|
|
|
{
|
|
|
|
b->my_errno = EIO;
|
|
|
|
tr_snprintf(b->errfile, sizeof(b->errfile), "error hashing piece %" PRIu32, b->pieceIndex);
|
|
|
|
b->result = TR_MAKEMETA_IO_READ;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
walk = std::copy(std::begin(*digest), std::end(*digest), walk);
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (b->abortFlag)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->result = TR_MAKEMETA_CANCELLED;
|
|
|
|
break;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
totalRemain -= thisPieceSize;
|
|
|
|
++b->pieceIndex;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
TR_ASSERT(b->abortFlag || size_t(walk - std::data(ret)) == std::size(ret));
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(b->abortFlag || !totalRemain);
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_sys_file_close(fd, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void getFileInfo(
|
|
|
|
char const* topFile,
|
|
|
|
tr_metainfo_builder_file const* file,
|
|
|
|
tr_variant* uninitialized_length,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* uninitialized_path)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* get the file size */
|
|
|
|
tr_variantInitInt(uninitialized_length, file->size);
|
2013-01-25 06:01:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how much of file->filename to walk past */
|
2021-10-24 17:40:15 +00:00
|
|
|
size_t offset = strlen(topFile);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (offset > 0 && topFile[offset - 1] != TR_PATH_DELIMITER)
|
2013-01-25 06:01:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++offset; /* +1 for the path delimiter */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the path list */
|
|
|
|
tr_variantInitList(uninitialized_path, 0);
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
auto filename = std::string_view{ file->filename };
|
|
|
|
if (std::size(filename) > offset)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-10 00:13:47 +00:00
|
|
|
filename.remove_prefix(offset);
|
|
|
|
auto token = std::string_view{};
|
|
|
|
while (tr_strvSep(&filename, &token, TR_PATH_DELIMITER))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-10 00:13:47 +00:00
|
|
|
tr_variantListAddStr(uninitialized_path, token);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void makeInfoDict(tr_variant* dict, tr_metainfo_builder* builder)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictReserve(dict, 5);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (builder->isFolder) /* root node is a directory */
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* list = tr_variantDictAddList(dict, TR_KEY_files, builder->fileCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (uint32_t i = 0; i < builder->fileCount; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d = tr_variantListAddDict(list, 2);
|
|
|
|
tr_variant* length = tr_variantDictAdd(d, TR_KEY_length);
|
|
|
|
tr_variant* pathVal = tr_variantDictAdd(d, TR_KEY_path);
|
|
|
|
getFileInfo(builder->top, &builder->files[i], length, pathVal);
|
2008-04-18 16:23:59 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-06-08 22:16:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(dict, TR_KEY_length, builder->files[0].size);
|
2014-06-08 22:16:01 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
if (auto* const base = tr_sys_path_basename(builder->top, nullptr); base != nullptr)
|
2016-03-13 10:41:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(dict, TR_KEY_name, base);
|
|
|
|
tr_free(base);
|
2016-03-13 10:41:52 +00:00
|
|
|
}
|
2007-07-25 01:00:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(dict, TR_KEY_piece_length, builder->pieceSize);
|
2007-07-25 01:00:17 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
if (auto const piece_hashes = getHashInfo(builder); !std::empty(piece_hashes))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-12-21 22:14:15 +00:00
|
|
|
tr_variantDictAddRaw(dict, TR_KEY_pieces, std::data(piece_hashes), std::size(piece_hashes));
|
2008-03-02 19:42:45 +00:00
|
|
|
}
|
2007-07-25 01:00:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(dict, TR_KEY_private, builder->isPrivate ? 1 : 0);
|
2021-10-18 23:05:39 +00:00
|
|
|
if (builder->source != nullptr)
|
|
|
|
{
|
|
|
|
tr_variantDictAddStr(dict, TR_KEY_source, builder->source);
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_realMakeMetaInfo(tr_metainfo_builder* builder)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
2013-01-25 06:01:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* allow an empty set, but if URLs *are* listed, verify them. #814, #971 */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < builder->trackerCount && builder->result == TR_MAKEMETA_OK; ++i)
|
2013-01-25 06:01:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_urlIsValidTracker(builder->trackers[i].announce))
|
2013-01-25 06:01:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(builder->errfile, builder->trackers[i].announce, sizeof(builder->errfile));
|
|
|
|
builder->result = TR_MAKEMETA_URL;
|
2009-09-17 01:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&top, 6);
|
2008-04-18 16:23:59 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (builder->fileCount == 0 || builder->totalSize == 0 || builder->pieceSize == 0 || builder->pieceCount == 0)
|
2010-07-07 22:50:18 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
builder->errfile[0] = '\0';
|
|
|
|
builder->my_errno = ENOENT;
|
|
|
|
builder->result = TR_MAKEMETA_IO_READ;
|
|
|
|
builder->isDone = true;
|
2010-07-07 22:50:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (builder->result == TR_MAKEMETA_OK && builder->trackerCount != 0)
|
2008-06-01 05:36:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int prevTier = -1;
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_variant* tier = nullptr;
|
2008-06-01 05:36:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (builder->trackerCount > 1)
|
2008-08-11 17:29:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* annList = tr_variantDictAddList(&top, TR_KEY_announce_list, 0);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < builder->trackerCount; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (prevTier != builder->trackers[i].tier)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
prevTier = builder->trackers[i].tier;
|
|
|
|
tier = tr_variantListAddList(annList, 0);
|
2008-08-11 17:29:53 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantListAddStr(tier, builder->trackers[i].announce);
|
2008-06-01 05:36:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(&top, TR_KEY_announce, builder->trackers[0].announce);
|
2008-06-01 05:36:23 +00:00
|
|
|
}
|
2008-08-11 17:29:53 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (builder->result == TR_MAKEMETA_OK && !builder->abortFlag)
|
2008-03-24 15:58:06 +00:00
|
|
|
{
|
2019-07-13 08:52:44 +00:00
|
|
|
if (!tr_str_is_empty(builder->comment))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddStr(&top, TR_KEY_comment, builder->comment);
|
|
|
|
}
|
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(&top, TR_KEY_created_by, TR_NAME "/" LONG_VERSION_STRING);
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_variantDictAddInt(&top, TR_KEY_creation_date, time(nullptr));
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(&top, TR_KEY_encoding, "UTF-8");
|
2017-04-19 12:04:45 +00:00
|
|
|
makeInfoDict(tr_variantDictAddDict(&top, TR_KEY_info, 666), builder);
|
2008-03-24 15:58:06 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* save the file */
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((builder->result == TR_MAKEMETA_OK) && (!builder->abortFlag) &&
|
2020-11-01 21:47:57 +00:00
|
|
|
(tr_variantToFile(&top, TR_VARIANT_FMT_BENC, builder->outputFile) != 0))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
builder->my_errno = errno;
|
|
|
|
tr_strlcpy(builder->errfile, builder->outputFile, sizeof(builder->errfile));
|
|
|
|
builder->result = TR_MAKEMETA_IO_WRITE;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
|
|
|
tr_variantFree(&top);
|
|
|
|
|
|
|
|
if (builder->abortFlag)
|
|
|
|
{
|
|
|
|
builder->result = TR_MAKEMETA_CANCELLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder->isDone = true;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** A threaded builder queue
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
static tr_metainfo_builder* queue = nullptr;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
static tr_thread* workerThread = nullptr;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
static std::recursive_mutex queue_mutex_;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void makeMetaWorkerFunc(void* /*user_data*/)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_metainfo_builder* builder = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* find the next builder to process */
|
2021-11-20 21:20:45 +00:00
|
|
|
queue_mutex_.lock();
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (queue != nullptr)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
builder = queue;
|
|
|
|
queue = queue->nextBuilder;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
queue_mutex_.unlock();
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if no builders, this worker thread is done */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (builder == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_realMakeMetaInfo(builder);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
workerThread = nullptr;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_makeMetaInfo(
|
|
|
|
tr_metainfo_builder* builder,
|
|
|
|
char const* outputFile,
|
|
|
|
tr_tracker_info const* trackers,
|
|
|
|
int trackerCount,
|
|
|
|
char const* comment,
|
2021-10-18 23:05:39 +00:00
|
|
|
bool isPrivate,
|
|
|
|
char const* source)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* free any variables from a previous run */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < builder->trackerCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(builder->trackers[i].announce);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(builder->trackers);
|
|
|
|
tr_free(builder->comment);
|
2021-10-18 23:05:39 +00:00
|
|
|
tr_free(builder->source);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(builder->outputFile);
|
|
|
|
|
|
|
|
/* initialize the builder variables */
|
|
|
|
builder->abortFlag = false;
|
2021-09-12 17:41:49 +00:00
|
|
|
builder->result = TR_MAKEMETA_OK;
|
2017-04-19 12:04:45 +00:00
|
|
|
builder->isDone = false;
|
|
|
|
builder->pieceIndex = 0;
|
|
|
|
builder->trackerCount = trackerCount;
|
|
|
|
builder->trackers = tr_new0(tr_tracker_info, builder->trackerCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < builder->trackerCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
builder->trackers[i].tier = trackers[i].tier;
|
|
|
|
builder->trackers[i].announce = tr_strdup(trackers[i].announce);
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
builder->comment = tr_strdup(comment);
|
|
|
|
builder->isPrivate = isPrivate;
|
2021-10-18 23:05:39 +00:00
|
|
|
builder->source = tr_strdup(source);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (!tr_str_is_empty(outputFile))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
builder->outputFile = tr_strdup(outputFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
builder->outputFile = tr_strvDup(tr_strvJoin(builder->top, ".torrent"sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enqueue the builder */
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = std::lock_guard(queue_mutex_);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
builder->nextBuilder = queue;
|
|
|
|
queue = builder;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (workerThread == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
workerThread = tr_threadNew(makeMetaWorkerFunc, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|