-
Notifications
You must be signed in to change notification settings - Fork 24
Content address method cleanup #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ericson2314
merged 6 commits into
haskell-nix:master
from
obsidiansystems:content-address-method-cleanup
Nov 5, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12b48ae
Rename `FileRecursive` to `NixArchive`
Ericson2314 43fc6d4
Clean up content address method types
Ericson2314 30f090f
Support more types of store paths
Ericson2314 59f1e00
Merge branch 'srk/readOnlyDsum' into content-address-method-cleanup
Ericson2314 457683d
Inline `makeTextPath`
Ericson2314 ce1d7da
Further deduplication within `makeFixedOutputPath`
Ericson2314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module System.Nix.FileContentAddress | ||
( FileIngestionMethod(..) | ||
) where | ||
|
||
import GHC.Generics (Generic) | ||
|
||
data FileIngestionMethod | ||
= FileIngestionMethod_Flat | ||
| FileIngestionMethod_NixArchive | ||
deriving (Bounded, Eq, Generic, Enum, Ord, Show) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module System.Nix.Store.ReadOnly | ||
( makeStorePath | ||
, makeTextPath | ||
( References(..) | ||
, makeStorePath | ||
, makeFixedOutputPath | ||
, computeStorePathForText | ||
, computeStorePathForPath | ||
) where | ||
|
||
|
@@ -15,8 +15,9 @@ import Data.Constraint.Extras (Has(has)) | |
import Data.Dependent.Sum (DSum((:=>))) | ||
import Data.HashSet (HashSet) | ||
import Data.Some (Some(Some)) | ||
import System.Nix.ContentAddress (ContentAddressMethod (..)) | ||
import System.Nix.Hash (BaseEncoding(Base16), HashAlgo(..)) | ||
import System.Nix.Store.Types (FileIngestionMethod(..), PathFilter, RepairMode) | ||
import System.Nix.Store.Types (PathFilter, RepairMode) | ||
import System.Nix.StorePath (StoreDir, StorePath, StorePathName) | ||
|
||
import qualified Crypto.Hash | ||
|
@@ -30,6 +31,23 @@ import qualified System.Nix.Hash | |
import qualified System.Nix.Nar | ||
import qualified System.Nix.StorePath | ||
|
||
data References = References | ||
{ references_others :: HashSet StorePath | ||
, references_self :: Bool | ||
} | ||
|
||
instance Semigroup References where | ||
a <> b = References | ||
{ references_others = references_others a <> references_others b | ||
, references_self = references_self a || references_self b | ||
} | ||
|
||
instance Monoid References where | ||
mempty = References | ||
{ references_others = mempty | ||
, references_self = False | ||
} | ||
|
||
makeStorePath | ||
:: StoreDir | ||
-> ByteString | ||
|
@@ -49,68 +67,64 @@ makeStorePath storeDir ty (hashAlgo :=> (digest :: Digest a)) nm = | |
, System.Nix.StorePath.unStorePathName nm | ||
] | ||
|
||
makeTextPath | ||
makeType | ||
:: StoreDir | ||
-> StorePathName | ||
-> Digest SHA256 | ||
-> HashSet StorePath | ||
-> StorePath | ||
makeTextPath storeDir nm h refs = makeStorePath storeDir ty (HashAlgo_SHA256 :=> h) nm | ||
where | ||
ty = | ||
Data.ByteString.intercalate | ||
":" | ||
$ "text" | ||
: Data.List.sort | ||
(System.Nix.StorePath.storePathToRawFilePath storeDir | ||
<$> Data.HashSet.toList refs) | ||
-> ByteString | ||
-> References | ||
-> ByteString | ||
makeType storeDir ty refs = | ||
Data.ByteString.intercalate ":" $ ty : (others ++ self) | ||
where | ||
others = Data.List.sort | ||
$ fmap (System.Nix.StorePath.storePathToRawFilePath storeDir) | ||
$ Data.HashSet.toList | ||
$ references_others refs | ||
self = ["self" | references_self refs] | ||
|
||
makeFixedOutputPath | ||
:: StoreDir | ||
-> FileIngestionMethod | ||
-> ContentAddressMethod | ||
-> DSum HashAlgo Digest | ||
-> References | ||
-> StorePathName | ||
-> StorePath | ||
makeFixedOutputPath storeDir recursive algoDigest@(hashAlgo :=> digest) = | ||
if recursive == FileIngestionMethod_FileRecursive | ||
&& Some hashAlgo == Some HashAlgo_SHA256 | ||
then makeStorePath storeDir "source" algoDigest | ||
else makeStorePath storeDir "output:out" (HashAlgo_SHA256 :=> h') | ||
makeFixedOutputPath storeDir method digest@(hashAlgo :=> h) refs = | ||
makeStorePath storeDir ty digest' | ||
where | ||
h' = | ||
Crypto.Hash.hash @ByteString @SHA256 | ||
$ "fixed:out:" | ||
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.algoToText hashAlgo) | ||
<> (if recursive == FileIngestionMethod_FileRecursive then ":r:" else ":") | ||
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.encodeDigestWith Base16 digest) | ||
<> ":" | ||
|
||
computeStorePathForText | ||
:: StoreDir | ||
-> StorePathName | ||
-> ByteString | ||
-> (HashSet StorePath -> StorePath) | ||
computeStorePathForText storeDir nm = | ||
makeTextPath storeDir nm | ||
. Crypto.Hash.hash | ||
(ty, digest') = case method of | ||
ContentAddressMethod_Text -> | ||
case hashAlgo of | ||
HashAlgo_SHA256 | ||
| references_self refs == False -> (makeType storeDir "text" refs, digest) | ||
_ -> error "unsupported" -- TODO do better; maybe we'll just remove this restriction too? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe should make this better :) |
||
_ -> | ||
if method == ContentAddressMethod_NixArchive | ||
&& Some hashAlgo == Some HashAlgo_SHA256 | ||
then (makeType storeDir "source" refs, digest) | ||
else let | ||
h' = | ||
Crypto.Hash.hash @ByteString @SHA256 | ||
$ "fixed:out:" | ||
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.algoToText hashAlgo) | ||
<> (if method == ContentAddressMethod_NixArchive then ":r:" else ":") | ||
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.encodeDigestWith Base16 h) | ||
<> ":" | ||
in ("output:out", HashAlgo_SHA256 :=> h') | ||
|
||
computeStorePathForPath | ||
:: StoreDir | ||
-> StorePathName -- ^ Name part of the newly created `StorePath` | ||
-> FilePath -- ^ Local `FilePath` to add | ||
-> FileIngestionMethod -- ^ Add target directory recursively | ||
digestPath | ||
:: FilePath -- ^ Local `FilePath` to add | ||
-> ContentAddressMethod -- ^ target directory method | ||
-> PathFilter -- ^ Path filter function | ||
-> RepairMode -- ^ Only used by local store backend | ||
-> IO StorePath | ||
computeStorePathForPath storeDir name pth recursive _pathFilter _repair = do | ||
selectedHash <- | ||
if recursive == FileIngestionMethod_FileRecursive | ||
then recursiveContentHash | ||
else flatContentHash | ||
pure $ makeFixedOutputPath storeDir recursive (HashAlgo_SHA256 :=> selectedHash) name | ||
-> IO (Digest SHA256) | ||
digestPath pth method _pathFilter _repair = | ||
case method of | ||
ContentAddressMethod_Flat -> flatContentHash | ||
ContentAddressMethod_NixArchive -> nixArchiveContentHash | ||
ContentAddressMethod_Text -> flatContentHash | ||
where | ||
recursiveContentHash :: IO (Digest SHA256) | ||
recursiveContentHash = | ||
nixArchiveContentHash :: IO (Digest SHA256) | ||
nixArchiveContentHash = | ||
Crypto.Hash.hashFinalize | ||
<$> execStateT streamNarUpdate (Crypto.Hash.hashInit @SHA256) | ||
|
||
|
@@ -127,3 +141,15 @@ computeStorePathForPath storeDir name pth recursive _pathFilter _repair = do | |
<$> System.Nix.Nar.narReadFile | ||
System.Nix.Nar.narEffectsIO | ||
pth | ||
|
||
computeStorePathForPath | ||
:: StoreDir | ||
-> StorePathName -- ^ Name part of the newly created `StorePath` | ||
-> FilePath -- ^ Local `FilePath` to add | ||
-> ContentAddressMethod -- ^ Add target directory methodly | ||
-> PathFilter -- ^ Path filter function | ||
-> RepairMode -- ^ Only used by local store backend | ||
-> IO StorePath | ||
computeStorePathForPath storeDir name pth method pathFilter repair = do | ||
selectedHash <- digestPath pth method pathFilter repair | ||
pure $ makeFixedOutputPath storeDir method (HashAlgo_SHA256 :=> selectedHash) mempty name |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split into
RepairMode.hs
andPathFilter.hs
? Not sure if worth just for these two.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just revisit such things later :)
I am not even sure
PathFilter
needs to go incore
.