summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-04-16 14:42:26 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-04-16 14:42:26 +0000
commit28a82bfa4ad7a3b905f00d023f7be7096a2f75d4 (patch)
treef492753c9b55c8dda891f31d00f4fe5eb86b9fc1 /src
parent2a84b9de43c759fb93aa7a0bb2d22d4b5f37af4e (diff)
* src/DirectDiskAdaptor.h: New class.
* src/DirectDiskAdaptor.cc: New class. * src/MultiDiskAdaptor.h: New class. * src/MultiDiskAdaptor.cc: New class. * src/CopyDiskAdaptor.h: New class. * src/CopyDiskAdaptor.cc: New class. * src/DiskAdaptor.h: New class. * src/DiskAdaptor.cc: New class.
Diffstat (limited to 'src')
-rw-r--r--src/CopyDiskAdaptor.cc54
-rw-r--r--src/CopyDiskAdaptor.h47
-rw-r--r--src/DirectDiskAdaptor.cc35
-rw-r--r--src/DirectDiskAdaptor.h37
-rw-r--r--src/DiskAdaptor.cc94
-rw-r--r--src/DiskAdaptor.h77
-rw-r--r--src/MultiDiskAdaptor.cc56
-rw-r--r--src/MultiDiskAdaptor.h43
8 files changed, 443 insertions, 0 deletions
diff --git a/src/CopyDiskAdaptor.cc b/src/CopyDiskAdaptor.cc
new file mode 100644
index 00000000..ca50fab6
--- /dev/null
+++ b/src/CopyDiskAdaptor.cc
@@ -0,0 +1,54 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#include "CopyDiskAdaptor.h"
+#include "Util.h"
+
+CopyDiskAdaptor::CopyDiskAdaptor(DiskWriter* diskWriter):DiskAdaptor(diskWriter) {}
+
+CopyDiskAdaptor::~CopyDiskAdaptor() {}
+
+void CopyDiskAdaptor::onDownloadComplete() {
+ closeFile();
+ fixFilename();
+ openFile();
+}
+
+void CopyDiskAdaptor::fixFilename() {
+ if(topDir != NULL) {
+ topDir->createDir(storeDir, true);
+ }
+ long long int offset = 0;
+ for(FileEntries::iterator itr = fileEntries.begin();
+ itr != fileEntries.end(); itr++) {
+ if(!itr->extracted && itr->requested) {
+ string dest = storeDir+"/"+itr->path;
+ //logger->info("writing file %s", dest.c_str());
+ Util::rangedFileCopy(dest, getFilePath(), offset, itr->length);
+ itr->extracted = true;
+ }
+ offset += itr->length;
+ }
+}
+
+string CopyDiskAdaptor::getFilePath() const {
+ return storeDir+"/"+tempFilename;
+}
diff --git a/src/CopyDiskAdaptor.h b/src/CopyDiskAdaptor.h
new file mode 100644
index 00000000..ec4a16e5
--- /dev/null
+++ b/src/CopyDiskAdaptor.h
@@ -0,0 +1,47 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#ifndef _D_COPY_DISK_ADAPTOR_H_
+#define _D_COPY_DISK_ADAPTOR_H_
+
+#include "DiskAdaptor.h"
+#include "DiskWriter.h"
+
+class CopyDiskAdaptor : public DiskAdaptor {
+private:
+ string tempFilename;
+ void fixFilename();
+protected:
+ string getFilePath() const;
+public:
+ CopyDiskAdaptor(DiskWriter* diskWriter);
+ ~CopyDiskAdaptor();
+
+ virtual void onDownloadComplete();
+
+ // tempFilename is relative to storeDir
+ void setTempFilename(const string& tempFilename) {
+ this->tempFilename = tempFilename;
+ }
+ string getTempFile() const { return this->tempFilename; }
+};
+
+#endif // _D_COPY_DISK_ADAPTOR_H_
diff --git a/src/DirectDiskAdaptor.cc b/src/DirectDiskAdaptor.cc
new file mode 100644
index 00000000..b6676f91
--- /dev/null
+++ b/src/DirectDiskAdaptor.cc
@@ -0,0 +1,35 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#include "DirectDiskAdaptor.h"
+
+DirectDiskAdaptor::DirectDiskAdaptor(DiskWriter* diskWriter):DiskAdaptor(diskWriter) {}
+
+DirectDiskAdaptor::~DirectDiskAdaptor() {}
+
+string DirectDiskAdaptor::getFilePath() const {
+ return storeDir+"/"+fileEntries.front().path;
+}
+
+void DirectDiskAdaptor::onDownloadComplete() {
+ closeFile();
+ openFile();
+}
diff --git a/src/DirectDiskAdaptor.h b/src/DirectDiskAdaptor.h
new file mode 100644
index 00000000..e7abc5dc
--- /dev/null
+++ b/src/DirectDiskAdaptor.h
@@ -0,0 +1,37 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#ifndef _D_DIRECT_DISK_ADAPTOR_H_
+#define _D_DIRECT_DISK_ADAPTOR_H_
+
+#include "DiskAdaptor.h"
+
+class DirectDiskAdaptor : public DiskAdaptor {
+protected:
+ string getFilePath() const;
+public:
+ DirectDiskAdaptor(DiskWriter* diskWriter);
+ virtual ~DirectDiskAdaptor();
+
+ virtual void onDownloadComplete();
+};
+
+#endif // _D_DIRECT_DISK_ADAPTOR_H_
diff --git a/src/DiskAdaptor.cc b/src/DiskAdaptor.cc
new file mode 100644
index 00000000..9f953d4a
--- /dev/null
+++ b/src/DiskAdaptor.cc
@@ -0,0 +1,94 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#include "DiskAdaptor.h"
+#include "DlAbortEx.h"
+
+void DiskAdaptor::openFile() {
+ diskWriter->openFile(getFilePath());
+}
+
+void DiskAdaptor::closeFile() {
+ diskWriter->closeFile();
+}
+
+void DiskAdaptor::openExistingFile() {
+ diskWriter->openExistingFile(getFilePath());
+}
+
+void DiskAdaptor::initAndOpenFile() {
+ diskWriter->initAndOpenFile(getFilePath());
+}
+
+void DiskAdaptor::writeData(const char* data, int len, long long int position) {
+ diskWriter->writeData(data, len, position);
+}
+
+int DiskAdaptor::readData(char* data, int len, long long int position) {
+ return diskWriter->readData(data, len, position);
+}
+
+string DiskAdaptor::sha1Sum(long long int offset, long long int length) {
+ return diskWriter->sha1Sum(offset, length);
+}
+
+FileEntry DiskAdaptor::getFileEntryFromPath(const string& fileEntryPath) const {
+ for(FileEntries::const_iterator itr = fileEntries.begin();
+ itr != fileEntries.end(); itr++) {
+ if(itr->path == fileEntryPath) {
+ return *itr;
+ }
+ }
+ throw new DlAbortEx("no such file entry <%s>", fileEntryPath.c_str());
+}
+
+bool DiskAdaptor::addDownloadEntry(const string& fileEntryPath) {
+ for(FileEntries::iterator itr = fileEntries.begin();
+ itr != fileEntries.end(); itr++) {
+ if(itr->path == fileEntryPath) {
+ itr->requested = true;
+ return true;
+ }
+ }
+ return false;
+}
+
+bool DiskAdaptor::addDownloadEntry(int index) {
+ if(fileEntries.size() <= (unsigned int)index) {
+ return false;
+ }
+ fileEntries.at(index).requested = true;
+ return true;
+}
+
+void DiskAdaptor::addAllDownloadEntry() {
+ for(FileEntries::iterator itr = fileEntries.begin();
+ itr != fileEntries.end(); itr++) {
+ itr->requested = true;
+ }
+}
+
+void DiskAdaptor::removeAllDownloadEntry() {
+ for(FileEntries::iterator itr = fileEntries.begin();
+ itr != fileEntries.end(); itr++) {
+ itr->requested = false;
+ }
+}
diff --git a/src/DiskAdaptor.h b/src/DiskAdaptor.h
new file mode 100644
index 00000000..9e2ba46e
--- /dev/null
+++ b/src/DiskAdaptor.h
@@ -0,0 +1,77 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#ifndef _D_DISK_ADAPTOR_H_
+#define _D_DISK_ADAPTOR_H_
+
+#include "common.h"
+#include "FileEntry.h"
+#include "Directory.h"
+#include "DiskWriter.h"
+
+class DiskAdaptor {
+protected:
+ DiskWriter* diskWriter;
+ string storeDir;
+ FileEntries fileEntries;
+ const Directory* topDir;
+ virtual string getFilePath() const = 0;
+public:
+ DiskAdaptor(DiskWriter* diskWriter):diskWriter(diskWriter), topDir(NULL) {}
+ virtual ~DiskAdaptor() {
+ delete diskWriter;
+ if(topDir != NULL) {
+ delete topDir;
+ }
+ }
+
+ virtual void openFile();
+ virtual void closeFile();
+ virtual void openExistingFile();
+ virtual void initAndOpenFile();
+ void writeData(const char* data, int len, long long int position);
+ int readData(char* data, int len, long long int position);
+ string sha1Sum(long long int offset, long long int length);
+
+ virtual void onDownloadComplete() = 0;
+
+ void setFileEntries(const FileEntries& fileEntries) {
+ this->fileEntries = fileEntries;
+ }
+ FileEntry getFileEntryFromPath(const string& fileEntryPath) const;
+ bool addDownloadEntry(const string& fileEntryPath);
+ bool addDownloadEntry(int index);
+ void addAllDownloadEntry();
+ void removeAllDownloadEntry();
+
+ void setStoreDir(const string& storeDir) { this->storeDir = storeDir; }
+ string getStoreDir() const { return this->storeDir; }
+
+ void setTopDir(const Directory* dirctory) {
+ if(this->topDir != NULL) {
+ delete topDir;
+ }
+ this->topDir = dirctory;
+ }
+ const Directory* getTopDir() const { return this->topDir; }
+};
+
+#endif // _D_DISK_ADAPTOR_H_
diff --git a/src/MultiDiskAdaptor.cc b/src/MultiDiskAdaptor.cc
new file mode 100644
index 00000000..0f9f1695
--- /dev/null
+++ b/src/MultiDiskAdaptor.cc
@@ -0,0 +1,56 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#include "MultiDiskAdaptor.h"
+
+MultiDiskAdaptor::MultiDiskAdaptor(MultiDiskWriter* diskWriter):DiskAdaptor(diskWriter) {}
+
+MultiDiskAdaptor::~MultiDiskAdaptor() {}
+
+void MultiDiskAdaptor::setDiskWriterFileEntries() {
+ ((MultiDiskWriter*)diskWriter)->setFileEntries(fileEntries);
+}
+
+string MultiDiskAdaptor::getFilePath() const {
+ return storeDir;
+}
+
+void MultiDiskAdaptor::openFile() {
+ topDir->createDir(storeDir, true);
+ setDiskWriterFileEntries();
+ DiskAdaptor::openFile();
+}
+
+void MultiDiskAdaptor::initAndOpenFile() {
+ topDir->createDir(storeDir, true);
+ setDiskWriterFileEntries();
+ DiskAdaptor::initAndOpenFile();
+}
+
+void MultiDiskAdaptor::openExistingFile() {
+ setDiskWriterFileEntries();
+ DiskAdaptor::openExistingFile();
+}
+
+void MultiDiskAdaptor::onDownloadComplete() {
+ closeFile();
+ openFile();
+}
diff --git a/src/MultiDiskAdaptor.h b/src/MultiDiskAdaptor.h
new file mode 100644
index 00000000..821c0ae3
--- /dev/null
+++ b/src/MultiDiskAdaptor.h
@@ -0,0 +1,43 @@
+/* <!-- copyright */
+/*
+ * aria2 - a simple utility for downloading files faster
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* copyright --> */
+#ifndef _D_MULTI_DISK_ADAPTOR_H_
+#define _D_MULTI_DISK_ADAPTOR_H_
+
+#include "DiskAdaptor.h"
+#include "MultiDiskWriter.h"
+
+class MultiDiskAdaptor : public DiskAdaptor {
+private:
+ void setDiskWriterFileEntries();
+protected:
+ virtual string getFilePath() const;
+public:
+ MultiDiskAdaptor(MultiDiskWriter* diskWriter);
+ virtual ~MultiDiskAdaptor();
+
+ virtual void openFile();
+ virtual void initAndOpenFile();
+ virtual void openExistingFile();
+ virtual void onDownloadComplete();
+};
+
+#endif // _D_MULTI_DISK_ADAPTOR_H_