2008-05-28 10:44:40 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003-2008, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_FILE_STORAGE_HPP_INCLUDED
|
|
|
|
#define TORRENT_FILE_STORAGE_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2009-10-20 18:44:11 +02:00
|
|
|
#include <ctime>
|
2008-05-28 10:44:40 +02:00
|
|
|
|
|
|
|
#include "libtorrent/size_type.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/peer_request.hpp"
|
2010-03-27 16:51:30 +01:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
2010-08-22 00:10:16 +02:00
|
|
|
#include "libtorrent/copy_ptr.hpp"
|
2008-05-28 10:44:40 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2010-08-23 10:48:02 +02:00
|
|
|
struct file;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
struct TORRENT_EXPORT file_entry
|
|
|
|
{
|
2010-08-23 10:48:02 +02:00
|
|
|
file_entry(): offset(0), size(0), file_base(0), file_index(0)
|
2009-07-01 10:35:45 +02:00
|
|
|
, mtime(0), pad_file(false), hidden_attribute(false)
|
|
|
|
, executable_attribute(false)
|
|
|
|
, symlink_attribute(false)
|
|
|
|
{}
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string path;
|
2010-08-22 00:10:16 +02:00
|
|
|
std::string symlink_path;
|
|
|
|
copy_ptr<sha1_hash> filehash;
|
2010-08-23 10:48:02 +02:00
|
|
|
// the offset of this file inside the torrent
|
|
|
|
size_type offset;
|
|
|
|
// the size of this file
|
|
|
|
size_type size;
|
2008-05-28 10:44:40 +02:00
|
|
|
// the offset in the file where the storage starts.
|
|
|
|
// This is always 0 unless parts of the torrent is
|
|
|
|
// compressed into a single file, such as a so-called part file.
|
|
|
|
size_type file_base;
|
2010-08-23 10:48:02 +02:00
|
|
|
// the index of this file, as ordered in the torrent
|
|
|
|
int file_index;
|
2010-08-22 00:10:16 +02:00
|
|
|
time_t mtime;
|
2008-10-03 07:49:41 +02:00
|
|
|
bool pad_file:1;
|
2009-01-11 23:27:43 +01:00
|
|
|
bool hidden_attribute:1;
|
|
|
|
bool executable_attribute:1;
|
2009-07-01 10:35:45 +02:00
|
|
|
bool symlink_attribute:1;
|
2008-05-28 10:44:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TORRENT_EXPORT file_slice
|
|
|
|
{
|
|
|
|
int file_index;
|
|
|
|
size_type offset;
|
|
|
|
size_type size;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TORRENT_EXPORT file_storage
|
|
|
|
{
|
|
|
|
friend class torrent_info;
|
|
|
|
public:
|
|
|
|
file_storage();
|
|
|
|
~file_storage() {}
|
|
|
|
|
|
|
|
bool is_valid() const { return m_piece_length > 0; }
|
|
|
|
|
2009-01-11 23:27:43 +01:00
|
|
|
enum flags_t
|
|
|
|
{
|
|
|
|
pad_file = 1,
|
|
|
|
attribute_hidden = 2,
|
2009-07-01 10:35:45 +02:00
|
|
|
attribute_executable = 4,
|
|
|
|
attribute_symlink = 8
|
2009-01-11 23:27:43 +01:00
|
|
|
};
|
|
|
|
|
2009-11-26 07:54:52 +01:00
|
|
|
void reserve(int num_files);
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
void add_file(file_entry const& e);
|
2009-10-26 02:29:39 +01:00
|
|
|
void add_file(std::string const& p, size_type size, int flags = 0
|
|
|
|
, std::time_t mtime = 0, std::string const& s_p = "");
|
2008-05-28 10:44:40 +02:00
|
|
|
void rename_file(int index, std::string const& new_filename);
|
2009-03-31 10:12:35 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
|
|
|
void add_file(std::wstring const& p, size_type size, int flags = 0
|
|
|
|
, std::time_t mtime = 0, std::string const& s_p = "");
|
2008-11-30 09:12:26 +01:00
|
|
|
void rename_file(int index, std::wstring const& new_filename);
|
2009-03-31 10:12:35 +02:00
|
|
|
void set_name(std::wstring const& n);
|
|
|
|
#endif
|
2008-05-28 10:44:40 +02:00
|
|
|
|
|
|
|
std::vector<file_slice> map_block(int piece, size_type offset
|
|
|
|
, int size) const;
|
|
|
|
peer_request map_file(int file, size_type offset, int size) const;
|
|
|
|
|
|
|
|
typedef std::vector<file_entry>::const_iterator iterator;
|
|
|
|
typedef std::vector<file_entry>::const_reverse_iterator reverse_iterator;
|
|
|
|
|
2008-07-09 12:45:07 +02:00
|
|
|
iterator file_at_offset(size_type offset) const;
|
2008-05-28 10:44:40 +02:00
|
|
|
iterator begin() const { return m_files.begin(); }
|
|
|
|
iterator end() const { return m_files.end(); }
|
|
|
|
reverse_iterator rbegin() const { return m_files.rbegin(); }
|
|
|
|
reverse_iterator rend() const { return m_files.rend(); }
|
|
|
|
int num_files() const
|
|
|
|
{ return int(m_files.size()); }
|
|
|
|
|
|
|
|
file_entry const& at(int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0 && index < int(m_files.size()));
|
|
|
|
return m_files[index];
|
|
|
|
}
|
|
|
|
|
2009-01-11 11:32:57 +01:00
|
|
|
size_type total_size() const { return m_total_size; }
|
2008-05-28 10:44:40 +02:00
|
|
|
void set_num_pieces(int n) { m_num_pieces = n; }
|
|
|
|
int num_pieces() const { TORRENT_ASSERT(m_piece_length > 0); return m_num_pieces; }
|
|
|
|
void set_piece_length(int l) { m_piece_length = l; }
|
|
|
|
int piece_length() const { TORRENT_ASSERT(m_piece_length > 0); return m_piece_length; }
|
|
|
|
int piece_size(int index) const;
|
|
|
|
|
|
|
|
void set_name(std::string const& n) { m_name = n; }
|
2010-03-29 02:31:30 +02:00
|
|
|
const std::string& name() const { return m_name; }
|
2008-05-28 10:44:40 +02:00
|
|
|
|
|
|
|
void swap(file_storage& ti)
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap(ti.m_piece_length, m_piece_length);
|
|
|
|
swap(ti.m_files, m_files);
|
|
|
|
swap(ti.m_total_size, m_total_size);
|
|
|
|
swap(ti.m_num_pieces, m_num_pieces);
|
|
|
|
swap(ti.m_name, m_name);
|
|
|
|
}
|
|
|
|
|
2009-01-11 11:32:57 +01:00
|
|
|
// if pad_file_limit >= 0, files larger than
|
|
|
|
// that limit will be padded, default is to
|
|
|
|
// not add any padding
|
|
|
|
void optimize(int pad_file_limit = -1);
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
private:
|
|
|
|
// the list of files that this torrent consists of
|
|
|
|
std::vector<file_entry> m_files;
|
|
|
|
|
2010-08-22 00:10:16 +02:00
|
|
|
std::string m_name;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
// the sum of all filesizes
|
|
|
|
size_type m_total_size;
|
|
|
|
|
|
|
|
// the number of pieces in the torrent
|
|
|
|
int m_num_pieces;
|
2010-08-22 00:10:16 +02:00
|
|
|
|
|
|
|
int m_piece_length;
|
2008-05-28 10:44:40 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_FILE_STORAGE_HPP_INCLUDED
|
|
|
|
|