1 /*
2 
3 Copyright (c) 2012-2018, Arvid Norberg, Daniel Wallin
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the distribution.
15     * Neither the name of the author nor the names of its
16       contributors may be used to endorse or promote products derived
17       from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
33 #ifndef TORRENT_STAT_CACHE_HPP
34 #define TORRENT_STAT_CACHE_HPP
35 
36 #include <vector>
37 #include <string>
38 #include <cstdint>
39 #include <mutex>
40 
41 #include "libtorrent/config.hpp"
42 #include "libtorrent/error_code.hpp"
43 #include "libtorrent/file_storage.hpp"
44 #include "libtorrent/units.hpp"
45 #include "libtorrent/aux_/vector.hpp"
46 
47 namespace libtorrent {
48 
49 	struct TORRENT_EXTRA_EXPORT stat_cache
50 	{
51 		stat_cache();
52 		~stat_cache();
53 
54 		void reserve(int num_files);
55 
56 		// returns the size of the file unless an error occurs, in which case ec
57 		// is set to indicate the error
58 		std::int64_t get_filesize(file_index_t i, file_storage const& fs
59 			, std::string const& save_path, error_code& ec);
60 
61 		void set_dirty(file_index_t i);
62 
63 		void clear();
64 
65 		// internal
66 		enum
67 		{
68 			not_in_cache = -1,
69 			file_error = -2 // (first index in m_errors)
70 		};
71 
72 		// internal
73 		void set_cache(file_index_t i, std::int64_t size);
74 		void set_error(file_index_t i, error_code const& ec);
75 
76 	private:
77 
78 		void set_cache_impl(file_index_t i, std::int64_t size);
79 		void set_error_impl(file_index_t i, error_code const& ec);
80 
81 		// returns the index to the specified error. Either an existing one or a
82 		// newly added entry
83 		int add_error(error_code const& ec);
84 
85 		struct stat_cache_t
86 		{
stat_cache_tlibtorrent::stat_cache::stat_cache_t87 			explicit stat_cache_t(std::int64_t s): file_size(s) {}
88 
89 			// the size of the file. Negative values have special meaning. -1 means
90 			// not-in-cache (i.e. there's no data for this file in the cache).
91 			// lower values (larger negative values) indicate that an error
92 			// occurred while stat()ing the file. The positive value is an index
93 			// into m_errors, that recorded the actual error.
94 			std::int64_t file_size;
95 		};
96 
97 		mutable std::mutex m_mutex;
98 
99 		// one entry per file
100 		aux::vector<stat_cache_t, file_index_t> m_stat_cache;
101 
102 		// These are the errors that have happened when stating files. Each entry
103 		// that had an error, refers to an index into this vector.
104 		std::vector<error_code> m_errors;
105 	};
106 }
107 
108 #endif // TORRENT_STAT_CACHE_HPP
109