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

« back to all changes in this revision

Viewing changes to src/torrent/data/file.h

  • 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:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2006, 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
#ifndef LIBTORRENT_FILE_H
 
38
#define LIBTORRENT_FILE_H
 
39
 
 
40
#include <torrent/common.h>
 
41
#include <torrent/path.h>
 
42
 
 
43
namespace torrent {
 
44
 
 
45
class LIBTORRENT_EXPORT File {
 
46
public:
 
47
  friend class FileList;
 
48
 
 
49
  typedef std::pair<uint32_t, uint32_t> range_type;
 
50
 
 
51
  File();
 
52
  ~File();
 
53
 
 
54
  bool                is_created() const;
 
55
  bool                is_open() const                          { return m_fd != -1; }
 
56
 
 
57
  bool                is_correct_size() const;
 
58
  bool                is_valid_position(uint64_t p) const;
 
59
 
 
60
  bool                has_permissions(int prot) const          { return !(prot & ~m_protection); }
 
61
 
 
62
  uint64_t            offset() const                           { return m_offset; }
 
63
 
 
64
  uint64_t            size_bytes() const                       { return m_size; }
 
65
  uint32_t            size_chunks() const                      { return m_range.second - m_range.first; }
 
66
 
 
67
  uint32_t            completed_chunks() const                 { return m_completed; }
 
68
 
 
69
  const range_type&   range() const                            { return m_range; }
 
70
  uint32_t            range_first() const                      { return m_range.first; }
 
71
  uint32_t            range_second() const                     { return m_range.second; }
 
72
 
 
73
  priority_t          priority() const                         { return m_priority; }
 
74
  void                set_priority(priority_t t)               { m_priority = t; }
 
75
 
 
76
  Path*               path()                                   { return &m_path; }
 
77
  const Path*         path() const                             { return &m_path; }
 
78
 
 
79
  const std::string&  frozen_path() const                      { return m_frozenPath; }
 
80
 
 
81
  uint32_t            match_depth_prev() const                 { return m_matchDepthPrev; }
 
82
  uint32_t            match_depth_next() const                 { return m_matchDepthNext; }
 
83
 
 
84
  // This should only be changed by libtorrent.
 
85
  int                 file_descriptor() const                  { return m_fd; }
 
86
  void                set_file_descriptor(int fd)              { m_fd = fd; }
 
87
 
 
88
  // This might actually be wanted, as it would be nice to allow the
 
89
  // File to decide if it needs to try creating the underlying file or
 
90
  // not.
 
91
  bool                prepare(int prot, int flags = 0);
 
92
 
 
93
  int                 protection() const                       { return m_protection; }
 
94
  void                set_protection(int prot)                 { m_protection = prot; }
 
95
 
 
96
  uint64_t            last_touched() const                     { return m_lastTouched; }
 
97
  void                set_last_touched(uint64_t t)             { m_lastTouched = t; }
 
98
 
 
99
protected:
 
100
  void                set_frozen_path(const std::string& path) { m_frozenPath = path; }
 
101
 
 
102
  void                set_offset(uint64_t off)                 { m_offset = off; }
 
103
  void                set_size_bytes(uint64_t size)            { m_size = size; }
 
104
  void                set_range(uint32_t chunkSize);
 
105
 
 
106
  void                set_completed(uint32_t v)                { m_completed = v; }
 
107
  void                inc_completed()                          { m_completed++; }
 
108
 
 
109
  void                set_match_depth_prev(uint32_t l)         { m_matchDepthPrev = l; }
 
110
  void                set_match_depth_next(uint32_t l)         { m_matchDepthNext = l; }
 
111
 
 
112
  bool                resize_file();
 
113
 
 
114
private:
 
115
  File(const File&);
 
116
  void operator = (const File&);
 
117
 
 
118
  Path                m_path;
 
119
 
 
120
  uint64_t            m_offset;
 
121
  uint64_t            m_size;
 
122
 
 
123
  range_type          m_range;
 
124
 
 
125
  uint32_t            m_completed;
 
126
  priority_t          m_priority;
 
127
 
 
128
  uint32_t            m_matchDepthPrev;
 
129
  uint32_t            m_matchDepthNext;
 
130
 
 
131
  // Move the below stuff up to the right places next incompatible API
 
132
  // change.
 
133
  int                 m_fd;
 
134
  std::string         m_frozenPath;
 
135
 
 
136
  int                 m_protection;
 
137
 
 
138
  uint64_t            m_lastTouched;
 
139
};
 
140
 
 
141
inline bool
 
142
File::is_valid_position(uint64_t p) const {
 
143
  return p >= m_offset && p < m_offset + m_size;
 
144
}
 
145
 
 
146
}
 
147
 
 
148
#endif