~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to src/torrent/resume.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas
  • Date: 2007-03-31 10:31:05 UTC
  • mto: (4.1.4 gutsy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070331103105-jzpp1rml6ud0ff75
Tags: upstream-0.11.4
ImportĀ upstreamĀ versionĀ 0.11.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
 
42
42
// For SocketAddressCompact.
43
43
#include "download/download_info.h"
 
44
#include "peer/peer_info.h"
 
45
#include "peer/peer_list.h"
 
46
 
 
47
#include "data/file.h"
 
48
#include "data/file_list.h"
44
49
 
45
50
#include "common.h"
46
51
#include "bitfield.h"
47
52
#include "download.h"
48
 
#include "file.h"
49
 
#include "file_list.h"
50
53
#include "object.h"
51
 
#include "peer_info.h"
52
 
#include "peer_list.h"
53
54
#include "tracker.h"
54
55
#include "tracker_list.h"
55
56
 
66
67
 
67
68
  const Object::list_type& files = object.get_key_list("files");
68
69
 
69
 
  if (files.size() != download.file_list().size())
 
70
  if (files.size() != download.file_list()->size_files())
70
71
    return;
71
72
 
72
73
  if (object.has_key_string("bitfield")) {
73
74
    const Object::string_type& bitfield = object.get_key_string("bitfield");
74
75
 
75
 
    if (bitfield.size() != download.bitfield()->size_bytes())
 
76
    if (bitfield.size() != download.file_list()->bitfield()->size_bytes())
76
77
      return;
77
78
 
78
79
    download.set_bitfield((uint8_t*)bitfield.c_str(), (uint8_t*)(bitfield.c_str() + bitfield.size()));
80
81
  } else if (object.has_key_value("bitfield")) {
81
82
    Object::value_type chunksDone = object.get_key_value("bitfield");
82
83
 
83
 
    if (chunksDone == download.bitfield()->size_bits())
 
84
    if (chunksDone == download.file_list()->bitfield()->size_bits())
84
85
      download.set_bitfield(true);
85
86
    else if (chunksDone == 0)
86
87
      download.set_bitfield(false);
94
95
  Object::list_type::const_iterator filesItr  = files.begin();
95
96
  Object::list_type::const_iterator filesLast = files.end();
96
97
 
97
 
  FileList fileList = download.file_list();
 
98
  FileList* fileList = download.file_list();
98
99
 
99
 
  for (unsigned int index = 0; index < fileList.size(); ++index, ++filesItr) {
 
100
  for (FileList::iterator listItr = fileList->begin(), listLast = fileList->end(); listItr != listLast; ++listItr, ++filesItr) {
100
101
    rak::file_stat fs;
101
 
    File file = fileList.get(index);
102
102
 
103
103
    // Check that the size and modified stamp matches. If not, then
104
104
    // clear the resume data for that range.
105
105
 
106
 
    if (!fs.update(fileList.root_dir() + file.path_str()) || fs.size() != (off_t)file.size_bytes() ||
107
 
        !filesItr->has_key_value("mtime") || filesItr->get_key_value("mtime") != fs.modified_time())
108
 
      download.clear_range(file.chunk_begin(), file.chunk_end());
 
106
    if (!fs.update(fileList->root_dir() + (*listItr)->path()->as_string()) ||
 
107
        (uint64_t)fs.size() != (*listItr)->size_bytes() ||
 
108
        !filesItr->has_key_value("mtime") ||
 
109
        filesItr->get_key_value("mtime") != fs.modified_time())
 
110
      download.clear_range((*listItr)->range().first, (*listItr)->range().second);
109
111
  }
110
112
}
111
113
 
122
124
  if (!download.is_hash_checked())
123
125
    return;
124
126
 
125
 
  const Bitfield* bitfield = download.bitfield();
 
127
  const Bitfield* bitfield = download.file_list()->bitfield();
126
128
 
127
129
  if (bitfield->is_all_set() || bitfield->is_all_unset())
128
130
    object.insert_key("bitfield", bitfield->size_set());
129
131
  else
130
 
    object.insert_key("bitfield", std::string((char*)download.bitfield()->begin(), download.bitfield()->size_bytes()));
 
132
    object.insert_key("bitfield", std::string((char*)bitfield->begin(), bitfield->size_bytes()));
131
133
  
132
134
  Object::list_type& files = object.has_key_list("files")
133
135
    ? object.get_key_list("files")
135
137
 
136
138
  Object::list_type::iterator filesItr = files.begin();
137
139
 
138
 
  FileList fileList = download.file_list();
 
140
  FileList* fileList = download.file_list();
139
141
 
140
 
  for (unsigned int index = 0; index < fileList.size(); ++index, ++filesItr) {
 
142
  for (FileList::iterator listItr = fileList->begin(), listLast = fileList->end(); listItr != listLast; ++listItr, ++filesItr) {
141
143
    if (filesItr == files.end())
142
144
      filesItr = files.insert(filesItr, Object(Object::TYPE_MAP));
143
145
    else if (!filesItr->is_map())
144
146
      *filesItr = Object(Object::TYPE_MAP);
145
147
 
146
148
    rak::file_stat fs;
147
 
    File file = fileList.get(index);
148
149
 
149
 
    if (!fs.update(fileList.root_dir() + file.path_str()) ||
150
 
        (onlyCompleted && file.completed_chunks() != file.size_chunks())) {
 
150
    if (!fs.update(fileList->root_dir() + (*listItr)->path()->as_string()) ||
 
151
        (onlyCompleted && (*listItr)->completed_chunks() != (*listItr)->size_chunks())) {
151
152
      filesItr->erase_key("mtime");
152
153
      continue;
153
154
    }
171
172
  Object::list_type::const_iterator filesItr  = files.begin();
172
173
  Object::list_type::const_iterator filesLast = files.end();
173
174
 
174
 
  FileList fileList = download.file_list();
 
175
  FileList* fileList = download.file_list();
175
176
 
176
 
  for (unsigned int index = 0; index < fileList.size(); ++index, ++filesItr) {
 
177
  for (FileList::iterator listItr = fileList->begin(), listLast = fileList->end(); listItr != listLast; ++listItr, ++filesItr) {
177
178
    if (filesItr == filesLast)
178
179
      return;
179
180
 
180
181
    // Update the priority from the fast resume data.
181
182
    if (filesItr->has_key_value("priority") &&
182
183
        filesItr->get_key_value("priority") >= 0 && filesItr->get_key_value("priority") <= PRIORITY_HIGH)
183
 
      fileList.get(index).set_priority((priority_t)filesItr->get_key_value("priority"));
 
184
      (*listItr)->set_priority((priority_t)filesItr->get_key_value("priority"));
184
185
  }
185
186
}
186
187
 
192
193
 
193
194
  Object::list_type::iterator filesItr = files.begin();
194
195
 
195
 
  FileList fileList = download.file_list();
 
196
  FileList* fileList = download.file_list();
196
197
 
197
 
  for (unsigned int index = 0; index < fileList.size(); ++index, ++filesItr) {
 
198
  for (FileList::iterator listItr = fileList->begin(), listLast = fileList->end(); listItr != listLast; ++listItr, ++filesItr) {
198
199
    if (filesItr == files.end())
199
200
      filesItr = files.insert(filesItr, Object(Object::TYPE_MAP));
200
201
    else if (!filesItr->is_map())
201
202
      *filesItr = Object(Object::TYPE_MAP);
202
203
 
203
 
    filesItr->insert_key("priority", (int64_t)fileList.get(index).priority());
 
204
    filesItr->insert_key("priority", (int64_t)(*listItr)->priority());
204
205
  }
205
206
}
206
207