From d181b2e057769ca7c9e127dc48f90e3b2763c4c6 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 30 Sep 2012 20:55:12 +0000 Subject: [PATCH] merge pad_file_at optimization from libtorrent_aio --- docs/make_torrent.rst | 30 ++++++++++++++++++----------- include/libtorrent/file_storage.hpp | 3 ++- src/file_storage.cpp | 17 ++++++++++------ src/torrent.cpp | 13 ++++++------- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/docs/make_torrent.rst b/docs/make_torrent.rst index 445d63345..45fc1070e 100644 --- a/docs/make_torrent.rst +++ b/docs/make_torrent.rst @@ -177,12 +177,12 @@ file structure. Its synopsis:: sha1_hash const& hash(int index) const; std::string const& symlink(int index) const; time_t mtime(int index) const; - int file_index(int index) const; size_type file_base(int index) const; void set_file_base(int index, size_type off); std::string file_path(int index) const; std::string file_name(int index) const; size_type file_size(int index) const; + bool pad_file_at(int index) const; size_type file_offset(int index) const; // iterator accessors @@ -195,6 +195,7 @@ file structure. Its synopsis:: std::string file_path(internal_file_entry const& fe) const; std::string file_name(internal_file_entry const& fe) const; size_type file_size(internal_file_entry const& fe) const; + bool pad_file_at(internal_file_entry const& fe) const; size_type file_offset(internal_file_entry const& fe) const; void set_name(std::string const& n); @@ -231,28 +232,35 @@ can be changed by calling ``set_name``. The built in functions to traverse a directory to add files will make sure this requirement is fulfilled. -hash() symlink() mtime() file_index() -------------------------------------- +hash() symlink() mtime() file_path() file_size() pad_file_at() +-------------------------------------------------------------- :: sha1_hash hash(int index) const; std::string const& symlink(int index) const; time_t mtime(int index) const; - int file_index(int index) const; + std::string file_path(int index) const; + size_type file_size(int index) const; + bool pad_file_at(int index) const; -These functions are used to query the symlink, file hash, -modification time and the file-index from a file index. +These functions are used to query attributes of files at +a given index. -The file hash is a sha-1 hash of the file, or 0 if none was +The ``file_hash()`` is a sha-1 hash of the file, or 0 if none was provided in the torrent file. This can potentially be used to join a bittorrent network with other file sharing networks. -The modification time is the posix time when a file was last -modified when the torrent was created, or 0 if it was not provided. +The ``mtime()`` is the modification time is the posix +time when a file was last modified when the torrent +was created, or 0 if it was not included in the torrent file. -The file index of a file is simply a 0 based index of the -file as they are ordered in the torrent file. +``file_path()`` returns the full path to a file. + +``file_size()`` returns the size of a file. + +``pad_file_at()`` returns true if the file at the given +index is a pad-file. file_base() set_file_base() --------------------------- diff --git a/include/libtorrent/file_storage.hpp b/include/libtorrent/file_storage.hpp index 1037da567..ada289ceb 100644 --- a/include/libtorrent/file_storage.hpp +++ b/include/libtorrent/file_storage.hpp @@ -249,12 +249,12 @@ namespace libtorrent sha1_hash hash(int index) const; std::string const& symlink(int index) const; time_t mtime(int index) const; - int file_index(int index) const; size_type file_base(int index) const; void set_file_base(int index, size_type off); std::string file_path(int index) const; std::string file_name(int index) const; size_type file_size(int index) const; + bool pad_file_at(int index) const; size_type file_offset(int index) const; sha1_hash hash(internal_file_entry const& fe) const; @@ -266,6 +266,7 @@ namespace libtorrent std::string file_path(internal_file_entry const& fe) const; std::string file_name(internal_file_entry const& fe) const; size_type file_size(internal_file_entry const& fe) const; + bool pad_file_at(internal_file_entry const& fe) const; size_type file_offset(internal_file_entry const& fe) const; #if !defined TORRENT_VERBOSE_LOGGING \ diff --git a/src/file_storage.cpp b/src/file_storage.cpp index c853ebc89..acaa4f345 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -398,12 +398,6 @@ namespace libtorrent return m_mtime[index]; } - int file_storage::file_index(int index) const - { - TORRENT_ASSERT(index >= 0 && index < int(m_files.size())); - return index; - } - void file_storage::set_file_base(int index, size_type off) { TORRENT_ASSERT(index >= 0 && index < int(m_files.size())); @@ -439,6 +433,12 @@ namespace libtorrent return m_files[index].size; } + bool file_storage::pad_file_at(int index) const + { + TORRENT_ASSERT(index >= 0 && index < int(m_files.size())); + return m_files[index].pad_file; + } + size_type file_storage::file_offset(int index) const { TORRENT_ASSERT(index >= 0 && index < int(m_files.size())); @@ -504,6 +504,11 @@ namespace libtorrent return fe.size; } + bool file_storage::pad_file_at(internal_file_entry const& fe) const + { + return fe.pad_file; + } + size_type file_storage::file_offset(internal_file_entry const& fe) const { return fe.offset; diff --git a/src/torrent.cpp b/src/torrent.cpp index 2e38b0823..873445404 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2849,8 +2849,7 @@ namespace libtorrent for (std::vector::iterator i = files.begin() , end(files.end()); i != end; ++i) { - file_entry const& fe = fs.at(i->file_index); - if (fe.pad_file) continue; + if (fs.pad_file_at(i->file_index)) continue; ret += i->size; } TORRENT_ASSERT(ret <= (std::min)(piece_size - offset, int(block_size()))); @@ -3176,11 +3175,11 @@ namespace libtorrent m_file_progress[file_index] += add; TORRENT_ASSERT(m_file_progress[file_index] - <= m_torrent_file->files().at(file_index).size); + <= m_torrent_file->files().file_size(file_index)); - if (m_file_progress[file_index] >= m_torrent_file->files().at(file_index).size) + if (m_file_progress[file_index] >= m_torrent_file->files().file_size(file_index)) { - if (!m_torrent_file->files().at(file_index).pad_file) + if (!m_torrent_file->files().pad_file_at(file_index)) { // don't finalize files if we discover that they exist // in whole (i.e. while checking). In that case, just assume @@ -4181,7 +4180,7 @@ namespace libtorrent for (int i = 0; i < (int)bitmask.size(); ++i) { size_type start = position; - position += m_torrent_file->files().at(i).size; + position += m_torrent_file->files().file_size(i); // is the file selected for download? if (!bitmask[i]) { @@ -6597,7 +6596,7 @@ namespace libtorrent , end(m_file_progress.end()); i != end; ++i) { int index = i - m_file_progress.begin(); - TORRENT_ASSERT(*i <= m_torrent_file->files().at(index).size); + TORRENT_ASSERT(*i <= m_torrent_file->files().file_size(index)); } } #endif