added missing file error_code.cpp
[libtorrent.git] / src / file_pool.cpp
blob2d3ea88cd929a83756c6053649652984ac7e1523
1 /*
3 Copyright (c) 2006, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include <boost/version.hpp>
34 #include "libtorrent/pch.hpp"
35 #include "libtorrent/file_pool.hpp"
36 #include "libtorrent/error_code.hpp"
38 #include <iostream>
40 namespace libtorrent
42 using boost::multi_index::nth_index;
43 using boost::multi_index::get;
45 boost::shared_ptr<file> file_pool::open_file(void* st, fs::path const& p
46 , file::open_mode m, error_code& ec)
48 TORRENT_ASSERT(st != 0);
49 TORRENT_ASSERT(p.is_complete());
50 TORRENT_ASSERT(m == file::in || m == (file::in | file::out));
51 boost::mutex::scoped_lock l(m_mutex);
52 typedef nth_index<file_set, 0>::type path_view;
53 path_view& pt = get<0>(m_files);
54 path_view::iterator i = pt.find(p);
55 if (i != pt.end())
57 lru_file_entry e = *i;
58 e.last_use = time_now();
60 if (e.key != st)
62 // this means that another instance of the storage
63 // is using the exact same file.
64 #if BOOST_VERSION >= 103500
65 ec = error_code(errors::file_collision, libtorrent_category);
66 #endif
67 return boost::shared_ptr<file>();
70 e.key = st;
71 if ((e.mode & m) != m)
73 // close the file before we open it with
74 // the new read/write privilages
75 i->file_ptr.reset();
76 TORRENT_ASSERT(e.file_ptr.unique());
77 e.file_ptr->close();
78 if (!e.file_ptr->open(p, m, ec))
80 m_files.erase(i);
81 return boost::shared_ptr<file>();
83 e.mode = m;
85 pt.replace(i, e);
86 return e.file_ptr;
88 // the file is not in our cache
89 if ((int)m_files.size() >= m_size)
91 // the file cache is at its maximum size, close
92 // the least recently used (lru) file from it
93 typedef nth_index<file_set, 1>::type lru_view;
94 lru_view& lt = get<1>(m_files);
95 lru_view::iterator i = lt.begin();
96 // the first entry in this view is the least recently used
97 TORRENT_ASSERT(lt.size() == 1 || (i->last_use <= boost::next(i)->last_use));
98 lt.erase(i);
100 lru_file_entry e;
101 e.file_ptr.reset(new (std::nothrow)file);
102 if (!e.file_ptr)
104 ec = error_code(ENOMEM, get_posix_category());
105 return e.file_ptr;
107 if (!e.file_ptr->open(p, m, ec))
108 return boost::shared_ptr<file>();
109 e.mode = m;
110 e.key = st;
111 e.file_path = p;
112 pt.insert(e);
113 return e.file_ptr;
116 void file_pool::release(fs::path const& p)
118 boost::mutex::scoped_lock l(m_mutex);
120 typedef nth_index<file_set, 0>::type path_view;
121 path_view& pt = get<0>(m_files);
122 path_view::iterator i = pt.find(p);
123 if (i != pt.end()) pt.erase(i);
126 void file_pool::release(void* st)
128 boost::mutex::scoped_lock l(m_mutex);
129 TORRENT_ASSERT(st != 0);
130 using boost::tie;
132 typedef nth_index<file_set, 2>::type key_view;
133 key_view& kt = get<2>(m_files);
135 key_view::iterator start, end;
136 tie(start, end) = kt.equal_range(st);
137 kt.erase(start, end);
140 void file_pool::resize(int size)
142 TORRENT_ASSERT(size > 0);
143 if (size == m_size) return;
144 boost::mutex::scoped_lock l(m_mutex);
145 m_size = size;
146 if (int(m_files.size()) <= m_size) return;
148 // close the least recently used files
149 typedef nth_index<file_set, 1>::type lru_view;
150 lru_view& lt = get<1>(m_files);
151 lru_view::iterator i = lt.begin();
152 while (int(m_files.size()) > m_size)
154 // the first entry in this view is the least recently used
155 TORRENT_ASSERT(lt.size() == 1 || (i->last_use <= boost::next(i)->last_use));
156 lt.erase(i++);