diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index dac4ffdc5..8945d8345 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -18,6 +18,7 @@ #include "libtorrent/session_types.hpp" // for save_state_flags_t #include "libtorrent/file_storage.hpp" // for file_flags_t #include "libtorrent/alert.hpp" +#include "libtorrent/create_torrent.hpp" // for create_flags_t #include using namespace boost::python; @@ -314,6 +315,7 @@ void bind_converters() to_python_converter>(); to_python_converter>(); to_python_converter>(); + to_python_converter>(); // work-around types to_python_converter, address_to_tuple< @@ -372,4 +374,5 @@ void bind_converters() to_bitfield_flag(); to_bitfield_flag(); to_bitfield_flag(); + to_bitfield_flag(); } diff --git a/bindings/python/src/create_torrent.cpp b/bindings/python/src/create_torrent.cpp index 8ee0412a0..9f7248b4e 100644 --- a/bindings/python/src/create_torrent.cpp +++ b/bindings/python/src/create_torrent.cpp @@ -109,7 +109,7 @@ namespace #endif // TORRENT_NO_DEPRECATE void add_files_callback(file_storage& fs, std::string const& file - , boost::python::object cb, std::uint32_t flags) + , boost::python::object cb, create_flags_t const flags) { add_files(fs, file, [&](std::string const& i) { return cb(i); }, flags); } @@ -126,6 +126,7 @@ namespace } struct dummy13 {}; + struct dummy14 {}; } void bind_create_torrent() @@ -140,7 +141,7 @@ void bind_create_torrent() #ifndef BOOST_NO_EXCEPTIONS void (*set_piece_hashes0)(create_torrent&, std::string const&) = &set_piece_hashes; #endif - void (*add_files0)(file_storage&, std::string const&, std::uint32_t) = add_files; + void (*add_files0)(file_storage&, std::string const&, create_flags_t) = add_files; std::string const& (file_storage::*file_storage_symlink)(file_index_t) const = &file_storage::symlink; sha1_hash (file_storage::*file_storage_hash)(file_index_t) const = &file_storage::hash; @@ -202,11 +203,12 @@ void bind_create_torrent() s.attr("flag_symlink") = file_storage::flag_symlink; } - class_("create_torrent", no_init) + { + scope s = class_("create_torrent", no_init) .def(init()) .def(init(arg("ti"))) - .def(init((arg("storage"), arg("piece_size") = 0 - , arg("pad_file_limit") = -1, arg("flags") = int(lt::create_torrent::optimize_alignment)))) + .def(init((arg("storage"), arg("piece_size") = 0 + , arg("pad_file_limit") = -1, arg("flags") = lt::create_torrent::optimize_alignment))) .def("generate", &create_torrent::generate) @@ -227,15 +229,22 @@ void bind_create_torrent() .def("set_root_cert", &create_torrent::set_root_cert, (arg("pem"))) ; - enum_("create_torrent_flags_t") + s.attr("optimize_alignment") = create_torrent::optimize_alignment; + s.attr("merkle") = create_torrent::merkle; + s.attr("modification_time") = create_torrent::modification_time; + s.attr("symlinks") = create_torrent::symlinks; + } + + { + scope s = class_("create_torrent_flags_t"); #ifndef TORRENT_NO_DEPRECATE - .value("optimize", create_torrent::optimize) + s.attr("optimize") = create_torrent::optimize; #endif - .value("optimize_alignment", create_torrent::optimize_alignment) - .value("merkle", create_torrent::merkle) - .value("modification_time", create_torrent::modification_time) - .value("symlinks", create_torrent::symlinks) - ; + s.attr("optimize_alignment") = create_torrent::optimize_alignment; + s.attr("merkle") = create_torrent::merkle; + s.attr("modification_time") = create_torrent::modification_time; + s.attr("symlinks") = create_torrent::symlinks; + } def("add_files", add_files0, (arg("fs"), arg("path"), arg("flags") = 0)); def("add_files", add_files_callback, (arg("fs"), arg("path") diff --git a/examples/make_torrent.cpp b/examples/make_torrent.cpp index d606746e0..205b6607e 100644 --- a/examples/make_torrent.cpp +++ b/examples/make_torrent.cpp @@ -182,7 +182,7 @@ int main(int argc, char* argv[]) std::vector similar; int pad_file_limit = -1; int piece_size = 0; - int flags = 0; + create_flags_t flags = {}; std::string root_cert; std::string outfile; diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 3fab4d463..18580be19 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -92,56 +92,56 @@ namespace libtorrent { class torrent_info; + // hidden + struct create_flags_tag; + using create_flags_t = flags::bitfield_flag; + // This class holds state for creating a torrent. After having added // all information to it, call create_torrent::generate() to generate // the torrent. The entry that's returned can then be bencoded into a // .torrent file using bencode(). struct TORRENT_EXPORT create_torrent { - // flags for create_torrent::create_torrent(). - enum flags_t - { - // This will insert pad files to align the files to piece boundaries, for - // optimized disk-I/O. This will minimize the number of bytes of pad- - // files, to keep the impact down for clients that don't support - // them. - optimize_alignment = 1, + // This will insert pad files to align the files to piece boundaries, for + // optimized disk-I/O. This will minimize the number of bytes of pad- + // files, to keep the impact down for clients that don't support + // them. + static constexpr create_flags_t optimize_alignment = 0_bit; #ifndef TORRENT_NO_DEPRECATE - // same as optimize_alignment, for backwards compatibility - optimize TORRENT_DEPRECATED_ENUM = 1, + // same as optimize_alignment, for backwards compatibility + static constexpr create_flags_t TORRENT_DEPRECATED_MEMBER optimize = 0_bit; #endif - // This will create a merkle hash tree torrent. A merkle torrent cannot - // be opened in clients that don't specifically support merkle torrents. - // The benefit is that the resulting torrent file will be much smaller and - // not grow with more pieces. When this option is specified, it is - // recommended to have a fairly small piece size, say 64 kiB. - // When creating merkle torrents, the full hash tree is also generated - // and should be saved off separately. It is accessed through the - // create_torrent::merkle_tree() function. - merkle = 2, + // This will create a merkle hash tree torrent. A merkle torrent cannot + // be opened in clients that don't specifically support merkle torrents. + // The benefit is that the resulting torrent file will be much smaller and + // not grow with more pieces. When this option is specified, it is + // recommended to have a fairly small piece size, say 64 kiB. + // When creating merkle torrents, the full hash tree is also generated + // and should be saved off separately. It is accessed through the + // create_torrent::merkle_tree() function. + static constexpr create_flags_t merkle = 1_bit; - // This will include the file modification time as part of the torrent. - // This is not enabled by default, as it might cause problems when you - // create a torrent from separate files with the same content, hoping to - // yield the same info-hash. If the files have different modification times, - // with this option enabled, you would get different info-hashes for the - // files. - modification_time = 4, + // This will include the file modification time as part of the torrent. + // This is not enabled by default, as it might cause problems when you + // create a torrent from separate files with the same content, hoping to + // yield the same info-hash. If the files have different modification times, + // with this option enabled, you would get different info-hashes for the + // files. + static constexpr create_flags_t modification_time = 2_bit; - // If this flag is set, files that are symlinks get a symlink attribute - // set on them and their data will not be included in the torrent. This - // is useful if you need to reconstruct a file hierarchy which contains - // symlinks. - symlinks = 8, + // If this flag is set, files that are symlinks get a symlink attribute + // set on them and their data will not be included in the torrent. This + // is useful if you need to reconstruct a file hierarchy which contains + // symlinks. + static constexpr create_flags_t symlinks = 3_bit; - // to create a torrent that can be updated via a *mutable torrent* - // (see `BEP 38`_). This also needs to be enabled for torrents that update - // another torrent. - // - // .. _`BEP 38`: http://www.bittorrent.org/beps/bep_0038.html - mutable_torrent_support = 16 - }; + // to create a torrent that can be updated via a *mutable torrent* + // (see `BEP 38`_). This also needs to be enabled for torrents that update + // another torrent. + // + // .. _`BEP 38`: http://www.bittorrent.org/beps/bep_0038.html + static constexpr create_flags_t mutable_torrent_support = 4_bit; // The ``piece_size`` is the size of each piece in bytes. It must // be a multiple of 16 kiB. If a piece size of 0 is specified, a @@ -167,7 +167,7 @@ namespace libtorrent { // eligible files are aligned to. The default is -1, which means the // piece size of the torrent. explicit create_torrent(file_storage& fs, int piece_size = 0 - , int pad_file_limit = -1, int flags = optimize_alignment + , int pad_file_limit = -1, create_flags_t flags = optimize_alignment , int alignment = -1); explicit create_torrent(torrent_info const& ti); @@ -388,9 +388,9 @@ namespace detail { // The ``flags`` argument should be the same as the flags passed to the `create_torrent`_ // constructor. TORRENT_EXPORT void add_files(file_storage& fs, std::string const& file - , std::function p, std::uint32_t flags = 0); + , std::function p, create_flags_t flags = {}); TORRENT_EXPORT void add_files(file_storage& fs, std::string const& file - , std::uint32_t flags = 0); + , create_flags_t flags = {}); // This function will assume that the files added to the torrent file exists at path // ``p``, read those files and hash the content and set the hashes in the ``create_torrent`` @@ -430,11 +430,11 @@ namespace detail { TORRENT_DEPRECATED_EXPORT void add_files(file_storage& fs, std::wstring const& wfile - , std::function p, std::uint32_t flags = 0); + , std::function p, create_flags_t flags = {}); TORRENT_DEPRECATED_EXPORT void add_files(file_storage& fs, std::wstring const& wfile - , std::uint32_t flags = 0); + , create_flags_t flags = {}); TORRENT_DEPRECATED_EXPORT void set_piece_hashes(create_torrent& t, std::wstring const& p diff --git a/simulation/test_web_seed.cpp b/simulation/test_web_seed.cpp index bb4e6efff..3608e747f 100644 --- a/simulation/test_web_seed.cpp +++ b/simulation/test_web_seed.cpp @@ -58,7 +58,7 @@ add_torrent_params create_torrent(file_storage& fs, bool const pad_files = false { lt::create_torrent t(fs, piece_size , pad_files ? piece_size : -1 - , pad_files ? create_torrent::optimize_alignment : 0); + , pad_files ? create_torrent::optimize_alignment : create_flags_t{}); std::vector piece; piece.reserve(fs.piece_length()); diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index c8c85c0ad..8c6d6983b 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -50,6 +50,16 @@ POSSIBILITY OF SUCH DAMAGE. using namespace std::placeholders; namespace libtorrent { + + constexpr create_flags_t create_torrent::optimize_alignment; +#ifndef TORRENT_NO_DEPRECATE + constexpr create_flags_t create_torrent::optimize; +#endif + constexpr create_flags_t create_torrent::merkle; + constexpr create_flags_t create_torrent::modification_time; + constexpr create_flags_t create_torrent::symlinks; + constexpr create_flags_t create_torrent::mutable_torrent_support; + namespace { bool default_pred(std::string const&) { return true; } @@ -105,7 +115,7 @@ namespace { void add_files_impl(file_storage& fs, std::string const& p , std::string const& l, std::function pred - , std::uint32_t const flags) + , create_flags_t const flags) { std::string f = combine_path(p, l); if (!pred(f)) return; @@ -195,7 +205,7 @@ namespace { #ifndef TORRENT_NO_DEPRECATE void add_files(file_storage& fs, std::wstring const& wfile - , std::function p, std::uint32_t flags) + , std::function p, create_flags_t const flags) { std::string utf8 = wchar_utf8(wfile); add_files_impl(fs, parent_path(complete(utf8)) @@ -203,7 +213,7 @@ namespace { } void add_files(file_storage& fs - , std::wstring const& wfile, std::uint32_t flags) + , std::wstring const& wfile, create_flags_t const flags) { std::string utf8 = wchar_utf8(wfile); add_files_impl(fs, parent_path(complete(utf8)) @@ -226,12 +236,12 @@ namespace { #endif // TORRENT_NO_DEPRECATE void add_files(file_storage& fs, std::string const& file - , std::function p, std::uint32_t flags) + , std::function p, create_flags_t const flags) { add_files_impl(fs, parent_path(complete(file)), filename(file), p, flags); } - void add_files(file_storage& fs, std::string const& file, std::uint32_t flags) + void add_files(file_storage& fs, std::string const& file, create_flags_t const flags) { add_files_impl(fs, parent_path(complete(file)), filename(file) , default_pred, flags); @@ -300,14 +310,14 @@ namespace { create_torrent::~create_torrent() = default; create_torrent::create_torrent(file_storage& fs, int piece_size - , int pad_file_limit, int flags, int alignment) + , int pad_file_limit, create_flags_t const flags, int alignment) : m_files(fs) , m_creation_date(time(nullptr)) , m_multifile(fs.num_files() > 1) , m_private(false) - , m_merkle_torrent((flags & merkle) != 0) - , m_include_mtime((flags & modification_time) != 0) - , m_include_symlinks((flags & symlinks) != 0) + , m_merkle_torrent(bool(flags & create_torrent::merkle)) + , m_include_mtime(bool(flags & create_torrent::modification_time)) + , m_include_symlinks(bool(flags & create_torrent::symlinks)) { // return instead of crash in release mode if (fs.num_files() == 0 || fs.total_size() == 0) return; @@ -353,7 +363,7 @@ namespace { #endif m_files.set_piece_length(piece_size); if (flags & (optimize_alignment | mutable_torrent_support)) - m_files.optimize(pad_file_limit, alignment, (flags & mutable_torrent_support) != 0); + m_files.optimize(pad_file_limit, alignment, bool(flags & mutable_torrent_support)); m_files.set_num_pieces(static_cast( (m_files.total_size() + m_files.piece_length() - 1) / m_files.piece_length())); diff --git a/test/test_storage.cpp b/test/test_storage.cpp index d9c6e441c..7aa4ffaff 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -130,7 +130,7 @@ std::shared_ptr setup_torrent_info(file_storage& fs fs.add_file(combine_path("temp_storage", combine_path("folder2", "test3.tmp")), 0); fs.add_file(combine_path("temp_storage", combine_path("_folder3", "test4.tmp")), 0); fs.add_file(combine_path("temp_storage", combine_path("_folder3", combine_path("subfolder", "test5.tmp"))), 8); - lt::create_torrent t(fs, 4, -1, 0); + lt::create_torrent t(fs, 4, -1, {}); char buf_[4] = {0, 0, 0, 0}; sha1_hash h = hasher(buf_).final(); @@ -442,7 +442,7 @@ void test_check_files(std::string const& test_path std::vector piece0 = new_piece(piece_size); std::vector piece2 = new_piece(piece_size); - lt::create_torrent t(fs, piece_size, -1, 0); + lt::create_torrent t(fs, piece_size, -1, {}); t.set_hash(piece_index_t(0), hasher(piece0).final()); t.set_hash(piece_index_t(1), sha1_hash(nullptr)); t.set_hash(piece_index_t(2), sha1_hash(nullptr)); @@ -539,7 +539,7 @@ void run_test(bool unbuffered) // | | | | | // | piece 0 | piece 1 | piece 2 | piece 3 | - lt::create_torrent t(fs, piece_size, -1, 0); + lt::create_torrent t(fs, piece_size, -1, {}); TEST_CHECK(t.num_pieces() == 4); t.set_hash(piece_index_t(0), hasher(piece0).final()); t.set_hash(piece_index_t(1), hasher(piece1).final()); @@ -584,7 +584,7 @@ void run_test(bool unbuffered) error_code ec; file_storage fs; fs.add_file(combine_path("temp_storage", "test1.tmp"), 3 * piece_size); - lt::create_torrent t(fs, piece_size, -1, 0); + lt::create_torrent t(fs, piece_size, -1, {}); TEST_CHECK(fs.file_path(file_index_t(0)) == combine_path("temp_storage", "test1.tmp")); t.set_hash(piece_index_t(0), hasher(piece0).final()); t.set_hash(piece_index_t(1), hasher(piece1).final());