~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

Viewing changes to libtorrent/src/file_storage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-11-13 02:39:45 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091113023945-te1bybo2912ejzuc
Tags: 1.2.0~rc3-4
* debian/control: bump build-dep on python-setuptools to (>= 0.6c9).
* debian/patches:
  - 25_r5921_fastresume_files.patch
    new, should fix problems with fresh configs;
  - 30_r5931_ipc_lockfile.patch:
    new, should fix an issue where Deluge will fail to start if there is a
    stale ipc lockfile. (Closes: #555849)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2003-2008, Arvid Norberg
4
 
All rights reserved.
5
 
 
6
 
Redistribution and use in source and binary forms, with or without
7
 
modification, are permitted provided that the following conditions
8
 
are met:
9
 
 
10
 
    * Redistributions of source code must retain the above copyright
11
 
      notice, this list of conditions and the following disclaimer.
12
 
    * Redistributions in binary form must reproduce the above copyright
13
 
      notice, this list of conditions and the following disclaimer in
14
 
      the documentation and/or other materials provided with the distribution.
15
 
    * Neither the name of the author nor the names of its
16
 
      contributors may be used to endorse or promote products derived
17
 
      from this software without specific prior written permission.
18
 
 
19
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 
POSSIBILITY OF SUCH DAMAGE.
30
 
 
31
 
*/
32
 
 
33
 
#include "libtorrent/pch.hpp"
34
 
 
35
 
#include "libtorrent/file_storage.hpp"
36
 
 
37
 
 
38
 
namespace libtorrent
39
 
{
40
 
        file_storage::file_storage()
41
 
                : m_piece_length(0)
42
 
                , m_total_size(0)
43
 
                , m_num_pieces(0)
44
 
        {}
45
 
 
46
 
        int file_storage::piece_size(int index) const
47
 
        {
48
 
                TORRENT_ASSERT(index >= 0 && index < num_pieces());
49
 
                if (index == num_pieces()-1)
50
 
                {
51
 
                        int size = int(total_size()
52
 
                                - size_type(num_pieces() - 1) * piece_length());
53
 
                        TORRENT_ASSERT(size > 0);
54
 
                        TORRENT_ASSERT(size <= piece_length());
55
 
                        return int(size);
56
 
                }
57
 
                else
58
 
                        return piece_length();
59
 
        }
60
 
 
61
 
        void file_storage::rename_file(int index, std::string const& new_filename)
62
 
        {
63
 
                TORRENT_ASSERT(index >= 0 && index < int(m_files.size()));
64
 
                m_files[index].path = new_filename;
65
 
        }
66
 
 
67
 
        file_storage::iterator file_storage::file_at_offset(size_type offset) const
68
 
        {
69
 
                // TODO: do a binary search
70
 
                std::vector<file_entry>::const_iterator i;
71
 
                for (i = begin(); i != end(); ++i)
72
 
                {
73
 
                        if (i->offset <= offset && i->offset + i->size > offset)
74
 
                                return i;
75
 
                }
76
 
                return i;
77
 
        }
78
 
 
79
 
        std::vector<file_slice> file_storage::map_block(int piece, size_type offset
80
 
                , int size_) const
81
 
        {
82
 
                TORRENT_ASSERT(num_files() > 0);
83
 
                std::vector<file_slice> ret;
84
 
 
85
 
                size_type start = piece * (size_type)m_piece_length + offset;
86
 
                size_type size = size_;
87
 
                TORRENT_ASSERT(start + size <= m_total_size);
88
 
 
89
 
                // find the file iterator and file offset
90
 
                // TODO: do a binary search on the file offsets
91
 
                size_type file_offset = start;
92
 
                std::vector<file_entry>::const_iterator file_iter;
93
 
 
94
 
                int counter = 0;
95
 
                for (file_iter = begin();; ++counter, ++file_iter)
96
 
                {
97
 
                        TORRENT_ASSERT(file_iter != end());
98
 
                        if (file_offset < file_iter->size)
99
 
                        {
100
 
                                file_slice f;
101
 
                                f.file_index = counter;
102
 
                                f.offset = file_offset + file_iter->file_base;
103
 
                                f.size = (std::min)(file_iter->size - file_offset, (size_type)size);
104
 
                                size -= f.size;
105
 
                                file_offset += f.size;
106
 
                                ret.push_back(f);
107
 
                        }
108
 
                        
109
 
                        TORRENT_ASSERT(size >= 0);
110
 
                        if (size <= 0) break;
111
 
 
112
 
                        file_offset -= file_iter->size;
113
 
                }
114
 
                return ret;
115
 
        }
116
 
        
117
 
        peer_request file_storage::map_file(int file_index, size_type file_offset
118
 
                , int size) const
119
 
        {
120
 
                TORRENT_ASSERT(file_index < num_files());
121
 
                TORRENT_ASSERT(file_index >= 0);
122
 
                size_type offset = file_offset + at(file_index).offset;
123
 
 
124
 
                peer_request ret;
125
 
                ret.piece = int(offset / piece_length());
126
 
                ret.start = int(offset - ret.piece * piece_length());
127
 
                ret.length = size;
128
 
                return ret;
129
 
        }
130
 
 
131
 
        void file_storage::add_file(fs::path const& file, size_type size)
132
 
        {
133
 
                TORRENT_ASSERT(size >= 0);
134
 
#if BOOST_VERSION < 103600
135
 
                if (!file.has_branch_path())
136
 
#else
137
 
                if (!file.has_parent_path())
138
 
#endif
139
 
                {
140
 
                        // you have already added at least one file with a
141
 
                        // path to the file (branch_path), which means that
142
 
                        // all the other files need to be in the same top
143
 
                        // directory as the first file.
144
 
                        TORRENT_ASSERT(m_files.empty());
145
 
                        m_name = file.string();
146
 
                }
147
 
                else
148
 
                {
149
 
                        if (m_files.empty())
150
 
                                m_name = *file.begin();
151
 
                }
152
 
                TORRENT_ASSERT(m_name == *file.begin());
153
 
                file_entry e;
154
 
                m_files.push_back(e);
155
 
                m_files.back().size = size;
156
 
                m_files.back().path = file;
157
 
                m_files.back().offset = m_total_size;
158
 
                m_total_size += size;
159
 
        }
160
 
 
161
 
        void file_storage::add_file(file_entry const& e)
162
 
        {
163
 
                add_file(e.path, e.size);
164
 
        }
165
 
}
166