00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef TORRENT_STORAGE_HPP_INCLUDE
00034 #define TORRENT_STORAGE_HPP_INCLUDE
00035
00036 #include <vector>
00037 #include <bitset>
00038
00039 #ifdef _MSC_VER
00040 #pragma warning(push, 1)
00041 #endif
00042
00043 #include <boost/limits.hpp>
00044 #include <boost/filesystem/path.hpp>
00045 #include <boost/thread.hpp>
00046 #include <boost/shared_ptr.hpp>
00047
00048 #ifdef _MSC_VER
00049 #pragma warning(pop)
00050 #endif
00051
00052
00053 #include "libtorrent/torrent_info.hpp"
00054 #include "libtorrent/config.hpp"
00055
00056 namespace libtorrent
00057 {
00058 namespace aux
00059 {
00060 struct piece_checker_data;
00061 }
00062
00063 class session;
00064 struct file_pool;
00065
00066 #if defined(_WIN32) && defined(UNICODE)
00067
00068 TORRENT_EXPORT std::wstring safe_convert(std::string const& s);
00069
00070 #endif
00071
00072 TORRENT_EXPORT std::vector<std::pair<size_type, std::time_t> > get_filesizes(
00073 torrent_info const& t
00074 , boost::filesystem::path p);
00075
00076 TORRENT_EXPORT bool match_filesizes(
00077 torrent_info const& t
00078 , boost::filesystem::path p
00079 , std::vector<std::pair<size_type, std::time_t> > const& sizes
00080 , std::string* error = 0);
00081
00082 struct TORRENT_EXPORT file_allocation_failed: std::exception
00083 {
00084 file_allocation_failed(const char* error_msg): m_msg(error_msg) {}
00085 virtual const char* what() const throw() { return m_msg.c_str(); }
00086 virtual ~file_allocation_failed() throw() {}
00087 std::string m_msg;
00088 };
00089
00090 class TORRENT_EXPORT storage
00091 {
00092 public:
00093 storage(
00094 const torrent_info& info
00095 , const boost::filesystem::path& path
00096 , file_pool& fp);
00097
00098 void swap(storage&);
00099
00100
00101 size_type read(char* buf, int slot, int offset, int size);
00102
00103
00104 void write(const char* buf, int slot, int offset, int size);
00105
00106 bool move_storage(boost::filesystem::path save_path);
00107
00108
00109
00110
00111 void release_files();
00112
00113 #ifndef NDEBUG
00114
00115
00116 void shuffle();
00117 #endif
00118
00119 private:
00120 class impl;
00121 boost::shared_ptr<impl> m_pimpl;
00122 };
00123
00124 class TORRENT_EXPORT piece_manager : boost::noncopyable
00125 {
00126 public:
00127
00128 piece_manager(
00129 const torrent_info& info
00130 , const boost::filesystem::path& path
00131 , file_pool& fp);
00132
00133 ~piece_manager();
00134
00135 bool check_fastresume(aux::piece_checker_data& d
00136 , std::vector<bool>& pieces, int& num_pieces, bool compact_mode);
00137 std::pair<bool, float> check_files(std::vector<bool>& pieces
00138 , int& num_pieces, boost::recursive_mutex& mutex);
00139
00140 void release_files();
00141
00142 bool is_allocating() const;
00143 void allocate_slots(int num_slots);
00144 void mark_failed(int index);
00145
00146 unsigned long piece_crc(
00147 int slot_index
00148 , int block_size
00149 , const std::bitset<256>& bitmask);
00150 int slot_for_piece(int piece_index) const;
00151
00152 size_type read(
00153 char* buf
00154 , int piece_index
00155 , int offset
00156 , int size);
00157
00158 void write(
00159 const char* buf
00160 , int piece_index
00161 , int offset
00162 , int size);
00163
00164 boost::filesystem::path const& save_path() const;
00165 bool move_storage(boost::filesystem::path const&);
00166
00167
00168
00169
00170
00171 void export_piece_map(std::vector<int>& pieces) const;
00172
00173 private:
00174 class impl;
00175 std::auto_ptr<impl> m_pimpl;
00176 };
00177
00178 }
00179
00180 #endif // TORRENT_STORAGE_HPP_INCLUDED
00181