@cryptotaxi247 / kubo / commits / 4909c5a56

correctly handle add flag constraints

License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Jan 23, 2018 at 12:30 UTC 4909c5a56d256e0b999d11b065227b94576609ce
1 file changed +57 -18
core/commands/add.go
+57 -18
@@ -114,11 +114,11 @@ You can now check what blocks have been created by:
114 cmdkit.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."),
115 cmdkit.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes] or rabin-[min]-[avg]-[max]").WithDefault("size-262144"),
116 cmdkit.BoolOption(pinOptionName, "Pin this object when adding.").WithDefault(true),
117 - cmdkit.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes. (experimental)"),
118 - cmdkit.BoolOption(noCopyOptionName, "Add the file using filestore. (experimental)"),
117 + cmdkit.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes. Implies CIDv1, defaults to on if CIDv1 is enabled. (experimental)"),
118 + cmdkit.BoolOption(noCopyOptionName, "Add the file using filestore. Implies raw-leaves. (experimental)"),
119 cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
120 - cmdkit.IntOption(cidVersionOptionName, "Cid version. Non-zero value will change default of 'raw-leaves' to true. (experimental)").WithDefault(0),
121 - cmdkit.StringOption(hashOptionName, "Hash function to use. Will set Cid version to 1 if used. (experimental)").WithDefault("sha2-256"),
120 + cmdkit.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)"),
121 + cmdkit.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
122 },
123 PreRun: func(req *cmds.Request, env cmds.Environment) error {
124 quiet, _ := req.Options[quietOptionName].(bool)
@@ -170,31 +170,70 @@ You can now check what blocks have been created by:
170 rawblks, rbset := req.Options[rawLeavesOptionName].(bool)
171 nocopy, _ := req.Options[noCopyOptionName].(bool)
172 fscache, _ := req.Options[fstoreCacheOptionName].(bool)
173 - cidVer, _ := req.Options[cidVersionOptionName].(int)
174 - hashFunStr, hfset := req.Options[hashOptionName].(string)
175 -
173 + cidVer, cidVerSet := req.Options[cidVersionOptionName].(int)
174 + hashFunStr, _ := req.Options[hashOptionName].(string)
175 +
176 + // Given the following constraints:
177 + //
178 + // nocopy -> filestoreEnabled
179 + // nocopy -> rawblocks
180 + // rawblocks -> cidv1
181 + // (hash != sha2-256) -> cidv1
182 + //
183 + // We solve for the values of rawblocks and cidv1 in the
184 + // following order of preference:
185 + //
186 + // 1. If cidv1 isn't fixed, set it to false and try solving.
187 + // 2. If rawblocks isn't fixed, set it to true and try solving.
188 + //
189 + // If neither solution works, give up (we have a conflict).
190 +
191 + // nocopy -> filestorEnabled
192 if nocopy && !cfg.Experimental.FilestoreEnabled {
193 res.SetError(errors.New("filestore is not enabled, see https://git.io/vy4XN"),
194 cmdkit.ErrClient)
195 return
196 }
197
182 - if hfset && hashFunStr != "sha2-256" && cidVer == 0 {
183 - cidVer = 1
184 - }
185 -
186 - if cidVer > 0 && !rbset {
198 + // nocopy -> rawblocks
199 + if nocopy && !rawblks {
200 + // fixed?
201 + if rbset {
202 + res.SetError(
203 + fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well"),
204 + cmdkit.ErrNormal,
205 + )
206 + return
207 + }
208 + // No, satisfy mandatory constraint.
209 rawblks = true
210 }
211
190 - // if rawblocks is not explicitly set but nocopy is, set rawblocks
191 - if nocopy && !rbset {
192 - rawblks = true
212 + if !cidVerSet {
213 + // Default to CIDv0 if possible.
214 + // Conditions: no raw blocks, sha2-256
215 + if hashFunStr == "sha2-256" && !rawblks {
216 + cidVer = 0
217 + } else {
218 + cidVer = 1
219 + }
220 + } else if cidVer == 0 {
221 + // CIDv0 *was* set...
222 + if hashFunStr != "sha2-256" {
223 + res.SetError(errors.New("CIDv0 only supports sha2-256"), cmdkit.ErrClient)
224 + return
225 + } else if rawblks {
226 + res.SetError(
227 + errors.New("CIDv0 incompatible with raw-leaves and/or nocopy"),
228 + cmdkit.ErrClient,
229 + )
230 + return
231 + }
232 }
233
195 - if nocopy && !rawblks {
196 - res.SetError(fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well"), cmdkit.ErrNormal)
197 - return
234 + // cidV1 -> raw blocks (by default)
235 + if cidVer > 0 && !rbset {
236 + rawblks = true
237 }
238
239 prefix, err := dag.PrefixForCidVersion(cidVer)