factor out has_any_file to storage_utils

This commit is contained in:
arvidn 2017-06-23 15:04:32 -04:00 committed by Arvid Norberg
parent b7642f75d5
commit ed79929528
3 changed files with 40 additions and 19 deletions

View File

@ -95,6 +95,14 @@ namespace aux {
, stat_cache& stat
, std::string const& save_path
, storage_error& ec);
// given the save_path, stat all files on file_storage until one exists. If a
// file exists, return true, otherwise return false.
TORRENT_EXTRA_EXPORT bool has_any_file(
file_storage const& fs
, std::string const& save_path
, stat_cache& stat
, storage_error& ec);
}}
#endif

View File

@ -293,27 +293,11 @@ namespace libtorrent {
{
m_stat_cache.reserve(files().num_files());
file_storage const& fs = files();
for (file_index_t i(0); i < fs.end_file(); ++i)
{
std::int64_t const sz = m_stat_cache.get_filesize(
i, files(), m_save_path, ec.ec);
if (aux::has_any_file(files(), m_save_path, m_stat_cache, ec))
return true;
if (sz < 0)
{
if (ec && ec.ec != boost::system::errc::no_such_file_or_directory)
{
ec.file(i);
ec.operation = operation_t::file_stat;
m_stat_cache.clear();
return false;
}
// some files not existing is expected and not an error
ec.ec.clear();
}
if (ec) return false;
if (sz > 0) return true;
}
file_status s;
stat_file(combine_path(m_save_path, m_part_file_name), &s, ec.ec);
if (!ec) return true;

View File

@ -566,4 +566,33 @@ namespace libtorrent { namespace aux {
return true;
}
bool has_any_file(
file_storage const& fs
, std::string const& save_path
, stat_cache& cache
, storage_error& ec)
{
for (file_index_t i(0); i < fs.end_file(); ++i)
{
std::int64_t const sz = cache.get_filesize(
i, fs, save_path, ec.ec);
if (sz < 0)
{
if (ec && ec.ec != boost::system::errc::no_such_file_or_directory)
{
ec.file(i);
ec.operation = operation_t::file_stat;
cache.clear();
return false;
}
// some files not existing is expected and not an error
ec.ec.clear();
}
if (sz > 0) return true;
}
return false;
}
}}