@cryptotaxi247 / kubo / commits / dd06956d3

pin cmd: proper CID encoding and backward compat

Michael Muré committed Jul 11, 2019 at 13:51 UTC dd06956d30df86126af314c7b88d846d44f3c583
2 files changed +138 -128
core/commands/ls.go
+1 -1
@@ -65,7 +65,7 @@ The JSON output contains type information.
65 cmds.BoolOption(lsHeadersOptionNameTime, "v", "Print table headers (Hash, Size, Name)."),
66 cmds.BoolOption(lsResolveTypeOptionName, "Resolve linked objects to find out their types.").WithDefault(true),
67 cmds.BoolOption(lsSizeOptionName, "Resolve linked objects to find out their file size.").WithDefault(true),
68 - cmds.BoolOption(lsStreamOptionName, "s", "Enable exprimental streaming of directory entries as they are traversed."),
68 + cmds.BoolOption(lsStreamOptionName, "s", "Enable experimental streaming of directory entries as they are traversed."),
69 },
70 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
71 api, err := cmdenv.GetApi(env, req)
core/commands/pin.go
+137 -127
@@ -314,7 +314,7 @@ Example:
314 Options: []cmds.Option{
315 cmds.StringOption(pinTypeOptionName, "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\".").WithDefault("all"),
316 cmds.BoolOption(pinQuietOptionName, "q", "Write just hashes of objects."),
317 - cmds.BoolOption(pinStreamOptionName, "s", "Don't buffer pins before sending."),
317 + cmds.BoolOption(pinStreamOptionName, "s", "Enable streaming of pins as they are discovered."),
318 },
319 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
320 n, err := cmdenv.GetNode(env)
@@ -349,9 +349,9 @@ Example:
349 }
350
351 if len(req.Arguments) > 0 {
352 - err = pinLsKeys(req.Context, req.Arguments, typeStr, n, api, emit)
352 + err = pinLsKeys(req, typeStr, n, api, emit)
353 } else {
354 - err = pinLsAll(req.Context, typeStr, n, emit)
354 + err = pinLsAll(req, typeStr, n, emit)
355 }
356 if err != nil {
357 return err
@@ -393,6 +393,140 @@ Example:
393 },
394 }
395
396 +type RefKeyObject struct {
397 + Cid string `json:",omitempty"`
398 + Type string `json:",omitempty"`
399 +}
400 +
401 +type RefObject struct {
402 + Type string
403 +}
404 +
405 +type RefKeyList struct {
406 + Keys map[string]RefObject `json:",omitempty"`
407 +}
408 +
409 +// Pin ls needs to output two different type depending on if it's streamed or not.
410 +// We use this to bypass the cmds lib refusing to have interface{}
411 +type PinLsOutputWrapper struct {
412 + RefKeyList
413 + RefKeyObject
414 +}
415 +
416 +func pinLsKeys(req *cmds.Request, typeStr string, n *core.IpfsNode, api coreiface.CoreAPI, emit func(value interface{}) error) error {
417 + mode, ok := pin.StringToMode(typeStr)
418 + if !ok {
419 + return fmt.Errorf("invalid pin mode '%s'", typeStr)
420 + }
421 +
422 + enc, err := cmdenv.GetCidEncoder(req)
423 + if err != nil {
424 + return err
425 + }
426 +
427 + for _, p := range req.Arguments {
428 + c, err := api.ResolvePath(req.Context, path.New(p))
429 + if err != nil {
430 + return err
431 + }
432 +
433 + pinType, pinned, err := n.Pinning.IsPinnedWithType(c.Cid(), mode)
434 + if err != nil {
435 + return err
436 + }
437 +
438 + if !pinned {
439 + return fmt.Errorf("path '%s' is not pinned", p)
440 + }
441 +
442 + switch pinType {
443 + case "direct", "indirect", "recursive", "internal":
444 + default:
445 + pinType = "indirect through " + pinType
446 + }
447 +
448 + err = emit(&PinLsOutputWrapper{
449 + RefKeyObject: RefKeyObject{
450 + Type: pinType,
451 + Cid: enc.Encode(c.Cid()),
452 + },
453 + })
454 + if err != nil {
455 + return err
456 + }
457 + }
458 +
459 + return nil
460 +}
461 +
462 +func pinLsAll(req *cmds.Request, typeStr string, n *core.IpfsNode, emit func(value interface{}) error) error {
463 + enc, err := cmdenv.GetCidEncoder(req)
464 + if err != nil {
465 + return err
466 + }
467 +
468 + keys := cid.NewSet()
469 +
470 + AddToResultKeys := func(keyList []cid.Cid, typeStr string) error {
471 + for _, c := range keyList {
472 + if keys.Visit(c) {
473 + err := emit(&PinLsOutputWrapper{
474 + RefKeyObject: RefKeyObject{
475 + Type: typeStr,
476 + Cid: enc.Encode(c),
477 + },
478 + })
479 + if err != nil {
480 + return err
481 + }
482 + }
483 + }
484 + return nil
485 + }
486 +
487 + if typeStr == "direct" || typeStr == "all" {
488 + err := AddToResultKeys(n.Pinning.DirectKeys(), "direct")
489 + if err != nil {
490 + return err
491 + }
492 + }
493 + if typeStr == "indirect" || typeStr == "all" {
494 + for _, k := range n.Pinning.RecursiveKeys() {
495 + var visitErr error
496 + err := dag.EnumerateChildren(req.Context, dag.GetLinksWithDAG(n.DAG), k, func(c cid.Cid) bool {
497 + r := keys.Visit(c)
498 + if r {
499 + err := emit(&PinLsOutputWrapper{
500 + RefKeyObject: RefKeyObject{
501 + Type: typeStr,
502 + Cid: enc.Encode(c),
503 + },
504 + })
505 + if err != nil {
506 + visitErr = err
507 + }
508 + }
509 + return r
510 + })
511 +
512 + if visitErr != nil {
513 + return visitErr
514 + }
515 + if err != nil {
516 + return err
517 + }
518 + }
519 + }
520 + if typeStr == "recursive" || typeStr == "all" {
521 + err := AddToResultKeys(n.Pinning.RecursiveKeys(), "recursive")
522 + if err != nil {
523 + return err
524 + }
525 + }
526 +
527 + return nil
528 +}
529 +
530 const (
531 pinUnpinOptionName = "unpin"
532 )
@@ -507,130 +641,6 @@ var verifyPinCmd = &cmds.Command{
641 },
642 }
643
510 -type RefKeyObject struct {
511 - Cid string
512 - Type string
513 -}
514 -
515 -type RefObject struct {
516 - Type string
517 -}
518 -
519 -type RefKeyList struct {
520 - Keys map[string]RefObject
521 -}
522 -
523 -// Pin ls needs to output two different type depending on if it's streamed or not.
524 -// We use this to bypass the cmds lib refusing to have interface{}
525 -type PinLsOutputWrapper struct {
526 - RefKeyList
527 - RefKeyObject
528 -}
529 -
530 -func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, api coreiface.CoreAPI, emit func(value interface{}) error) error {
531 - mode, ok := pin.StringToMode(typeStr)
532 - if !ok {
533 - return fmt.Errorf("invalid pin mode '%s'", typeStr)
534 - }
535 -
536 - for _, p := range args {
537 - c, err := api.ResolvePath(ctx, path.New(p))
538 - if err != nil {
539 - return err
540 - }
541 -
542 - pinType, pinned, err := n.Pinning.IsPinnedWithType(c.Cid(), mode)
543 - if err != nil {
544 - return err
545 - }
546 -
547 - if !pinned {
548 - return fmt.Errorf("path '%s' is not pinned", p)
549 - }
550 -
551 - switch pinType {
552 - case "direct", "indirect", "recursive", "internal":
553 - default:
554 - pinType = "indirect through " + pinType
555 - }
556 -
557 - err = emit(&PinLsOutputWrapper{
558 - RefKeyObject: RefKeyObject{
559 - Type: pinType,
560 - Cid: c.Cid().String(),
561 - },
562 - })
563 - if err != nil {
564 - return err
565 - }
566 - }
567 -
568 - return nil
569 -}
570 -
571 -func pinLsAll(ctx context.Context, typeStr string, n *core.IpfsNode, emit func(value interface{}) error) error {
572 - keys := cid.NewSet()
573 -
574 - AddToResultKeys := func(keyList []cid.Cid, typeStr string) error {
575 - for _, c := range keyList {
576 - if keys.Visit(c) {
577 - err := emit(&PinLsOutputWrapper{
578 - RefKeyObject: RefKeyObject{
579 - Type: typeStr,
580 - Cid: c.String(),
581 - },
582 - })
583 - if err != nil {
584 - return err
585 - }
586 - }
587 - }
588 - return nil
589 - }
590 -
591 - if typeStr == "direct" || typeStr == "all" {
592 - err := AddToResultKeys(n.Pinning.DirectKeys(), "direct")
593 - if err != nil {
594 - return err
595 - }
596 - }
597 - if typeStr == "recursive" || typeStr == "all" {
598 - err := AddToResultKeys(n.Pinning.RecursiveKeys(), "recursive")
599 - if err != nil {
600 - return err
601 - }
602 - }
603 - if typeStr == "indirect" || typeStr == "all" {
604 - for _, k := range n.Pinning.RecursiveKeys() {
605 - var visitErr error
606 - err := dag.EnumerateChildren(ctx, dag.GetLinksWithDAG(n.DAG), k, func(c cid.Cid) bool {
607 - r := keys.Visit(c)
608 - if r {
609 - err := emit(&PinLsOutputWrapper{
610 - RefKeyObject: RefKeyObject{
611 - Type: "indirect",
612 - Cid: c.String(),
613 - },
614 - })
615 - if err != nil {
616 - visitErr = err
617 - }
618 - }
619 - return r
620 - })
621 -
622 - if visitErr != nil {
623 - return visitErr
624 - }
625 - if err != nil {
626 - return err
627 - }
628 - }
629 - }
630 -
631 - return nil
632 -}
633 -
644 // PinVerifyRes is the result returned for each pin checked in "pin verify"
645 type PinVerifyRes struct {
646 Cid string