2006-11-14 14:36:10 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2006-2018, Arvid Norberg
|
2006-11-14 14:36:10 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2009-11-25 07:55:34 +01:00
|
|
|
#include "libtorrent/assert.hpp"
|
2006-11-14 14:36:10 +01:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
2008-07-18 01:41:46 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2016-10-25 23:27:48 +02:00
|
|
|
#include "libtorrent/file_storage.hpp"
|
2016-12-22 16:42:33 +01:00
|
|
|
#include "libtorrent/units.hpp"
|
2017-04-09 00:24:50 +02:00
|
|
|
#include "libtorrent/disk_interface.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2016-12-28 19:12:20 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
#include "libtorrent/aux_/win_util.hpp"
|
|
|
|
#endif
|
2006-11-14 14:36:10 +01:00
|
|
|
|
2016-08-13 03:31:55 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2017-01-01 17:08:57 +01:00
|
|
|
file_pool::file_pool(int size) : m_size(size) {}
|
2016-07-10 13:34:45 +02:00
|
|
|
file_pool::~file_pool() = default;
|
2011-08-07 06:41:10 +02:00
|
|
|
|
2013-01-07 02:56:40 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2014-07-06 21:18:00 +02:00
|
|
|
void set_low_priority(file_handle const& f)
|
2013-01-07 02:56:40 +01:00
|
|
|
{
|
|
|
|
// file prio is only supported on vista and up
|
|
|
|
// so load the functions dynamically
|
2018-03-22 17:01:38 +01:00
|
|
|
enum FILE_INFO_BY_HANDLE_CLASS_LOCAL {
|
2013-01-07 02:56:40 +01:00
|
|
|
FileBasicInfo,
|
|
|
|
FileStandardInfo,
|
|
|
|
FileNameInfo,
|
|
|
|
FileRenameInfo,
|
|
|
|
FileDispositionInfo,
|
|
|
|
FileAllocationInfo,
|
|
|
|
FileEndOfFileInfo,
|
|
|
|
FileStreamInfo,
|
|
|
|
FileCompressionInfo,
|
|
|
|
FileAttributeTagInfo,
|
|
|
|
FileIdBothDirectoryInfo,
|
|
|
|
FileIdBothDirectoryRestartInfo,
|
|
|
|
FileIoPriorityHintInfo,
|
2015-08-18 16:42:03 +02:00
|
|
|
FileRemoteProtocolInfo,
|
2013-01-07 02:56:40 +01:00
|
|
|
MaximumFileInfoByHandleClass
|
2018-03-22 17:01:38 +01:00
|
|
|
};
|
2013-01-07 02:56:40 +01:00
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
enum PRIORITY_HINT_LOCAL {
|
2013-01-07 02:56:40 +01:00
|
|
|
IoPriorityHintVeryLow = 0,
|
|
|
|
IoPriorityHintLow,
|
|
|
|
IoPriorityHintNormal,
|
|
|
|
MaximumIoPriorityHintType
|
2018-03-22 17:01:38 +01:00
|
|
|
};
|
2013-01-07 02:56:40 +01:00
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
struct FILE_IO_PRIORITY_HINT_INFO_LOCAL {
|
2016-04-17 22:56:07 +02:00
|
|
|
PRIORITY_HINT_LOCAL PriorityHint;
|
2018-03-22 17:01:38 +01:00
|
|
|
};
|
2013-01-07 02:56:40 +01:00
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
using SetFileInformationByHandle_t = BOOL (WINAPI *)(HANDLE
|
|
|
|
, FILE_INFO_BY_HANDLE_CLASS_LOCAL, LPVOID, DWORD);
|
2016-12-28 19:12:20 +01:00
|
|
|
auto SetFileInformationByHandle =
|
2018-03-22 17:01:38 +01:00
|
|
|
aux::get_library_procedure<aux::kernel32, SetFileInformationByHandle_t>(
|
|
|
|
"SetFileInformationByHandle");
|
2016-12-28 19:12:20 +01:00
|
|
|
|
|
|
|
if (SetFileInformationByHandle == nullptr) return;
|
2013-01-07 02:56:40 +01:00
|
|
|
|
2016-04-17 22:56:07 +02:00
|
|
|
FILE_IO_PRIORITY_HINT_INFO_LOCAL io_hint;
|
2013-01-07 02:56:40 +01:00
|
|
|
io_hint.PriorityHint = IoPriorityHintLow;
|
|
|
|
SetFileInformationByHandle(f->native_handle(),
|
|
|
|
FileIoPriorityHintInfo, &io_hint, sizeof(io_hint));
|
|
|
|
}
|
|
|
|
#endif // TORRENT_WINDOWS
|
|
|
|
|
2016-12-31 18:35:10 +01:00
|
|
|
file_handle file_pool::open_file(storage_index_t st, std::string const& p
|
2017-04-09 00:24:50 +02:00
|
|
|
, file_index_t const file_index, file_storage const& fs
|
2017-05-26 20:49:21 +02:00
|
|
|
, open_mode_t const m, error_code& ec)
|
2006-11-14 14:36:10 +01:00
|
|
|
{
|
2015-02-08 15:44:33 +01:00
|
|
|
// potentially used to hold a reference to a file object that's
|
|
|
|
// about to be destructed. If we have such object we assign it to
|
2016-05-01 00:54:23 +02:00
|
|
|
// this member to be destructed after we release the std::mutex. On some
|
2015-02-08 15:44:33 +01:00
|
|
|
// operating systems (such as OSX) closing a file may take a long
|
2016-05-01 00:54:23 +02:00
|
|
|
// time. We don't want to hold the std::mutex for that.
|
2015-02-08 15:44:33 +01:00
|
|
|
file_handle defer_destruction;
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
std::unique_lock<std::mutex> l(m_mutex);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
TORRENT_ASSERT(is_complete(p));
|
2017-07-15 02:59:20 +02:00
|
|
|
TORRENT_ASSERT((m & open_mode::rw_mask) == open_mode::read_only
|
|
|
|
|| (m & open_mode::rw_mask) == open_mode::read_write);
|
2016-10-25 23:27:48 +02:00
|
|
|
auto const i = m_files.find(std::make_pair(st, file_index));
|
2009-05-09 23:11:25 +02:00
|
|
|
if (i != m_files.end())
|
2006-11-14 14:36:10 +01:00
|
|
|
{
|
2009-05-09 23:11:25 +02:00
|
|
|
lru_file_entry& e = i->second;
|
2015-03-12 05:34:54 +01:00
|
|
|
e.last_use = aux::time_now();
|
2006-11-14 14:36:10 +01:00
|
|
|
|
2008-10-19 07:03:17 +02:00
|
|
|
// if we asked for a file in write mode,
|
|
|
|
// and the cached file is is not opened in
|
|
|
|
// write mode, re-open it
|
2017-07-15 02:59:20 +02:00
|
|
|
if ((((e.mode & open_mode::rw_mask) != open_mode::read_write)
|
|
|
|
&& ((m & open_mode::rw_mask) == open_mode::read_write))
|
|
|
|
|| (e.mode & open_mode::random_access) != (m & open_mode::random_access))
|
2006-11-14 14:36:10 +01:00
|
|
|
{
|
2017-01-01 17:08:57 +01:00
|
|
|
file_handle new_file = std::make_shared<file>();
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2013-08-12 09:30:57 +02:00
|
|
|
std::string full_path = fs.file_path(file_index, p);
|
2017-01-01 17:08:57 +01:00
|
|
|
if (!new_file->open(full_path, m, ec))
|
2014-07-06 21:18:00 +02:00
|
|
|
return file_handle();
|
2010-01-23 04:02:32 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_low_prio_io)
|
2017-01-01 17:08:57 +01:00
|
|
|
set_low_priority(new_file);
|
2010-01-23 04:02:32 +01:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-01-01 17:08:57 +01:00
|
|
|
TORRENT_ASSERT(new_file->is_open());
|
|
|
|
defer_destruction = std::move(e.file_ptr);
|
|
|
|
e.file_ptr = std::move(new_file);
|
2006-11-14 14:36:10 +01:00
|
|
|
e.mode = m;
|
|
|
|
}
|
|
|
|
return e.file_ptr;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-02-25 05:41:21 +01:00
|
|
|
lru_file_entry e;
|
2016-09-01 03:42:18 +02:00
|
|
|
e.file_ptr = std::make_shared<file>();
|
2008-02-25 05:41:21 +01:00
|
|
|
if (!e.file_ptr)
|
|
|
|
{
|
2015-11-22 19:12:11 +01:00
|
|
|
ec = error_code(boost::system::errc::not_enough_memory, generic_category());
|
2017-01-01 17:08:57 +01:00
|
|
|
return file_handle();
|
2008-02-25 05:41:21 +01:00
|
|
|
}
|
2013-08-12 09:30:57 +02:00
|
|
|
std::string full_path = fs.file_path(file_index, p);
|
2010-08-23 10:48:02 +02:00
|
|
|
if (!e.file_ptr->open(full_path, m, ec))
|
2014-07-06 21:18:00 +02:00
|
|
|
return file_handle();
|
2013-01-07 02:56:40 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (m_low_prio_io)
|
|
|
|
set_low_priority(e.file_ptr);
|
|
|
|
#endif
|
2006-11-14 14:36:10 +01:00
|
|
|
e.mode = m;
|
2014-07-06 21:18:00 +02:00
|
|
|
file_handle file_ptr = e.file_ptr;
|
2016-12-31 23:28:01 +01:00
|
|
|
m_files.insert(std::make_pair(std::make_pair(st, file_index), e));
|
|
|
|
TORRENT_ASSERT(file_ptr->is_open());
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-08-18 16:42:03 +02:00
|
|
|
if (int(m_files.size()) >= m_size)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
// the file cache is at its maximum size, close
|
|
|
|
// the least recently used (lru) file from it
|
2017-04-14 18:42:05 +02:00
|
|
|
defer_destruction = remove_oldest(l);
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
return file_ptr;
|
2006-11-14 14:36:10 +01:00
|
|
|
}
|
|
|
|
|
2017-04-09 00:24:50 +02:00
|
|
|
namespace {
|
|
|
|
|
2017-07-23 03:59:55 +02:00
|
|
|
file_open_mode_t to_file_open_mode(open_mode_t const mode)
|
2017-04-09 00:24:50 +02:00
|
|
|
{
|
2017-07-23 03:59:55 +02:00
|
|
|
file_open_mode_t ret;
|
2017-07-15 02:59:20 +02:00
|
|
|
open_mode_t const rw_mode = mode & open_mode::rw_mask;
|
2017-05-26 20:49:21 +02:00
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
ret = (rw_mode == open_mode::read_only)
|
2017-05-26 20:49:21 +02:00
|
|
|
? file_open_mode::read_only
|
2017-07-15 02:59:20 +02:00
|
|
|
: (rw_mode == open_mode::write_only)
|
2017-05-26 20:49:21 +02:00
|
|
|
? file_open_mode::write_only
|
2017-07-15 02:59:20 +02:00
|
|
|
: (rw_mode == open_mode::read_write)
|
2017-05-26 20:49:21 +02:00
|
|
|
? file_open_mode::read_write
|
2017-07-23 03:59:55 +02:00
|
|
|
: file_open_mode_t{};
|
2017-05-26 20:49:21 +02:00
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
if (mode & open_mode::sparse) ret |= file_open_mode::sparse;
|
|
|
|
if (mode & open_mode::no_atime) ret |= file_open_mode::no_atime;
|
|
|
|
if (mode & open_mode::random_access) ret |= file_open_mode::random_access;
|
2017-04-09 00:24:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<open_file_state> file_pool::get_status(storage_index_t const st) const
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2017-04-09 00:24:50 +02:00
|
|
|
std::vector<open_file_state> ret;
|
2016-08-14 19:11:59 +02:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_mutex);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
auto const start = m_files.lower_bound(std::make_pair(st, file_index_t(0)));
|
2016-10-25 23:27:48 +02:00
|
|
|
auto const end = m_files.upper_bound(std::make_pair(st
|
2016-12-22 16:42:33 +01:00
|
|
|
, std::numeric_limits<file_index_t>::max()));
|
2015-08-18 16:42:03 +02:00
|
|
|
|
2016-10-25 23:27:48 +02:00
|
|
|
for (auto i = start; i != end; ++i)
|
2017-04-09 00:24:50 +02:00
|
|
|
{
|
|
|
|
ret.push_back({i->first.second, to_file_open_mode(i->second.mode)
|
|
|
|
, i->second.last_use});
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2016-08-14 19:11:59 +02:00
|
|
|
return ret;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 07:21:31 +02:00
|
|
|
file_handle file_pool::remove_oldest(std::unique_lock<std::mutex>&)
|
2009-05-09 23:11:25 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
using value_type = decltype(m_files)::value_type;
|
2016-10-25 23:27:48 +02:00
|
|
|
auto const i = std::min_element(m_files.begin(), m_files.end()
|
|
|
|
, [] (value_type const& lhs, value_type const& rhs)
|
2016-05-25 06:31:52 +02:00
|
|
|
{ return lhs.second.last_use < rhs.second.last_use; });
|
2017-04-14 18:42:05 +02:00
|
|
|
if (i == m_files.end()) return file_handle();
|
2011-08-07 06:41:10 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
file_handle file_ptr = i->second.file_ptr;
|
2009-05-09 23:11:25 +02:00
|
|
|
m_files.erase(i);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// closing a file may be long running operation (mac os x)
|
2017-04-14 18:42:05 +02:00
|
|
|
// let the calling function destruct it after releasing the mutex
|
|
|
|
return file_ptr;
|
2009-05-09 23:11:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 18:35:10 +01:00
|
|
|
void file_pool::release(storage_index_t const st, file_index_t file_index)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::unique_lock<std::mutex> l(m_mutex);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-10-25 23:27:48 +02:00
|
|
|
auto const i = m_files.find(std::make_pair(st, file_index));
|
2011-08-07 06:41:10 +02:00
|
|
|
if (i == m_files.end()) return;
|
2016-05-25 06:31:52 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
file_handle file_ptr = i->second.file_ptr;
|
2011-08-07 06:41:10 +02:00
|
|
|
m_files.erase(i);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-31 23:28:01 +01:00
|
|
|
// closing a file may take a long time (mac os x), so make sure
|
|
|
|
// we're not holding the mutex
|
2014-07-06 21:18:00 +02:00
|
|
|
l.unlock();
|
|
|
|
file_ptr.reset();
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
|
2010-01-23 04:02:32 +01:00
|
|
|
// closes files belonging to the specified
|
2016-12-31 18:35:10 +01:00
|
|
|
// storage, or all if none is specified.
|
|
|
|
void file_pool::release()
|
2006-11-14 14:36:10 +01:00
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::unique_lock<std::mutex> l(m_mutex);
|
2016-12-31 18:35:10 +01:00
|
|
|
m_files.clear();
|
|
|
|
l.unlock();
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-31 18:35:10 +01:00
|
|
|
void file_pool::release(storage_index_t const st)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_mutex);
|
2006-11-14 14:36:10 +01:00
|
|
|
|
2017-03-12 16:34:42 +01:00
|
|
|
auto const begin = m_files.lower_bound(std::make_pair(st, file_index_t(0)));
|
2017-01-01 17:08:57 +01:00
|
|
|
auto const end = m_files.upper_bound(std::make_pair(st
|
|
|
|
, std::numeric_limits<file_index_t>::max()));
|
2016-12-31 23:28:01 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<file_handle> to_close;
|
2017-03-12 16:34:42 +01:00
|
|
|
for (auto it = begin; it != end; ++it)
|
|
|
|
to_close.push_back(std::move(it->second.file_ptr));
|
|
|
|
if (!to_close.empty()) m_files.erase(begin, end);
|
2014-07-06 21:18:00 +02:00
|
|
|
l.unlock();
|
2016-12-31 23:28:01 +01:00
|
|
|
// the files are closed here while the lock is not held
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2006-11-14 16:53:38 +01:00
|
|
|
void file_pool::resize(int size)
|
|
|
|
{
|
2017-04-14 18:42:05 +02:00
|
|
|
// these are destructed _after_ the mutex is released
|
|
|
|
std::vector<file_handle> defer_destruction;
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
std::unique_lock<std::mutex> l(m_mutex);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(size > 0);
|
2014-01-06 04:50:25 +01:00
|
|
|
|
|
|
|
if (size == m_size) return;
|
2006-11-14 16:53:38 +01:00
|
|
|
m_size = size;
|
|
|
|
if (int(m_files.size()) <= m_size) return;
|
|
|
|
|
|
|
|
// close the least recently used files
|
|
|
|
while (int(m_files.size()) > m_size)
|
2017-04-14 18:42:05 +02:00
|
|
|
defer_destruction.push_back(remove_oldest(l));
|
2006-11-14 16:53:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 00:06:02 +01:00
|
|
|
void file_pool::close_oldest()
|
|
|
|
{
|
2017-03-09 00:01:59 +01:00
|
|
|
std::unique_lock<std::mutex> l(m_mutex);
|
2017-03-01 00:06:02 +01:00
|
|
|
|
2017-03-09 00:01:59 +01:00
|
|
|
using value_type = decltype(m_files)::value_type;
|
|
|
|
auto const i = std::min_element(m_files.begin(), m_files.end()
|
|
|
|
, [] (value_type const& lhs, value_type const& rhs)
|
|
|
|
{ return lhs.second.opened < rhs.second.opened; });
|
2017-03-01 00:06:02 +01:00
|
|
|
if (i == m_files.end()) return;
|
|
|
|
|
|
|
|
file_handle file_ptr = i->second.file_ptr;
|
|
|
|
m_files.erase(i);
|
|
|
|
|
|
|
|
// closing a file may be long running operation (mac os x)
|
|
|
|
l.unlock();
|
|
|
|
file_ptr.reset();
|
|
|
|
l.lock();
|
|
|
|
}
|
2006-11-14 14:36:10 +01:00
|
|
|
}
|