2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2020 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
31 #include <libtorrent/aux_/vector.hpp>
32 #include <libtorrent/fwd.hpp>
36 #include "base/path.h"
38 #ifdef QBT_USES_LIBTORRENT2
39 #include <libtorrent/disk_interface.hpp>
40 #include <libtorrent/file_storage.hpp>
41 #include <libtorrent/io_context.hpp>
45 #include <libtorrent/storage.hpp>
48 #ifdef QBT_USES_LIBTORRENT2
49 std::unique_ptr
<lt::disk_interface
> customDiskIOConstructor(
50 lt::io_context
&ioContext
, lt::settings_interface
const &settings
, lt::counters
&counters
);
51 std::unique_ptr
<lt::disk_interface
> customPosixDiskIOConstructor(
52 lt::io_context
&ioContext
, lt::settings_interface
const &settings
, lt::counters
&counters
);
53 std::unique_ptr
<lt::disk_interface
> customMMapDiskIOConstructor(
54 lt::io_context
&ioContext
, lt::settings_interface
const &settings
, lt::counters
&counters
);
56 class CustomDiskIOThread final
: public lt::disk_interface
59 explicit CustomDiskIOThread(std::unique_ptr
<libtorrent::disk_interface
> nativeDiskIOThread
);
61 lt::storage_holder
new_torrent(const lt::storage_params
&storageParams
, const std::shared_ptr
<void> &torrent
) override
;
62 void remove_torrent(lt::storage_index_t storageIndex
) override
;
63 void async_read(lt::storage_index_t storageIndex
, const lt::peer_request
&peerRequest
64 , std::function
<void (lt::disk_buffer_holder
, const lt::storage_error
&)> handler
65 , lt::disk_job_flags_t flags
) override
;
66 bool async_write(lt::storage_index_t storageIndex
, const lt::peer_request
&peerRequest
67 , const char *buf
, std::shared_ptr
<lt::disk_observer
> diskObserver
68 , std::function
<void (const lt::storage_error
&)> handler
, lt::disk_job_flags_t flags
) override
;
69 void async_hash(lt::storage_index_t storageIndex
, lt::piece_index_t piece
, lt::span
<lt::sha256_hash
> hash
, lt::disk_job_flags_t flags
70 , std::function
<void (lt::piece_index_t
, const lt::sha1_hash
&, const lt::storage_error
&)> handler
) override
;
71 void async_hash2(lt::storage_index_t storage
, lt::piece_index_t piece
, int offset
, lt::disk_job_flags_t flags
72 , std::function
<void (lt::piece_index_t
, const lt::sha256_hash
&, const lt::storage_error
&)> handler
) override
;
73 void async_move_storage(lt::storage_index_t storage
, std::string path
, lt::move_flags_t flags
74 , std::function
<void (lt::status_t
, const std::string
&, const lt::storage_error
&)> handler
) override
;
75 void async_release_files(lt::storage_index_t storage
, std::function
<void ()> handler
) override
;
76 void async_check_files(lt::storage_index_t storage
, const lt::add_torrent_params
*resume_data
77 , lt::aux::vector
<std::string
, lt::file_index_t
> links
78 , std::function
<void (lt::status_t
, const lt::storage_error
&)> handler
) override
;
79 void async_stop_torrent(lt::storage_index_t storage
, std::function
<void ()> handler
) override
;
80 void async_rename_file(lt::storage_index_t storage
, lt::file_index_t index
, std::string name
81 , std::function
<void (const std::string
&, lt::file_index_t
, const lt::storage_error
&)> handler
) override
;
82 void async_delete_files(lt::storage_index_t storage
, lt::remove_flags_t options
, std::function
<void (const lt::storage_error
&)> handler
) override
;
83 void async_set_file_priority(lt::storage_index_t storage
, lt::aux::vector
<lt::download_priority_t
, lt::file_index_t
> priorities
84 , std::function
<void (const lt::storage_error
&, lt::aux::vector
<lt::download_priority_t
, lt::file_index_t
>)> handler
) override
;
85 void async_clear_piece(lt::storage_index_t storage
, lt::piece_index_t index
, std::function
<void (lt::piece_index_t
)> handler
) override
;
86 void update_stats_counters(lt::counters
&counters
) const override
;
87 std::vector
<lt::open_file_state
> get_status(lt::storage_index_t index
) const override
;
88 void abort(bool wait
) override
;
89 void submit_jobs() override
;
90 void settings_updated() override
;
93 void handleCompleteFiles(libtorrent::storage_index_t storage
, const Path
&savePath
);
95 std::unique_ptr
<lt::disk_interface
> m_nativeDiskIO
;
100 lt::file_storage files
;
101 lt::aux::vector
<lt::download_priority_t
, lt::file_index_t
> filePriorities
;
103 QHash
<lt::storage_index_t
, StorageData
> m_storageData
;
108 lt::storage_interface
*customStorageConstructor(const lt::storage_params
¶ms
, lt::file_pool
&pool
);
110 class CustomStorage final
: public lt::default_storage
113 explicit CustomStorage(const lt::storage_params
¶ms
, lt::file_pool
&filePool
);
115 bool verify_resume_data(const lt::add_torrent_params
&rd
, const lt::aux::vector
<std::string
, lt::file_index_t
> &links
, lt::storage_error
&ec
) override
;
116 void set_file_priority(lt::aux::vector
<lt::download_priority_t
, lt::file_index_t
> &priorities
, lt::storage_error
&ec
) override
;
117 lt::status_t
move_storage(const std::string
&savePath
, lt::move_flags_t flags
, lt::storage_error
&ec
) override
;
120 void handleCompleteFiles(const Path
&savePath
);
122 lt::aux::vector
<lt::download_priority_t
, lt::file_index_t
> m_filePriorities
;