From 0f23b4ab5c4c8bf5b937344c865fb195040f3c33 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Thu, 30 Mar 2023 16:27:47 +0400 Subject: [PATCH] xftp: re-create output file if decryption restarts (#701) --- src/Simplex/FileTransfer/Agent.hs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Simplex/FileTransfer/Agent.hs b/src/Simplex/FileTransfer/Agent.hs index 0e6166802..ab4beffb9 100644 --- a/src/Simplex/FileTransfer/Agent.hs +++ b/src/Simplex/FileTransfer/Agent.hs @@ -56,7 +56,7 @@ import Simplex.Messaging.Agent.Store.SQLite import Simplex.Messaging.Encoding.String import Simplex.Messaging.Protocol (EntityId, XFTPServer, XFTPServerWithAuth) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (liftError, liftIOEither, tshow) +import Simplex.Messaging.Util (liftError, liftIOEither, tshow, whenM) import System.FilePath (takeFileName, ()) import UnliftIO import UnliftIO.Concurrent @@ -219,11 +219,10 @@ runXFTPLocalWorker c doWork = do decryptFile f `catchError` (workerInternalError c rcvFileId rcvFileEntityId tmpPath . show) noWorkToDo = void . atomically $ tryTakeTMVar doWork decryptFile :: RcvFile -> m () - decryptFile RcvFile {rcvFileId, rcvFileEntityId, key, nonce, tmpPath, savePath, chunks} = do + decryptFile RcvFile {rcvFileId, rcvFileEntityId, key, nonce, tmpPath, savePath, status, chunks} = do fsSavePath <- toFSFilePath savePath - -- TODO test; recreate file if it's in status RFSDecrypting - -- when (status == RFSDecrypting) $ - -- whenM (doesFileExist fsSavePath) (removeFile fsSavePath >> createEmptyFile fsSavePath) + when (status == RFSDecrypting) $ + whenM (doesFileExist fsSavePath) (removeFile fsSavePath >> createEmptyFile fsSavePath) withStore' c $ \db -> updateRcvFileStatus db rcvFileId RFSDecrypting chunkPaths <- getChunkPaths chunks encSize <- liftIO $ foldM (\s path -> (s +) . fromIntegral <$> getFileSize path) 0 chunkPaths