fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / src / file_storage.cpp
blobdd732bef1cd3a6888c2ef03bb67106912df15d5c
1 /*
3 Copyright (c) 2003-2008, 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 "libtorrent/pch.hpp"
35 #include "libtorrent/file_storage.hpp"
38 namespace libtorrent
40 file_storage::file_storage()
41 : m_piece_length(0)
42 , m_total_size(0)
43 , m_num_pieces(0)
46 int file_storage::piece_size(int index) const
48 TORRENT_ASSERT(index >= 0 && index < num_pieces());
49 if (index == num_pieces()-1)
51 int size = int(total_size()
52 - size_type(num_pieces() - 1) * piece_length());
53 TORRENT_ASSERT(size > 0);
54 TORRENT_ASSERT(size <= piece_length());
55 return int(size);
57 else
58 return piece_length();
61 void file_storage::rename_file(int index, std::string const& new_filename)
63 TORRENT_ASSERT(index >= 0 && index < int(m_files.size()));
64 m_files[index].path = new_filename;
67 file_storage::iterator file_storage::file_at_offset(size_type offset) const
69 // TODO: do a binary search
70 std::vector<file_entry>::const_iterator i;
71 for (i = begin(); i != end(); ++i)
73 if (i->offset <= offset && i->offset + i->size > offset)
74 return i;
76 return i;
79 std::vector<file_slice> file_storage::map_block(int piece, size_type offset
80 , int size_) const
82 TORRENT_ASSERT(num_files() > 0);
83 std::vector<file_slice> ret;
85 size_type start = piece * (size_type)m_piece_length + offset;
86 size_type size = size_;
87 TORRENT_ASSERT(start + size <= m_total_size);
89 // find the file iterator and file offset
90 // TODO: do a binary search on the file offsets
91 size_type file_offset = start;
92 std::vector<file_entry>::const_iterator file_iter;
94 int counter = 0;
95 for (file_iter = begin();; ++counter, ++file_iter)
97 TORRENT_ASSERT(file_iter != end());
98 if (file_offset < file_iter->size)
100 file_slice f;
101 f.file_index = counter;
102 f.offset = file_offset + file_iter->file_base;
103 f.size = (std::min)(file_iter->size - file_offset, (size_type)size);
104 size -= f.size;
105 file_offset += f.size;
106 ret.push_back(f);
109 TORRENT_ASSERT(size >= 0);
110 if (size <= 0) break;
112 file_offset -= file_iter->size;
114 return ret;
117 peer_request file_storage::map_file(int file_index, size_type file_offset
118 , int size) const
120 TORRENT_ASSERT(file_index < num_files());
121 TORRENT_ASSERT(file_index >= 0);
122 size_type offset = file_offset + at(file_index).offset;
124 peer_request ret;
125 ret.piece = int(offset / piece_length());
126 ret.start = int(offset - ret.piece * piece_length());
127 ret.length = size;
128 return ret;
131 void file_storage::add_file(fs::path const& file, size_type size)
133 TORRENT_ASSERT(size >= 0);
134 #if BOOST_VERSION < 103600
135 if (!file.has_branch_path())
136 #else
137 if (!file.has_parent_path())
138 #endif
140 // you have already added at least one file with a
141 // path to the file (branch_path), which means that
142 // all the other files need to be in the same top
143 // directory as the first file.
144 TORRENT_ASSERT(m_files.empty());
145 m_name = file.string();
147 else
149 if (m_files.empty())
150 m_name = *file.begin();
152 TORRENT_ASSERT(m_name == *file.begin());
153 file_entry e;
154 m_files.push_back(e);
155 m_files.back().size = size;
156 m_files.back().path = file;
157 m_files.back().offset = m_total_size;
158 m_total_size += size;
161 void file_storage::add_file(file_entry const& e)
163 add_file(e.path, e.size);