~ubuntu-branches/ubuntu/wily/libtorrent/wily-proposed

« back to all changes in this revision

Viewing changes to src/torrent/data/file_manager.cc

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#include "config.h"
 
38
 
 
39
#include <algorithm>
 
40
 
 
41
#include "data/socket_file.h"
 
42
#include "torrent/exceptions.h"
 
43
 
 
44
#include "file.h"
 
45
#include "file_manager.h"
 
46
#include "manager.h"
 
47
 
 
48
namespace torrent {
 
49
 
 
50
FileManager::FileManager() :
 
51
  m_maxOpenFiles(0),
 
52
  m_filesOpenedCounter(0),
 
53
  m_filesClosedCounter(0),
 
54
  m_filesFailedCounter(0) {}
 
55
 
 
56
FileManager::~FileManager() {
 
57
  if (!empty())
 
58
    throw internal_error("FileManager::~FileManager() called but empty() != true.");
 
59
}
 
60
 
 
61
void
 
62
FileManager::set_max_open_files(size_type s) {
 
63
  if (s < 4 || s > (1 << 16))
 
64
    throw input_error("Max open files must be between 4 and 2^16.");
 
65
 
 
66
  m_maxOpenFiles = s;
 
67
 
 
68
  while (size() > m_maxOpenFiles)
 
69
    close_least_active();
 
70
}
 
71
 
 
72
bool
 
73
FileManager::open(value_type file, int prot, int flags) {
 
74
  if (file->is_open())
 
75
    close(file);
 
76
 
 
77
  if (size() > m_maxOpenFiles)
 
78
    throw internal_error("FileManager::open_file(...) m_openSize > m_maxOpenFiles.");
 
79
 
 
80
  if (size() == m_maxOpenFiles)
 
81
    close_least_active();
 
82
 
 
83
  SocketFile fd;
 
84
 
 
85
  if (!fd.open(file->frozen_path(), prot, flags)) {
 
86
    m_filesFailedCounter++;
 
87
    return false;
 
88
  }
 
89
 
 
90
  file->set_protection(prot);
 
91
  file->set_file_descriptor(fd.fd());
 
92
  base_type::push_back(file);
 
93
 
 
94
  // Consider storing the position of the file here.
 
95
 
 
96
  m_filesOpenedCounter++;
 
97
  return true;
 
98
}
 
99
 
 
100
void
 
101
FileManager::close(value_type file) {
 
102
  if (!file->is_open())
 
103
    return;
 
104
 
 
105
  SocketFile(file->file_descriptor()).close();
 
106
 
 
107
  file->set_protection(0);
 
108
  file->set_file_descriptor(-1);
 
109
  
 
110
  iterator itr = std::find(begin(), end(), file);
 
111
 
 
112
  if (itr == end())
 
113
    throw internal_error("FileManager::close_file(...) itr == end().");
 
114
 
 
115
  *itr = back();
 
116
  base_type::pop_back();
 
117
 
 
118
  m_filesClosedCounter++;
 
119
}
 
120
 
 
121
struct FileManagerActivity {
 
122
  FileManagerActivity() : m_last(rak::timer::max().usec()), m_file(NULL) {}
 
123
 
 
124
  void operator ()(File* f) {
 
125
    if (f->is_open() && f->last_touched() <= m_last) {
 
126
      m_last = f->last_touched();
 
127
      m_file = f;
 
128
    }
 
129
  }
 
130
 
 
131
  uint64_t   m_last;
 
132
  File*      m_file;
 
133
};
 
134
 
 
135
void
 
136
FileManager::close_least_active() {
 
137
  close(std::for_each(begin(), end(), FileManagerActivity()).m_file);
 
138
}
 
139
 
 
140
}