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 #ifndef TORRENT_FILE_STORAGE_HPP_INCLUDED
34 #define TORRENT_FILE_STORAGE_HPP_INCLUDED
40 #pragma warning(push, 1)
43 #include <boost/filesystem/path.hpp>
49 #include "libtorrent/size_type.hpp"
50 #include "libtorrent/assert.hpp"
51 #include "libtorrent/peer_request.hpp"
55 namespace fs
= boost::filesystem
;
57 struct TORRENT_EXPORT file_entry
59 file_entry(): offset(0), size(0), file_base(0) {}
62 size_type offset
; // the offset of this file inside the torrent
63 size_type size
; // the size of this file
64 // the offset in the file where the storage starts.
65 // This is always 0 unless parts of the torrent is
66 // compressed into a single file, such as a so-called part file.
70 struct TORRENT_EXPORT file_slice
77 class TORRENT_EXPORT file_storage
79 friend class torrent_info
;
84 bool is_valid() const { return m_piece_length
> 0; }
86 void add_file(file_entry
const& e
);
87 void add_file(fs::path
const& p
, size_type size
);
88 void rename_file(int index
, std::string
const& new_filename
);
90 std::vector
<file_slice
> map_block(int piece
, size_type offset
92 peer_request
map_file(int file
, size_type offset
, int size
) const;
94 typedef std::vector
<file_entry
>::const_iterator iterator
;
95 typedef std::vector
<file_entry
>::const_reverse_iterator reverse_iterator
;
97 iterator
file_at_offset(size_type offset
) const;
98 iterator
begin() const { return m_files
.begin(); }
99 iterator
end() const { return m_files
.end(); }
100 reverse_iterator
rbegin() const { return m_files
.rbegin(); }
101 reverse_iterator
rend() const { return m_files
.rend(); }
102 int num_files() const
103 { return int(m_files
.size()); }
105 file_entry
const& at(int index
) const
107 TORRENT_ASSERT(index
>= 0 && index
< int(m_files
.size()));
108 return m_files
[index
];
111 size_type
total_size() const { TORRENT_ASSERT(m_piece_length
> 0); return m_total_size
; }
112 void set_num_pieces(int n
) { m_num_pieces
= n
; }
113 int num_pieces() const { TORRENT_ASSERT(m_piece_length
> 0); return m_num_pieces
; }
114 void set_piece_length(int l
) { m_piece_length
= l
; }
115 int piece_length() const { TORRENT_ASSERT(m_piece_length
> 0); return m_piece_length
; }
116 int piece_size(int index
) const;
118 void set_name(std::string
const& n
) { m_name
= n
; }
119 const std::string
& name() const { TORRENT_ASSERT(m_piece_length
> 0); return m_name
; }
121 void swap(file_storage
& ti
)
124 swap(ti
.m_piece_length
, m_piece_length
);
125 swap(ti
.m_files
, m_files
);
126 swap(ti
.m_total_size
, m_total_size
);
127 swap(ti
.m_num_pieces
, m_num_pieces
);
128 swap(ti
.m_name
, m_name
);
134 // the list of files that this torrent consists of
135 std::vector
<file_entry
> m_files
;
137 // the sum of all filesizes
138 size_type m_total_size
;
140 // the number of pieces in the torrent
146 #endif // TORRENT_FILE_STORAGE_HPP_INCLUDED