Skip to content

Commit 1b8110a

Browse files
authored
xftp server: restore file status from log (#1461)
* xftp server: restore file blocking info from log * fix parse * rework * update * rename
1 parent dad7e1b commit 1b8110a

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/Simplex/FileTransfer/Server.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ processXFTPRequest HTTP2Body {bodyPart} = \case
415415
sId <- ExceptT $ addFileRetry st file 3 ts
416416
rcps <- mapM (ExceptT . addRecipientRetry st 3 sId) rks
417417
lift $ withFileLog $ \sl -> do
418-
logAddFile sl sId file ts
418+
logAddFile sl sId file ts EntityActive
419419
logAddRecipients sl sId rcps
420420
stats <- asks serverStats
421421
lift $ incFileStat filesCreated
@@ -426,7 +426,7 @@ processXFTPRequest HTTP2Body {bodyPart} = \case
426426
addFileRetry :: FileStore -> FileInfo -> Int -> RoundedSystemTime -> M (Either XFTPErrorType XFTPFileId)
427427
addFileRetry st file n ts =
428428
retryAdd n $ \sId -> runExceptT $ do
429-
ExceptT $ addFile st sId file ts
429+
ExceptT $ addFile st sId file ts EntityActive
430430
pure sId
431431
addRecipientRetry :: FileStore -> Int -> XFTPFileId -> RcvPublicAuthKey -> M (Either XFTPErrorType FileRecipient)
432432
addRecipientRetry st n sId rpk =

src/Simplex/FileTransfer/Server/Store.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ newFileStore = do
7070
usedStorage <- newTVarIO 0
7171
pure FileStore {files, recipients, usedStorage}
7272

73-
addFile :: FileStore -> SenderId -> FileInfo -> RoundedSystemTime -> STM (Either XFTPErrorType ())
74-
addFile FileStore {files} sId fileInfo createdAt =
73+
addFile :: FileStore -> SenderId -> FileInfo -> RoundedSystemTime -> ServerEntityStatus -> STM (Either XFTPErrorType ())
74+
addFile FileStore {files} sId fileInfo createdAt status =
7575
ifM (TM.member sId files) (pure $ Left DUPLICATE_) $ do
76-
f <- newFileRec sId fileInfo createdAt
76+
f <- newFileRec sId fileInfo createdAt status
7777
TM.insert sId f files
7878
pure $ Right ()
7979

80-
newFileRec :: SenderId -> FileInfo -> RoundedSystemTime -> STM FileRec
81-
newFileRec senderId fileInfo createdAt = do
80+
newFileRec :: SenderId -> FileInfo -> RoundedSystemTime -> ServerEntityStatus -> STM FileRec
81+
newFileRec senderId fileInfo createdAt status = do
8282
recipientIds <- newTVar S.empty
8383
filePath <- newTVar Nothing
84-
fileStatus <- newTVar EntityActive
84+
fileStatus <- newTVar status
8585
pure FileRec {senderId, fileInfo, filePath, recipientIds, createdAt, fileStatus}
8686

8787
setFilePath :: FileStore -> SenderId -> FilePath -> STM (Either XFTPErrorType ())

src/Simplex/FileTransfer/Server/StoreLog.hs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ module Simplex.FileTransfer.Server.StoreLog
1919
)
2020
where
2121

22+
import Control.Applicative ((<|>))
2223
import Control.Concurrent.STM
2324
import Control.Monad.Except
2425
import qualified Data.Attoparsec.ByteString.Char8 as A
2526
import qualified Data.ByteString.Char8 as B
2627
import qualified Data.ByteString.Lazy.Char8 as LB
27-
import Data.Composition ((.:), (.:.))
28+
import Data.Composition ((.:), (.::))
2829
import Data.List.NonEmpty (NonEmpty)
2930
import qualified Data.List.NonEmpty as L
3031
import Data.Map.Strict (Map)
@@ -33,13 +34,13 @@ import Simplex.FileTransfer.Protocol (FileInfo (..))
3334
import Simplex.FileTransfer.Server.Store
3435
import Simplex.Messaging.Encoding.String
3536
import Simplex.Messaging.Protocol (BlockingInfo, RcvPublicAuthKey, RecipientId, SenderId)
36-
import Simplex.Messaging.Server.QueueStore (RoundedSystemTime)
37+
import Simplex.Messaging.Server.QueueStore (RoundedSystemTime, ServerEntityStatus (..))
3738
import Simplex.Messaging.Server.StoreLog
3839
import Simplex.Messaging.Util (bshow)
3940
import System.IO
4041

4142
data FileStoreLogRecord
42-
= AddFile SenderId FileInfo RoundedSystemTime
43+
= AddFile SenderId FileInfo RoundedSystemTime ServerEntityStatus
4344
| PutFile SenderId FilePath
4445
| AddRecipients SenderId (NonEmpty FileRecipient)
4546
| DeleteFile SenderId
@@ -49,15 +50,15 @@ data FileStoreLogRecord
4950

5051
instance StrEncoding FileStoreLogRecord where
5152
strEncode = \case
52-
AddFile sId file createdAt -> strEncode (Str "FNEW", sId, file, createdAt)
53+
AddFile sId file createdAt status -> strEncode (Str "FNEW", sId, file, createdAt, status)
5354
PutFile sId path -> strEncode (Str "FPUT", sId, path)
5455
AddRecipients sId rcps -> strEncode (Str "FADD", sId, rcps)
5556
DeleteFile sId -> strEncode (Str "FDEL", sId)
5657
BlockFile sId info -> strEncode (Str "FBLK", sId, info)
5758
AckFile rId -> strEncode (Str "FACK", rId)
5859
strP =
5960
A.choice
60-
[ "FNEW " *> (AddFile <$> strP_ <*> strP_ <*> strP),
61+
[ "FNEW " *> (AddFile <$> strP_ <*> strP_ <*> strP <*> (_strP <|> pure EntityActive)),
6162
"FPUT " *> (PutFile <$> strP_ <*> strP),
6263
"FADD " *> (AddRecipients <$> strP_ <*> strP),
6364
"FDEL " *> (DeleteFile <$> strP),
@@ -68,8 +69,8 @@ instance StrEncoding FileStoreLogRecord where
6869
logFileStoreRecord :: StoreLog 'WriteMode -> FileStoreLogRecord -> IO ()
6970
logFileStoreRecord = writeStoreLogRecord
7071

71-
logAddFile :: StoreLog 'WriteMode -> SenderId -> FileInfo -> RoundedSystemTime -> IO ()
72-
logAddFile s = logFileStoreRecord s .:. AddFile
72+
logAddFile :: StoreLog 'WriteMode -> SenderId -> FileInfo -> RoundedSystemTime -> ServerEntityStatus -> IO ()
73+
logAddFile s = logFileStoreRecord s .:: AddFile
7374

7475
logPutFile :: StoreLog 'WriteMode -> SenderId -> FilePath -> IO ()
7576
logPutFile s = logFileStoreRecord s .: PutFile
@@ -99,7 +100,7 @@ readFileStore f st = mapM_ (addFileLogRecord . LB.toStrict) . LB.lines =<< LB.re
99100
Left e -> B.putStrLn $ "Log processing error (" <> bshow e <> "): " <> B.take 100 s
100101
_ -> pure ()
101102
addToStore = \case
102-
AddFile sId file createdAt -> addFile st sId file createdAt
103+
AddFile sId file createdAt status -> addFile st sId file createdAt status
103104
PutFile qId path -> setFilePath st qId path
104105
AddRecipients sId rcps -> runExceptT $ addRecipients sId rcps
105106
DeleteFile sId -> deleteFile st sId
@@ -113,8 +114,9 @@ writeFileStore s FileStore {files, recipients} = do
113114
readTVarIO files >>= mapM_ (logFile allRcps)
114115
where
115116
logFile :: Map RecipientId (SenderId, RcvPublicAuthKey) -> FileRec -> IO ()
116-
logFile allRcps FileRec {senderId, fileInfo, filePath, recipientIds, createdAt} = do
117-
logAddFile s senderId fileInfo createdAt
117+
logFile allRcps FileRec {senderId, fileInfo, filePath, recipientIds, createdAt, fileStatus} = do
118+
status <- readTVarIO fileStatus
119+
logAddFile s senderId fileInfo createdAt status
118120
(rcpErrs, rcps) <- M.mapEither getRcp . M.fromSet id <$> readTVarIO recipientIds
119121
mapM_ (logAddRecipients s senderId) $ L.nonEmpty $ M.elems rcps
120122
mapM_ (B.putStrLn . ("Error storing log: " <>)) rcpErrs

0 commit comments

Comments
 (0)