remove restrictions on pad_file_limit and alignment when creating torrents. This makes the torrent creator more flexible to create all-padded files for instance
This commit is contained in:
parent
b79dd1fee1
commit
735916ad44
|
@ -166,11 +166,11 @@ namespace libtorrent
|
||||||
// The ``flags`` arguments specifies options for the torrent creation. It can
|
// The ``flags`` arguments specifies options for the torrent creation. It can
|
||||||
// be any combination of the flags defined by create_torrent::flags_t.
|
// be any combination of the flags defined by create_torrent::flags_t.
|
||||||
//
|
//
|
||||||
// ``alignment`` is used when pad files are enabled. This is the size eligible
|
// ``alignment`` is used when pad files are enabled. This is the size
|
||||||
// files are aligned to. The default is the default bittorrent block size of
|
// eligible files are aligned to. The default is -1, which means the
|
||||||
// 16 kiB. It is common to align to the piece size of the torrent.
|
// piece size of the torrent.
|
||||||
create_torrent(file_storage& fs, int piece_size = 0
|
create_torrent(file_storage& fs, int piece_size = 0
|
||||||
, int pad_file_limit = -1, int flags = optimize, int alignment = 0x4000);
|
, int pad_file_limit = -1, int flags = optimize, int alignment = -1);
|
||||||
create_torrent(torrent_info const& ti);
|
create_torrent(torrent_info const& ti);
|
||||||
|
|
||||||
// internal
|
// internal
|
||||||
|
|
|
@ -402,10 +402,13 @@ namespace libtorrent
|
||||||
swap(ti.m_piece_length, m_piece_length);
|
swap(ti.m_piece_length, m_piece_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if pad_file_limit >= 0, files larger than
|
// if pad_file_limit >= 0, files larger than that limit will be padded,
|
||||||
// that limit will be padded, default is to
|
// default is to not add any padding (-1). The alignment specifies the
|
||||||
// not add any padding
|
// alignment files should be padded to. This defaults to the piece size
|
||||||
void optimize(int pad_file_limit = -1, int alignment = 0x10000);
|
// (-1) but it may also make sense to set it to 16 kiB, or something
|
||||||
|
// divisible by 16 kiB.
|
||||||
|
// If pad_file_limit is 0, every file will be padded (except empty ones).
|
||||||
|
void optimize(int pad_file_limit = -1, int alignment = -1);
|
||||||
|
|
||||||
// These functions are used to query attributes of files at
|
// These functions are used to query attributes of files at
|
||||||
// a given index.
|
// a given index.
|
||||||
|
|
|
@ -264,7 +264,8 @@ namespace libtorrent
|
||||||
|
|
||||||
create_torrent::~create_torrent() {}
|
create_torrent::~create_torrent() {}
|
||||||
|
|
||||||
create_torrent::create_torrent(file_storage& fs, int piece_size, int pad_file_limit, int flags, int alignment)
|
create_torrent::create_torrent(file_storage& fs, int piece_size
|
||||||
|
, int pad_file_limit, int flags, int alignment)
|
||||||
: m_files(fs)
|
: m_files(fs)
|
||||||
, m_creation_date(time(0))
|
, m_creation_date(time(0))
|
||||||
, m_multifile(fs.num_files() > 1)
|
, m_multifile(fs.num_files() > 1)
|
||||||
|
|
|
@ -681,23 +681,15 @@ namespace libtorrent
|
||||||
|
|
||||||
void file_storage::optimize(int pad_file_limit, int alignment)
|
void file_storage::optimize(int pad_file_limit, int alignment)
|
||||||
{
|
{
|
||||||
// it doesn't make any sense to pad files that
|
if (alignment == -1)
|
||||||
// are smaller than one block
|
alignment = m_piece_length;
|
||||||
if (pad_file_limit >= 0 && pad_file_limit < 0x4000)
|
|
||||||
pad_file_limit = 0x4000;
|
|
||||||
|
|
||||||
// also, it doesn't make any sense to pad files
|
|
||||||
// that are smaller than the alignment, since they
|
|
||||||
// won't get aligned anyway; they are used as padding
|
|
||||||
if (pad_file_limit >= 0 && pad_file_limit < alignment)
|
|
||||||
pad_file_limit = alignment;
|
|
||||||
|
|
||||||
size_type off = 0;
|
size_type off = 0;
|
||||||
int padding_file = 0;
|
int padding_file = 0;
|
||||||
for (std::vector<internal_file_entry>::iterator i = m_files.begin();
|
for (std::vector<internal_file_entry>::iterator i = m_files.begin();
|
||||||
i != m_files.end(); ++i)
|
i != m_files.end(); ++i)
|
||||||
{
|
{
|
||||||
if ((off & (alignment-1)) == 0)
|
if ((off % alignment) == 0)
|
||||||
{
|
{
|
||||||
// this file position is aligned, pick the largest
|
// this file position is aligned, pick the largest
|
||||||
// available file to put here
|
// available file to put here
|
||||||
|
@ -721,32 +713,38 @@ namespace libtorrent
|
||||||
// not piece-aligned and the file size exceeds the
|
// not piece-aligned and the file size exceeds the
|
||||||
// limit, and it's not a padding file itself.
|
// limit, and it's not a padding file itself.
|
||||||
// so add a padding file in front of it
|
// so add a padding file in front of it
|
||||||
int pad_size = alignment - (off & (alignment-1));
|
int pad_size = alignment - (off % alignment);
|
||||||
|
|
||||||
// find the largest file that fits in pad_size
|
// find the largest file that fits in pad_size
|
||||||
std::vector<internal_file_entry>::iterator best_match = m_files.end();
|
std::vector<internal_file_entry>::iterator best_match = m_files.end();
|
||||||
for (std::vector<internal_file_entry>::iterator j = i+1; j < m_files.end(); ++j)
|
|
||||||
{
|
|
||||||
if (j->size > pad_size) continue;
|
|
||||||
if (best_match == m_files.end() || j->size > best_match->size)
|
|
||||||
best_match = j;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (best_match != m_files.end())
|
// if pad_file_limit is 0, it means all files are padded, there's
|
||||||
|
// no point in trying to find smaller files to use as filling
|
||||||
|
if (pad_file_limit > 0)
|
||||||
{
|
{
|
||||||
// we found one
|
for (std::vector<internal_file_entry>::iterator j = i+1; j < m_files.end(); ++j)
|
||||||
// We cannot have found i, because i->size > pad_file_limit
|
{
|
||||||
// which is forced to be no less than alignment. We only
|
if (j->size > pad_size) continue;
|
||||||
// look for files <= pad_size, which never is greater than
|
if (best_match == m_files.end() || j->size > best_match->size)
|
||||||
// alignment
|
best_match = j;
|
||||||
TORRENT_ASSERT(best_match != i);
|
}
|
||||||
int index = best_match - m_files.begin();
|
|
||||||
int cur_index = i - m_files.begin();
|
if (best_match != m_files.end())
|
||||||
reorder_file(index, cur_index);
|
{
|
||||||
i = m_files.begin() + cur_index;
|
// we found one
|
||||||
i->offset = off;
|
// We cannot have found i, because i->size > pad_file_limit
|
||||||
off += i->size;
|
// which is forced to be no less than alignment. We only
|
||||||
continue;
|
// look for files <= pad_size, which never is greater than
|
||||||
|
// alignment
|
||||||
|
TORRENT_ASSERT(best_match != i);
|
||||||
|
int index = best_match - m_files.begin();
|
||||||
|
int cur_index = i - m_files.begin();
|
||||||
|
reorder_file(index, cur_index);
|
||||||
|
i = m_files.begin() + cur_index;
|
||||||
|
i->offset = off;
|
||||||
|
off += i->size;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// we could not find a file that fits in pad_size
|
// we could not find a file that fits in pad_size
|
||||||
|
@ -780,7 +778,7 @@ namespace libtorrent
|
||||||
|
|
||||||
reorder_file(index, cur_index);
|
reorder_file(index, cur_index);
|
||||||
|
|
||||||
TORRENT_ASSERT((off & (alignment-1)) == 0);
|
TORRENT_ASSERT((off % alignment) == 0);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
i->offset = off;
|
i->offset = off;
|
||||||
|
|
Loading…
Reference in New Issue