From b733431ac4d378c66d13eb590a771504bcb290dc Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 9 May 2013 00:48:41 +0900 Subject: [PATCH] Add DownloadHandle::getNumFiles and getFile API --- src/aria2api.cc | 52 +++++++++++++++++++++++++++++++------- src/includes/aria2/aria2.h | 7 +++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/aria2api.cc b/src/aria2api.cc index 30f0b02ea..7c61d3be9 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -391,6 +391,22 @@ void createUriEntry } } // namespace +namespace { +FileData createFileData +(const SharedHandle& fe, int index, const BitfieldMan* bf) +{ + FileData file; + file.index = index; + file.path = fe->getPath(); + file.length = fe->getLength(); + file.completedLength = bf->getOffsetCompletedLength + (fe->getOffset(), fe->getLength()); + file.selected = fe->isRequested(); + createUriEntry(std::back_inserter(file.uris), fe); + return file; +} +} // namespace + namespace { template void createFileEntry @@ -400,15 +416,7 @@ void createFileEntry { size_t index = 1; for(; first != last; ++first) { - FileData file; - file.index = index++; - file.path = (*first)->getPath(); - file.length = (*first)->getLength(); - file.completedLength = bf->getOffsetCompletedLength - ((*first)->getOffset(), (*first)->getLength()); - file.selected = (*first)->isRequested(); - createUriEntry(std::back_inserter(file.uris), *first); - out++ = file; + out++ = createFileData(*first, index++, bf); } } } // namespace @@ -530,6 +538,21 @@ struct RequestGroupDH : public DownloadHandle { group->getPieceStorage()); return res; } + virtual int getNumFiles() + { + const SharedHandle& dctx = group->getDownloadContext(); + return dctx->getFileEntries().size(); + } + virtual FileData getFile(int index) + { + const SharedHandle& dctx = group->getDownloadContext(); + BitfieldMan bf(dctx->getPieceLength(), dctx->getTotalLength()); + const SharedHandle& ps = group->getPieceStorage(); + if(ps) { + bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength()); + } + return createFileData(dctx->getFileEntries()[index-1], index, &bf); + } SharedHandle group; TransferStat ts; }; @@ -608,6 +631,17 @@ struct DownloadResultDH : public DownloadHandle { dr->totalLength, dr->pieceLength, dr->bitfield); return res; } + virtual int getNumFiles() + { + return dr->fileEntries.size(); + } + virtual FileData getFile(int index) + { + BitfieldMan bf(dr->pieceLength, dr->totalLength); + bf.setBitfield(reinterpret_cast(dr->bitfield.data()), + dr->bitfield.size()); + return createFileData(dr->fileEntries[index-1], index, &bf); + } SharedHandle dr; }; } // namespace diff --git a/src/includes/aria2/aria2.h b/src/includes/aria2/aria2.h index add04f477..9e025866c 100644 --- a/src/includes/aria2/aria2.h +++ b/src/includes/aria2/aria2.h @@ -217,6 +217,7 @@ struct UriData { }; struct FileData { + // 1-based index int index; std::string path; int64_t length; @@ -265,6 +266,12 @@ struct DownloadHandle { virtual A2Gid getBelongsTo() = 0; virtual const std::string& getDir() = 0; virtual std::vector getFiles() = 0; + // Returns the number of files. The return value is equivalent to + // getFiles().size(). + virtual int getNumFiles() = 0; + // Returns the FileData of the file at the specified |index|. Please + // note that the index is 1-based. + virtual FileData getFile(int index) = 0; }; // Returns handle for the download denoted by the |gid|. The caller