Commit 4909c5a5 authored by Steven Allen's avatar Steven Allen

correctly handle add flag constraints

License: MIT
Signed-off-by: default avatarSteven Allen <steven@stebalien.com>
parent fcef972c
...@@ -114,11 +114,11 @@ You can now check what blocks have been created by: ...@@ -114,11 +114,11 @@ You can now check what blocks have been created by:
cmdkit.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."), cmdkit.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."),
cmdkit.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes] or rabin-[min]-[avg]-[max]").WithDefault("size-262144"), cmdkit.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes] or rabin-[min]-[avg]-[max]").WithDefault("size-262144"),
cmdkit.BoolOption(pinOptionName, "Pin this object when adding.").WithDefault(true), cmdkit.BoolOption(pinOptionName, "Pin this object when adding.").WithDefault(true),
cmdkit.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes. (experimental)"), cmdkit.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes. Implies CIDv1, defaults to on if CIDv1 is enabled. (experimental)"),
cmdkit.BoolOption(noCopyOptionName, "Add the file using filestore. (experimental)"), cmdkit.BoolOption(noCopyOptionName, "Add the file using filestore. Implies raw-leaves. (experimental)"),
cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"), cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
cmdkit.IntOption(cidVersionOptionName, "Cid version. Non-zero value will change default of 'raw-leaves' to true. (experimental)").WithDefault(0), cmdkit.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)"),
cmdkit.StringOption(hashOptionName, "Hash function to use. Will set Cid version to 1 if used. (experimental)").WithDefault("sha2-256"), cmdkit.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
}, },
PreRun: func(req *cmds.Request, env cmds.Environment) error { PreRun: func(req *cmds.Request, env cmds.Environment) error {
quiet, _ := req.Options[quietOptionName].(bool) quiet, _ := req.Options[quietOptionName].(bool)
...@@ -170,31 +170,70 @@ You can now check what blocks have been created by: ...@@ -170,31 +170,70 @@ You can now check what blocks have been created by:
rawblks, rbset := req.Options[rawLeavesOptionName].(bool) rawblks, rbset := req.Options[rawLeavesOptionName].(bool)
nocopy, _ := req.Options[noCopyOptionName].(bool) nocopy, _ := req.Options[noCopyOptionName].(bool)
fscache, _ := req.Options[fstoreCacheOptionName].(bool) fscache, _ := req.Options[fstoreCacheOptionName].(bool)
cidVer, _ := req.Options[cidVersionOptionName].(int) cidVer, cidVerSet := req.Options[cidVersionOptionName].(int)
hashFunStr, hfset := req.Options[hashOptionName].(string) hashFunStr, _ := req.Options[hashOptionName].(string)
// Given the following constraints:
//
// nocopy -> filestoreEnabled
// nocopy -> rawblocks
// rawblocks -> cidv1
// (hash != sha2-256) -> cidv1
//
// We solve for the values of rawblocks and cidv1 in the
// following order of preference:
//
// 1. If cidv1 isn't fixed, set it to false and try solving.
// 2. If rawblocks isn't fixed, set it to true and try solving.
//
// If neither solution works, give up (we have a conflict).
// nocopy -> filestorEnabled
if nocopy && !cfg.Experimental.FilestoreEnabled { if nocopy && !cfg.Experimental.FilestoreEnabled {
res.SetError(errors.New("filestore is not enabled, see https://git.io/vy4XN"), res.SetError(errors.New("filestore is not enabled, see https://git.io/vy4XN"),
cmdkit.ErrClient) cmdkit.ErrClient)
return return
} }
if hfset && hashFunStr != "sha2-256" && cidVer == 0 { // nocopy -> rawblocks
cidVer = 1 if nocopy && !rawblks {
// fixed?
if rbset {
res.SetError(
fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well"),
cmdkit.ErrNormal,
)
return
} }
// No, satisfy mandatory constraint.
if cidVer > 0 && !rbset {
rawblks = true rawblks = true
} }
// if rawblocks is not explicitly set but nocopy is, set rawblocks if !cidVerSet {
if nocopy && !rbset { // Default to CIDv0 if possible.
rawblks = true // Conditions: no raw blocks, sha2-256
if hashFunStr == "sha2-256" && !rawblks {
cidVer = 0
} else {
cidVer = 1
} }
} else if cidVer == 0 {
if nocopy && !rawblks { // CIDv0 *was* set...
res.SetError(fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well"), cmdkit.ErrNormal) if hashFunStr != "sha2-256" {
res.SetError(errors.New("CIDv0 only supports sha2-256"), cmdkit.ErrClient)
return return
} else if rawblks {
res.SetError(
errors.New("CIDv0 incompatible with raw-leaves and/or nocopy"),
cmdkit.ErrClient,
)
return
}
}
// cidV1 -> raw blocks (by default)
if cidVer > 0 && !rbset {
rawblks = true
} }
prefix, err := dag.PrefixForCidVersion(cidVer) prefix, err := dag.PrefixForCidVersion(cidVer)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment