From cc432a8cd84fb7c3becd0f603531e3ea00ccb929 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 19 Mar 2009 17:32:40 +0000 Subject: [PATCH] added error handling for set_piece_hashes --- ChangeLog | 1 + docs/make_torrent.rst | 13 +++++++ include/libtorrent/create_torrent.hpp | 54 +++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8cb7ef10f..27526de22 100644 --- a/ChangeLog +++ b/ChangeLog @@ -45,6 +45,7 @@ release 0.14.3 changing DHT port * fixed move_storage bug when files were renamed to be moved out of the root directory + * added error handling for set_piece_hashes release 0.14.2 diff --git a/docs/make_torrent.rst b/docs/make_torrent.rst index f9991f68e..5a28ab4e4 100644 --- a/docs/make_torrent.rst +++ b/docs/make_torrent.rst @@ -93,9 +93,19 @@ set_piece_hashes() void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f); template void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f); + template + void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f + , error_code& ec); + template + void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f + , error_code& ec); void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p); void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p); + void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p + , error_code& ec); + void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p + , error_code& ec); 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`` @@ -104,6 +114,9 @@ must have the following signature:: void Fun(int); +The overloads that don't take an ``error_code&`` may throw an exception in case of a +file error, the other overloads sets the error code to reflect the error, if any. + file_storage ============ diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index ae8a2c1f9..39051d3a4 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -224,7 +224,8 @@ namespace libtorrent }; template - void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f) + void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f + , error_code& ec) { file_pool fp; boost::scoped_ptr st( @@ -238,17 +239,39 @@ namespace libtorrent // read hits the disk and will block. Progress should // be updated in between reads st->read(buf.bytes(), i, 0, t.piece_size(i)); + if (st->error()) + { + ec = st->error(); + return; + } hasher h(buf.bytes(), t.piece_size(i)); t.set_hash(i, h.final()); f(i); } } + template + void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f) + { + error_code ec; + set_piece_hashes(t, p, f, ec); + if (ec) throw libtorrent_exception(ec); + } + +#ifndef BOOST_NOEXCEPTIONS inline void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p) { - set_piece_hashes(t, p, detail::nop); + error_code ec; + set_piece_hashes(t, p, detail::nop, ec); + if (ec) throw libtorrent_exception(ec); } + inline void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, error_code& ec) + { + set_piece_hashes(t, p, detail::nop, ec); + } +#endif + // wpath versions template @@ -273,7 +296,8 @@ namespace libtorrent } template - void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f) + void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f + , error_code& ec) { file_pool fp; std::string utf8; @@ -289,15 +313,37 @@ namespace libtorrent // read hits the disk and will block. Progress should // be updated in between reads st->read(&buf[0], i, 0, t.piece_size(i)); + if (st->error()) + { + ec = st->error(); + return; + } hasher h(&buf[0], t.piece_size(i)); t.set_hash(i, h.final()); f(i); } } +#ifndef BOOST_NOEXCEPTIONS + template + void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f) + { + error_code ec; + set_piece_hashes(t, p, f, ec); + if (ec) throw libtorrent_exception(ec); + } + inline void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p) { - set_piece_hashes(t, p, detail::nop); + error_code ec; + set_piece_hashes(t, p, detail::nop, ec); + if (ec) throw libtorrent_exception(ec); + } +#endif + + inline void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, error_code& ec) + { + set_piece_hashes(t, p, detail::nop, ec); } }