2021-10-13 11:23:03 +00:00
|
|
|
package inspect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-18 11:12:58 +00:00
|
|
|
"os"
|
2021-10-13 11:23:03 +00:00
|
|
|
|
|
|
|
common "github.com/nspcc-dev/neofs-node/cmd/neofs-lens/internal"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
|
2022-05-12 16:37:46 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-12 16:37:46 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-10-13 11:23:03 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagAddress = "address"
|
|
|
|
flagEnginePath = "path"
|
|
|
|
flagHeader = "header"
|
|
|
|
flagOutFile = "out"
|
|
|
|
flagWriteCache = "writecache"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
vAddress string
|
|
|
|
vHeader bool
|
|
|
|
vPath string
|
|
|
|
vOut string
|
|
|
|
vWriteCache bool
|
|
|
|
)
|
|
|
|
|
|
|
|
// Command contains `inspect` command definition.
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "inspect",
|
|
|
|
Short: "Object inspection",
|
|
|
|
Long: `Inspect specific object in storage engine component.`,
|
|
|
|
Run: objectInspectCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Command.Flags().StringVar(&vAddress, flagAddress, "", "Object address")
|
|
|
|
_ = Command.MarkFlagRequired(flagAddress)
|
|
|
|
|
|
|
|
Command.Flags().StringVar(&vPath, flagEnginePath, "",
|
|
|
|
"Path to storage engine component",
|
|
|
|
)
|
|
|
|
_ = Command.MarkFlagFilename(flagEnginePath)
|
|
|
|
_ = Command.MarkFlagRequired(flagEnginePath)
|
|
|
|
|
|
|
|
Command.Flags().StringVar(&vOut, flagOutFile, "",
|
|
|
|
"File to save object payload")
|
|
|
|
|
|
|
|
Command.Flags().BoolVar(&vHeader, flagHeader, false, "Inspect only header")
|
|
|
|
Command.Flags().BoolVar(&vWriteCache, flagWriteCache, false,
|
|
|
|
"Process write-cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
func objectInspectCmd(cmd *cobra.Command, _ []string) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
|
|
|
|
err := addr.DecodeString(vAddress)
|
2021-10-13 11:23:03 +00:00
|
|
|
common.ExitOnErr(cmd, common.Errf("invalid address argument: %w", err))
|
|
|
|
|
|
|
|
if vOut == "" && !vHeader {
|
|
|
|
common.ExitOnErr(cmd, fmt.Errorf("either --%s or --%s flag must be provided",
|
|
|
|
flagHeader, flagOutFile))
|
|
|
|
}
|
|
|
|
|
|
|
|
if vWriteCache {
|
|
|
|
db, err := writecache.OpenDB(vPath, true)
|
|
|
|
common.ExitOnErr(cmd, common.Errf("could not open write-cache db: %w", err))
|
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
data, err := writecache.Get(db, []byte(vAddress))
|
2021-10-13 11:23:03 +00:00
|
|
|
common.ExitOnErr(cmd, common.Errf("could not fetch object: %w", err))
|
|
|
|
printObjectInfo(cmd, data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blz := blobovnicza.New(
|
|
|
|
blobovnicza.WithPath(vPath),
|
2022-06-28 13:42:50 +00:00
|
|
|
blobovnicza.WithReadOnly(true))
|
2021-10-13 11:23:03 +00:00
|
|
|
common.ExitOnErr(cmd, blz.Open())
|
|
|
|
|
|
|
|
defer blz.Close()
|
|
|
|
|
2022-05-23 14:15:16 +00:00
|
|
|
var prm blobovnicza.GetPrm
|
2021-10-13 11:23:03 +00:00
|
|
|
prm.SetAddress(addr)
|
|
|
|
res, err := blz.Get(prm)
|
|
|
|
common.ExitOnErr(cmd, common.Errf("could not fetch object: %w", err))
|
|
|
|
|
|
|
|
printObjectInfo(cmd, res.Object())
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
func printContainerID(cmd *cobra.Command, recv func() (cid.ID, bool)) {
|
|
|
|
var val string
|
|
|
|
|
|
|
|
id, ok := recv()
|
|
|
|
if ok {
|
|
|
|
val = id.String()
|
|
|
|
} else {
|
|
|
|
val = "<empty>"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("CID:", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printObjectID(cmd *cobra.Command, recv func() (oid.ID, bool)) {
|
|
|
|
var val string
|
|
|
|
|
|
|
|
id, ok := recv()
|
|
|
|
if ok {
|
|
|
|
val = id.String()
|
|
|
|
} else {
|
|
|
|
val = "<empty>"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("ID:", val)
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:23:03 +00:00
|
|
|
func printObjectInfo(cmd *cobra.Command, data []byte) {
|
|
|
|
obj := object.New()
|
|
|
|
err := obj.Unmarshal(data)
|
|
|
|
common.ExitOnErr(cmd, common.Errf("can't unmarshal object: %w", err))
|
|
|
|
|
|
|
|
if vHeader {
|
|
|
|
cmd.Println("Version:", obj.Version())
|
|
|
|
cmd.Println("Type:", obj.Type())
|
2022-05-12 16:37:46 +00:00
|
|
|
printContainerID(cmd, obj.ContainerID)
|
|
|
|
printObjectID(cmd, obj.ID)
|
2021-10-13 11:23:03 +00:00
|
|
|
cmd.Println("Owner:", obj.OwnerID())
|
|
|
|
cmd.Println("CreatedAt:", obj.CreationEpoch())
|
|
|
|
cmd.Println("PayloadSize:", obj.PayloadSize())
|
|
|
|
cmd.Println("Attributes:")
|
|
|
|
for _, attr := range obj.Attributes() {
|
|
|
|
cmd.Printf(" %s: %s\n", attr.Key(), attr.Value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vOut != "" {
|
2022-03-18 11:12:58 +00:00
|
|
|
err := os.WriteFile(vOut, obj.Payload(), 0644)
|
2021-10-13 11:23:03 +00:00
|
|
|
common.ExitOnErr(cmd, common.Errf("couldn't write payload: %w", err))
|
|
|
|
}
|
|
|
|
}
|