2014-01-19 01:09:44 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
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.
|
|
|
|
*
|
2014-01-19 01:29:38 +00:00
|
|
|
* $Id$
|
2014-01-19 01:09:44 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-21 00:00:00 +00:00
|
|
|
#include <assert.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <stdio.h>
|
2014-06-08 20:01:10 +00:00
|
|
|
#include <stdlib.h> /* mkstemp() */
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2013-01-31 05:11:32 +00:00
|
|
|
#include "platform.h" /* TR_PATH_DELIMETER */
|
2013-01-26 23:08:51 +00:00
|
|
|
#include "torrent.h"
|
2013-01-31 21:58:25 +00:00
|
|
|
#include "trevent.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
|
|
|
bool verbose = false;
|
|
|
|
|
|
|
|
int current_test = 0;
|
|
|
|
|
|
|
|
bool
|
|
|
|
should_print (bool pass)
|
|
|
|
{
|
|
|
|
if (!pass)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#ifdef VERBOSE
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
check_condition_impl (const char * file, int line, bool condition)
|
|
|
|
{
|
|
|
|
const bool pass = condition;
|
|
|
|
|
|
|
|
if (should_print (pass))
|
|
|
|
fprintf (stderr, "%s %s:%d\n", pass?"PASS":"FAIL", file, line);
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
check_streq_impl (const char * file, int line, const char * expected, const char * actual)
|
|
|
|
{
|
|
|
|
const bool pass = !tr_strcmp0 (expected, actual);
|
|
|
|
|
|
|
|
if (should_print (pass)) {
|
|
|
|
if (pass)
|
|
|
|
fprintf (stderr, "PASS %s:%d\n", file, line);
|
|
|
|
else
|
|
|
|
fprintf (stderr, "FAIL %s:%d, expected \"%s\", got \"%s\"\n", file, line, expected?expected:" (null)", actual?actual:" (null)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
check_int_eq_impl (const char * file, int line, int64_t expected, int64_t actual)
|
|
|
|
{
|
|
|
|
const bool pass = expected == actual;
|
|
|
|
|
|
|
|
if (should_print (pass)) {
|
|
|
|
if (pass)
|
|
|
|
fprintf (stderr, "PASS %s:%d\n", file, line);
|
|
|
|
else
|
|
|
|
fprintf (stderr, "FAIL %s:%d, expected \"%"PRId64"\", got \"%"PRId64"\"\n", file, line, expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
check_ptr_eq_impl (const char * file, int line, const void * expected, const void * actual)
|
|
|
|
{
|
|
|
|
const bool pass = expected == actual;
|
|
|
|
|
|
|
|
if (should_print (pass)) {
|
|
|
|
if (pass)
|
|
|
|
fprintf (stderr, "PASS %s:%d\n", file, line);
|
|
|
|
else
|
|
|
|
fprintf (stderr, "FAIL %s:%d, expected \"%p\", got \"%p\"\n", file, line, expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
runTests (const testFunc * const tests, int numTests)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
(void) current_test; /* Use test even if we don't have any tests to run */
|
|
|
|
|
|
|
|
for (i=0; i<numTests; i++)
|
|
|
|
if ((ret = (*tests[i])()))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0; /* All tests passed */
|
|
|
|
}
|
2013-01-20 04:41:38 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <sys/types.h> /* stat(), opendir() */
|
|
|
|
#include <sys/stat.h> /* stat() */
|
|
|
|
#include <dirent.h> /* opendir() */
|
|
|
|
#include <unistd.h> /* getcwd() */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h> /* strcmp() */
|
|
|
|
|
|
|
|
#include "variant.h"
|
|
|
|
|
2014-06-08 19:09:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2013-01-20 04:41:38 +00:00
|
|
|
|
|
|
|
static char*
|
|
|
|
tr_getcwd (void)
|
|
|
|
{
|
|
|
|
char * result;
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
result = _getcwd (buf, sizeof (buf));
|
|
|
|
#else
|
|
|
|
result = getcwd (buf, sizeof (buf));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno));
|
|
|
|
*buf = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr_strdup (buf);
|
|
|
|
}
|
|
|
|
|
2014-06-08 19:09:41 +00:00
|
|
|
char *
|
|
|
|
libtest_sandbox_create (void)
|
|
|
|
{
|
2014-06-08 20:01:10 +00:00
|
|
|
char * path = tr_getcwd ();
|
2014-06-08 19:09:41 +00:00
|
|
|
char * sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL);
|
2014-06-08 20:01:10 +00:00
|
|
|
tr_free (path);
|
2014-06-08 19:09:41 +00:00
|
|
|
tr_mkdtemp (sandbox);
|
|
|
|
return sandbox;
|
|
|
|
}
|
|
|
|
|
2013-01-20 04:41:38 +00:00
|
|
|
static void
|
|
|
|
rm_rf (const char * killme)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (!stat (killme, &sb))
|
|
|
|
{
|
|
|
|
DIR * odir;
|
|
|
|
|
|
|
|
if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme))))
|
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
for (d = readdir(odir); d != NULL; d=readdir(odir))
|
|
|
|
{
|
|
|
|
if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,".."))
|
|
|
|
{
|
|
|
|
char * tmp = tr_buildPath (killme, d->d_name, NULL);
|
|
|
|
rm_rf (tmp);
|
|
|
|
tr_free (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir (odir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf (stderr, "cleanup: removing %s\n", killme);
|
|
|
|
|
2013-02-04 21:45:20 +00:00
|
|
|
tr_remove (killme);
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 19:09:41 +00:00
|
|
|
void
|
|
|
|
libtest_sandbox_destroy (const char * sandbox)
|
|
|
|
{
|
|
|
|
rm_rf (sandbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-01-21 00:00:00 +00:00
|
|
|
#define MEM_K 1024
|
|
|
|
#define MEM_B_STR "B"
|
|
|
|
#define MEM_K_STR "KiB"
|
|
|
|
#define MEM_M_STR "MiB"
|
|
|
|
#define MEM_G_STR "GiB"
|
|
|
|
#define MEM_T_STR "TiB"
|
|
|
|
|
|
|
|
#define DISK_K 1000
|
|
|
|
#define DISK_B_STR "B"
|
|
|
|
#define DISK_K_STR "kB"
|
|
|
|
#define DISK_M_STR "MB"
|
|
|
|
#define DISK_G_STR "GB"
|
|
|
|
#define DISK_T_STR "TB"
|
|
|
|
|
|
|
|
#define SPEED_K 1000
|
|
|
|
#define SPEED_B_STR "B/s"
|
|
|
|
#define SPEED_K_STR "kB/s"
|
|
|
|
#define SPEED_M_STR "MB/s"
|
|
|
|
#define SPEED_G_STR "GB/s"
|
|
|
|
#define SPEED_T_STR "TB/s"
|
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
tr_session *
|
|
|
|
libttest_session_init (tr_variant * settings)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2013-02-01 05:57:47 +00:00
|
|
|
size_t len;
|
|
|
|
const char * str;
|
|
|
|
char * sandbox;
|
|
|
|
char * path;
|
|
|
|
tr_quark q;
|
|
|
|
static bool formatters_inited = false;
|
|
|
|
tr_session * session;
|
|
|
|
tr_variant local_settings;
|
|
|
|
|
|
|
|
tr_variantInitDict (&local_settings, 10);
|
|
|
|
|
|
|
|
if (settings == NULL)
|
|
|
|
settings = &local_settings;
|
|
|
|
|
2014-06-08 19:09:41 +00:00
|
|
|
sandbox = libtest_sandbox_create ();
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
if (!formatters_inited)
|
|
|
|
{
|
|
|
|
formatters_inited = true;
|
|
|
|
tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
|
|
|
|
tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
|
|
|
|
tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
|
|
|
|
}
|
2013-01-21 00:00:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
/* download dir */
|
|
|
|
q = TR_KEY_download_dir;
|
|
|
|
if (tr_variantDictFindStr (settings, q, &str, &len))
|
|
|
|
path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str);
|
|
|
|
else
|
|
|
|
path = tr_buildPath (sandbox, "Downloads", NULL);
|
|
|
|
tr_mkdirp (path, 0700);
|
|
|
|
tr_variantDictAddStr (settings, q, path);
|
|
|
|
tr_free (path);
|
|
|
|
|
|
|
|
/* incomplete dir */
|
|
|
|
q = TR_KEY_incomplete_dir;
|
|
|
|
if (tr_variantDictFindStr (settings, q, &str, &len))
|
|
|
|
path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str);
|
|
|
|
else
|
|
|
|
path = tr_buildPath (sandbox, "Incomplete", NULL);
|
|
|
|
tr_variantDictAddStr (settings, q, path);
|
|
|
|
tr_free (path);
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
path = tr_buildPath (sandbox, "blocklists", NULL);
|
|
|
|
tr_mkdirp (path, 0700);
|
|
|
|
tr_free (path);
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
q = TR_KEY_port_forwarding_enabled;
|
|
|
|
if (!tr_variantDictFind (settings, q))
|
|
|
|
tr_variantDictAddBool (settings, q, false);
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
q = TR_KEY_dht_enabled;
|
|
|
|
if (!tr_variantDictFind (settings, q))
|
|
|
|
tr_variantDictAddBool (settings, q, false);
|
|
|
|
|
|
|
|
q = TR_KEY_message_level;
|
|
|
|
if (!tr_variantDictFind (settings, q))
|
|
|
|
tr_variantDictAddInt (settings, q, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR);
|
|
|
|
|
|
|
|
session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, settings);
|
|
|
|
|
|
|
|
tr_free (sandbox);
|
|
|
|
tr_variantFree (&local_settings);
|
|
|
|
return session;
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-01 05:57:47 +00:00
|
|
|
libttest_session_close (tr_session * session)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2014-06-08 19:09:41 +00:00
|
|
|
char * sandbox;
|
2013-02-01 05:57:47 +00:00
|
|
|
|
2014-06-08 19:09:41 +00:00
|
|
|
sandbox = tr_strdup (tr_sessionGetConfigDir (session));
|
2013-01-20 04:41:38 +00:00
|
|
|
tr_sessionClose (session);
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logFreeQueue (tr_logGetQueue ());
|
2013-01-21 21:11:00 +00:00
|
|
|
session = NULL;
|
|
|
|
|
2014-06-08 19:09:41 +00:00
|
|
|
libtest_sandbox_destroy (sandbox);
|
|
|
|
tr_free (sandbox);
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
2013-01-21 00:00:00 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_torrent *
|
2013-02-01 05:57:47 +00:00
|
|
|
libttest_zero_torrent_init (tr_session * session)
|
2013-01-21 00:00:00 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int metainfo_len;
|
|
|
|
char * metainfo;
|
|
|
|
const char * metainfo_base64;
|
|
|
|
tr_torrent * tor;
|
|
|
|
tr_ctor * ctor;
|
|
|
|
|
|
|
|
/*
|
|
|
|
1048576 files-filled-with-zeroes/1048576
|
|
|
|
4096 files-filled-with-zeroes/4096
|
|
|
|
512 files-filled-with-zeroes/512
|
|
|
|
*/
|
|
|
|
metainfo_base64 =
|
|
|
|
"ZDg6YW5ub3VuY2UzMTpodHRwOi8vd3d3LmV4YW1wbGUuY29tL2Fubm91bmNlMTA6Y3JlYXRlZCBi"
|
|
|
|
"eTI1OlRyYW5zbWlzc2lvbi8yLjYxICgxMzQwNykxMzpjcmVhdGlvbiBkYXRlaTEzNTg3MDQwNzVl"
|
|
|
|
"ODplbmNvZGluZzU6VVRGLTg0OmluZm9kNTpmaWxlc2xkNjpsZW5ndGhpMTA0ODU3NmU0OnBhdGhs"
|
|
|
|
"NzoxMDQ4NTc2ZWVkNjpsZW5ndGhpNDA5NmU0OnBhdGhsNDo0MDk2ZWVkNjpsZW5ndGhpNTEyZTQ6"
|
|
|
|
"cGF0aGwzOjUxMmVlZTQ6bmFtZTI0OmZpbGVzLWZpbGxlZC13aXRoLXplcm9lczEyOnBpZWNlIGxl"
|
|
|
|
"bmd0aGkzMjc2OGU2OnBpZWNlczY2MDpRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj"
|
|
|
|
"/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv17"
|
|
|
|
"26aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGEx"
|
|
|
|
"Uv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJ"
|
|
|
|
"tGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GI"
|
|
|
|
"QxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZC"
|
|
|
|
"S1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8K"
|
|
|
|
"T9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9um"
|
|
|
|
"o/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9"
|
|
|
|
"e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRh"
|
|
|
|
"MVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMY"
|
|
|
|
"SbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLOlf5A+Tz30nMBVuNM2hpV3wg/103"
|
|
|
|
"OnByaXZhdGVpMGVlZQ==";
|
|
|
|
|
|
|
|
/* create the torrent ctor */
|
|
|
|
metainfo = tr_base64_decode (metainfo_base64, -1, &metainfo_len);
|
|
|
|
assert (metainfo != NULL);
|
|
|
|
assert (metainfo_len > 0);
|
|
|
|
assert (session != NULL);
|
|
|
|
ctor = tr_ctorNew (session);
|
|
|
|
tr_ctorSetMetainfo (ctor, (uint8_t*)metainfo, metainfo_len);
|
|
|
|
tr_ctorSetPaused (ctor, TR_FORCE, true);
|
|
|
|
|
|
|
|
/* create the torrent */
|
|
|
|
err = 0;
|
2013-05-22 20:35:38 +00:00
|
|
|
tor = tr_torrentNew (ctor, &err, NULL);
|
2013-01-21 00:00:00 +00:00
|
|
|
assert (!err);
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free (metainfo);
|
|
|
|
tr_ctorFree (ctor);
|
|
|
|
return tor;
|
|
|
|
}
|
2013-01-26 23:08:51 +00:00
|
|
|
|
|
|
|
void
|
2013-02-01 05:57:47 +00:00
|
|
|
libttest_zero_torrent_populate (tr_torrent * tor, bool complete)
|
2013-01-26 23:08:51 +00:00
|
|
|
{
|
|
|
|
tr_file_index_t i;
|
|
|
|
|
|
|
|
for (i=0; i<tor->info.fileCount; ++i)
|
|
|
|
{
|
2013-08-17 17:03:15 +00:00
|
|
|
int err;
|
2013-01-26 23:08:51 +00:00
|
|
|
uint64_t j;
|
|
|
|
FILE * fp;
|
|
|
|
char * path;
|
|
|
|
char * dirname;
|
|
|
|
const tr_file * file = &tor->info.files[i];
|
|
|
|
struct stat sb;
|
|
|
|
|
2013-01-31 05:10:08 +00:00
|
|
|
if (!complete && (i==0))
|
|
|
|
path = tr_strdup_printf ("%s%c%s.part", tor->currentDir, TR_PATH_DELIMITER, file->name);
|
|
|
|
else
|
|
|
|
path = tr_strdup_printf ("%s%c%s", tor->currentDir, TR_PATH_DELIMITER, file->name);
|
2013-01-26 23:08:51 +00:00
|
|
|
dirname = tr_dirname (path);
|
|
|
|
tr_mkdirp (dirname, 0700);
|
|
|
|
fp = fopen (path, "wb+");
|
|
|
|
for (j=0; j<file->length; ++j)
|
2013-01-31 05:10:08 +00:00
|
|
|
fputc (((!complete) && (i==0) && (j<tor->info.pieceSize)) ? '\1' : '\0', fp);
|
2013-01-26 23:08:51 +00:00
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
tr_free (dirname);
|
|
|
|
tr_free (path);
|
|
|
|
|
|
|
|
path = tr_torrentFindFile (tor, i);
|
|
|
|
assert (path != NULL);
|
2013-08-17 17:03:15 +00:00
|
|
|
err = errno;
|
|
|
|
errno = 0;
|
|
|
|
stat (path, &sb);
|
|
|
|
assert (errno == 0);
|
|
|
|
errno = err;
|
2013-01-26 23:08:51 +00:00
|
|
|
tr_free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
sync ();
|
2013-01-31 21:58:25 +00:00
|
|
|
libttest_blockingTorrentVerify (tor);
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2013-01-31 05:10:08 +00:00
|
|
|
if (complete)
|
|
|
|
assert (tr_torrentStat(tor)->leftUntilDone == 0);
|
|
|
|
else
|
|
|
|
assert (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
|
2013-01-26 23:08:51 +00:00
|
|
|
}
|
2013-01-31 21:58:25 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
onVerifyDone (tr_torrent * tor UNUSED, bool aborted UNUSED, void * done)
|
|
|
|
{
|
|
|
|
*(bool*)done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libttest_blockingTorrentVerify (tr_torrent * tor)
|
|
|
|
{
|
|
|
|
bool done = false;
|
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
assert (tor->session != NULL);
|
|
|
|
assert (!tr_amInEventThread (tor->session));
|
2013-01-31 21:58:25 +00:00
|
|
|
|
|
|
|
tr_torrentVerify (tor, onVerifyDone, &done);
|
|
|
|
while (!done)
|
|
|
|
tr_wait_msec (10);
|
|
|
|
}
|
2014-06-08 19:09:41 +00:00
|
|
|
|
2014-06-08 20:01:10 +00:00
|
|
|
static void
|
|
|
|
build_parent_dir (const char* path)
|
2014-06-08 19:09:41 +00:00
|
|
|
{
|
|
|
|
char * dir;
|
|
|
|
const int tmperr = errno;
|
|
|
|
|
|
|
|
dir = tr_dirname (path);
|
|
|
|
errno = 0;
|
|
|
|
tr_mkdirp (dir, 0700);
|
|
|
|
assert (errno == 0);
|
|
|
|
tr_free (dir);
|
|
|
|
|
2014-06-08 20:01:10 +00:00
|
|
|
errno = tmperr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libtest_create_file_with_contents (const char* path, const void* payload, size_t n)
|
|
|
|
{
|
|
|
|
FILE * fp;
|
|
|
|
const int tmperr = errno;
|
|
|
|
|
|
|
|
build_parent_dir (path);
|
|
|
|
|
2014-06-08 19:09:41 +00:00
|
|
|
tr_remove (path);
|
|
|
|
fp = fopen (path, "wb");
|
2014-06-08 20:01:10 +00:00
|
|
|
fwrite (payload, 1, n, fp);
|
2014-06-08 19:09:41 +00:00
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
sync ();
|
|
|
|
|
|
|
|
errno = tmperr;
|
|
|
|
}
|
|
|
|
|
2014-06-08 20:01:10 +00:00
|
|
|
void
|
|
|
|
libtest_create_file_with_string_contents (const char * path, const char* str)
|
|
|
|
{
|
|
|
|
libtest_create_file_with_contents (path, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libtest_create_tmpfile_with_contents (char* tmpl, const void* payload, size_t n)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
const int tmperr = errno;
|
|
|
|
size_t n_left = n;
|
|
|
|
|
|
|
|
build_parent_dir (tmpl);
|
|
|
|
|
|
|
|
fd = mkstemp (tmpl);
|
|
|
|
while (n_left > 0)
|
|
|
|
{
|
|
|
|
const ssize_t n = write (fd, payload, n_left);
|
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Error writing '%s': %s", tmpl, tr_strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n_left -= n;
|
|
|
|
}
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
sync ();
|
|
|
|
|
|
|
|
errno = tmperr;
|
|
|
|
}
|