2010-10-10 04:22:57 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2012-2018, Arvid Norberg, Daniel Wallin
|
2010-10-10 04:22:57 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifndef TORRENT_STAT_CACHE_HPP
|
|
|
|
#define TORRENT_STAT_CACHE_HPP
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <vector>
|
2015-12-11 07:08:57 +01:00
|
|
|
#include <string>
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
2018-06-06 13:44:02 +02:00
|
|
|
#include <mutex>
|
2015-12-14 01:44:42 +01:00
|
|
|
|
2010-10-10 04:22:57 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2015-12-11 07:08:57 +01:00
|
|
|
#include "libtorrent/error_code.hpp"
|
|
|
|
#include "libtorrent/file_storage.hpp"
|
2016-12-22 16:42:33 +01:00
|
|
|
#include "libtorrent/units.hpp"
|
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT stat_cache
|
2013-11-29 06:29:39 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
stat_cache();
|
|
|
|
~stat_cache();
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2015-12-11 07:08:57 +01:00
|
|
|
void reserve(int num_files);
|
2015-12-06 18:45:20 +01:00
|
|
|
|
2015-12-11 07:08:57 +01:00
|
|
|
// returns the size of the file unless an error occurs, in which case ec
|
|
|
|
// is set to indicate the error
|
2016-12-22 16:42:33 +01:00
|
|
|
std::int64_t get_filesize(file_index_t i, file_storage const& fs
|
2015-12-11 07:08:57 +01:00
|
|
|
, std::string const& save_path, error_code& ec);
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void set_dirty(file_index_t i);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
2015-12-11 07:08:57 +01:00
|
|
|
// internal
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
not_in_cache = -1,
|
|
|
|
file_error = -2 // (first index in m_errors)
|
|
|
|
};
|
|
|
|
|
|
|
|
// internal
|
2016-12-22 16:42:33 +01:00
|
|
|
void set_cache(file_index_t i, std::int64_t size);
|
|
|
|
void set_error(file_index_t i, error_code const& ec);
|
2015-12-11 07:08:57 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
private:
|
|
|
|
|
2018-06-06 13:44:02 +02:00
|
|
|
void set_cache_impl(file_index_t i, std::int64_t size);
|
|
|
|
void set_error_impl(file_index_t i, error_code const& ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-12-11 07:08:57 +01:00
|
|
|
// returns the index to the specified error. Either an existing one or a
|
|
|
|
// newly added entry
|
|
|
|
int add_error(error_code const& ec);
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
struct stat_cache_t
|
|
|
|
{
|
2018-09-04 13:46:09 +02:00
|
|
|
explicit stat_cache_t(std::int64_t s): file_size(s) {}
|
2015-12-11 07:08:57 +01:00
|
|
|
|
|
|
|
// the size of the file. Negative values have special meaning. -1 means
|
|
|
|
// not-in-cache (i.e. there's no data for this file in the cache).
|
|
|
|
// lower values (larger negative values) indicate that an error
|
|
|
|
// occurred while stat()ing the file. The positive value is an index
|
|
|
|
// into m_errors, that recorded the actual error.
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_size;
|
2014-07-06 21:18:00 +02:00
|
|
|
};
|
2015-12-11 07:08:57 +01:00
|
|
|
|
2018-06-06 13:44:02 +02:00
|
|
|
mutable std::mutex m_mutex;
|
|
|
|
|
2015-12-11 07:08:57 +01:00
|
|
|
// one entry per file
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<stat_cache_t, file_index_t> m_stat_cache;
|
2015-12-11 07:08:57 +01:00
|
|
|
|
|
|
|
// These are the errors that have happened when stating files. Each entry
|
|
|
|
// that had an error, refers to an index into this vector.
|
|
|
|
std::vector<error_code> m_errors;
|
2014-07-06 21:18:00 +02:00
|
|
|
};
|
2010-10-10 04:22:57 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif // TORRENT_STAT_CACHE_HPP
|