~ubuntu-branches/debian/lenny/libtorrent/lenny

« back to all changes in this revision

Viewing changes to src/torrent/resume.cc

  • Committer: Bazaar Package Importer
  • Author(s): James Vega
  • Date: 2008-04-15 21:38:37 UTC
  • mfrom: (4.1.6 hardy)
  • Revision ID: james.westby@ubuntu.com-20080415213837-znilhi7a44yec99u
Tags: 0.11.9-1.1
* Non-maintainer upload.
* Fix FTBFS with gcc-4.3.  (Closes: #420920)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// libTorrent - BitTorrent library
2
 
// Copyright (C) 2005-2006, Jari Sundell
 
2
// Copyright (C) 2005-2007, Jari Sundell
3
3
//
4
4
// This program is free software; you can redistribute it and/or modify
5
5
// it under the terms of the GNU General Public License as published by
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
 
 
148
    filesItr->insert_key("completed", (int64_t)(*listItr)->completed_chunks());
 
149
 
146
150
    rak::file_stat fs;
147
 
    File file = fileList.get(index);
148
151
 
149
 
    if (!fs.update(fileList.root_dir() + file.path_str()) ||
150
 
        (onlyCompleted && file.completed_chunks() != file.size_chunks())) {
 
152
    if (!fs.update(fileList->root_dir() + (*listItr)->path()->as_string()) ||
 
153
        (onlyCompleted && (*listItr)->completed_chunks() != (*listItr)->size_chunks())) {
151
154
      filesItr->erase_key("mtime");
152
155
      continue;
153
156
    }
171
174
  Object::list_type::const_iterator filesItr  = files.begin();
172
175
  Object::list_type::const_iterator filesLast = files.end();
173
176
 
174
 
  FileList fileList = download.file_list();
 
177
  FileList* fileList = download.file_list();
175
178
 
176
 
  for (unsigned int index = 0; index < fileList.size(); ++index, ++filesItr) {
 
179
  for (FileList::iterator listItr = fileList->begin(), listLast = fileList->end(); listItr != listLast; ++listItr, ++filesItr) {
177
180
    if (filesItr == filesLast)
178
181
      return;
179
182
 
180
183
    // Update the priority from the fast resume data.
181
184
    if (filesItr->has_key_value("priority") &&
182
185
        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"));
 
186
      (*listItr)->set_priority((priority_t)filesItr->get_key_value("priority"));
 
187
 
 
188
    if (filesItr->has_key_value("completed"))
 
189
      fileList->set_file_completed_chunks(listItr, std::max<int64_t>(filesItr->get_key_value("completed"), (*listItr)->size_chunks()));
184
190
  }
185
191
}
186
192
 
192
198
 
193
199
  Object::list_type::iterator filesItr = files.begin();
194
200
 
195
 
  FileList fileList = download.file_list();
 
201
  FileList* fileList = download.file_list();
196
202
 
197
 
  for (unsigned int index = 0; index < fileList.size(); ++index, ++filesItr) {
 
203
  for (FileList::iterator listItr = fileList->begin(), listLast = fileList->end(); listItr != listLast; ++listItr, ++filesItr) {
198
204
    if (filesItr == files.end())
199
205
      filesItr = files.insert(filesItr, Object(Object::TYPE_MAP));
200
206
    else if (!filesItr->is_map())
201
207
      *filesItr = Object(Object::TYPE_MAP);
202
208
 
203
 
    filesItr->insert_key("priority", (int64_t)fileList.get(index).priority());
 
209
    filesItr->insert_key("priority", (int64_t)(*listItr)->priority());
204
210
  }
205
211
}
206
212