ValidateImportConfig validates the Import configuration according to UnixFS spec requirements. See: https://specs.ipfs.tech/unixfs/#hamt-structure-and-parameters
(cfg *Import)
| 79 | // ValidateImportConfig validates the Import configuration according to UnixFS spec requirements. |
| 80 | // See: https://specs.ipfs.tech/unixfs/#hamt-structure-and-parameters |
| 81 | func ValidateImportConfig(cfg *Import) error { |
| 82 | // Validate CidVersion |
| 83 | if !cfg.CidVersion.IsDefault() { |
| 84 | cidVer := cfg.CidVersion.WithDefault(DefaultCidVersion) |
| 85 | if cidVer != 0 && cidVer != 1 { |
| 86 | return fmt.Errorf("Import.CidVersion must be 0 or 1, got %d", cidVer) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Validate UnixFSFileMaxLinks |
| 91 | if !cfg.UnixFSFileMaxLinks.IsDefault() { |
| 92 | maxLinks := cfg.UnixFSFileMaxLinks.WithDefault(DefaultUnixFSFileMaxLinks) |
| 93 | if maxLinks <= 0 { |
| 94 | return fmt.Errorf("Import.UnixFSFileMaxLinks must be positive, got %d", maxLinks) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Validate UnixFSDirectoryMaxLinks |
| 99 | if !cfg.UnixFSDirectoryMaxLinks.IsDefault() { |
| 100 | maxLinks := cfg.UnixFSDirectoryMaxLinks.WithDefault(DefaultUnixFSDirectoryMaxLinks) |
| 101 | if maxLinks < 0 { |
| 102 | return fmt.Errorf("Import.UnixFSDirectoryMaxLinks must be non-negative, got %d", maxLinks) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Validate UnixFSHAMTDirectoryMaxFanout if set |
| 107 | if !cfg.UnixFSHAMTDirectoryMaxFanout.IsDefault() { |
| 108 | fanout := cfg.UnixFSHAMTDirectoryMaxFanout.WithDefault(DefaultUnixFSHAMTDirectoryMaxFanout) |
| 109 | |
| 110 | // Valid values are powers of 2 between 8 and 1024: 8, 16, 32, 64, 128, 256, 512, 1024 |
| 111 | if fanout < 8 || !isPowerOfTwo(fanout) || fanout > 1024 { |
| 112 | return fmt.Errorf("Import.UnixFSHAMTDirectoryMaxFanout must be a power of 2, between 8 and 1024 (got %d)", fanout) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Validate BatchMaxNodes |
| 117 | if !cfg.BatchMaxNodes.IsDefault() { |
| 118 | maxNodes := cfg.BatchMaxNodes.WithDefault(DefaultBatchMaxNodes) |
| 119 | if maxNodes <= 0 { |
| 120 | return fmt.Errorf("Import.BatchMaxNodes must be positive, got %d", maxNodes) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Validate BatchMaxSize |
| 125 | if !cfg.BatchMaxSize.IsDefault() { |
| 126 | maxSize := cfg.BatchMaxSize.WithDefault(DefaultBatchMaxSize) |
| 127 | if maxSize <= 0 { |
| 128 | return fmt.Errorf("Import.BatchMaxSize must be positive, got %d", maxSize) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Validate UnixFSChunker format |
| 133 | if !cfg.UnixFSChunker.IsDefault() { |
| 134 | chunker := cfg.UnixFSChunker.WithDefault(DefaultUnixFSChunker) |
| 135 | if !isValidChunker(chunker) { |
| 136 | return fmt.Errorf("Import.UnixFSChunker invalid format: %q (expected \"size-<bytes>\", \"rabin-<min>-<avg>-<max>\", or \"buzhash\")", chunker) |
| 137 | } |
| 138 | } |