184
184
copy_file(old_path, new_path);
186
186
#ifndef BOOST_NO_EXCEPTIONS
187
} catch (std::exception& e) { ec = error_code(errno, get_posix_category()); }
188
#if BOOST_VERSION >= 103500
189
catch (boost::system::system_error& e)
194
catch (boost::filesystem::filesystem_error& e)
196
ec = error_code(e.system_error(), get_system_category());
198
#endif // BOOST_VERSION
199
#endif // BOOST_NO_EXCEPTIONS
191
202
template <class Path>
300
311
// in sparse mode, allow the files to be more recent
301
312
// than the resume data, but only by 5 minutes
302
313
if ((compact_mode && (time > s->second + 1 || time < s->second - 1)) ||
303
(!compact_mode && (time > s->second + 5 * 60) || time < s->second - 1))
314
(!compact_mode && (time > s->second + 5 * 60 || time < s->second - 1)))
305
316
if (error) *error = "timestamp mismatch for file '"
306
317
+ i->path.external_file_string()
339
351
bool write_resume_data(entry& rd) const;
340
352
sha1_hash hash_for_slot(int slot, partial_hash& ph, int piece_size);
354
void delete_one_file(std::string const& p);
342
355
int read_impl(char* buf, int slot, int offset, int size, bool fill_zero);
446
459
#ifndef BOOST_NO_EXCEPTIONS
448
catch (std::exception& e)
461
#if BOOST_VERSION >= 103500
462
catch (boost::system::system_error& e)
464
set_error(m_save_path / file_iter->path, e.code());
468
catch (boost::filesystem::filesystem_error& e)
450
470
set_error(m_save_path / file_iter->path
451
, error_code(errno, get_posix_category()));
471
, error_code(e.system_error(), get_system_category()));
474
#endif // BOOST_VERSION
475
#endif // BOOST_NO_EXCEPTIONS
456
477
std::vector<boost::uint8_t>().swap(m_file_priority);
457
478
// close files that were opened in write mode
483
bool storage::has_any_file()
485
file_storage::iterator i = m_files.begin();
486
file_storage::iterator end = m_files.end();
488
for (; i != end; ++i)
490
bool file_exists = false;
491
fs::path f = m_save_path / i->path;
492
#ifndef BOOST_NO_EXCEPTIONS
496
#if TORRENT_USE_WPATH
497
fs::wpath wf = safe_convert(f.string());
498
file_exists = exists(wf);
500
file_exists = exists(f);
502
#ifndef BOOST_NO_EXCEPTIONS
504
#if BOOST_VERSION >= 103500
505
catch (boost::system::system_error& e)
507
set_error(f, e.code());
511
catch (boost::filesystem::filesystem_error& e)
513
set_error(f, error_code(e.system_error(), get_system_category()));
516
#endif // BOOST_VERSION
517
#endif // BOOST_NO_EXCEPTIONS
518
if (file_exists && i->size > 0)
462
524
bool storage::rename_file(int index, std::string const& new_filename)
464
526
if (index < 0 || index >= m_files.num_files()) return true;
502
564
set_error(old_name, e.code());
506
catch (std::exception& e)
568
catch (boost::filesystem::filesystem_error& e)
508
set_error(old_name, error_code(errno, get_posix_category()));
570
set_error(old_name, error_code(e.system_error()
571
, get_system_category()));
574
#endif // BOOST_VERSION
586
void storage::delete_one_file(std::string const& p)
588
#if TORRENT_USE_WPATH
589
#ifndef BOOST_NO_EXCEPTIONS
592
{ fs::remove(safe_convert(p)); }
593
#ifndef BOOST_NO_EXCEPTIONS
594
#if BOOST_VERSION >= 103500
595
catch (boost::system::system_error& e)
597
set_error(p, e.code());
600
catch (boost::filesystem::filesystem_error& e)
602
set_error(p, error_code(e.system_error(), get_system_category()));
604
#endif // BOOST_VERSION
605
#endif // BOOST_NO_EXCEPTIONS
606
#else // TORRENT_USE_WPATH
607
if (std::remove(p.c_str()) != 0 && errno != ENOENT)
609
set_error(p, error_code(errno, get_posix_category()));
522
614
bool storage::delete_files()
524
616
// make sure we don't have the files open
525
617
m_pool.release(this);
526
618
buffer().swap(m_scratch_buffer);
529
std::string error_file;
531
620
// delete the files from disk
532
621
std::set<std::string> directories;
533
622
typedef std::set<std::string>::iterator iter_t;
543
632
std::pair<iter_t, bool> ret = directories.insert((m_save_path / bp).string());
544
633
bp = bp.branch_path();
546
#if TORRENT_USE_WPATH
548
{ fs::remove(safe_convert(p)); }
549
catch (std::exception& e)
555
if (std::remove(p.c_str()) != 0 && errno != ENOENT)
563
638
// remove the directories. Reverse order to delete
566
641
for (std::set<std::string>::reverse_iterator i = directories.rbegin()
567
642
, end(directories.rend()); i != end; ++i)
569
#if TORRENT_USE_WPATH
571
{ fs::remove(safe_convert(*i)); }
572
catch (std::exception& e)
578
if (std::remove(i->c_str()) != 0 && errno != ENOENT)
588
m_error = error_code(error, get_posix_category());
589
m_error_file.swap(error_file);
647
if (error()) return true;
609
665
fl.push_back(entry(p));
614
entry::list_type& fl = rd["mapped_files"].list();
615
for (file_storage::iterator i = m_mapped_files->begin()
616
, end(m_mapped_files->end()); i != end; ++i)
618
fl.push_back(i->path.string());
759
805
m_pool.release(this);
808
std::set<std::string> to_move;
809
file_storage const& f = files();
811
for (file_storage::iterator i = f.begin()
812
, end(f.end()); i != end; ++i)
814
to_move.insert(to_move.begin(), *i->path.begin());
817
for (std::set<std::string>::const_iterator i = to_move.begin()
818
, end(to_move.end()); i != end; ++i)
761
821
#if TORRENT_USE_WPATH
762
old_path = safe_convert((m_save_path / files().name()).string());
763
new_path = safe_convert((save_path / files().name()).string());
822
old_path = safe_convert((m_save_path / *i).string());
823
new_path = safe_convert((save_path / *i).string());
765
old_path = m_save_path / files().name();
766
new_path = save_path / files().name();
769
#ifndef BOOST_NO_EXCEPTIONS
773
rename(old_path, new_path);
774
m_save_path = save_path;
776
#ifndef BOOST_NO_EXCEPTIONS
778
catch (std::exception& e)
781
recursive_copy(old_path, new_path, ec);
784
set_error(m_save_path / files().name(), ec);
787
m_save_path = save_path;
788
recursive_remove(old_path);
825
old_path = m_save_path / *i;
826
new_path = save_path / *i;
829
#ifndef BOOST_NO_EXCEPTIONS
833
rename(old_path, new_path);
834
#ifndef BOOST_NO_EXCEPTIONS
836
catch (std::exception& e)
839
recursive_copy(old_path, new_path, ec);
842
set_error(m_save_path / files().name(), ec);
847
recursive_remove(old_path);
853
if (ret) m_save_path = save_path;
794
858
#ifdef TORRENT_DEBUG
1664
1728
int piece_manager::check_no_fastresume(std::string& error)
1666
file_storage::iterator i = m_files.begin();
1667
file_storage::iterator end = m_files.end();
1669
for (; i != end; ++i)
1730
bool has_files = m_storage->has_any_file();
1732
if (m_storage->error())
1733
return fatal_disk_error;
1671
bool file_exists = false;
1672
fs::path f = m_save_path / i->path;
1673
#ifndef BOOST_NO_EXCEPTIONS
1677
#if TORRENT_USE_WPATH
1678
fs::wpath wf = safe_convert(f.string());
1679
file_exists = exists(wf);
1681
file_exists = exists(f);
1683
#ifndef BOOST_NO_EXCEPTIONS
1685
catch (std::exception& e)
1690
TORRENT_ASSERT(!error.empty());
1691
return fatal_disk_error;
1694
if (file_exists && i->size > 0)
1696
m_state = state_full_check;
1697
m_piece_to_slot.clear();
1698
m_piece_to_slot.resize(m_files.num_pieces(), has_no_slot);
1699
m_slot_to_piece.clear();
1700
m_slot_to_piece.resize(m_files.num_pieces(), unallocated);
1701
if (m_storage_mode == storage_mode_compact)
1703
m_unallocated_slots.clear();
1704
m_free_slots.clear();
1706
TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_files.num_pieces());
1707
return need_full_check;
1737
m_state = state_full_check;
1738
m_piece_to_slot.clear();
1739
m_piece_to_slot.resize(m_files.num_pieces(), has_no_slot);
1740
m_slot_to_piece.clear();
1741
m_slot_to_piece.resize(m_files.num_pieces(), unallocated);
1742
if (m_storage_mode == storage_mode_compact)
1744
m_unallocated_slots.clear();
1745
m_free_slots.clear();
1747
TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_files.num_pieces());
1748
return need_full_check;
1711
1751
if (m_storage_mode == storage_mode_compact)
1713
1753
// in compact mode without checking, we need to