libktorrent  2.2.0
cachefile.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTCACHEFILE_H
21 #define BTCACHEFILE_H
22 
23 #include <QMap>
24 #include <QMutex>
25 #include <QFile>
26 #include <QSharedPointer>
27 #include <util/constants.h>
28 
29 namespace bt
30 {
31  class PreallocationThread;
32 
33 
38  class MMappeable
39  {
40  public:
41  virtual ~MMappeable() {}
42 
46  virtual void unmapped() = 0;
47  };
48 
54  class CacheFile : public QObject
55  {
56  Q_OBJECT
57  public:
58  CacheFile();
59  ~CacheFile() override;
60 
61  enum Mode
62  {
63  READ,WRITE,RW
64  };
65 
66 
73  void open(const QString & path,Uint64 size);
74 
76  void changePath(const QString & npath);
77 
87  void* map(MMappeable* thing,Uint64 off,Uint32 size,Mode mode);
88 
94  void unmap(void* ptr,Uint32 size);
95 
100  void close();
101 
108  void read(Uint8* buf,Uint32 size,Uint64 off);
109 
116  void write(const Uint8* buf,Uint32 size,Uint64 off);
117 
122 
124  Uint64 diskUsage();
125 
126  typedef QSharedPointer<CacheFile> Ptr;
127 
128  private:
129  void growFile(Uint64 to_write);
130  void closeTemporary();
131  void openFile(Mode mode);
132  void unmapAll();
133  bool allocateBytes(bt::Uint64 off,bt::Uint64 size);
134 
135  private Q_SLOTS:
136  void aboutToClose();
137 
138  private:
139  QFile* fptr;
140  bool read_only;
141  Uint64 max_size,file_size;
142  QString path;
143  struct Entry
144  {
145  MMappeable* thing;
146  void* ptr;
147  Uint32 size;
148  Uint64 offset;
149  Uint32 diff;
150  Mode mode;
151  };
152  QMap<void*,Entry> mappings; // mappings where offset wasn't a multiple of 4K
153  mutable QMutex mutex;
154  bool manual_close;
155  };
156 
157 }
158 
159 #endif
bt::CacheFile::changePath
void changePath(const QString &npath)
Change the path of the file.
bt::CacheFile::diskUsage
Uint64 diskUsage()
Get the number of bytes this cache file is taking up.
bt::CacheFile::preallocate
void preallocate(PreallocationThread *prealloc)
bt::MMappeable
Definition: cachefile.h:39
bt::CacheFile::map
void * map(MMappeable *thing, Uint64 off, Uint32 size, Mode mode)
bt::PreallocationThread
Definition: preallocationthread.h:42
bt::CacheFile::read
void read(Uint8 *buf, Uint32 size, Uint64 off)
bt::CacheFile::open
void open(const QString &path, Uint64 size)
bt::MMappeable::unmapped
virtual void unmapped()=0
bt::CacheFile::write
void write(const Uint8 *buf, Uint32 size, Uint64 off)
bt::CacheFile::unmap
void unmap(void *ptr, Uint32 size)
bt::CacheFile
Definition: cachefile.h:55
bt::CacheFile::close
void close()