2010-06-16 14:27:24 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2012-2014 Mnemosyne LLC
|
2010-06-16 14:27:24 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2010-06-16 14:27:24 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <array>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <stdio.h> /* fprintf() */
|
2011-03-16 18:04:23 +00:00
|
|
|
#include <stdlib.h> /* EXIT_FAILURE */
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <vector>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2015-04-11 10:51:59 +00:00
|
|
|
#include <libtransmission/error.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
|
|
|
#include <libtransmission/utils.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2010-08-08 22:53:24 +00:00
|
|
|
#include <libtransmission/version.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
static char constexpr MyName[] = "transmission-edit";
|
|
|
|
static char constexpr Usage[] = "Usage: transmission-edit [options] torrent-file(s)";
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
struct app_options
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
std::vector<char const*> files;
|
|
|
|
char const* add = nullptr;
|
|
|
|
char const* deleteme = nullptr;
|
|
|
|
std::array<char const*, 2> replace;
|
|
|
|
bool show_version = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
static auto constexpr Options = std::array<tr_option, 5>{
|
|
|
|
{ { 'a', "add", "Add a tracker's announce URL", "a", true, "<url>" },
|
|
|
|
{ 'd', "delete", "Delete a tracker's announce URL", "d", true, "<url>" },
|
|
|
|
{ 'r', "replace", "Search and replace a substring in the announce URLs", "r", true, "<old> <new>" },
|
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr } }
|
|
|
|
};
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
static int parseCommandLine(app_options& opts, int argc, char const* const* argv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &optarg)) != TR_OPT_DONE)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (c)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'a':
|
2021-12-17 20:48:02 +00:00
|
|
|
opts.add = optarg;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'd':
|
2021-12-17 20:48:02 +00:00
|
|
|
opts.deleteme = optarg;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'r':
|
2021-12-17 20:48:02 +00:00
|
|
|
opts.replace[0] = optarg;
|
|
|
|
c = tr_getopt(Usage, argc, argv, std::data(Options), &optarg);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (c != TR_OPT_UNK)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
opts.replace[1] = optarg;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'V':
|
2021-12-17 20:48:02 +00:00
|
|
|
opts.show_version = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_OPT_UNK:
|
2021-12-17 20:48:02 +00:00
|
|
|
opts.files.push_back(optarg);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
return 1;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 18:24:30 +00:00
|
|
|
static bool removeURL(tr_variant* metainfo, std::string_view url)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto sv = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* announce_list;
|
|
|
|
bool changed = false;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-11-14 18:24:30 +00:00
|
|
|
if (tr_variantDictFindStrView(metainfo, TR_KEY_announce, &sv) && url == sv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
printf("\tRemoved \"%" TR_PRIsv "\" from \"announce\"\n", TR_PRIsv_ARG(sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictRemove(metainfo, TR_KEY_announce);
|
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(metainfo, TR_KEY_announce_list, &announce_list))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tier;
|
|
|
|
int tierIndex = 0;
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((tier = tr_variantListChild(announce_list, tierIndex)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int nodeIndex = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant const* node;
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((node = tr_variantListChild(tier, nodeIndex)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
if (tr_variantGetStrView(node, &sv) && url == sv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
printf("\tRemoved \"%" TR_PRIsv "\" from \"announce-list\" tier #%d\n", TR_PRIsv_ARG(sv), tierIndex + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantListRemove(tier, nodeIndex);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++nodeIndex;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantListSize(tier) == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
printf("\tNo URLs left in tier #%d... removing tier\n", tierIndex + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantListRemove(announce_list, tierIndex);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++tierIndex;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantListSize(announce_list) == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\tNo tiers left... removing announce-list\n");
|
|
|
|
tr_variantDictRemove(metainfo, TR_KEY_announce_list);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if we removed the "announce" field and there's still another track left,
|
|
|
|
* use it as the "announce" field */
|
2021-11-14 18:24:30 +00:00
|
|
|
if (changed && !tr_variantDictFindStrView(metainfo, TR_KEY_announce, &sv))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
tr_variant* const tier = tr_variantListChild(announce_list, 0);
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tier != nullptr)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
tr_variant const* const node = tr_variantListChild(tier, 0);
|
2021-11-14 18:24:30 +00:00
|
|
|
if ((node != nullptr) && tr_variantGetStrView(node, &sv))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
tr_variantDictAddStr(metainfo, TR_KEY_announce, sv);
|
|
|
|
printf("\tAdded \"%" TR_PRIsv "\" to announce\n", TR_PRIsv_ARG(sv));
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return changed;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 18:24:30 +00:00
|
|
|
static std::string replaceSubstr(std::string_view str, std::string_view oldval, std::string_view newval)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto ret = std::string{};
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-11-14 18:24:30 +00:00
|
|
|
while (!std::empty(str))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto const pos = str.find(oldval);
|
|
|
|
ret += str.substr(0, pos);
|
|
|
|
if (pos == str.npos)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2021-12-17 20:48:02 +00:00
|
|
|
ret += newval;
|
2021-11-14 18:24:30 +00:00
|
|
|
str.remove_prefix(pos + std::size(oldval));
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 18:24:30 +00:00
|
|
|
return ret;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 18:24:30 +00:00
|
|
|
static bool replaceURL(tr_variant* metainfo, std::string_view oldval, std::string_view newval)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto sv = std::string_view{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* announce_list;
|
|
|
|
bool changed = false;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-11-14 18:24:30 +00:00
|
|
|
if (tr_variantDictFindStrView(metainfo, TR_KEY_announce, &sv) && tr_strvContains(sv, oldval))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto const newstr = replaceSubstr(sv, oldval, newval);
|
|
|
|
printf("\tReplaced in \"announce\": \"%" TR_PRIsv "\" --> \"%s\"\n", TR_PRIsv_ARG(sv), newstr.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(metainfo, TR_KEY_announce, newstr);
|
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(metainfo, TR_KEY_announce_list, &announce_list))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tier;
|
|
|
|
int tierCount = 0;
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((tier = tr_variantListChild(announce_list, tierCount)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* node;
|
|
|
|
int nodeCount = 0;
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((node = tr_variantListChild(tier, nodeCount)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
if (tr_variantGetStrView(node, &sv) && tr_strvContains(sv, oldval))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto const newstr = replaceSubstr(sv, oldval, newval);
|
|
|
|
printf(
|
|
|
|
"\tReplaced in \"announce-list\" tier %d: \"%" TR_PRIsv "\" --> \"%s\"\n",
|
|
|
|
tierCount + 1,
|
|
|
|
TR_PRIsv_ARG(sv),
|
|
|
|
newstr.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(node);
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantInitStr(node, newstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++nodeCount;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++tierCount;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return changed;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool announce_list_has_url(tr_variant* announce_list, char const* url)
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int tierCount = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant* tier;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((tier = tr_variantListChild(announce_list, tierCount)) != nullptr)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int nodeCount = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant const* node;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((node = tr_variantListChild(tier, nodeCount)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto sv = std::string_view{};
|
|
|
|
if (tr_variantGetStrView(node, &sv) && sv == url)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++nodeCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++tierCount;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool addURL(tr_variant* metainfo, char const* url)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-11-14 18:24:30 +00:00
|
|
|
auto announce = std::string_view{};
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_variant* announce_list = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool changed = false;
|
2021-11-14 18:24:30 +00:00
|
|
|
bool const had_announce = tr_variantDictFindStrView(metainfo, TR_KEY_announce, &announce);
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const had_announce_list = tr_variantDictFindList(metainfo, TR_KEY_announce_list, &announce_list);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!had_announce && !had_announce_list)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* this new tracker is the only one, so add it to "announce"... */
|
|
|
|
printf("\tAdded \"%s\" in \"announce\"\n", url);
|
|
|
|
tr_variantDictAddStr(metainfo, TR_KEY_announce, url);
|
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!had_announce_list)
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
announce_list = tr_variantDictAddList(metainfo, TR_KEY_announce_list, 2);
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (had_announce)
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* we're moving from an 'announce' to an 'announce-list',
|
|
|
|
* so copy the old announce URL to the list */
|
|
|
|
tr_variant* tier = tr_variantListAddList(announce_list, 1);
|
|
|
|
tr_variantListAddStr(tier, announce);
|
|
|
|
changed = true;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* If the user-specified URL isn't in the announce list yet, add it */
|
|
|
|
if (!announce_list_has_url(announce_list, url))
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tier = tr_variantListAddList(announce_list, 1);
|
|
|
|
tr_variantListAddStr(tier, url);
|
|
|
|
printf("\tAdded \"%s\" to \"announce-list\" tier %zu\n", url, tr_variantListSize(announce_list));
|
|
|
|
changed = true;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return changed;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int changedCount = 0;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logSetLevel(TR_LOG_ERROR);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
auto options = app_options{};
|
|
|
|
if (parseCommandLine(options, argc, (char const* const*)argv) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.show_version)
|
2010-08-08 22:53:24 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
fprintf(stderr, "%s %s\n", MyName, LONG_VERSION_STRING);
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_SUCCESS;
|
2010-08-08 22:53:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (std::empty(options.files))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: No torrent files specified.\n");
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_getopt_usage(MyName, Usage, std::data(Options));
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.add == nullptr && options.deleteme == nullptr && options.replace[0] == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: Must specify -a, -d or -r\n");
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_getopt_usage(MyName, Usage, std::data(Options));
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
for (auto const& filename : options.files)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
bool changed = false;
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("%s\n", filename);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
if (!tr_variantFromFile(&top, TR_VARIANT_PARSE_BENC, filename, &error))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\tError reading file: %s\n", error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
continue;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.deleteme != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
changed |= removeURL(&top, options.deleteme);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.add != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
changed = addURL(&top, options.add);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.replace[0] != nullptr && options.replace[1] != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
changed |= replaceURL(&top, options.replace[0], options.replace[1]);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (changed)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++changedCount;
|
|
|
|
tr_variantToFile(&top, TR_VARIANT_FMT_BENC, filename);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("Changed %d files\n", changedCount);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|