Parcourir la source

Add DownloadHandle::getFiles() API function

Tatsuhiro Tsujikawa il y a 12 ans
Parent
commit
2109ba23a8
2 fichiers modifiés avec 129 ajouts et 0 suppressions
  1. 109 0
      src/aria2api.cc
  2. 20 0
      src/includes/aria2/aria2.h

+ 109 - 0
src/aria2api.cc

@@ -52,6 +52,9 @@
 #include "LogFactory.h"
 #include "PieceStorage.h"
 #include "DownloadContext.h"
+#include "FileEntry.h"
+#include "BitfieldMan.h"
+#include "DownloadContext.h"
 
 namespace aria2 {
 
@@ -217,6 +220,93 @@ std::vector<A2Gid> getActiveDownload(Session* session)
   return res;
 }
 
+namespace {
+template<typename OutputIterator, typename InputIterator>
+void createUriEntry
+(OutputIterator out,
+ InputIterator first, InputIterator last,
+ UriStatus status)
+{
+  for(; first != last; ++first) {
+    UriData uriData;
+    uriData.uri = *first;
+    uriData.status = status;
+    out++ = uriData;
+  }
+}
+} // namespace
+
+namespace {
+template<typename OutputIterator>
+void createUriEntry
+(OutputIterator out, const SharedHandle<FileEntry>& file)
+{
+  createUriEntry(out,
+                 file->getSpentUris().begin(),
+                 file->getSpentUris().end(),
+                 URI_USED);
+  createUriEntry(out,
+                 file->getRemainingUris().begin(),
+                 file->getRemainingUris().end(),
+                 URI_WAITING);
+}
+} // namespace
+
+namespace {
+template<typename OutputIterator, typename InputIterator>
+void createFileEntry
+(OutputIterator out,
+ InputIterator first, InputIterator last,
+ const BitfieldMan* bf)
+{
+  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;
+  }
+}
+} // namespace
+
+namespace {
+template<typename OutputIterator, typename InputIterator>
+void createFileEntry
+(OutputIterator out,
+ InputIterator first, InputIterator last,
+ int64_t totalLength,
+ int32_t pieceLength,
+ const std::string& bitfield)
+{
+  BitfieldMan bf(pieceLength, totalLength);
+  bf.setBitfield(reinterpret_cast<const unsigned char*>(bitfield.data()),
+                 bitfield.size());
+  createFileEntry(out, first, last, &bf);
+}
+} // namespace
+
+namespace {
+template<typename OutputIterator, typename InputIterator>
+void createFileEntry
+(OutputIterator out,
+ InputIterator first, InputIterator last,
+ int64_t totalLength,
+ int32_t pieceLength,
+ const SharedHandle<PieceStorage>& ps)
+{
+  BitfieldMan bf(pieceLength, totalLength);
+  if(ps) {
+    bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength());
+  }
+  createFileEntry(out, first, last, &bf);
+}
+} // namespace
+
 namespace {
 struct RequestGroupDH : public DownloadHandle {
   RequestGroupDH(const SharedHandle<RequestGroup>& group)
@@ -290,6 +380,17 @@ struct RequestGroupDH : public DownloadHandle {
   {
     return group->getOption()->get(PREF_DIR);
   }
+  virtual std::vector<FileData> getFiles()
+  {
+    std::vector<FileData> res;
+    const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
+    createFileEntry(std::back_inserter(res),
+                    dctx->getFileEntries().begin(),
+                    dctx->getFileEntries().end(),
+                    dctx->getTotalLength(), dctx->getPieceLength(),
+                    group->getPieceStorage());
+    return res;
+  }
   SharedHandle<RequestGroup> group;
   TransferStat ts;
 };
@@ -360,6 +461,14 @@ struct DownloadResultDH : public DownloadHandle {
   {
     return dr->dir;
   }
+  virtual std::vector<FileData> getFiles()
+  {
+    std::vector<FileData> res;
+    createFileEntry(std::back_inserter(res),
+                    dr->fileEntries.begin(), dr->fileEntries.end(),
+                    dr->totalLength, dr->pieceLength, dr->bitfield);
+    return res;
+  }
   SharedHandle<DownloadResult> dr;
 };
 } // namespace

+ 20 - 0
src/includes/aria2/aria2.h

@@ -134,6 +134,25 @@ int addUri(Session* session,
 // Returns the array of active download GID.
 std::vector<A2Gid> getActiveDownload(Session* session);
 
+enum UriStatus {
+  URI_USED,
+  URI_WAITING
+};
+
+struct UriData {
+  std::string uri;
+  UriStatus status;
+};
+
+struct FileData {
+  int index;
+  std::string path;
+  int64_t length;
+  int64_t completedLength;
+  bool selected;
+  std::vector<UriData> uris;
+};
+
 enum DOWNLOAD_STATUS {
   DOWNLOAD_ACTIVE,
   DOWNLOAD_WAITING,
@@ -158,6 +177,7 @@ struct DownloadHandle {
   virtual const std::vector<A2Gid>& getFollowedBy() = 0;
   virtual A2Gid getBelongsTo() = 0;
   virtual const std::string& getDir() = 0;
+  virtual std::vector<FileData> getFiles() = 0;
 };
 
 // Returns handle for the download denoted by the |gid|. The caller