Skip to content

Commit

Permalink
Refactor tr_torrentFindFile2 (#921)
Browse files Browse the repository at this point in the history
* Factor-out file seek in tr_torrentFindFile2.

* Update libtransmission/torrent.c

Co-Authored-By: RobCrowston <crowston@protonmail.com>

* Fix code style.

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
Co-authored-by: Charles Kerr <ckerr@github.com>
  • Loading branch information
3 people committed Apr 27, 2020
1 parent def7634 commit 6bb8b2e
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions libtransmission/torrent.c
Expand Up @@ -3478,6 +3478,20 @@ void tr_torrentGotBlock(tr_torrent* tor, tr_block_index_t block)
****
***/

static void find_file_in_dir(char const* name, char const* search_dir, char const** base, char const** subpath,
tr_sys_path_info* file_info)
{
char* filename = tr_buildPath(search_dir, name, NULL);

if (tr_sys_path_get_info(filename, 0, file_info, NULL))
{
*base = search_dir;
*subpath = name;
}

tr_free(filename);
}

bool tr_torrentFindFile2(tr_torrent const* tor, tr_file_index_t fileNum, char const** base, char** subpath, time_t* mtime)
{
TR_ASSERT(tr_isTorrent(tor));
Expand All @@ -3494,29 +3508,13 @@ bool tr_torrentFindFile2(tr_torrent const* tor, tr_file_index_t fileNum, char co
/* look in the download dir... */
if (b == NULL)
{
char* filename = tr_buildPath(tor->downloadDir, file->name, NULL);

if (tr_sys_path_get_info(filename, 0, &file_info, NULL))
{
b = tor->downloadDir;
s = file->name;
}

tr_free(filename);
find_file_in_dir(file->name, tor->downloadDir, &b, &s, &file_info);
}

/* look in the incomplete dir... */
if (b == NULL && tor->incompleteDir != NULL)
{
char* filename = tr_buildPath(tor->incompleteDir, file->name, NULL);

if (tr_sys_path_get_info(filename, 0, &file_info, NULL))
{
b = tor->incompleteDir;
s = file->name;
}

tr_free(filename);
find_file_in_dir(file->name, tor->incompleteDir, &b, &s, &file_info);
}

if (b == NULL)
Expand All @@ -3527,29 +3525,13 @@ bool tr_torrentFindFile2(tr_torrent const* tor, tr_file_index_t fileNum, char co
/* look for a .part file in the incomplete dir... */
if (b == NULL && tor->incompleteDir != NULL)
{
char* filename = tr_buildPath(tor->incompleteDir, part, NULL);

if (tr_sys_path_get_info(filename, 0, &file_info, NULL))
{
b = tor->incompleteDir;
s = part;
}

tr_free(filename);
find_file_in_dir(part, tor->incompleteDir, &b, &s, &file_info);
}

/* look for a .part file in the download dir... */
if (b == NULL)
{
char* filename = tr_buildPath(tor->downloadDir, part, NULL);

if (tr_sys_path_get_info(filename, 0, &file_info, NULL))
{
b = tor->downloadDir;
s = part;
}

tr_free(filename);
find_file_in_dir(part, tor->downloadDir, &b, &s, &file_info);
}

/* return the results */
Expand Down

0 comments on commit 6bb8b2e

Please sign in to comment.