filestore.go 6.3 KB
Newer Older
1 2 3 4 5
package commands

import (
	"context"
	"fmt"
6
	"io"
7

Jan Winkelmann's avatar
Jan Winkelmann committed
8
	oldCmds "github.com/ipfs/go-ipfs/commands"
Łukasz Magiera's avatar
Łukasz Magiera committed
9
	lgc "github.com/ipfs/go-ipfs/commands/legacy"
10
	"github.com/ipfs/go-ipfs/core"
11
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jan Winkelmann's avatar
Jan Winkelmann committed
12
	e "github.com/ipfs/go-ipfs/core/commands/e"
13
	"github.com/ipfs/go-ipfs/filestore"
Jan Winkelmann's avatar
Jan Winkelmann committed
14

15
	cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
16
	"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
Steven Allen's avatar
Steven Allen committed
17
	cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
18 19 20
)

var FileStoreCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
21
	Helptext: cmdkit.HelpText{
22 23 24
		Tagline: "Interact with filestore objects.",
	},
	Subcommands: map[string]*cmds.Command{
25 26 27
		"ls":     lsFileStore,
		"verify": lgc.NewCommand(verifyFileStore),
		"dups":   lgc.NewCommand(dupsFileStore),
28 29 30 31
	},
}

var lsFileStore = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
32
	Helptext: cmdkit.HelpText{
33
		Tagline: "List objects in filestore.",
34 35 36 37 38 39 40 41 42 43
		LongDescription: `
List objects in the filestore.

If one or more <obj> is specified only list those specific objects,
otherwise list all objects.

The output is:

<hash> <size> <path> <offset>
`,
44
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
45 46
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("obj", false, true, "Cid of objects to list."),
47
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
48 49
	Options: []cmdkit.Option{
		cmdkit.BoolOption("file-order", "sort the results based on the path of the backing file"),
50
	},
keks's avatar
keks committed
51
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Steven Allen's avatar
Steven Allen committed
52
		_, fs, err := getFilestore(env)
53
		if err != nil {
keks's avatar
keks committed
54
			return err
55
		}
56
		args := req.Arguments
57
		if len(args) > 0 {
58
			out := perKeyActionToChan(req.Context, args, func(c cid.Cid) *filestore.ListRes {
59
				return filestore.List(fs, c)
Jan Winkelmann's avatar
Jan Winkelmann committed
60 61
			})

keks's avatar
keks committed
62 63
			return res.Emit(out)
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
64

keks's avatar
keks committed
65 66 67 68
		fileOrder, _ := req.Options["file-order"].(bool)
		next, err := filestore.ListAll(fs, fileOrder)
		if err != nil {
			return err
69
		}
keks's avatar
keks committed
70 71 72

		out := listResToChan(req.Context, next)
		return res.Emit(out)
73
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
74
	PostRun: cmds.PostRunMap{
75
		cmds.CLI: streamResult(func(v interface{}, out io.Writer) nonFatalError {
76 77 78
			r := v.(*filestore.ListRes)
			if r.ErrorMsg != "" {
				return nonFatalError(r.ErrorMsg)
keks's avatar
keks committed
79
			}
80 81 82
			fmt.Fprintf(out, "%s\n", r.FormatLong())
			return ""
		}),
83 84 85 86
	},
	Type: filestore.ListRes{},
}

Jan Winkelmann's avatar
Jan Winkelmann committed
87 88
var verifyFileStore = &oldCmds.Command{
	Helptext: cmdkit.HelpText{
89
		Tagline: "Verify objects in filestore.",
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		LongDescription: `
Verify objects in the filestore.

If one or more <obj> is specified only verify those specific objects,
otherwise verify all objects.

The output is:

<status> <hash> <size> <path> <offset>

Where <status> is one of:
ok:       the block can be reconstructed
changed:  the contents of the backing file have changed
no-file:  the backing file could not be found
error:    there was some other problem reading the file
missing:  <obj> could not be found in the filestore
ERROR:    internal error, most likely due to a corrupt database

For ERROR entries the error will also be printed to stderr.
`,
110
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
111 112
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("obj", false, true, "Cid of objects to verify."),
113
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
114 115
	Options: []cmdkit.Option{
		cmdkit.BoolOption("file-order", "verify the objects based on the order of the backing file"),
116
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
117 118
	Run: func(req oldCmds.Request, res oldCmds.Response) {
		_, fs, err := getFilestore(req.InvocContext())
119
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
120
			res.SetError(err, cmdkit.ErrNormal)
121 122
			return
		}
123 124
		args := req.Arguments()
		if len(args) > 0 {
125
			out := perKeyActionToChan(req.Context(), args, func(c cid.Cid) *filestore.ListRes {
126
				return filestore.Verify(fs, c)
Jan Winkelmann's avatar
Jan Winkelmann committed
127
			})
128 129
			res.SetOutput(out)
		} else {
130 131
			fileOrder, _, _ := req.Option("file-order").Bool()
			next, err := filestore.VerifyAll(fs, fileOrder)
132
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
133
				res.SetError(err, cmdkit.ErrNormal)
134 135
				return
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
136
			out := listResToChan(req.Context(), next)
137
			res.SetOutput(out)
138 139
		}
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
140 141 142 143 144 145 146 147
	Marshalers: oldCmds.MarshalerMap{
		oldCmds.Text: func(res oldCmds.Response) (io.Reader, error) {
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

			r, ok := v.(*filestore.ListRes)
148
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
149
				return nil, e.TypeErr(r, v)
150
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
151 152 153

			if r.Status == filestore.StatusOtherError {
				fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg)
154
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
155
			fmt.Fprintf(res.Stdout(), "%s %s\n", r.Status.Format(), r.FormatLong())
156 157
			return nil, nil
		},
158 159 160 161
	},
	Type: filestore.ListRes{},
}

Jan Winkelmann's avatar
Jan Winkelmann committed
162 163
var dupsFileStore = &oldCmds.Command{
	Helptext: cmdkit.HelpText{
164
		Tagline: "List blocks that are both in the filestore and standard block storage.",
165
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
166 167
	Run: func(req oldCmds.Request, res oldCmds.Response) {
		_, fs, err := getFilestore(req.InvocContext())
168
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
169
			res.SetError(err, cmdkit.ErrNormal)
170 171 172 173
			return
		}
		ch, err := fs.FileManager().AllKeysChan(req.Context())
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
174
			res.SetError(err, cmdkit.ErrNormal)
175 176 177 178 179 180 181 182 183 184 185
			return
		}

		out := make(chan interface{}, 128)
		res.SetOutput((<-chan interface{})(out))

		go func() {
			defer close(out)
			for cid := range ch {
				have, err := fs.MainBlockstore().Has(cid)
				if err != nil {
186 187 188 189
					select {
					case out <- &RefWrapper{Err: err.Error()}:
					case <-req.Context().Done():
					}
190 191 192
					return
				}
				if have {
193 194 195 196 197
					select {
					case out <- &RefWrapper{Ref: cid.String()}:
					case <-req.Context().Done():
						return
					}
198 199 200 201 202 203 204 205
				}
			}
		}()
	},
	Marshalers: refsMarshallerMap,
	Type:       RefWrapper{},
}

206
func getFilestore(env interface{}) (*core.IpfsNode, *filestore.Filestore, error) {
207
	n, err := cmdenv.GetNode(env)
208 209 210 211 212
	if err != nil {
		return nil, nil, err
	}
	fs := n.Filestore
	if fs == nil {
213
		return n, nil, filestore.ErrFilestoreNotEnabled
214 215 216 217
	}
	return n, fs, err
}

Jan Winkelmann's avatar
Jan Winkelmann committed
218
func listResToChan(ctx context.Context, next func() *filestore.ListRes) <-chan interface{} {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	out := make(chan interface{}, 128)
	go func() {
		defer close(out)
		for {
			r := next()
			if r == nil {
				return
			}
			select {
			case out <- r:
			case <-ctx.Done():
				return
			}
		}
	}()
	return out
}
236

237
func perKeyActionToChan(ctx context.Context, args []string, action func(cid.Cid) *filestore.ListRes) <-chan interface{} {
238 239 240 241 242 243
	out := make(chan interface{}, 128)
	go func() {
		defer close(out)
		for _, arg := range args {
			c, err := cid.Decode(arg)
			if err != nil {
244 245
				select {
				case out <- &filestore.ListRes{
246 247
					Status:   filestore.StatusOtherError,
					ErrorMsg: fmt.Sprintf("%s: %v", arg, err),
248 249
				}:
				case <-ctx.Done():
250
				}
251

252 253 254 255 256 257 258 259 260 261 262 263
				continue
			}
			r := action(c)
			select {
			case out <- r:
			case <-ctx.Done():
				return
			}
		}
	}()
	return out
}