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-26 23:08:51 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h> /* remove() */
|
|
|
|
#include <string.h> /* strcmp() */
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "cache.h"
|
2014-09-21 17:55:39 +00:00
|
|
|
#include "file.h"
|
2013-01-26 23:08:51 +00:00
|
|
|
#include "resume.h"
|
2013-01-31 17:39:06 +00:00
|
|
|
#include "trevent.h"
|
2013-01-26 23:08:51 +00:00
|
|
|
#include "torrent.h" /* tr_isTorrent() */
|
|
|
|
#include "variant.h"
|
|
|
|
|
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
zeroes_completeness_func (tr_torrent * torrent UNUSED,
|
|
|
|
tr_completeness completeness,
|
|
|
|
bool wasRunning UNUSED,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
*(tr_completeness*)user_data = completeness;
|
|
|
|
}
|
|
|
|
|
2013-01-27 06:20:39 +00:00
|
|
|
#define check_file_location(tor, i, expected_path) \
|
|
|
|
do { \
|
|
|
|
char * path = tr_torrentFindFile (tor, i); \
|
|
|
|
char * expected = expected_path; \
|
|
|
|
check_streq (expected, path); \
|
|
|
|
tr_free (expected); \
|
|
|
|
tr_free (path); \
|
|
|
|
} while (0)
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
struct test_incomplete_dir_data
|
2013-01-31 17:39:06 +00:00
|
|
|
{
|
2013-02-01 05:57:47 +00:00
|
|
|
tr_session * session;
|
2013-01-31 17:39:06 +00:00
|
|
|
tr_torrent * tor;
|
|
|
|
tr_block_index_t block;
|
|
|
|
tr_piece_index_t pieceIndex;
|
|
|
|
uint32_t offset;
|
|
|
|
struct evbuffer * buf;
|
|
|
|
bool done;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2013-02-01 05:57:47 +00:00
|
|
|
test_incomplete_dir_threadfunc (void * vdata)
|
2013-01-31 17:39:06 +00:00
|
|
|
{
|
2013-02-01 05:57:47 +00:00
|
|
|
struct test_incomplete_dir_data * data = vdata;
|
|
|
|
tr_cacheWriteBlock (data->session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf);
|
2013-01-31 17:39:06 +00:00
|
|
|
tr_torrentGotBlock (data->tor, data->block);
|
|
|
|
data->done = true;
|
|
|
|
}
|
|
|
|
|
2013-01-26 23:08:51 +00:00
|
|
|
static int
|
2013-02-01 05:57:47 +00:00
|
|
|
test_incomplete_dir_impl (const char * incomplete_dir, const char * download_dir)
|
2013-01-26 23:08:51 +00:00
|
|
|
{
|
2013-01-31 17:39:06 +00:00
|
|
|
size_t i;
|
2013-02-01 05:57:47 +00:00
|
|
|
tr_session * session;
|
2013-01-26 23:08:51 +00:00
|
|
|
tr_torrent * tor;
|
|
|
|
tr_completeness completeness;
|
2013-01-27 06:20:39 +00:00
|
|
|
const tr_completeness completeness_unset = -1;
|
2014-12-07 10:10:16 +00:00
|
|
|
const time_t deadline = time(NULL) + 300;
|
2013-02-01 05:57:47 +00:00
|
|
|
tr_variant settings;
|
2013-01-26 23:08:51 +00:00
|
|
|
|
|
|
|
/* init the session */
|
2013-02-01 05:57:47 +00:00
|
|
|
tr_variantInitDict (&settings, 3);
|
|
|
|
tr_variantDictAddStr (&settings, TR_KEY_download_dir, download_dir);
|
|
|
|
tr_variantDictAddStr (&settings, TR_KEY_incomplete_dir, incomplete_dir);
|
|
|
|
tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, true);
|
|
|
|
session = libttest_session_init (&settings);
|
|
|
|
tr_variantFree (&settings);
|
|
|
|
download_dir = tr_sessionGetDownloadDir (session);
|
|
|
|
incomplete_dir = tr_sessionGetIncompleteDir (session);
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2013-01-27 06:20:39 +00:00
|
|
|
/* init an incomplete torrent.
|
|
|
|
the test zero_torrent will be missing its first piece */
|
2013-02-01 05:57:47 +00:00
|
|
|
tor = libttest_zero_torrent_init (session);
|
|
|
|
libttest_zero_torrent_populate (tor, false);
|
2013-01-26 23:08:51 +00:00
|
|
|
check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
|
2013-01-27 06:20:39 +00:00
|
|
|
check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name));
|
|
|
|
check_file_location (tor, 1, tr_buildPath(incomplete_dir, tor->info.files[1].name, NULL));
|
2013-01-26 23:08:51 +00:00
|
|
|
check_int_eq (tor->info.pieceSize, tr_torrentStat(tor)->leftUntilDone);
|
|
|
|
|
2013-01-31 17:39:06 +00:00
|
|
|
completeness = completeness_unset;
|
|
|
|
tr_torrentSetCompletenessCallback (tor, zeroes_completeness_func, &completeness);
|
|
|
|
|
2013-01-26 23:08:51 +00:00
|
|
|
/* now finish writing it */
|
|
|
|
{
|
2013-01-31 17:39:06 +00:00
|
|
|
tr_block_index_t first, last;
|
2013-01-27 06:20:39 +00:00
|
|
|
char * zero_block = tr_new0 (char, tor->blockSize);
|
2013-02-01 05:57:47 +00:00
|
|
|
struct test_incomplete_dir_data data;
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
data.session = session;
|
2013-01-31 17:39:06 +00:00
|
|
|
data.tor = tor;
|
|
|
|
data.pieceIndex = 0;
|
|
|
|
data.buf = evbuffer_new ();
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2013-01-31 17:39:06 +00:00
|
|
|
tr_torGetPieceBlockRange (tor, data.pieceIndex, &first, &last);
|
|
|
|
for (i=first; i<=last; ++i)
|
2013-01-26 23:08:51 +00:00
|
|
|
{
|
2013-01-31 17:39:06 +00:00
|
|
|
evbuffer_add (data.buf, zero_block, tor->blockSize);
|
|
|
|
data.block = i;
|
|
|
|
data.done = false;
|
|
|
|
data.offset = data.block * tor->blockSize;
|
2013-02-01 05:57:47 +00:00
|
|
|
tr_runInEventThread (session, test_incomplete_dir_threadfunc, &data);
|
2013-01-31 17:39:06 +00:00
|
|
|
do { tr_wait_msec(50); } while (!data.done);
|
2013-01-26 23:08:51 +00:00
|
|
|
}
|
2013-01-27 06:20:39 +00:00
|
|
|
|
2013-01-31 17:39:06 +00:00
|
|
|
evbuffer_free (data.buf);
|
2013-01-27 06:20:39 +00:00
|
|
|
tr_free (zero_block);
|
2013-01-26 23:08:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 21:58:25 +00:00
|
|
|
libttest_blockingTorrentVerify (tor);
|
2013-01-31 17:39:06 +00:00
|
|
|
check_int_eq (0, tr_torrentStat(tor)->leftUntilDone);
|
|
|
|
|
|
|
|
while ((completeness==completeness_unset) && (time(NULL)<=deadline))
|
|
|
|
tr_wait_msec (50);
|
|
|
|
|
|
|
|
check_int_eq (TR_SEED, completeness);
|
|
|
|
for (i=0; i<tor->info.fileCount; ++i)
|
2013-02-01 05:57:47 +00:00
|
|
|
check_file_location (tor, i, tr_buildPath (download_dir, tor->info.files[i].name, NULL));
|
2013-01-26 23:08:51 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_torrentRemove (tor, true, remove);
|
2013-02-01 05:57:47 +00:00
|
|
|
libttest_session_close (session);
|
2013-01-26 23:08:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
static int
|
|
|
|
test_incomplete_dir (void)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* test what happens when incompleteDir is a subdir of downloadDir*/
|
|
|
|
if ((rv = test_incomplete_dir_impl ("Downloads/Incomplete", "Downloads")))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/* test what happens when downloadDir is a subdir of incompleteDir */
|
|
|
|
if ((rv = test_incomplete_dir_impl ("Downloads", "Downloads/Complete")))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/* test what happens when downloadDir and incompleteDir are siblings */
|
|
|
|
if ((rv = test_incomplete_dir_impl ("Incomplete", "Downloads")))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-26 23:08:51 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-02-06 05:51:51 +00:00
|
|
|
static int
|
|
|
|
test_set_location (void)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int state;
|
|
|
|
char * target_dir;
|
|
|
|
tr_torrent * tor;
|
|
|
|
tr_session * session;
|
2014-12-07 10:10:16 +00:00
|
|
|
const time_t deadline = time(NULL) + 300;
|
2013-02-06 05:51:51 +00:00
|
|
|
|
|
|
|
/* init the session */
|
|
|
|
session = libttest_session_init (NULL);
|
|
|
|
target_dir = tr_buildPath (tr_sessionGetConfigDir (session), "target", NULL);
|
2014-09-21 17:55:39 +00:00
|
|
|
tr_sys_dir_create (target_dir, TR_SYS_DIR_CREATE_PARENTS, 0777, NULL);
|
2013-02-06 05:51:51 +00:00
|
|
|
|
|
|
|
/* init a torrent. */
|
|
|
|
tor = libttest_zero_torrent_init (session);
|
|
|
|
libttest_zero_torrent_populate (tor, true);
|
|
|
|
libttest_blockingTorrentVerify (tor);
|
|
|
|
check_int_eq (0, tr_torrentStat(tor)->leftUntilDone);
|
|
|
|
|
|
|
|
/* now move it */
|
|
|
|
state = -1;
|
|
|
|
tr_torrentSetLocation (tor, target_dir, true, NULL, &state);
|
|
|
|
while ((state==TR_LOC_MOVING) && (time(NULL)<=deadline))
|
|
|
|
tr_wait_msec (50);
|
|
|
|
check_int_eq (TR_LOC_DONE, state);
|
|
|
|
|
|
|
|
/* confirm the torrent is still complete after being moved */
|
|
|
|
libttest_blockingTorrentVerify (tor);
|
|
|
|
check_int_eq (0, tr_torrentStat(tor)->leftUntilDone);
|
|
|
|
|
|
|
|
/* confirm the filest really got moved */
|
2014-12-13 15:22:39 +00:00
|
|
|
libttest_sync ();
|
2013-02-06 05:51:51 +00:00
|
|
|
for (i=0; i<tor->info.fileCount; ++i)
|
|
|
|
check_file_location (tor, i, tr_buildPath (target_dir, tor->info.files[i].name, NULL));
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free (target_dir);
|
|
|
|
tr_torrentRemove (tor, true, remove);
|
|
|
|
libttest_session_close (session);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-01-26 23:08:51 +00:00
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
2013-02-06 05:51:51 +00:00
|
|
|
const testFunc tests[] = { test_incomplete_dir,
|
|
|
|
test_set_location };
|
2013-01-26 23:08:51 +00:00
|
|
|
|
|
|
|
return runTests (tests, NUM_TESTS (tests));
|
|
|
|
}
|
|
|
|
|
|
|
|
|