Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "repo rm-root" command to unlink the files API root. #4446

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ func (d *cmdDetails) usesRepo() bool { return !d.doesNotUseRepo }
// properties so that other code can make decisions about whether to invoke a
// command or return an error to the user.
var cmdDetailsMap = map[string]cmdDetails{
"init": {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
"daemon": {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true},
"commands": {doesNotUseRepo: true},
"version": {doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
"log": {cannotRunOnClient: true},
"diag/cmds": {cannotRunOnClient: true},
"repo/fsck": {cannotRunOnDaemon: true},
"config/edit": {cannotRunOnDaemon: true, doesNotUseRepo: true},
"cid": {doesNotUseRepo: true},
"init": {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
"daemon": {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true},
"commands": {doesNotUseRepo: true},
"version": {doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
"log": {cannotRunOnClient: true},
"diag/cmds": {cannotRunOnClient: true},
"repo/fsck": {cannotRunOnDaemon: true},
"repo/rm-files-root": {cannotRunOnDaemon: true},
"config/edit": {cannotRunOnDaemon: true, doesNotUseRepo: true},
"cid": {doesNotUseRepo: true},
}
1 change: 1 addition & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func TestCommands(t *testing.T) {
"/repo/stat",
"/repo/verify",
"/repo/version",
"/repo/rm-files-root",
"/resolve",
"/shutdown",
"/stats",
Expand Down
97 changes: 92 additions & 5 deletions core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
"sync"
"text/tabwriter"

core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

cmds "gx/ipfs/QmQkW9fnCsg9SLHdViiAh6qfBppodsPZVpU92dZLqYtEfs/go-ipfs-cmds"
cid "gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
config "gx/ipfs/QmUAuYuiafnJRZxDDX7MuruMNsicYNuyub5vUeAcupUBNs/go-ipfs-config"
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
b58 "gx/ipfs/QmWFAMPqsEyUX7gDUsRVmMWz59FxSpJ1b2v6bJ1yYzo7jY/go-base58-fast/base58"
bstore "gx/ipfs/QmXjKkjMDTtXAiLBwstVexofB8LeruZmE2eBd85GwGFFLA/go-ipfs-blockstore"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
Expand All @@ -36,11 +39,12 @@ var RepoCmd = &cmds.Command{
},

Subcommands: map[string]*cmds.Command{
"stat": repoStatCmd,
"gc": repoGcCmd,
"fsck": repoFsckCmd,
"version": repoVersionCmd,
"verify": repoVerifyCmd,
"stat": repoStatCmd,
"gc": repoGcCmd,
"fsck": repoFsckCmd,
"version": repoVersionCmd,
"verify": repoVerifyCmd,
"rm-files-root": repoRmFilesRootCmd,
},
}

Expand Down Expand Up @@ -262,6 +266,89 @@ daemons are running.
},
}

var repoRmFilesRootCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Unlink the root used by the `ipfs files` comands.",
ShortDescription: `
'ipfs repo rm-files-root' will unlink the root used by the files API ('ipfs
files' commands) without trying to read the root itself. The root and
its children will be removed the next time the garbage collector runs,
unless pinned.

This command is designed to recover form the situation when the root
becomes unavailable and recovering it (such as recreating it, or
fetching it from the network) is not possible. This command should
only be used as a last resort as using this command could lead to data
loss if there are unpinned nodes connected to the root.

This command can only run when the ipfs daemon is not running.
`,
},
Options: []cmdkit.Option{
cmdkit.BoolOption("confirm", "Really perform operation."),
cmdkit.BoolOption("remove-existing-root", "Remove even if it root exists locally."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
confirm, _ := req.Options["confirm"].(bool)
removeExistingRoot, _ := req.Options["remove-existing-root"].(bool)

if !confirm {
return fmt.Errorf("this is a potentially dangerous operation please pass --confirm to proceed")
}

configRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}

// Can't use a full node as that interferes with the removal
// of the files root, so open the repo directly
repo, err := fsrepo.Open(configRoot)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's... unfortunate. I run my daemon as a separate user. I'm fine with this as a temporary limitation, but we should fix it eventually.

if err != nil {
return err
}
defer repo.Close()
bs := bstore.NewBlockstore(repo.Datastore())

// Get the old root and display it to the user so that they can
// can do something to prevent from being garbage collected,
// such as pin it
val, err := repo.Datastore().Get(core.FilesRootKey)
if err == ds.ErrNotFound || val == nil {
return cmds.EmitOnce(res, &MessageOutput{"`ipfs files` root not found.\n"})
}

var cidStr string
var have bool
c, err := cid.Cast(val)
if err == nil {
cidStr = c.String()
have, _ = bs.Has(c)
} else {
cidStr = b58.Encode(val)
}

if have && !removeExistingRoot {
return fmt.Errorf("`ipfs files` root %s exists locally. Are you sure you want to unlink this? Pass --remove-existing-root to continue", cidStr)
}

err = repo.Datastore().Delete(core.FilesRootKey)
if err != nil {
return fmt.Errorf("unable to remove `ipfs files` root: %s. Root hash was %s", err.Error(), cidStr)
}
return cmds.EmitOnce(res, &MessageOutput{
fmt.Sprintf("Unlinked files API root. Root hash was %s.\n", cidStr),
})
},
Type: MessageOutput{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, msg *MessageOutput) error {
_, err := fmt.Fprintf(w, "%s", msg.Message)
return err
}),
},
}

type VerifyProgress struct {
Msg string
Progress int
Expand Down
8 changes: 5 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ import (

const IpnsValidatorTag = "ipns"

// FilesRootKey is the datastore key for the files root.
var FilesRootKey ds.Key = ds.NewKey("/local/filesroot")

const kReprovideFrequency = time.Hour * 12
const discoveryConnTimeout = time.Second * 30
const DefaultIpnsCacheSize = 128
Expand Down Expand Up @@ -856,13 +859,12 @@ func (n *IpfsNode) loadBootstrapPeers() ([]pstore.PeerInfo, error) {
}

func (n *IpfsNode) loadFilesRoot() error {
dsk := ds.NewKey("/local/filesroot")
pf := func(ctx context.Context, c cid.Cid) error {
return n.Repo.Datastore().Put(dsk, c.Bytes())
return n.Repo.Datastore().Put(FilesRootKey, c.Bytes())
}

var nd *merkledag.ProtoNode
val, err := n.Repo.Datastore().Get(dsk)
val, err := n.Repo.Datastore().Get(FilesRootKey)

switch {
case err == ds.ErrNotFound || val == nil:
Expand Down
49 changes: 49 additions & 0 deletions test/sharness/t0089-repo-rm-files-root.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
#
# Copyright (c) 2016 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test ipfs repo fsck"

. lib/test-lib.sh

test_init_ipfs

ROOT_HASH=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn

test_expect_success "ipfs repo rm-files-root fails without --confirm" '
test_must_fail ipfs repo rm-files-root 2> err &&
cat err &&
fgrep -q "please pass --confirm to proceed" err
'

test_expect_success "ipfs repo rm-files-root fails to remove existing root without --remove-existing-root" '
test_must_fail ipfs repo rm-files-root --confirm 2> err &&
cat err &&
fgrep -q "Are you sure you want to unlink this?" err
'

test_expect_success "ipfs repo rm-files-root" '
ipfs repo rm-files-root --confirm --remove-existing-root | tee rm-files-root.actual &&
echo "Unlinked files API root. Root hash was $ROOT_HASH." > rm-files-root.expected &&
test_cmp rm-files-root.expected rm-files-root.actual
'

test_expect_success "files api root really removed" '
ipfs repo rm-files-root --confirm | tee rm-files-root-post.actual &&
echo "Files API root not found." > rm-files-root-post.expected &&
test_cmp rm-files-root-post.expected rm-files-root-post.actual
'

test_launch_ipfs_daemon

test_expect_success "ipfs repo rm-files-root does not run on daemon" '
test_must_fail ipfs repo rm-files-root --confirm 2> err &&
cat err &&
fgrep -q "ipfs daemon is running" err
'

test_kill_ipfs_daemon

test_done