summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-08-13 16:27:28 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-08-13 16:27:28 +0000
commitbdb12bc15138469c67facb553a264b50af45d065 (patch)
treecfef8d07cc2b9b0066521900187c25bb5778ee9b /src
parentde4de77503973ae3b2be186eeb8bbb1ba277d04b (diff)
2008-08-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Now aria2 uses name attribute in Metalink as local filename in BitTorrent downloads. BUG#2033999 * src/BtContext.h * src/BtDependency.cc * src/DefaultBtContext.cc * src/DefaultBtContext.h * src/SingleFileDownloadContext.cc * src/SingleFileDownloadContext.h * test/BtDependencyTest.cc * test/DefaultBtContextTest.cc * test/MockBtContext.h
Diffstat (limited to 'src')
-rw-r--r--src/BtContext.h3
-rw-r--r--src/BtDependency.cc3
-rw-r--r--src/DefaultBtContext.cc30
-rw-r--r--src/DefaultBtContext.h17
-rw-r--r--src/SingleFileDownloadContext.cc5
-rw-r--r--src/SingleFileDownloadContext.h5
6 files changed, 42 insertions, 21 deletions
diff --git a/src/BtContext.h b/src/BtContext.h
index 3b867e46..15b184fb 100644
--- a/src/BtContext.h
+++ b/src/BtContext.h
@@ -61,7 +61,8 @@ public:
virtual std::deque<SharedHandle<AnnounceTier> >
getAnnounceTiers() const = 0;
- virtual void load(const std::string& torrentFile) = 0;
+ virtual void load(const std::string& torrentFile,
+ const std::string& overrideName = "") = 0;
/**
* Returns the peer id of localhost, 20 byte length
diff --git a/src/BtDependency.cc b/src/BtDependency.cc
index 36c30fe7..75de477e 100644
--- a/src/BtDependency.cc
+++ b/src/BtDependency.cc
@@ -70,7 +70,8 @@ bool BtDependency::resolve()
diskAdaptor->openExistingFile();
std::string content = Util::toString(diskAdaptor);
btContext->loadFromMemory(content,
- File(dependee->getFilePath()).getBasename());
+ File(dependee->getFilePath()).getBasename(),
+ _dependant->getDownloadContext()->getName());
if(_option->defined(PREF_PEER_ID_PREFIX)) {
btContext->setPeerIdPrefix(_option->get(PREF_PEER_ID_PREFIX));
}
diff --git a/src/DefaultBtContext.cc b/src/DefaultBtContext.cc
index 8c407e04..7e5b292f 100644
--- a/src/DefaultBtContext.cc
+++ b/src/DefaultBtContext.cc
@@ -114,12 +114,18 @@ void DefaultBtContext::extractPieceHash(const unsigned char* hashData,
void DefaultBtContext::extractFileEntries(const Dictionary* infoDic,
const std::string& defaultName,
+ const std::string& overrideName,
const std::deque<std::string>& urlList) {
- const Data* nameData = dynamic_cast<const Data*>(infoDic->get(BtContext::C_NAME));
- if(nameData) {
- name = nameData->toString();
+ if(overrideName.empty()) {
+ const Data* nameData =
+ dynamic_cast<const Data*>(infoDic->get(BtContext::C_NAME));
+ if(nameData) {
+ name = nameData->toString();
+ } else {
+ name = File(defaultName).getBasename()+".file";
+ }
} else {
- name = File(defaultName).getBasename()+".file";
+ name = overrideName;
}
const List* files = dynamic_cast<const List*>(infoDic->get(BtContext::C_FILES));
if(files) {
@@ -271,7 +277,8 @@ void DefaultBtContext::extractNodes(const List* nodes)
void DefaultBtContext::loadFromMemory(const unsigned char* content,
size_t length,
- const std::string& defaultName)
+ const std::string& defaultName,
+ const std::string& overrideName)
{
SharedHandle<MetaEntry> rootEntry(MetaFileUtil::bdecoding(content, length));
const Dictionary* rootDic = dynamic_cast<const Dictionary*>(rootEntry.get());
@@ -279,20 +286,23 @@ void DefaultBtContext::loadFromMemory(const unsigned char* content,
throw DlAbortEx
(StringFormat("torrent file does not contain a root dictionary .").str());
}
- processRootDictionary(rootDic, defaultName);
+ processRootDictionary(rootDic, defaultName, overrideName);
}
-void DefaultBtContext::load(const std::string& torrentFile) {
+void DefaultBtContext::load(const std::string& torrentFile,
+ const std::string& overrideName) {
SharedHandle<MetaEntry> rootEntry(MetaFileUtil::parseMetaFile(torrentFile));
const Dictionary* rootDic = dynamic_cast<const Dictionary*>(rootEntry.get());
if(!rootDic) {
throw DlAbortEx
(StringFormat("torrent file does not contain a root dictionary .").str());
}
- processRootDictionary(rootDic, torrentFile);
+ processRootDictionary(rootDic, torrentFile, overrideName);
}
-void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const std::string& defaultName)
+void DefaultBtContext::processRootDictionary(const Dictionary* rootDic,
+ const std::string& defaultName,
+ const std::string& overrideName)
{
clear();
const Dictionary* infoDic =
@@ -347,7 +357,7 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
std::deque<std::string> urlList;
extractUrlList(urlList, rootDic->get(BtContext::C_URL_LIST));
// retrieve file entries
- extractFileEntries(infoDic, defaultName, urlList);
+ extractFileEntries(infoDic, defaultName, overrideName, urlList);
if((totalLength+pieceLength-1)/pieceLength != numPieces) {
throw DlAbortEx("Too few/many piece hash.");
}
diff --git a/src/DefaultBtContext.h b/src/DefaultBtContext.h
index 8cc3aeba..b83f1f8f 100644
--- a/src/DefaultBtContext.h
+++ b/src/DefaultBtContext.h
@@ -78,6 +78,7 @@ private:
size_t hashLength);
void extractFileEntries(const Dictionary* infoDic,
const std::string& defaultName,
+ const std::string& overrideName,
const std::deque<std::string>& urlList);
void extractAnnounce(const Data* announceData);
void extractAnnounceList(const List* announceListData);
@@ -86,7 +87,9 @@ private:
void extractNodes(const List* nodes);
- void processRootDictionary(const Dictionary* rootDic, const std::string& defaultName);
+ void processRootDictionary(const Dictionary* rootDic,
+ const std::string& defaultName,
+ const std::string& overrideName);
public:
DefaultBtContext();
@@ -115,15 +118,19 @@ private:
virtual std::deque<SharedHandle<AnnounceTier> > getAnnounceTiers() const;
- virtual void load(const std::string& torrentFile);
+ virtual void load(const std::string& torrentFile,
+ const std::string& overrideName = "");
void loadFromMemory(const unsigned char* content, size_t length,
- const std::string& defaultName);
+ const std::string& defaultName,
+ const std::string& overrideName = "");
- void loadFromMemory(const std::string& context, const std::string& defaultName)
+ void loadFromMemory(const std::string& context,
+ const std::string& defaultName,
+ const std::string& overrideName = "")
{
loadFromMemory(reinterpret_cast<const unsigned char*>(context.c_str()),
- context.size(), defaultName);
+ context.size(), defaultName, overrideName);
}
virtual const std::string& getName() const;
diff --git a/src/SingleFileDownloadContext.cc b/src/SingleFileDownloadContext.cc
index c54a35a3..c32ad208 100644
--- a/src/SingleFileDownloadContext.cc
+++ b/src/SingleFileDownloadContext.cc
@@ -96,4 +96,9 @@ void SingleFileDownloadContext::setTotalLength(uint64_t totalLength)
_fileEntry->setLength(totalLength);
}
+const std::string& SingleFileDownloadContext::getName() const
+{
+ return _fileEntry->getPath();
+}
+
} // namespace aria2
diff --git a/src/SingleFileDownloadContext.h b/src/SingleFileDownloadContext.h
index b15bdec5..f1944c94 100644
--- a/src/SingleFileDownloadContext.h
+++ b/src/SingleFileDownloadContext.h
@@ -95,10 +95,7 @@ public:
virtual std::deque<SharedHandle<FileEntry> > getFileEntries() const;
- virtual const std::string& getName() const
- {
- return _filename;
- }
+ virtual const std::string& getName() const;
virtual size_t getPieceLength() const
{