package commands import ( "bytes" "errors" "fmt" "io" "io/ioutil" "strings" "github.com/ipfs/go-ipfs/blocks" bs "github.com/ipfs/go-ipfs/blocks/blockstore" key "github.com/ipfs/go-ipfs/blocks/key" cmds "github.com/ipfs/go-ipfs/commands" "github.com/ipfs/go-ipfs/pin" mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash" u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" ) type BlockStat struct { Key string Size int } func (bs BlockStat) String() string { return fmt.Sprintf("Key: %s\nSize: %d\n", bs.Key, bs.Size) } var BlockCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Manipulate raw IPFS blocks.", ShortDescription: ` 'ipfs block' is a plumbing command used to manipulate raw ipfs blocks. Reads from stdin or writes to stdout, and is a base58 encoded multihash. `, }, Subcommands: map[string]*cmds.Command{ "stat": blockStatCmd, "get": blockGetCmd, "put": blockPutCmd, "rm": blockRmCmd, }, } var blockStatCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Print information of a raw IPFS block.", ShortDescription: ` 'ipfs block stat' is a plumbing command for retrieving information on raw ipfs blocks. It outputs the following to stdout: Key - the base58 encoded multihash Size - the size of the block in bytes `, }, Arguments: []cmds.Argument{ cmds.StringArg("key", true, false, "The base58 multihash of an existing block to stat.").EnableStdin(), }, Run: func(req cmds.Request, res cmds.Response) { b, err := getBlockForKey(req, req.Arguments()[0]) if err != nil { res.SetError(err, cmds.ErrNormal) return } res.SetOutput(&BlockStat{ Key: b.Key().B58String(), Size: len(b.Data()), }) }, Type: BlockStat{}, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { bs := res.Output().(*BlockStat) return strings.NewReader(bs.String()), nil }, }, } var blockGetCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Get a raw IPFS block.", ShortDescription: ` 'ipfs block get' is a plumbing command for retrieving raw ipfs blocks. It outputs to stdout, and is a base58 encoded multihash. `, }, Arguments: []cmds.Argument{ cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get.").EnableStdin(), }, Run: func(req cmds.Request, res cmds.Response) { b, err := getBlockForKey(req, req.Arguments()[0]) if err != nil { res.SetError(err, cmds.ErrNormal) return } res.SetOutput(bytes.NewReader(b.Data())) }, } var blockPutCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Stores input as an IPFS block.", ShortDescription: ` 'ipfs block put' is a plumbing command for storing raw ipfs blocks. It reads from stdin, and is a base58 encoded multihash. `, }, Arguments: []cmds.Argument{ cmds.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(), }, Run: func(req cmds.Request, res cmds.Response) { n, err := req.InvocContext().GetNode() if err != nil { res.SetError(err, cmds.ErrNormal) return } file, err := req.Files().NextFile() if err != nil { res.SetError(err, cmds.ErrNormal) return } data, err := ioutil.ReadAll(file) if err != nil { res.SetError(err, cmds.ErrNormal) return } err = file.Close() if err != nil { res.SetError(err, cmds.ErrNormal) return } b := blocks.NewBlock(data) log.Debugf("BlockPut key: '%q'", b.Key()) k, err := n.Blocks.AddBlock(b) if err != nil { res.SetError(err, cmds.ErrNormal) return } res.SetOutput(&BlockStat{ Key: k.String(), Size: len(data), }) }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { bs := res.Output().(*BlockStat) return strings.NewReader(bs.Key + "\n"), nil }, }, Type: BlockStat{}, } func getBlockForKey(req cmds.Request, skey string) (blocks.Block, error) { n, err := req.InvocContext().GetNode() if err != nil { return nil, err } if !u.IsValidHash(skey) { return nil, errors.New("Not a valid hash") } h, err := mh.FromB58String(skey) if err != nil { return nil, err } k := key.Key(h) b, err := n.Blocks.GetBlock(req.Context(), k) if err != nil { return nil, err } log.Debugf("ipfs block: got block with key: %q", b.Key()) return b, nil } var blockRmCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Remove IPFS block(s).", ShortDescription: ` 'ipfs block rm' is a plumbing command for removing raw ipfs blocks. It takes a list of base58 encoded multihashs to remove. `, }, Arguments: []cmds.Argument{ cmds.StringArg("hash", true, true, "Bash58 encoded multihash of block(s) to remove."), }, Options: []cmds.Option{ cmds.BoolOption("ignore-pins", "Ignore pins.").Default(false), }, Run: func(req cmds.Request, res cmds.Response) { ignorePins, _, err := req.Option("ignore-pins").Bool() if err != nil { res.SetError(err, cmds.ErrNormal) return } n, err := req.InvocContext().GetNode() if err != nil { res.SetError(err, cmds.ErrNormal) return } hashes := req.Arguments() keys := make([]key.Key, 0, len(hashes)) for _, hash := range hashes { k := key.B58KeyDecode(hash) keys = append(keys, k) } rdr, wtr := io.Pipe() go func() { pinning := n.Pinning if ignorePins { pinning = nil } err := rmBlocks(n.Blockstore, pinning, wtr, keys) if err != nil { wtr.CloseWithError(fmt.Errorf("Some blocks not deleted: %s", err)) } else { wtr.Close() } }() res.SetOutput(rdr) return }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { return res.(io.Reader), nil }, }, } // pins may be nil func rmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, out io.Writer, keys []key.Key) error { var unlocker bs.Unlocker defer func() { if unlocker != nil { unlocker.Unlock() } }() if pins != nil { // Need to make sure that some operation that is // finishing with a pin is ocurr simultaneously. unlocker = blocks.GCLock() err := checkIfPinned(pins, keys) if err != nil { return err } } for _, k := range keys { err := blocks.DeleteBlock(k) if err != nil { return fmt.Errorf("%s: %s", k, err) } if out != nil { fmt.Fprintf(out, "deleted %s\n", k) } } return nil } func checkIfPinned(pins pin.Pinner, keys []key.Key) error { for _, k := range keys { reason, pinned, err := pins.IsPinned(k) if err != nil { return err } if pinned { return fmt.Errorf("%s pinned via %s", k, reason) } } return nil }