3 Copyright (c) 2003-2008, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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"
40 file_storage::file_storage()
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());
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
)
79 std::vector
<file_slice
> file_storage::map_block(int piece
, size_type offset
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
;
95 for (file_iter
= begin();; ++counter
, ++file_iter
)
97 TORRENT_ASSERT(file_iter
!= end());
98 if (file_offset
< file_iter
->size
)
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
);
105 file_offset
+= f
.size
;
109 TORRENT_ASSERT(size
>= 0);
110 if (size
<= 0) break;
112 file_offset
-= file_iter
->size
;
117 peer_request
file_storage::map_file(int file_index
, size_type file_offset
120 TORRENT_ASSERT(file_index
< num_files());
121 TORRENT_ASSERT(file_index
>= 0);
122 size_type offset
= file_offset
+ at(file_index
).offset
;
125 ret
.piece
= int(offset
/ piece_length());
126 ret
.start
= int(offset
- ret
.piece
* piece_length());
131 void file_storage::add_file(fs::path
const& file
, size_type size
)
133 TORRENT_ASSERT(size
>= 0);
134 if (!file
.has_branch_path())
136 // you have already added at least one file with a
137 // path to the file (branch_path), which means that
138 // all the other files need to be in the same top
139 // directory as the first file.
140 TORRENT_ASSERT(m_files
.empty());
141 m_name
= file
.string();
146 m_name
= *file
.begin();
148 TORRENT_ASSERT(m_name
== *file
.begin());
150 m_files
.push_back(e
);
151 m_files
.back().size
= size
;
152 m_files
.back().path
= file
;
153 m_files
.back().offset
= m_total_size
;
154 m_total_size
+= size
;
157 void file_storage::add_file(file_entry
const& e
)
159 add_file(e
.path
, e
.size
);