~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to src/storage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-05-13 12:08:59 UTC
  • mfrom: (3.2.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090513120859-b5qqlwi43aai3pq3
Tags: 0.14.3-1
* New Upstream Version
  - new package libtorrent-rasterbar3 (bump up library soname).
* debian/control:
  - add Vcs-* stuff (switch to git-buildpackage) and bump up
    Standards-Version to 3.8.1 (no changes required);
  - build-depends on debhelper (>= 7.0.50) and use override_dh_command;
  - move -dbg package to the new 'debug' section;
  - build-depends on autotools-dev and overwrite config.{sub,guess} with a
    recent version in debian/rules.
  - build-depends on quilt and python-docutils:
    + debian/patches/fix_html_docs.patch: fix html documentation for offline
      browsing and add another missing doc file;
    + rebuild docs at build time in debian/rules;
* debian/example.makefile: install a simple makefile for example programs
  included in -doc package.
* debian/rules, debian/python-libtorrent.install: don't rely on hardcoded
  python version, use --install-layout=deb when building python bindings,
  and install to '*-packages' instead of 'site-packages' (this should
  prepare for python2.6 and minimize Ubuntu diff).

Show diffs side-by-side

added added

removed removed

Lines of Context:
184
184
                        copy_file(old_path, new_path);
185
185
                }
186
186
#ifndef BOOST_NO_EXCEPTIONS
187
 
                } catch (std::exception& e) { ec = error_code(errno, get_posix_category()); }
188
 
#endif
 
187
                }
 
188
#if BOOST_VERSION >= 103500
 
189
                catch (boost::system::system_error& e)
 
190
                {
 
191
                        ec = e.code();
 
192
                }
 
193
#else
 
194
                catch (boost::filesystem::filesystem_error& e)
 
195
                {
 
196
                        ec = error_code(e.system_error(), get_system_category());
 
197
                }
 
198
#endif // BOOST_VERSION
 
199
#endif // BOOST_NO_EXCEPTIONS
189
200
        }
190
201
 
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)))
304
315
                        {
305
316
                                if (error) *error = "timestamp mismatch for file '"
306
317
                                        + i->path.external_file_string()
325
336
                        TORRENT_ASSERT(m_save_path.is_complete());
326
337
                }
327
338
 
 
339
                bool has_any_file();
328
340
                bool rename_file(int index, std::string const& new_filename);
329
341
                bool release_files();
330
342
                bool delete_files();
339
351
                bool write_resume_data(entry& rd) const;
340
352
                sha1_hash hash_for_slot(int slot, partial_hash& ph, int piece_size);
341
353
 
 
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);
343
356
 
344
357
                ~storage()
445
458
                        }
446
459
#ifndef BOOST_NO_EXCEPTIONS
447
460
                        }
448
 
                        catch (std::exception& e)
 
461
#if BOOST_VERSION >= 103500
 
462
                        catch (boost::system::system_error& e)
 
463
                        {
 
464
                                set_error(m_save_path / file_iter->path, e.code());
 
465
                                return true;
 
466
                        }
 
467
#else
 
468
                        catch (boost::filesystem::filesystem_error& e)
449
469
                        {
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()));
452
472
                                return true;
453
473
                        }
454
 
#endif
 
474
#endif // BOOST_VERSION
 
475
#endif // BOOST_NO_EXCEPTIONS
455
476
                }
456
477
                std::vector<boost::uint8_t>().swap(m_file_priority);
457
478
                // close files that were opened in write mode
459
480
                return false;
460
481
        }
461
482
 
 
483
        bool storage::has_any_file()
 
484
        {
 
485
                file_storage::iterator i = m_files.begin();
 
486
                file_storage::iterator end = m_files.end();
 
487
 
 
488
                for (; i != end; ++i)
 
489
                {
 
490
                        bool file_exists = false;
 
491
                        fs::path f = m_save_path / i->path;
 
492
#ifndef BOOST_NO_EXCEPTIONS
 
493
                        try
 
494
                        {
 
495
#endif
 
496
#if TORRENT_USE_WPATH
 
497
                                fs::wpath wf = safe_convert(f.string());
 
498
                                file_exists = exists(wf);
 
499
#else
 
500
                                file_exists = exists(f);
 
501
#endif
 
502
#ifndef BOOST_NO_EXCEPTIONS
 
503
                        }
 
504
#if BOOST_VERSION >= 103500
 
505
                        catch (boost::system::system_error& e)
 
506
                        {
 
507
                                set_error(f, e.code());
 
508
                                return false;
 
509
                        }
 
510
#else
 
511
                        catch (boost::filesystem::filesystem_error& e)
 
512
                        {
 
513
                                set_error(f, error_code(e.system_error(), get_system_category()));
 
514
                                return false;
 
515
                        }
 
516
#endif // BOOST_VERSION
 
517
#endif // BOOST_NO_EXCEPTIONS
 
518
                        if (file_exists && i->size > 0)
 
519
                                return true;
 
520
                }
 
521
                return false;
 
522
        }
 
523
 
462
524
        bool storage::rename_file(int index, std::string const& new_filename)
463
525
        {
464
526
                if (index < 0 || index >= m_files.num_files()) return true;
502
564
                        set_error(old_name, e.code());
503
565
                        return true;
504
566
                }
505
 
#endif
506
 
                catch (std::exception& e)
 
567
#else
 
568
                catch (boost::filesystem::filesystem_error& e)
507
569
                {
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()));
509
572
                        return true;
510
573
                }
 
574
#endif // BOOST_VERSION
511
575
#endif
512
576
                return false;
513
577
        }
519
583
                return false;
520
584
        }
521
585
 
 
586
        void storage::delete_one_file(std::string const& p)
 
587
        {
 
588
#if TORRENT_USE_WPATH
 
589
#ifndef BOOST_NO_EXCEPTIONS
 
590
                try
 
591
#endif
 
592
                { fs::remove(safe_convert(p)); }
 
593
#ifndef BOOST_NO_EXCEPTIONS
 
594
#if BOOST_VERSION >= 103500
 
595
                catch (boost::system::system_error& e)
 
596
                {
 
597
                        set_error(p, e.code());
 
598
                }
 
599
#else
 
600
                catch (boost::filesystem::filesystem_error& e)
 
601
                {
 
602
                        set_error(p, error_code(e.system_error(), get_system_category()));
 
603
                }
 
604
#endif // BOOST_VERSION
 
605
#endif // BOOST_NO_EXCEPTIONS
 
606
#else // TORRENT_USE_WPATH
 
607
                if (std::remove(p.c_str()) != 0 && errno != ENOENT)
 
608
                {
 
609
                        set_error(p, error_code(errno, get_posix_category()));
 
610
                }
 
611
#endif
 
612
        }
 
613
 
522
614
        bool storage::delete_files()
523
615
        {
524
616
                // make sure we don't have the files open
525
617
                m_pool.release(this);
526
618
                buffer().swap(m_scratch_buffer);
527
619
 
528
 
                int error = 0;
529
 
                std::string error_file;
530
 
 
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();
545
634
                        }
546
 
#if TORRENT_USE_WPATH
547
 
                        try
548
 
                        { fs::remove(safe_convert(p)); }
549
 
                        catch (std::exception& e)
550
 
                        {
551
 
                                error = errno;
552
 
                                error_file = p;
553
 
                        }
554
 
#else
555
 
                        if (std::remove(p.c_str()) != 0 && errno != ENOENT)
556
 
                        {
557
 
                                error = errno;
558
 
                                error_file = p;
559
 
                        }
560
 
#endif
 
635
                        delete_one_file(p);
561
636
                }
562
637
 
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)
568
643
                {
569
 
#if TORRENT_USE_WPATH
570
 
                        try
571
 
                        { fs::remove(safe_convert(*i)); }
572
 
                        catch (std::exception& e)
573
 
                        {
574
 
                                error = errno;
575
 
                                error_file = *i;
576
 
                        }
577
 
#else
578
 
                        if (std::remove(i->c_str()) != 0 && errno != ENOENT)
579
 
                        {
580
 
                                error = errno;
581
 
                                error_file = *i;
582
 
                        }
583
 
#endif
 
644
                        delete_one_file(*i);
584
645
                }
585
646
 
586
 
                if (error)
587
 
                {
588
 
                        m_error = error_code(error, get_posix_category());
589
 
                        m_error_file.swap(error_file);
590
 
                        return true;
591
 
                }
 
647
                if (error()) return true;
592
648
                return false;
593
649
        }
594
650
 
609
665
                        fl.push_back(entry(p));
610
666
                }
611
667
                
612
 
                if (m_mapped_files)
613
 
                {
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)
617
 
                        {
618
 
                                fl.push_back(i->path.string());
619
 
                        }
620
 
                }
621
 
 
622
668
                return false;
623
669
        }
624
670
 
758
804
 
759
805
                m_pool.release(this);
760
806
 
 
807
                bool ret = true;
 
808
                std::set<std::string> to_move;
 
809
                file_storage const& f = files();
 
810
 
 
811
                for (file_storage::iterator i = f.begin()
 
812
                        , end(f.end()); i != end; ++i)
 
813
                {
 
814
                        to_move.insert(to_move.begin(), *i->path.begin());
 
815
                }
 
816
 
 
817
                for (std::set<std::string>::const_iterator i = to_move.begin()
 
818
                        , end(to_move.end()); i != end; ++i)
 
819
                {
 
820
                        
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());
764
824
#else
765
 
                old_path = m_save_path / files().name();
766
 
                new_path = save_path / files().name();
767
 
#endif
768
 
 
769
 
#ifndef BOOST_NO_EXCEPTIONS
770
 
                try
771
 
                {
772
 
#endif
773
 
                        rename(old_path, new_path);
774
 
                        m_save_path = save_path;
775
 
                        return true;
776
 
#ifndef BOOST_NO_EXCEPTIONS
777
 
                }
778
 
                catch (std::exception& e)
779
 
                {
780
 
                        error_code ec;
781
 
                        recursive_copy(old_path, new_path, ec);
782
 
                        if (ec)
783
 
                        {
784
 
                                set_error(m_save_path / files().name(), ec);
785
 
                                return true;
786
 
                        }
787
 
                        m_save_path = save_path;
788
 
                        recursive_remove(old_path);
789
 
                }
790
 
#endif
791
 
                return false;
 
825
                        old_path = m_save_path / *i;
 
826
                        new_path = save_path / *i;
 
827
#endif
 
828
 
 
829
#ifndef BOOST_NO_EXCEPTIONS
 
830
                        try
 
831
                        {
 
832
#endif
 
833
                                rename(old_path, new_path);
 
834
#ifndef BOOST_NO_EXCEPTIONS
 
835
                        }
 
836
                        catch (std::exception& e)
 
837
                        {
 
838
                                error_code ec;
 
839
                                recursive_copy(old_path, new_path, ec);
 
840
                                if (ec)
 
841
                                {
 
842
                                        set_error(m_save_path / files().name(), ec);
 
843
                                        ret = false;
 
844
                                }
 
845
                                else
 
846
                                {
 
847
                                        recursive_remove(old_path);
 
848
                                }
 
849
                        }
 
850
#endif
 
851
                }
 
852
 
 
853
                if (ret) m_save_path = save_path;
 
854
 
 
855
                return ret;
792
856
        }
793
857
 
794
858
#ifdef TORRENT_DEBUG
1663
1727
 
1664
1728
        int piece_manager::check_no_fastresume(std::string& error)
1665
1729
        {
1666
 
                file_storage::iterator i = m_files.begin();
1667
 
                file_storage::iterator end = m_files.end();
1668
 
 
1669
 
                for (; i != end; ++i)
 
1730
                bool has_files = m_storage->has_any_file();
 
1731
 
 
1732
                if (m_storage->error())
 
1733
                        return fatal_disk_error;
 
1734
 
 
1735
                if (has_files)
1670
1736
                {
1671
 
                        bool file_exists = false;
1672
 
                        fs::path f = m_save_path / i->path;
1673
 
#ifndef BOOST_NO_EXCEPTIONS
1674
 
                        try
1675
 
                        {
1676
 
#endif
1677
 
#if TORRENT_USE_WPATH
1678
 
                                fs::wpath wf = safe_convert(f.string());
1679
 
                                file_exists = exists(wf);
1680
 
#else
1681
 
                                file_exists = exists(f);
1682
 
#endif
1683
 
#ifndef BOOST_NO_EXCEPTIONS
1684
 
                        }
1685
 
                        catch (std::exception& e)
1686
 
                        {
1687
 
                                error = f.string();
1688
 
                                error += ": ";
1689
 
                                error += e.what();
1690
 
                                TORRENT_ASSERT(!error.empty());
1691
 
                                return fatal_disk_error;
1692
 
                        }
1693
 
#endif
1694
 
                        if (file_exists && i->size > 0)
1695
 
                        {
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)
1702
 
                                {
1703
 
                                        m_unallocated_slots.clear();
1704
 
                                        m_free_slots.clear();
1705
 
                                }
1706
 
                                TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_files.num_pieces());
1707
 
                                return need_full_check;
1708
 
                        }
 
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)
 
1743
                        {
 
1744
                                m_unallocated_slots.clear();
 
1745
                                m_free_slots.clear();
 
1746
                        }
 
1747
                        TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_files.num_pieces());
 
1748
                        return need_full_check;
1709
1749
                }
1710
 
        
 
1750
 
1711
1751
                if (m_storage_mode == storage_mode_compact)
1712
1752
                {
1713
1753
                        // in compact mode without checking, we need to