Skip to content

Commit c5f3c1e

Browse files
committed
remote: generalize error in sockPutS, sockGetS, add Types.WorkerMagic, workerMagic serializer, HandshakeSError
1 parent 2c46d34 commit c5f3c1e

File tree

7 files changed

+87
-21
lines changed

7 files changed

+87
-21
lines changed

hnix-store-remote/hnix-store-remote.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ library
9292
, System.Nix.Store.Remote.Types.StoreConfig
9393
, System.Nix.Store.Remote.Types.SubstituteMode
9494
, System.Nix.Store.Remote.Types.Verbosity
95+
, System.Nix.Store.Remote.Types.WorkerMagic
9596
, System.Nix.Store.Remote.Types.WorkerOp
9697

9798
build-depends:

hnix-store-remote/src/System/Nix/Store/Remote/Client.hs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ import qualified Network.Socket.ByteString
2323
import System.Nix.Store.Remote.Logger (processOutput)
2424
import System.Nix.Store.Remote.MonadStore
2525
import System.Nix.Store.Remote.Socket (sockPutS, sockGetS)
26-
import System.Nix.Store.Remote.Serializer (bool, enum, int, protoVersion, text)
26+
import System.Nix.Store.Remote.Serializer (bool, enum, int, mapErrorS, protoVersion, text, workerMagic)
2727
import System.Nix.Store.Remote.Types.Logger (Logger)
2828
import System.Nix.Store.Remote.Types.ProtoVersion (ProtoVersion(..), ourProtoVersion)
2929
import System.Nix.Store.Remote.Types.StoreConfig (PreStoreConfig(..), StoreConfig(..))
30+
import System.Nix.Store.Remote.Types.WorkerMagic (WorkerMagic(..))
3031
import System.Nix.Store.Remote.Types.WorkerOp (WorkerOp)
3132

32-
workerMagic1 :: Int
33-
workerMagic1 = 0x6e697863
34-
workerMagic2 :: Int
35-
workerMagic2 = 0x6478696f
36-
3733
type Run a = IO (Either RemoteStoreError a, [Logger])
3834

3935
simpleOp :: WorkerOp -> MonadRemoteStore Bool
@@ -44,7 +40,7 @@ simpleOpArgs op args = do
4440
runOpArgs op args
4541
err <- gotError
4642
Data.Bool.bool
47-
(sockGetS bool)
43+
(sockGetS $ mapErrorS RemoteStoreError_SerializerGet bool)
4844
(do
4945
-- TODO: don't use show
5046
getErrors >>= throwError . RemoteStoreError_Fixme . show
@@ -67,7 +63,7 @@ runOpArgsIO
6763
)
6864
-> MonadRemoteStore ()
6965
runOpArgsIO op encoder = do
70-
sockPutS enum op
66+
sockPutS (mapErrorS RemoteStoreError_SerializerPut enum) op
7167

7268
soc <- getStoreSocket
7369
encoder (liftIO . Network.Socket.ByteString.sendAll soc)
@@ -93,11 +89,22 @@ runStoreSocket preStoreConfig code =
9389
where
9490
greet :: MonadRemoteStoreHandshake ProtoVersion
9591
greet = do
96-
sockPutS int workerMagic1
9792

98-
magic <- sockGetS int
93+
sockPutS
94+
(mapErrorS
95+
RemoteStoreError_SerializerHandshake
96+
workerMagic
97+
)
98+
WorkerMagic_One
99+
100+
magic <-
101+
sockGetS
102+
$ mapErrorS
103+
RemoteStoreError_SerializerHandshake
104+
workerMagic
105+
99106
unless
100-
(magic == workerMagic2)
107+
(magic == WorkerMagic_Two)
101108
$ throwError RemoteStoreError_WorkerMagic2Mismatch
102109

103110
daemonVersion <- sockGetS protoVersion
@@ -111,13 +118,19 @@ runStoreSocket preStoreConfig code =
111118
$ sockPutS int (0 :: Int) -- affinity, obsolete
112119

113120
when (daemonVersion >= ProtoVersion 1 11) $ do
114-
sockPutS bool False -- reserveSpace, obsolete
121+
sockPutS
122+
(mapErrorS RemoteStoreError_SerializerPut bool)
123+
False -- reserveSpace, obsolete
115124

116125
-- not quite right, should be min of the two
117126
-- as well as two ^ above
118127
when (ourProtoVersion >= ProtoVersion 1 33) $ do
119128
-- If we were buffering I/O, we would flush the output here.
120-
_daemonNixVersion <- sockGetS text
129+
_daemonNixVersion <-
130+
sockGetS
131+
$ mapErrorS
132+
RemoteStoreError_SerializerGet
133+
text
121134
return ()
122135

123136
-- TODO do something with it

hnix-store-remote/src/System/Nix/Store/Remote/MonadStore.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import Data.ByteString (ByteString)
3838
import Data.Word (Word64)
3939
import Network.Socket (Socket)
4040
import System.Nix.StorePath (HasStoreDir(..), StoreDir)
41-
import System.Nix.Store.Remote.Serializer (SError)
41+
import System.Nix.Store.Remote.Serializer (HandshakeSError, LoggerSError, SError)
4242
import System.Nix.Store.Remote.Types.Logger (Logger, isError)
4343
import System.Nix.Store.Remote.Types.ProtoVersion (HasProtoVersion(..), ProtoVersion)
4444
import System.Nix.Store.Remote.Types.StoreConfig (HasStoreSocket(..), PreStoreConfig, StoreConfig)
@@ -55,6 +55,8 @@ data RemoteStoreError
5555
| RemoteStoreError_Disconnected
5656
| RemoteStoreError_GetAddrInfoFailed
5757
| RemoteStoreError_SerializerGet SError
58+
| RemoteStoreError_SerializerHandshake HandshakeSError
59+
| RemoteStoreError_SerializerLogger LoggerSError
5860
| RemoteStoreError_SerializerPut SError
5961
| RemoteStoreError_NoDataProvided
6062
| RemoteStoreError_ProtocolMismatch

hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ module System.Nix.Store.Remote.Serializer
6363
, loggerOpCode
6464
, logger
6565
, verbosity
66+
-- * Handshake
67+
, HandshakeSError(..)
68+
, workerMagic
6669
) where
6770

6871
import Control.Monad.Except (MonadError, throwError, )
@@ -902,3 +905,20 @@ verbosity = Serializer
902905
{ getS = mapPrimE $ getS enum
903906
, putS = mapPrimE . putS enum
904907
}
908+
909+
-- * Handshake
910+
911+
data HandshakeSError
912+
= HandshakeSError_InvalidWorkerMagic Word64
913+
deriving (Eq, Ord, Generic, Show)
914+
915+
workerMagic :: NixSerializer r HandshakeSError WorkerMagic
916+
workerMagic = Serializer
917+
{ getS = do
918+
c <- getS int
919+
either
920+
(pure $ throwError (HandshakeSError_InvalidWorkerMagic c))
921+
pure
922+
$ word64ToWorkerMagic c
923+
, putS = putS int . workerMagicToWord64
924+
}

hnix-store-remote/src/System/Nix/Store/Remote/Socket.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Data.Serialize.Put (Put, runPut)
1010
import Network.Socket.ByteString (recv, sendAll)
1111
import System.Nix.StorePath (HasStoreDir, StorePath)
1212
import System.Nix.Store.Remote.MonadStore (MonadRemoteStore0, RemoteStoreError(..), getStoreDir, getStoreSocket)
13-
import System.Nix.Store.Remote.Serializer (NixSerializer, SError, runP, runSerialT)
13+
import System.Nix.Store.Remote.Serializer (NixSerializer, runP, runSerialT)
1414
import System.Nix.Store.Remote.Serialize.Prim (getInt, getByteString, getByteStrings, getPath, getPathsOrFail)
1515
import System.Nix.Store.Remote.Types (HasStoreSocket(..))
1616

@@ -50,27 +50,28 @@ sockPut p = do
5050

5151
sockPutS
5252
:: ( MonadReader r m
53-
, MonadError RemoteStoreError m
53+
, MonadError e m
5454
, MonadIO m
5555
, HasStoreSocket r
5656
)
57-
=> NixSerializer r SError a
57+
=> NixSerializer r e a
5858
-> a
5959
-> m ()
6060
sockPutS s a = do
6161
r <- ask
6262
case runP s r a of
6363
Right x -> liftIO $ sendAll (hasStoreSocket r) x
64-
Left e -> throwError $ RemoteStoreError_SerializerPut e
64+
Left e -> throwError e
6565

6666
sockGetS
67-
:: forall r m a
67+
:: forall r e m a
6868
. ( HasStoreSocket r
6969
, MonadError RemoteStoreError m
70+
, MonadError e m
7071
, MonadReader r m
7172
, MonadIO m
7273
)
73-
=> NixSerializer r SError a
74+
=> NixSerializer r e a
7475
-> m a
7576
sockGetS s = do
7677
r <- ask
@@ -79,7 +80,7 @@ sockGetS s = do
7980

8081
case res of
8182
Right x -> pure x
82-
Left e -> throwError $ RemoteStoreError_SerializerGet e
83+
Left e -> throwError e
8384
where
8485
sockGet8' :: MonadError RemoteStoreError m => m ByteString
8586
sockGet8' = do

hnix-store-remote/src/System/Nix/Store/Remote/Types.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module System.Nix.Store.Remote.Types
77
, module System.Nix.Store.Remote.Types.StoreConfig
88
, module System.Nix.Store.Remote.Types.SubstituteMode
99
, module System.Nix.Store.Remote.Types.Verbosity
10+
, module System.Nix.Store.Remote.Types.WorkerMagic
1011
, module System.Nix.Store.Remote.Types.WorkerOp
1112
) where
1213

@@ -18,4 +19,5 @@ import System.Nix.Store.Remote.Types.ProtoVersion
1819
import System.Nix.Store.Remote.Types.StoreConfig
1920
import System.Nix.Store.Remote.Types.SubstituteMode
2021
import System.Nix.Store.Remote.Types.Verbosity
22+
import System.Nix.Store.Remote.Types.WorkerMagic
2123
import System.Nix.Store.Remote.Types.WorkerOp
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module System.Nix.Store.Remote.Types.WorkerMagic
2+
( WorkerMagic(..)
3+
, workerMagicToWord64
4+
, word64ToWorkerMagic
5+
) where
6+
7+
import Data.Word (Word64)
8+
import GHC.Generics (Generic)
9+
10+
-- | WorkerMagic
11+
--
12+
-- Magic numbers exchange during handshake
13+
data WorkerMagic
14+
= WorkerMagic_One
15+
| WorkerMagic_Two
16+
deriving (Eq, Generic, Ord, Show)
17+
18+
workerMagicToWord64 :: WorkerMagic -> Word64
19+
workerMagicToWord64 = \case
20+
WorkerMagic_One -> 0x6e697863
21+
WorkerMagic_Two -> 0x6478696f
22+
23+
word64ToWorkerMagic :: Word64 -> Either String WorkerMagic
24+
word64ToWorkerMagic = \case
25+
0x6e697863 -> Right WorkerMagic_One
26+
0x6478696f -> Right WorkerMagic_Two
27+
x -> Left $ "Invalid WorkerMagic: " ++ show x

0 commit comments

Comments
 (0)