📄 file_list.cc
字号:
// libTorrent - BitTorrent library// Copyright (C) 2005-2006, Jari Sundell//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// // This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// // You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//// In addition, as a special exception, the copyright holders give// permission to link the code of portions of this program with the// OpenSSL library under certain conditions as described in each// individual source file, and distribute linked combinations// including the two.//// You must obey the GNU General Public License in all respects for// all of the code used other than OpenSSL. If you modify file(s)// with this exception, you may extend this exception to your version// of the file(s), but you are not obligated to do so. If you do not// wish to do so, delete this exception statement from your version.// If you delete this exception statement from all source files in the// program, then also delete it here.//// Contact: Jari Sundell <jaris@ifi.uio.no>//// Skomakerveien 33// 3185 Skoppum, NORWAY#include "config.h"#include <algorithm>#include <functional>#include <limits>#include <memory>#include <set>#include <rak/error_number.h>#include <rak/file_stat.h>#include <rak/fs_stat.h>#include <rak/functional.h>#include <rak/debugp.h>#include "data/chunk.h"#include "data/memory_chunk.h"#include "data/socket_file.h"#include "torrent/exceptions.h"#include "torrent/path.h"#include "file.h"#include "file_list.h"#include "file_manager.h"#include "manager.h"#include "piece.h"namespace torrent {voidverify_file_list(const FileList* fl) { if (fl->empty()) throw internal_error("verify_file_list() 1."); if ((*fl->begin())->match_depth_prev() != 0 || (*fl->rbegin())->match_depth_next() != 0) throw internal_error("verify_file_list() 2."); for (FileList::const_iterator itr = fl->begin(), last = fl->end() - 1; itr != last; itr++) if ((*itr)->match_depth_next() != (*(itr + 1))->match_depth_prev() || (*itr)->match_depth_next() >= (*itr)->path()->size()) throw internal_error("verify_file_list() 3.");}FileList::FileList() : m_isOpen(false), m_torrentSize(0), m_chunkSize(0), m_maxFileSize((uint64_t)64 << 30) {}FileList::~FileList() { // Can we skip close()? close(); std::for_each(begin(), end(), rak::call_delete<File>()); base_type::clear(); m_torrentSize = 0;}boolFileList::is_valid_piece(const Piece& piece) const { return piece.index() < size_chunks() && piece.length() != 0 && // Make sure offset does not overflow 32 bits. piece.offset() + piece.length() >= piece.offset() && piece.offset() + piece.length() <= chunk_index_size(piece.index());}boolFileList::is_multi_file() const { // Currently only check if we got just one file. In the future this // should be a bool, which will be set based on what flags are // passed when the torrent was loaded. return size() != 1;}uint64_tFileList::completed_bytes() const { // Chunk size needs to be cast to a uint64_t for the below to work. uint64_t cs = chunk_size(); if (m_bitfield.empty()) return m_bitfield.is_all_set() ? size_bytes() : (completed_chunks() * cs); if (!m_bitfield.get(size_chunks() - 1) || size_bytes() % cs == 0) { // The last chunk is not done, or the last chunk is the same size // as the others. return completed_chunks() * cs; } else { if (completed_chunks() == 0) throw internal_error("FileList::bytes_completed() completed_chunks() == 0."); return (completed_chunks() - 1) * cs + size_bytes() % cs; }}uint64_tFileList::left_bytes() const { uint64_t left = size_bytes() - completed_bytes(); if (left > ((uint64_t)1 << 60)) throw internal_error("FileList::bytes_left() is too large."); if (completed_chunks() == size_chunks() && left != 0) throw internal_error("FileList::bytes_left() has an invalid size."); return left;}uint32_tFileList::chunk_index_size(uint32_t index) const { if (index + 1 != size_chunks() || size_bytes() % chunk_size() == 0) return chunk_size(); else return size_bytes() % chunk_size();}voidFileList::set_root_dir(const std::string& path) { if (is_open()) throw input_error("Tried to change the root directory on an open download."); std::string::size_type last = path.find_last_not_of('/'); if (last == std::string::npos) m_rootDir = "."; else m_rootDir = path.substr(0, last + 1);}voidFileList::set_max_file_size(uint64_t size) { if (is_open()) throw input_error("Tried to change the max file size for an open download."); m_maxFileSize = size;}// This function should really ensure that we arn't dealing files// spread over multiple mount-points.uint64_tFileList::free_diskspace() const { uint64_t freeDiskspace = std::numeric_limits<uint64_t>::max(); for (path_list::const_iterator itr = m_indirectLinks.begin(), last = m_indirectLinks.end(); itr != last; ++itr) { rak::fs_stat stat; if (!stat.update(*itr)) continue; freeDiskspace = std::min<uint64_t>(freeDiskspace, stat.bytes_avail()); } return freeDiskspace != std::numeric_limits<uint64_t>::max() ? freeDiskspace : 0;}FileList::iterator_rangeFileList::split(iterator position, split_type* first, split_type* last) { if (is_open()) throw internal_error("FileList::split(...) is_open()."); if (first == last || position == end()) throw internal_error("FileList::split(...) invalid arguments."); if (position != begin()) (*(position - 1))->set_match_depth_next(0); if (position + 1 != end()) (*(position + 1))->set_match_depth_prev(0); File* oldFile = *position; uint64_t offset = oldFile->offset(); size_type index = std::distance(begin(), position); size_type length = std::distance(first, last); base_type::insert(position, length - 1, NULL); position = begin() + index; iterator itr = position; while (first != last) { File* newFile = new File(); newFile->set_offset(offset); newFile->set_size_bytes(first->first); newFile->set_range(m_chunkSize); *newFile->path() = first->second; offset += first->first; *itr = newFile; itr++; first++; } if (offset != oldFile->offset() + oldFile->size_bytes()) throw internal_error("FileList::split(...) split size does not match the old size."); delete oldFile; return iterator_range(position, itr);}FileList::iteratorFileList::merge(iterator first, iterator last, const Path& path) { File* newFile = new File; // Set the path before deleting any iterators in case it refers to // one of the objects getting deleted. *newFile->path() = path; if (first == last) { if (first == end()) newFile->set_offset(m_torrentSize); else newFile->set_offset((*first)->offset()); first = base_type::insert(first, newFile); } else { newFile->set_offset((*first)->offset()); for (iterator itr = first; itr != last; ++itr) { newFile->set_size_bytes(newFile->size_bytes() + (*itr)->size_bytes()); delete *itr; } first = base_type::erase(first + 1, last) - 1; *first = newFile; } newFile->set_range(m_chunkSize); if (first == begin()) newFile->set_match_depth_prev(0); else set_match_depth(*(first - 1), newFile); if (first + 1 == end()) newFile->set_match_depth_next(0); else set_match_depth(newFile, *(first + 1)); return first;}// If the user supplies an invalid range, it will bork in weird ways.voidFileList::update_paths(iterator first, iterator last) { // Check if we're open? if (first == last) return; if (first != begin()) set_match_depth(*(first - 1), *first); while (first != last && ++first != end()) set_match_depth(*(first - 1), *first); verify_file_list(this);}// Initialize FileList and add a dummy file that may be split into// multiple files.voidFileList::initialize(uint64_t torrentSize, uint32_t chunkSize) { if (sizeof(off_t) != 8) throw internal_error("Last minute panic; sizeof(off_t) != 8.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -