@cryptotaxi247 / kubo / commits / 63ddffdb5

Add "files update" command.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Aug 13, 2017 at 04:57 UTC 63ddffdb5d1d070b48765cdad4239130cde9f816
1 file changed +73 -9
core/commands/files/files.go
+73 -9
@@ -58,15 +58,16 @@ from the parent directory.
58 // that uses "hash"
59 },
60 Subcommands: map[string]*cmds.Command{
61 - "read": FilesReadCmd,
62 - "write": FilesWriteCmd,
63 - "mv": FilesMvCmd,
64 - "cp": FilesCpCmd,
65 - "ls": FilesLsCmd,
66 - "mkdir": FilesMkdirCmd,
67 - "stat": FilesStatCmd,
68 - "rm": FilesRmCmd,
69 - "flush": FilesFlushCmd,
61 + "read": FilesReadCmd,
62 + "write": FilesWriteCmd,
63 + "mv": FilesMvCmd,
64 + "cp": FilesCpCmd,
65 + "ls": FilesLsCmd,
66 + "mkdir": FilesMkdirCmd,
67 + "stat": FilesStatCmd,
68 + "rm": FilesRmCmd,
69 + "flush": FilesFlushCmd,
70 + "update": FilesUpdateCmd,
71 },
72 }
73
@@ -793,6 +794,69 @@ are run with the '--flush=false'.
794 },
795 }
796
797 +var FilesUpdateCmd = &cmds.Command{
798 + Helptext: cmds.HelpText{
799 + Tagline: "Change the cid version of hash function of the root node of a given path.",
800 + ShortDescription: `
801 +Flush a given path to disk. This is only useful when other commands
802 +are run with the '--flush=false'.
803 +`,
804 + },
805 + Arguments: []cmds.Argument{
806 + cmds.StringArg("path", false, false, "Path to flush. Default: '/'."),
807 + },
808 + Run: func(req cmds.Request, res cmds.Response) {
809 + nd, err := req.InvocContext().GetNode()
810 + if err != nil {
811 + res.SetError(err, cmds.ErrNormal)
812 + return
813 + }
814 +
815 + path := "/"
816 + if len(req.Arguments()) > 0 {
817 + path = req.Arguments()[0]
818 + }
819 +
820 + flush, _, _ := req.Option("flush").Bool()
821 +
822 + prefix, err := getPrefix(req)
823 + if err != nil {
824 + res.SetError(err, cmds.ErrNormal)
825 + return
826 + }
827 +
828 + err = updatePath(nd.FilesRoot, path, prefix, flush)
829 + if err != nil {
830 + res.SetError(err, cmds.ErrNormal)
831 + return
832 + }
833 + },
834 +}
835 +
836 +func updatePath(rt *mfs.Root, pth string, prefix *cid.Prefix, flush bool) error {
837 + if prefix == nil {
838 + return nil
839 + }
840 +
841 + nd, err := mfs.Lookup(rt, pth)
842 + if err != nil {
843 + return err
844 + }
845 +
846 + switch n := nd.(type) {
847 + case *mfs.Directory:
848 + n.SetPrefix(prefix)
849 + default:
850 + return fmt.Errorf("can only update directories")
851 + }
852 +
853 + if flush {
854 + nd.Flush()
855 + }
856 +
857 + return nil
858 +}
859 +
860 var FilesRmCmd = &cmds.Command{
861 Helptext: cmds.HelpText{
862 Tagline: "Remove a file.",