| 1 | package objectcmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | |
| 7 | cmds "github.com/ipfs/go-ipfs-cmds" |
| 8 | "github.com/ipfs/kubo/core/commands/cmdenv" |
| 9 | "github.com/ipfs/kubo/core/commands/cmdutils" |
| 10 | |
| 11 | "github.com/ipfs/kubo/core/coreiface/options" |
| 12 | ) |
| 13 | |
| 14 | var ObjectPatchCmd = &cmds.Command{ |
| 15 | Status: cmds.Deprecated, // https://github.com/ipfs/kubo/issues/7936 |
| 16 | Helptext: cmds.HelpText{ |
| 17 | Tagline: "Deprecated way to create a new merkledag object based on an existing one. Use MFS with 'files cp|rm' instead.", |
| 18 | ShortDescription: ` |
| 19 | 'ipfs object patch <root> <cmd> <args>' is a plumbing command used to |
| 20 | build custom dag-pb objects. It mutates objects, creating new objects as a |
| 21 | result. This is the Merkle-DAG version of modifying an object. |
| 22 | |
| 23 | DEPRECATED and provided for legacy reasons. |
| 24 | For modern use cases, use MFS with 'files' commands: 'ipfs files --help'. |
| 25 | |
| 26 | $ ipfs files cp /ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn /some-dir |
| 27 | $ ipfs files cp /ipfs/Qmayz4F4UzqcAMitTzU4zCSckDofvxstDuj3y7ajsLLEVs /some-dir/added-file.jpg |
| 28 | $ ipfs files stat --hash /some-dir |
| 29 | |
| 30 | The above will add 'added-file.jpg' to the directory placed under /some-dir |
| 31 | and the CID of updated directory is returned by 'files stat' |
| 32 | |
| 33 | 'files cp' does not download the data, only the root block, which makes it |
| 34 | possible to build arbitrary directory trees without fetching them in full to |
| 35 | the local node. |
| 36 | `, |
| 37 | }, |
| 38 | Arguments: []cmds.Argument{}, |
| 39 | Subcommands: map[string]*cmds.Command{ |
| 40 | "append-data": RemovedObjectCmd, |
| 41 | "add-link": patchAddLinkCmd, |
| 42 | "rm-link": patchRmLinkCmd, |
| 43 | "set-data": RemovedObjectCmd, |
| 44 | }, |
| 45 | Options: []cmds.Option{ |
| 46 | cmdutils.AllowBigBlockOption, |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | var patchRmLinkCmd = &cmds.Command{ |
| 51 | Status: cmds.Deprecated, // https://github.com/ipfs/kubo/issues/7936 |
| 52 | Helptext: cmds.HelpText{ |
| 53 | Tagline: "Deprecated way to remove a link from dag-pb object.", |
| 54 | ShortDescription: ` |
| 55 | Remove a Merkle-link from the given object and return the hash of the result. |
| 56 | |
| 57 | DEPRECATED and provided for legacy reasons. |
| 58 | |
| 59 | This command operates at the dag-pb level and only supports removing links |
| 60 | from small, flat UnixFS directories (not HAMTShard). Removing links from |
| 61 | files or large sharded directories will produce invalid UnixFS structures. |
| 62 | |
| 63 | For working with any UnixFS directories (including large/sharded ones), |
| 64 | use 'ipfs files rm' instead: 'ipfs files --help'. |
| 65 | `, |
| 66 | }, |
| 67 | Arguments: []cmds.Argument{ |
| 68 | cmds.StringArg("root", true, false, "The hash of the node to modify."), |
| 69 | cmds.StringArg("name", true, false, "Name of the link to remove."), |
| 70 | }, |
| 71 | Options: []cmds.Option{ |
| 72 | cmds.BoolOption(allowNonUnixFSOptionName, "", "Skip UnixFS validation, allowing link removal on non-directory nodes."), |
| 73 | }, |
| 74 | Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { |
| 75 | api, err := cmdenv.GetApi(env, req) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | enc, err := cmdenv.GetCidEncoder(req) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | root, err := cmdutils.PathOrCidPath(req.Arguments[0]) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | |
| 90 | name := req.Arguments[1] |
| 91 | allowNonUnixFS, _ := req.Options[allowNonUnixFSOptionName].(bool) |
| 92 | p, err := api.Object().RmLink(req.Context, root, name, |
| 93 | options.Object.RmLinkSkipUnixFSValidation(allowNonUnixFS)) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | if err := cmdutils.CheckCIDSize(req, p.RootCid(), api.Dag()); err != nil { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | return cmds.EmitOnce(res, &Object{Hash: enc.Encode(p.RootCid())}) |
| 103 | }, |
| 104 | Type: Object{}, |
| 105 | Encoders: cmds.EncoderMap{ |
| 106 | cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { |
| 107 | fmt.Fprintln(w, out.Hash) |
| 108 | return nil |
| 109 | }), |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | const ( |
| 114 | createOptionName = "create" |
| 115 | allowNonUnixFSOptionName = "allow-non-unixfs" |
| 116 | ) |
| 117 | |
| 118 | var patchAddLinkCmd = &cmds.Command{ |
| 119 | Status: cmds.Deprecated, // https://github.com/ipfs/kubo/issues/7936 |
| 120 | Helptext: cmds.HelpText{ |
| 121 | Tagline: "Deprecated way to add a link to a given dag-pb.", |
| 122 | ShortDescription: ` |
| 123 | Add a Merkle-link to the given object and return the hash of the result. |
| 124 | |
| 125 | DEPRECATED and provided for legacy reasons. |
| 126 | |
| 127 | This command operates at the dag-pb level and only supports adding links |
| 128 | to small, flat UnixFS directories (not HAMTShard). Adding links to files |
| 129 | or large sharded directories will produce invalid UnixFS structures. |
| 130 | |
| 131 | For working with any UnixFS directories (including large/sharded ones), |
| 132 | use MFS and 'files' commands instead: 'ipfs files --help'. |
| 133 | |
| 134 | $ ipfs files cp /ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn /some-dir |
| 135 | $ ipfs files cp /ipfs/Qmayz4F4UzqcAMitTzU4zCSckDofvxstDuj3y7ajsLLEVs /some-dir/added-file.jpg |
| 136 | $ ipfs files stat --hash /some-dir |
| 137 | |
| 138 | The above will add 'added-file.jpg' to the directory placed under /some-dir |
| 139 | and the CID of updated directory is returned by 'files stat'. |
| 140 | |
| 141 | 'files cp' does not download the data, only the root block, which makes it |
| 142 | possible to build arbitrary directory trees without fetching them in full to |
| 143 | the local node. |
| 144 | `, |
| 145 | }, |
| 146 | Arguments: []cmds.Argument{ |
| 147 | cmds.StringArg("root", true, false, "The hash of the node to modify."), |
| 148 | cmds.StringArg("name", true, false, "Name of link to create."), |
| 149 | cmds.StringArg("ref", true, false, "IPFS object to add link to."), |
| 150 | }, |
| 151 | Options: []cmds.Option{ |
| 152 | cmds.BoolOption(createOptionName, "p", "Create intermediary nodes."), |
| 153 | cmds.BoolOption(allowNonUnixFSOptionName, "", "Skip UnixFS validation, allowing links on non-directory nodes."), |
| 154 | }, |
| 155 | Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { |
| 156 | api, err := cmdenv.GetApi(env, req) |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | |
| 161 | enc, err := cmdenv.GetCidEncoder(req) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | root, err := cmdutils.PathOrCidPath(req.Arguments[0]) |
| 167 | if err != nil { |
| 168 | return err |
| 169 | } |
| 170 | |
| 171 | name := req.Arguments[1] |
| 172 | |
| 173 | child, err := cmdutils.PathOrCidPath(req.Arguments[2]) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | create, _ := req.Options[createOptionName].(bool) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | allowNonUnixFS, _ := req.Options[allowNonUnixFSOptionName].(bool) |
| 183 | |
| 184 | p, err := api.Object().AddLink(req.Context, root, name, child, |
| 185 | options.Object.Create(create), |
| 186 | options.Object.SkipUnixFSValidation(allowNonUnixFS)) |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | |
| 191 | if err := cmdutils.CheckCIDSize(req, p.RootCid(), api.Dag()); err != nil { |
| 192 | return err |
| 193 | } |
| 194 | |
| 195 | return cmds.EmitOnce(res, &Object{Hash: enc.Encode(p.RootCid())}) |
| 196 | }, |
| 197 | Type: Object{}, |
| 198 | Encoders: cmds.EncoderMap{ |
| 199 | cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { |
| 200 | fmt.Fprintln(w, out.Hash) |
| 201 | return nil |
| 202 | }), |
| 203 | }, |
| 204 | } |