|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/ipfs/go-datastore" |
| 8 | + dsq "github.com/ipfs/go-datastore/query" |
| 9 | + "github.com/urfave/cli/v2" |
| 10 | + "golang.org/x/xerrors" |
| 11 | + |
| 12 | + "github.com/filecoin-project/lotus/node/repo" |
| 13 | +) |
| 14 | + |
| 15 | +var f3Cmd = &cli.Command{ |
| 16 | + Name: "f3", |
| 17 | + Description: "f3 related commands", |
| 18 | + Subcommands: []*cli.Command{ |
| 19 | + f3ClearStateCmd, |
| 20 | + }, |
| 21 | +} |
| 22 | + |
| 23 | +var f3ClearStateCmd = &cli.Command{ |
| 24 | + Name: "clear-state", |
| 25 | + Description: "remove all f3 state", |
| 26 | + Flags: []cli.Flag{ |
| 27 | + &cli.BoolFlag{ |
| 28 | + Name: "really-do-it", |
| 29 | + }, |
| 30 | + }, |
| 31 | + Action: func(cctx *cli.Context) error { |
| 32 | + r, err := repo.NewFS(cctx.String("repo")) |
| 33 | + if err != nil { |
| 34 | + return xerrors.Errorf("opening fs repo: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + exists, err := r.Exists() |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + if !exists { |
| 42 | + return xerrors.Errorf("lotus repo doesn't exist") |
| 43 | + } |
| 44 | + |
| 45 | + lr, err := r.Lock(repo.FullNode) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + defer lr.Close() //nolint:errcheck |
| 50 | + |
| 51 | + ds, err := lr.Datastore(context.Background(), "/metadata") |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + dryRun := !cctx.Bool("really-do-it") |
| 57 | + |
| 58 | + q, err := ds.Query(context.Background(), dsq.Query{ |
| 59 | + Prefix: "/f3", |
| 60 | + KeysOnly: true, |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + return xerrors.Errorf("datastore query: %w", err) |
| 64 | + } |
| 65 | + defer q.Close() //nolint:errcheck |
| 66 | + |
| 67 | + batch, err := ds.Batch(cctx.Context) |
| 68 | + if err != nil { |
| 69 | + return xerrors.Errorf("failed to create a datastore batch: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + for r, ok := q.NextSync(); ok; r, ok = q.NextSync() { |
| 73 | + if r.Error != nil { |
| 74 | + return xerrors.Errorf("failed to read datastore: %w", err) |
| 75 | + } |
| 76 | + _, _ = fmt.Fprintf(cctx.App.Writer, "deleting: %q\n", r.Key) |
| 77 | + if !dryRun { |
| 78 | + if err := batch.Delete(cctx.Context, datastore.NewKey(r.Key)); err != nil { |
| 79 | + return xerrors.Errorf("failed to delete %q: %w", r.Key, err) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + if !dryRun { |
| 84 | + if err := batch.Commit(cctx.Context); err != nil { |
| 85 | + return xerrors.Errorf("failed to flush the batch: %w", err) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if err := ds.Close(); err != nil { |
| 90 | + return xerrors.Errorf("error when closing datastore: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + if dryRun { |
| 94 | + _, _ = fmt.Fprintln(cctx.App.Writer, "NOTE: dry run complete, re-run with --really-do-it to actually delete F3 state") |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | + }, |
| 99 | +} |
0 commit comments