|
| 1 | +{-# LANGUAGE KindSignatures #-} |
| 2 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 3 | +{-# LANGUAGE TupleSections #-} |
| 4 | +{-# LANGUAGE TypeApplications #-} |
| 5 | +{-# LANGUAGE OverloadedStrings #-} |
| 6 | + |
| 7 | +{-| |
| 8 | +Description : Allowed effects for interacting with Nar files. |
| 9 | +Maintainer : Shea Levy <[email protected]> |
| 10 | +|-} |
| 11 | +module System.Nix.Nar where |
| 12 | + |
| 13 | +import Control.Monad (replicateM, replicateM_) |
| 14 | +import Data.Monoid ((<>)) |
| 15 | +import Control.Applicative |
| 16 | +import qualified Data.ByteString.Lazy.Char8 as BSL |
| 17 | +import qualified Data.Set as Set |
| 18 | +import qualified Data.Binary as B |
| 19 | +import qualified Data.Text as T |
| 20 | +import qualified Data.Text.Encoding as E |
| 21 | +import qualified Data.Binary.Put as B |
| 22 | +import qualified Data.Binary.Get as B |
| 23 | +import Debug.Trace |
| 24 | + |
| 25 | +import System.Nix.Path |
| 26 | + |
| 27 | + |
| 28 | +data NarEffects (m :: * -> *) = NarEffets { |
| 29 | + readFile :: FilePath -> m BSL.ByteString |
| 30 | + , listDir :: FilePath -> m [FileSystemObject] |
| 31 | + , narFromFileBytes :: BSL.ByteString -> m Nar |
| 32 | + , narFromDirectory :: FilePath -> m Nar |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +-- Directly taken from Eelco thesis |
| 37 | +-- https://nixos.org/%7Eeelco/pubs/phd-thesis.pdf |
| 38 | + |
| 39 | +-- TODO: Should we use rootedPath, validPath rather than FilePath? |
| 40 | +data Nar = Nar { narFile :: FileSystemObject } |
| 41 | + deriving (Eq, Ord, Show) |
| 42 | + |
| 43 | +data FileSystemObject = |
| 44 | + Regular IsExecutable BSL.ByteString |
| 45 | + | Directory (Set.Set (PathName, FileSystemObject)) |
| 46 | + | SymLink BSL.ByteString |
| 47 | + deriving (Eq, Show) |
| 48 | + |
| 49 | +-- TODO - is this right? How does thesis define ordering of FSOs? |
| 50 | +instance Ord FileSystemObject where |
| 51 | + compare (Regular _ c1) (Regular _ c2) = compare c1 c2 |
| 52 | + compare (Regular _ _) _ = GT |
| 53 | + compare (Directory s1) (Directory s2) = compare s1 s2 |
| 54 | + compare (Directory _) _ = GT |
| 55 | + compare (SymLink l1) (SymLink l2) = compare l1 l2 |
| 56 | + |
| 57 | +data IsExecutable = NonExecutable | Executable |
| 58 | + deriving (Eq, Show) |
| 59 | + |
| 60 | +-- data NarFile = NarFile |
| 61 | +-- { narFileIsExecutable :: IsExecutable |
| 62 | +-- , narFilePath :: FilePath -- TODO: Correct type? |
| 63 | +-- } deriving (Show) |
| 64 | + |
| 65 | +data DebugPut = PutAscii | PutBinary |
| 66 | + |
| 67 | +putNar :: Nar -> B.Put |
| 68 | +putNar = putNar' PutBinary |
| 69 | + |
| 70 | +putNar' :: DebugPut -> Nar -> B.Put |
| 71 | +putNar' dbg (Nar file) = header <> |
| 72 | + parens (putFile file) |
| 73 | + where |
| 74 | + |
| 75 | + str' = case dbg of |
| 76 | + PutAscii -> strDebug |
| 77 | + PutBinary -> str |
| 78 | + |
| 79 | + header = str' "nix-archive-1" |
| 80 | + parens m = str' "(" <> m <> str ")" |
| 81 | + |
| 82 | + putFile (Regular isExec contents) = |
| 83 | + str' "type" <> str' "regular" |
| 84 | + <> if isExec == Executable |
| 85 | + then str' "executable" <> str' "" |
| 86 | + else str' "" |
| 87 | + <> str' "contents" <> str' contents |
| 88 | + |
| 89 | + putFile (SymLink target) = |
| 90 | + str' "type" <> str' "symlink" <> str' "target" <> str' target |
| 91 | + |
| 92 | + putFile (Directory entries) = |
| 93 | + str' "type" <> str' "directory" |
| 94 | + <> foldMap putEntry entries |
| 95 | + |
| 96 | + putEntry (PathName name, fso) = |
| 97 | + str' "entry" <> |
| 98 | + parens (str' "name" <> |
| 99 | + str' (BSL.fromStrict $ E.encodeUtf8 name) <> |
| 100 | + str' "node" <> |
| 101 | + putFile fso) |
| 102 | + |
| 103 | +getNar :: B.Get Nar |
| 104 | +getNar = fmap Nar $ header >> parens getFile |
| 105 | + where header = trace "header " $ assertStr "nix-archive-1" |
| 106 | + |
| 107 | + padLen n = let r = n `mod` 8 |
| 108 | + p = (8 - n) `mod` 8 |
| 109 | + in trace ("padLen: " ++ show p) p |
| 110 | + |
| 111 | + str = do |
| 112 | + n <- fmap fromIntegral B.getInt64le |
| 113 | + s <- B.getLazyByteString n |
| 114 | + p <- B.getByteString (padLen $ fromIntegral n) |
| 115 | + traceShow (n,s) $ return s |
| 116 | + |
| 117 | + assertStr s = trace ("Assert " ++ show s) $ do |
| 118 | + s' <- str |
| 119 | + if s == s' |
| 120 | + then trace ("Assert " ++ show s ++ " passed") (return s) |
| 121 | + else trace ("Assert " ++ show s ++ " failed") (fail "No") |
| 122 | + |
| 123 | + parens m = assertStr "(" *> m <* assertStr ")" |
| 124 | + |
| 125 | + getFile :: B.Get FileSystemObject |
| 126 | + getFile = trace "getFile" (getRegularFile) |
| 127 | + <|> trace "getDir" (getDirectory) |
| 128 | + <|> trace "getLink" (getSymLink) |
| 129 | + |
| 130 | + getRegularFile = trace "regular" $ do |
| 131 | + trace "TESTING" (assertStr "type") |
| 132 | + trace "HI" $ assertStr "regular" |
| 133 | + trace "HI AGOIN" $ assertStr "contents" |
| 134 | + contents <- str |
| 135 | + return $ Regular (maybe NonExecutable |
| 136 | + (const Executable) Nothing) contents |
| 137 | + |
| 138 | + getDirectory = do |
| 139 | + assertStr "type" |
| 140 | + assertStr "directory" |
| 141 | + fs <- many getEntry |
| 142 | + return $ Directory (Set.fromList fs) |
| 143 | + |
| 144 | + getSymLink = do |
| 145 | + assertStr "type" |
| 146 | + assertStr "symlink" |
| 147 | + assertStr "target" |
| 148 | + fmap SymLink str |
| 149 | + |
| 150 | + getEntry = do |
| 151 | + assertStr "entry" |
| 152 | + parens $ do |
| 153 | + assertStr "name" |
| 154 | + mname <- pathName . E.decodeUtf8 . BSL.toStrict <$> str |
| 155 | + assertStr "node" |
| 156 | + file <- parens getFile |
| 157 | + maybe (fail "Bad PathName") (return . (,file)) mname |
| 158 | + |
| 159 | +str :: BSL.ByteString -> B.Put |
| 160 | +str t = let len = BSL.length t |
| 161 | + in int len <> pad t |
| 162 | + |
| 163 | +int :: Integral a => a -> B.Put |
| 164 | +int n = B.putInt64le $ fromIntegral n |
| 165 | + |
| 166 | +pad :: BSL.ByteString -> B.Put |
| 167 | +pad bs = |
| 168 | + let padLen = BSL.length bs `div` 8 |
| 169 | + in B.put bs >> B.put (BSL.replicate padLen '\NUL') |
| 170 | + |
| 171 | +strDebug :: BSL.ByteString -> B.Put |
| 172 | +strDebug t = let len = BSL.length t |
| 173 | + in intDebug len <> padDebug t |
| 174 | + |
| 175 | +intDebug :: Integral a => a -> B.Put |
| 176 | +intDebug a = B.put (show @Int (fromIntegral a)) |
| 177 | + |
| 178 | +padDebug :: BSL.ByteString -> B.Put |
| 179 | +padDebug bs = |
| 180 | + let padLen = BSL.length bs `div` 8 |
| 181 | + in B.put bs >> B.put (BSL.replicate padLen '_') |
0 commit comments