Skip to content

Commit 8022e13

Browse files
hacdiaslidel
andauthored
config: introduce Import section (#10421)
Co-authored-by: Marcin Rataj <[email protected]>
1 parent ae05085 commit 8022e13

File tree

11 files changed

+305
-8
lines changed

11 files changed

+305
-8
lines changed

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Config struct {
3636
Experimental Experiments
3737
Plugins Plugins
3838
Pinning Pinning
39+
Import Import
3940

4041
Internal Internal // experimental/unstable options
4142
}

config/import.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package config
2+
3+
const (
4+
DefaultCidVersion = 0
5+
DefaultUnixFSRawLeaves = false
6+
DefaultUnixFSChunker = "size-262144"
7+
DefaultHashFunction = "sha2-256"
8+
)
9+
10+
// Import configures the default options for ingesting data. This affects commands
11+
// that ingest data, such as 'ipfs add', 'ipfs dag put, 'ipfs block put', 'ipfs files write'.
12+
type Import struct {
13+
CidVersion OptionalInteger
14+
UnixFSRawLeaves Flag
15+
UnixFSChunker OptionalString
16+
HashFunction OptionalString
17+
}

config/profile.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ fetching may be degraded.
204204
return nil
205205
},
206206
},
207+
"legacy-cid-v0": {
208+
Description: `Makes UnixFS import produce legacy CIDv0 with no raw leaves, sha2-256 and 256 KiB chunks.`,
209+
210+
Transform: func(c *Config) error {
211+
c.Import.CidVersion = *NewOptionalInteger(0)
212+
c.Import.UnixFSRawLeaves = False
213+
c.Import.UnixFSChunker = *NewOptionalString("size-262144")
214+
c.Import.HashFunction = *NewOptionalString("sha2-256")
215+
return nil
216+
},
217+
},
218+
"test-cid-v1": {
219+
Description: `Makes UnixFS import produce modern CIDv1 with raw leaves, sha2-256 and 1 MiB chunks.`,
220+
221+
Transform: func(c *Config) error {
222+
c.Import.CidVersion = *NewOptionalInteger(1)
223+
c.Import.UnixFSRawLeaves = True
224+
c.Import.UnixFSChunker = *NewOptionalString("size-1048576")
225+
c.Import.HashFunction = *NewOptionalString("sha2-256")
226+
return nil
227+
},
228+
},
207229
}
208230

209231
func getAvailablePort() (port int, err error) {

core/commands/add.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
gopath "path"
99
"strings"
1010

11+
"github.com/ipfs/kubo/config"
1112
"github.com/ipfs/kubo/core/commands/cmdenv"
1213

1314
"github.com/cheggaaa/pb"
@@ -155,12 +156,12 @@ See 'dag export' and 'dag import' for more information.
155156
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
156157
cmds.BoolOption(onlyHashOptionName, "n", "Only chunk and hash - do not write to disk."),
157158
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object."),
158-
cmds.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes], rabin-[min]-[avg]-[max] or buzhash").WithDefault("size-262144"),
159+
cmds.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes], rabin-[min]-[avg]-[max] or buzhash"),
159160
cmds.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes."),
160161
cmds.BoolOption(noCopyOptionName, "Add the file using filestore. Implies raw-leaves. (experimental)"),
161162
cmds.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
162163
cmds.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. Passing version 1 will cause the raw-leaves option to default to true."),
163-
cmds.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
164+
cmds.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)"),
164165
cmds.BoolOption(inlineOptionName, "Inline small blocks into CIDs. (experimental)"),
165166
cmds.IntOption(inlineLimitOptionName, "Maximum block size to inline. (experimental)").WithDefault(32),
166167
cmds.BoolOption(pinOptionName, "Pin locally to protect added files from garbage collection.").WithDefault(true),
@@ -191,6 +192,16 @@ See 'dag export' and 'dag import' for more information.
191192
return err
192193
}
193194

195+
nd, err := cmdenv.GetNode(env)
196+
if err != nil {
197+
return err
198+
}
199+
200+
cfg, err := nd.Repo.Config()
201+
if err != nil {
202+
return err
203+
}
204+
194205
progress, _ := req.Options[progressOptionName].(bool)
195206
trickle, _ := req.Options[trickleOptionName].(bool)
196207
wrap, _ := req.Options[wrapOptionName].(bool)
@@ -207,6 +218,24 @@ See 'dag export' and 'dag import' for more information.
207218
inlineLimit, _ := req.Options[inlineLimitOptionName].(int)
208219
toFilesStr, toFilesSet := req.Options[toFilesOptionName].(string)
209220

221+
if chunker == "" {
222+
chunker = cfg.Import.UnixFSChunker.WithDefault(config.DefaultUnixFSChunker)
223+
}
224+
225+
if hashFunStr == "" {
226+
hashFunStr = cfg.Import.HashFunction.WithDefault(config.DefaultHashFunction)
227+
}
228+
229+
if !cidVerSet && !cfg.Import.CidVersion.IsDefault() {
230+
cidVerSet = true
231+
cidVer = int(cfg.Import.CidVersion.WithDefault(config.DefaultCidVersion))
232+
}
233+
234+
if !rbset && cfg.Import.UnixFSRawLeaves != config.Default {
235+
rbset = true
236+
rawblks = cfg.Import.UnixFSRawLeaves.WithDefault(config.DefaultUnixFSRawLeaves)
237+
}
238+
210239
if onlyHash && toFilesSet {
211240
return fmt.Errorf("%s and %s options are not compatible", onlyHashOptionName, toFilesOptionName)
212241
}

core/commands/block.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/ipfs/boxo/files"
1010

11+
"github.com/ipfs/kubo/config"
1112
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
1213
"github.com/ipfs/kubo/core/commands/cmdutils"
1314

@@ -153,7 +154,7 @@ only for backward compatibility when a legacy CIDv0 is required (--format=v0).
153154
},
154155
Options: []cmds.Option{
155156
cmds.StringOption(blockCidCodecOptionName, "Multicodec to use in returned CID").WithDefault("raw"),
156-
cmds.StringOption(mhtypeOptionName, "Multihash hash function").WithDefault("sha2-256"),
157+
cmds.StringOption(mhtypeOptionName, "Multihash hash function"),
157158
cmds.IntOption(mhlenOptionName, "Multihash hash length").WithDefault(-1),
158159
cmds.BoolOption(pinOptionName, "Pin added blocks recursively").WithDefault(false),
159160
cmdutils.AllowBigBlockOption,
@@ -165,7 +166,21 @@ only for backward compatibility when a legacy CIDv0 is required (--format=v0).
165166
return err
166167
}
167168

169+
nd, err := cmdenv.GetNode(env)
170+
if err != nil {
171+
return err
172+
}
173+
174+
cfg, err := nd.Repo.Config()
175+
if err != nil {
176+
return err
177+
}
178+
168179
mhtype, _ := req.Options[mhtypeOptionName].(string)
180+
if mhtype == "" {
181+
mhtype = cfg.Import.HashFunction.WithDefault(config.DefaultHashFunction)
182+
}
183+
169184
mhtval, ok := mh.Names[mhtype]
170185
if !ok {
171186
return fmt.Errorf("unrecognized multihash function: %s", mhtype)

core/commands/dag/dag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ into an object of the specified format.
8787
cmds.StringOption("store-codec", "Codec that the stored object will be encoded with").WithDefault("dag-cbor"),
8888
cmds.StringOption("input-codec", "Codec that the input object is encoded in").WithDefault("dag-json"),
8989
cmds.BoolOption("pin", "Pin this object when adding."),
90-
cmds.StringOption("hash", "Hash function to use").WithDefault("sha2-256"),
90+
cmds.StringOption("hash", "Hash function to use"),
9191
cmdutils.AllowBigBlockOption,
9292
},
9393
Run: dagPut,

core/commands/dag/put.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
blocks "github.com/ipfs/go-block-format"
88
"github.com/ipfs/go-cid"
99
ipldlegacy "github.com/ipfs/go-ipld-legacy"
10+
"github.com/ipfs/kubo/config"
1011
"github.com/ipfs/kubo/core/commands/cmdenv"
1112
"github.com/ipfs/kubo/core/commands/cmdutils"
1213
"github.com/ipld/go-ipld-prime/multicodec"
@@ -32,11 +33,25 @@ func dagPut(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) e
3233
return err
3334
}
3435

36+
nd, err := cmdenv.GetNode(env)
37+
if err != nil {
38+
return err
39+
}
40+
41+
cfg, err := nd.Repo.Config()
42+
if err != nil {
43+
return err
44+
}
45+
3546
inputCodec, _ := req.Options["input-codec"].(string)
3647
storeCodec, _ := req.Options["store-codec"].(string)
3748
hash, _ := req.Options["hash"].(string)
3849
dopin, _ := req.Options["pin"].(bool)
3950

51+
if hash == "" {
52+
hash = cfg.Import.HashFunction.WithDefault(config.DefaultHashFunction)
53+
}
54+
4055
var icodec mc.Code
4156
if err := icodec.Set(inputCodec); err != nil {
4257
return err

core/commands/files.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
humanize "github.com/dustin/go-humanize"
14+
"github.com/ipfs/kubo/config"
1415
"github.com/ipfs/kubo/core"
1516
"github.com/ipfs/kubo/core/commands/cmdenv"
1617

@@ -802,18 +803,28 @@ See '--to-files' in 'ipfs add --help' for more information.
802803
return err
803804
}
804805

806+
nd, err := cmdenv.GetNode(env)
807+
if err != nil {
808+
return err
809+
}
810+
811+
cfg, err := nd.Repo.Config()
812+
if err != nil {
813+
return err
814+
}
815+
805816
create, _ := req.Options[filesCreateOptionName].(bool)
806817
mkParents, _ := req.Options[filesParentsOptionName].(bool)
807818
trunc, _ := req.Options[filesTruncateOptionName].(bool)
808819
flush, _ := req.Options[filesFlushOptionName].(bool)
809820
rawLeaves, rawLeavesDef := req.Options[filesRawLeavesOptionName].(bool)
810821

811-
prefix, err := getPrefixNew(req)
812-
if err != nil {
813-
return err
822+
if !rawLeavesDef && cfg.Import.UnixFSRawLeaves != config.Default {
823+
rawLeavesDef = true
824+
rawLeaves = cfg.Import.UnixFSRawLeaves.WithDefault(config.DefaultUnixFSRawLeaves)
814825
}
815826

816-
nd, err := cmdenv.GetNode(env)
827+
prefix, err := getPrefixNew(req)
817828
if err != nil {
818829
return err
819830
}

docs/changelogs/v0.29.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Overview](#overview)
88
- [🔦 Highlights](#-highlights)
99
- [Add search functionality for pin names](#add-search-functionality-for-pin-names)
10+
- [Customizing `ipfs add` defaults](#customizing-ipfs-add-defaults)
1011
- [📝 Changelog](#-changelog)
1112
- [👨‍👩‍👧‍👦 Contributors](#-contributors)
1213

@@ -18,6 +19,16 @@
1819

1920
It is now possible to search for pins by name. To do so, use `ipfs pin ls --name "SomeName"`. The search is case-sensitive and will return all pins having a name which contains the exact word provided.
2021

22+
#### Customizing `ipfs add` defaults
23+
24+
This release supports overriding global data ingestion defaults used by commands like `ipfs add` via user-defined [`Import.*` configuration options](../config.md#import).
25+
The hash function, CID version, or UnixFS raw leaves and chunker behaviors can be set once, and used as the new implicit default for `ipfs add`.
26+
27+
> [!TIP]
28+
> As a convenience, two CID [profiles](../config.md#profile) are provided: `legacy-cid-v0` and `test-cid-v1`.
29+
> A test profile that defaults to modern CIDv1 can be applied via `ipfs config profile apply test-cid-v1`.
30+
> We encourage users to try it and report any issues.
31+
2132
### 📝 Changelog
2233

2334
### 👨‍👩‍👧‍👦 Contributors

docs/config.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ config file at runtime.
175175
- [`DNS`](#dns)
176176
- [`DNS.Resolvers`](#dnsresolvers)
177177
- [`DNS.MaxCacheTTL`](#dnsmaxcachettl)
178+
- [`Import`](#import)
179+
- [`Import.CidVersion`](#importcidversion)
180+
- [`Import.UnixFSRawLeaves`](#importunixfsrawleaves)
181+
- [`Import.UnixFSChunker`](#importunixfschunker)
182+
- [`Import.HashFunction`](#importhashfunction)
178183

179184
## Profiles
180185

@@ -265,6 +270,21 @@ documented in `ipfs config profile --help`.
265270

266271
Use this profile with caution.
267272

273+
- `legacy-cid-v0`
274+
275+
Makes UnixFS import (`ipfs add`) produce legacy CIDv0 with no raw leaves, sha2-256 and 256 KiB chunks.
276+
277+
> [!WARNING]
278+
> This profile is provided for legacy users and should not be used for new projects.
279+
280+
- `test-cid-v1`
281+
282+
Makes UnixFS import (`ipfs add`) produce modern CIDv1 with raw leaves, sha2-256 and 1 MiB chunks.
283+
284+
> [!NOTE]
285+
> This profile will become the new implicit default, provided for testing purposes.
286+
> Follow [kubo#4143](https://github.com/ipfs/kubo/issues/4143) for more details.
287+
268288
## Types
269289

270290
This document refers to the standard JSON types (e.g., `null`, `string`,
@@ -2377,3 +2397,41 @@ Note: this does NOT work with Go's default DNS resolver. To make this a global s
23772397
Default: Respect DNS Response TTL
23782398

23792399
Type: `optionalDuration`
2400+
2401+
## `Import`
2402+
2403+
Options to configure the default options used for ingesting data, in commands such as `ipfs add` or `ipfs block put`. All affected commands are detailed per option.
2404+
2405+
Note that using flags will override the options defined here.
2406+
2407+
### `Import.CidVersion`
2408+
2409+
The default CID version. Commands affected: `ipfs add`.
2410+
2411+
Default: `0`
2412+
2413+
Type: `optionalInteger`
2414+
2415+
### `Import.UnixFSRawLeaves`
2416+
2417+
The default UnixFS raw leaves option. Commands affected: `ipfs add`, `ipfs files write`.
2418+
2419+
Default: `false` if `CidVersion=0`; `true` if `CidVersion=1`
2420+
2421+
Type: `flag`
2422+
2423+
### `Import.UnixFSChunker`
2424+
2425+
The default UnixFS chunker. Commands affected: `ipfs add`.
2426+
2427+
Default: `size-262144`
2428+
2429+
Type: `optionalString`
2430+
2431+
### `Import.HashFunction`
2432+
2433+
The default hash function. Commands affected: `ipfs add`, `ipfs block put`, `ipfs dag put`.
2434+
2435+
Default: `sha2-256`
2436+
2437+
Type: `optionalString`

0 commit comments

Comments
 (0)