don't fail renaming a file in a torrent just because it doesn't exist (and the destination directory doesn't exist)

This commit is contained in:
Arvid Norberg 2014-12-26 21:25:37 +00:00
parent 75ae204cf4
commit ae47823970
5 changed files with 41 additions and 7 deletions

View File

@ -131,6 +131,7 @@ namespace libtorrent
TORRENT_EXTRA_EXPORT void remove_all(std::string const& f
, error_code& ec);
TORRENT_EXTRA_EXPORT void remove(std::string const& f, error_code& ec);
TORRENT_EXTRA_EXPORT bool exists(std::string const& f, error_code& ec);
TORRENT_EXTRA_EXPORT bool exists(std::string const& f);
TORRENT_EXTRA_EXPORT boost::int64_t file_size(std::string const& f);
TORRENT_EXTRA_EXPORT bool is_directory(std::string const& f

View File

@ -993,7 +993,7 @@ namespace libtorrent
// renames the file with the given index to the new name
// the name may include a directory path
// returns false on failure
bool rename_file(int index, std::string const& name);
void rename_file(int index, std::string const& name);
// unless this returns true, new connections must wait
// with their initialization.

View File

@ -865,15 +865,20 @@ namespace libtorrent
return s.file_size;
}
bool exists(std::string const& f)
bool exists(std::string const& f, error_code& ec)
{
error_code ec;
file_status s;
stat_file(f, &s, ec);
if (ec) return false;
return true;
}
bool exists(std::string const& f)
{
error_code ec;
return exists(f, ec);
}
void remove(std::string const& inf, error_code& ec)
{
ec.clear();

View File

@ -479,12 +479,33 @@ namespace libtorrent
return false;
}
void default_storage::rename_file(int index, std::string const& new_filename, storage_error& ec)
void default_storage::rename_file(int index, std::string const& new_filename
, storage_error& ec)
{
if (index < 0 || index >= files().num_files()) return;
std::string old_name = files().file_path(index, m_save_path);
m_pool.release(this, index);
// if the old file doesn't exist, just succeed and change the filename
// that will be created. This shortcut is important because the
// destination directory may not exist yet, which would cause a failure
// even though we're not moving a file (yet). It's better for it to
// fail later when we try to write to the file the first time, because
// the user then will have had a chance to make the destination directory
// valid.
if (!exists(old_name, ec.ec))
{
if (ec.ec == boost::system::errc::no_such_file_or_directory)
{
ec.ec.clear();
return;
}
ec.file = index;
ec.operation = storage_error::rename;
return;
}
#if TORRENT_DEBUG_FILE_LEAKS
print_open_files("release files", m_files.name().c_str());
#endif

View File

@ -8203,7 +8203,7 @@ namespace libtorrent
return m_save_path;
}
bool torrent::rename_file(int index, std::string const& name)
void torrent::rename_file(int index, std::string const& name)
{
INVARIANT_CHECK;
@ -8211,12 +8211,19 @@ namespace libtorrent
TORRENT_ASSERT(index < m_torrent_file->num_files());
// stoage may be NULL during shutdown
if (!m_storage.get()) return false;
if (!m_storage.get())
{
if (alerts().should_post<file_rename_failed_alert>())
alerts().post_alert(file_rename_failed_alert(get_handle()
, index, error_code(errors::session_is_closing
, get_libtorrent_category())));
return;
}
inc_refcount("rename_file");
m_ses.disk_thread().async_rename_file(m_storage.get(), index, name
, boost::bind(&torrent::on_file_renamed, shared_from_this(), _1));
return true;
return;
}
void torrent::move_storage(std::string const& save_path, int flags)