2006-11-14 14:36:10 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2006, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2006-11-14 14:36:10 +01:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
|
|
|
|
2006-11-15 22:39:58 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2006-11-14 14:36:10 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
using boost::multi_index::nth_index;
|
|
|
|
using boost::multi_index::get;
|
|
|
|
|
|
|
|
boost::shared_ptr<file> file_pool::open_file(void* st, fs::path const& p, file::open_mode m)
|
|
|
|
{
|
|
|
|
assert(st != 0);
|
|
|
|
assert(p.is_complete());
|
2007-04-12 07:08:23 +02:00
|
|
|
boost::mutex::scoped_lock l(m_mutex);
|
2006-11-14 14:36:10 +01:00
|
|
|
typedef nth_index<file_set, 0>::type path_view;
|
|
|
|
path_view& pt = get<0>(m_files);
|
|
|
|
path_view::iterator i = pt.find(p);
|
|
|
|
if (i != pt.end())
|
|
|
|
{
|
|
|
|
lru_file_entry e = *i;
|
2007-04-05 00:27:36 +02:00
|
|
|
e.last_use = time_now();
|
2006-11-14 14:36:10 +01:00
|
|
|
|
2007-04-15 09:15:49 +02:00
|
|
|
if (e.key != st)
|
|
|
|
{
|
|
|
|
// this means that another instance of the storage
|
|
|
|
// is using the exact same file.
|
|
|
|
throw file_error("torrent uses the same file as another torrent "
|
|
|
|
"(" + p.string() + ")");
|
|
|
|
}
|
2006-11-14 14:36:10 +01:00
|
|
|
|
|
|
|
e.key = st;
|
|
|
|
if ((e.mode & m) != m)
|
|
|
|
{
|
|
|
|
// close the file before we open it with
|
|
|
|
// the new read/write privilages
|
|
|
|
i->file_ptr.reset();
|
|
|
|
assert(e.file_ptr.unique());
|
|
|
|
e.file_ptr.reset();
|
|
|
|
e.file_ptr.reset(new file(p, m));
|
|
|
|
e.mode = m;
|
|
|
|
}
|
|
|
|
pt.replace(i, e);
|
|
|
|
return e.file_ptr;
|
|
|
|
}
|
|
|
|
// the file is not in our cache
|
|
|
|
if ((int)m_files.size() >= m_size)
|
|
|
|
{
|
|
|
|
// the file cache is at its maximum size, close
|
|
|
|
// the least recently used (lru) file from it
|
|
|
|
typedef nth_index<file_set, 1>::type lru_view;
|
|
|
|
lru_view& lt = get<1>(m_files);
|
|
|
|
lru_view::iterator i = lt.begin();
|
|
|
|
// the first entry in this view is the least recently used
|
|
|
|
assert(lt.size() == 1 || (i->last_use <= boost::next(i)->last_use));
|
|
|
|
lt.erase(i);
|
|
|
|
}
|
|
|
|
lru_file_entry e(boost::shared_ptr<file>(new file(p, m)));
|
|
|
|
e.mode = m;
|
|
|
|
e.key = st;
|
|
|
|
e.file_path = p;
|
|
|
|
pt.insert(e);
|
|
|
|
return e.file_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_pool::release(void* st)
|
|
|
|
{
|
2007-04-12 07:08:23 +02:00
|
|
|
boost::mutex::scoped_lock l(m_mutex);
|
2006-11-14 14:36:10 +01:00
|
|
|
assert(st != 0);
|
|
|
|
using boost::tie;
|
|
|
|
|
|
|
|
typedef nth_index<file_set, 2>::type key_view;
|
|
|
|
key_view& kt = get<2>(m_files);
|
|
|
|
|
|
|
|
key_view::iterator start, end;
|
|
|
|
tie(start, end) = kt.equal_range(st);
|
|
|
|
kt.erase(start, end);
|
|
|
|
}
|
|
|
|
|
2006-11-14 16:53:38 +01:00
|
|
|
void file_pool::resize(int size)
|
|
|
|
{
|
|
|
|
assert(size > 0);
|
|
|
|
if (size == m_size) return;
|
2007-04-12 07:08:23 +02:00
|
|
|
boost::mutex::scoped_lock l(m_mutex);
|
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
|
|
|
|
typedef nth_index<file_set, 1>::type lru_view;
|
|
|
|
lru_view& lt = get<1>(m_files);
|
|
|
|
lru_view::iterator i = lt.begin();
|
|
|
|
while (int(m_files.size()) > m_size)
|
|
|
|
{
|
|
|
|
// the first entry in this view is the least recently used
|
|
|
|
assert(lt.size() == 1 || (i->last_use <= boost::next(i)->last_use));
|
|
|
|
lt.erase(i++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-14 14:36:10 +01:00
|
|
|
}
|