From 10978a9e22ce8c606d157e16366430e1d97fa47d Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Wed, 7 Jun 2023 11:07:49 -0400
Subject: [PATCH] the actual directory downloading function (used by
 list-dircap example app)

---
 src/Tahoe/Download.hs | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/src/Tahoe/Download.hs b/src/Tahoe/Download.hs
index dc62783..c5a64c4 100644
--- a/src/Tahoe/Download.hs
+++ b/src/Tahoe/Download.hs
@@ -229,3 +229,29 @@ downloadShare storageIndex (shareNum, s) = do
     let massaged = first (ShareDownloadError . (displayException :: SomeException -> String)) shareBytes
     print' "Downloaded it"
     pure (shareNum, LB.fromStrict <$> massaged)
+
+{- | Download the data associated with a directory capability and interpret it
+ as a collection of entries.
+-}
+downloadDirectory ::
+    (MonadIO m, Readable readCap, Verifiable v, Verifier readCap ~ v) =>
+    -- | Information about the servers from which to consider downloading shares
+    -- representing the application data.
+    Map.Map StorageServerID StorageServerAnnouncement ->
+    -- | The read capability for the application data.
+    DirectoryCapability readCap ->
+    -- | Get functions for interacting with a server given its URL.
+    LookupServer m ->
+    -- | Either a description of how the recovery failed or the recovered
+    -- application data.
+    m (Either DirectoryDownloadError Directory)
+downloadDirectory anns (DirectoryCapability cap) lookupServer = do
+    bs <- download anns cap lookupServer
+    pure $ do
+        bs' <- first UnderlyingDownloadError bs
+        first (const DecodingError) $ Directory.parse (LB.toStrict bs')
+
+data DirectoryDownloadError
+    = UnderlyingDownloadError DownloadError
+    | DecodingError
+    deriving (Ord, Eq, Show)
-- 
GitLab