aria2/src/MultiDiskAdaptor.h

169 lines
4.3 KiB
C++

/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#ifndef _D_MULTI_DISK_ADAPTOR_H_
#define _D_MULTI_DISK_ADAPTOR_H_
#include "DiskAdaptor.h"
namespace aria2 {
class MultiFileAllocationIterator;
class FileEntry;
class DiskWriter;
class DiskWriterEntry {
private:
SharedHandle<FileEntry> fileEntry;
SharedHandle<DiskWriter> diskWriter;
public:
DiskWriterEntry(const SharedHandle<FileEntry>& fileEntry);
~DiskWriterEntry();
std::string getFilePath(const std::string& topDir) const;
void initAndOpenFile(const std::string& topDir);
void openFile(const std::string& topDir);
void openExistingFile(const std::string& topDir);
void closeFile();
bool fileExists(const std::string& topDir);
int64_t size() const;
SharedHandle<FileEntry> getFileEntry() const;
void setDiskWriter(const SharedHandle<DiskWriter>& diskWriter);
SharedHandle<DiskWriter> getDiskWriter() const;
bool operator<(const DiskWriterEntry& entry) const;
};
typedef SharedHandle<DiskWriterEntry> DiskWriterEntryHandle;
typedef std::deque<DiskWriterEntryHandle> DiskWriterEntries;
class MultiDiskAdaptor : public DiskAdaptor {
friend class MultiFileAllocationIterator;
private:
std::string topDir;
int32_t pieceLength;
DiskWriterEntries diskWriterEntries;
bool _directIOAllowed;
void resetDiskWriterEntries();
void mkdir() const;
bool isInRange(const DiskWriterEntryHandle entry, int64_t offset) const;
int32_t calculateLength(const DiskWriterEntryHandle entry,
int64_t fileOffset,
int32_t rem) const;
std::string getTopDirPath() const;
public:
MultiDiskAdaptor();
virtual ~MultiDiskAdaptor();
virtual void initAndOpenFile();
virtual void openFile();
virtual void openExistingFile();
virtual void closeFile();
virtual void onDownloadComplete();
virtual void writeData(const unsigned char* data, int32_t len,
int64_t offset);
virtual int32_t readData(unsigned char* data, int32_t len, int64_t offset);
virtual bool fileExists();
virtual std::string getFilePath()
{
return getTopDirPath();
}
virtual int64_t size() const;
virtual SharedHandle<FileAllocationIterator> fileAllocationIterator();
virtual void enableDirectIO();
virtual void disableDirectIO();
void setTopDir(const std::string& topDir) {
this->topDir = topDir;
}
const std::string& getTopDir() const {
return topDir;
}
void setPieceLength(int32_t pieceLength) {
this->pieceLength = pieceLength;
}
int32_t getPieceLength() const {
return pieceLength;
}
virtual bool directIOAllowed() const
{
return _directIOAllowed;
}
void setDirectIOAllowed(bool b)
{
_directIOAllowed = b;
}
};
typedef SharedHandle<MultiDiskAdaptor> MultiDiskAdaptorHandle;
} // namespace aria2
#endif // _D_MULTI_DISK_ADAPTOR_H_