fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / include / libtorrent / file_storage.hpp
blob47fcb92ebf00bb169c9132616fd1a7afaeaa15b4
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 #ifndef TORRENT_FILE_STORAGE_HPP_INCLUDED
34 #define TORRENT_FILE_STORAGE_HPP_INCLUDED
36 #include <string>
37 #include <vector>
39 #ifdef _MSC_VER
40 #pragma warning(push, 1)
41 #endif
43 #include <boost/filesystem/path.hpp>
45 #ifdef _MSC_VER
46 #pragma warning(pop)
47 #endif
49 #include "libtorrent/size_type.hpp"
50 #include "libtorrent/assert.hpp"
51 #include "libtorrent/peer_request.hpp"
53 namespace libtorrent
55 namespace fs = boost::filesystem;
57 struct TORRENT_EXPORT file_entry
59 file_entry(): offset(0), size(0), file_base(0) {}
61 fs::path path;
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.
67 size_type file_base;
70 struct TORRENT_EXPORT file_slice
72 int file_index;
73 size_type offset;
74 size_type size;
77 class TORRENT_EXPORT file_storage
79 friend class torrent_info;
80 public:
81 file_storage();
82 ~file_storage() {}
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
91 , int size) const;
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)
123 using std::swap;
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);
131 private:
132 int m_piece_length;
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
141 int m_num_pieces;
142 std::string m_name;
146 #endif // TORRENT_FILE_STORAGE_HPP_INCLUDED