2022-06-01 12:42:28 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-06-01 12:42:28 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var objectHeadCmd = &cobra.Command{
|
|
|
|
Use: "head",
|
|
|
|
Short: "Get object header",
|
|
|
|
Long: "Get object header",
|
|
|
|
Run: getObjectHeader,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectHeadCmd() {
|
|
|
|
commonflags.Init(objectHeadCmd)
|
2022-10-20 09:40:33 +00:00
|
|
|
initFlagSession(objectHeadCmd, "HEAD")
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
flags := objectHeadCmd.Flags()
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
|
|
|
_ = objectHeadCmd.MarkFlagRequired(commonflags.CIDFlag)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(commonflags.OIDFlag, "", commonflags.OIDFlagUsage)
|
|
|
|
_ = objectHeadCmd.MarkFlagRequired(commonflags.OIDFlag)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(fileFlag, "", "File to write header to. Default: stdout.")
|
2022-06-23 13:52:47 +00:00
|
|
|
flags.Bool(commonflags.JSON, false, "Marshal output in JSON")
|
2022-06-01 12:42:28 +00:00
|
|
|
flags.Bool("proto", false, "Marshal output in Protobuf")
|
|
|
|
flags.Bool(rawFlag, false, rawFlagDesc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObjectHeader(cmd *cobra.Command, _ []string) {
|
|
|
|
var cnr cid.ID
|
|
|
|
var obj oid.ID
|
|
|
|
|
|
|
|
objAddr := readObjectAddress(cmd, &cnr, &obj)
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
2022-10-10 18:31:19 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
|
|
|
|
2022-06-01 12:42:28 +00:00
|
|
|
var prm internalclient.HeadObjectPrm
|
2022-10-10 18:31:19 +00:00
|
|
|
prm.SetClient(cli)
|
2022-06-01 12:42:28 +00:00
|
|
|
Prepare(cmd, &prm)
|
2022-10-10 18:31:19 +00:00
|
|
|
readSession(cmd, &prm, pk, cnr, obj)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
raw, _ := cmd.Flags().GetBool(rawFlag)
|
|
|
|
prm.SetRawFlag(raw)
|
|
|
|
prm.SetAddress(objAddr)
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
res, err := internalclient.HeadObject(cmd.Context(), prm)
|
2022-06-01 12:42:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if ok := printSplitInfoErr(cmd, err); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-22 06:43:42 +00:00
|
|
|
if ok := printECInfoErr(cmd, err); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
err = saveAndPrintHeader(cmd, res.Header(), cmd.Flag(fileFlag).Value.String())
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func saveAndPrintHeader(cmd *cobra.Command, obj *objectSDK.Object, filename string) error {
|
2022-06-01 12:42:28 +00:00
|
|
|
bs, err := marshalHeader(cmd, obj)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal header: %w", err)
|
|
|
|
}
|
|
|
|
if len(bs) != 0 {
|
|
|
|
if filename == "" {
|
|
|
|
cmd.Println(string(bs))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = os.WriteFile(filename, bs, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write header to file: %w", err)
|
|
|
|
}
|
|
|
|
cmd.Printf("[%s] Header successfully saved.", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return printHeader(cmd, obj)
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func marshalHeader(cmd *cobra.Command, hdr *objectSDK.Object) ([]byte, error) {
|
2022-06-23 13:52:47 +00:00
|
|
|
toJSON, _ := cmd.Flags().GetBool(commonflags.JSON)
|
2022-06-01 12:42:28 +00:00
|
|
|
toProto, _ := cmd.Flags().GetBool("proto")
|
|
|
|
switch {
|
|
|
|
case toJSON && toProto:
|
|
|
|
return nil, errors.New("'--json' and '--proto' flags are mutually exclusive")
|
|
|
|
case toJSON:
|
|
|
|
return hdr.MarshalJSON()
|
|
|
|
case toProto:
|
|
|
|
return hdr.Marshal()
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 13:11:56 +00:00
|
|
|
func printObjectID(cmd *cobra.Command, recv func() (oid.ID, bool)) {
|
2022-06-01 12:42:28 +00:00
|
|
|
var strID string
|
|
|
|
|
|
|
|
id, ok := recv()
|
|
|
|
if ok {
|
|
|
|
strID = id.String()
|
|
|
|
} else {
|
|
|
|
strID = "<empty>"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("ID: %s\n", strID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printContainerID(cmd *cobra.Command, recv func() (cid.ID, bool)) {
|
|
|
|
var strID string
|
|
|
|
|
|
|
|
id, ok := recv()
|
|
|
|
if ok {
|
|
|
|
strID = id.String()
|
|
|
|
} else {
|
|
|
|
strID = "<empty>"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("CID: %s\n", strID)
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func printHeader(cmd *cobra.Command, obj *objectSDK.Object) error {
|
2022-06-01 12:42:28 +00:00
|
|
|
printObjectID(cmd, obj.ID)
|
|
|
|
printContainerID(cmd, obj.ContainerID)
|
|
|
|
cmd.Printf("Owner: %s\n", obj.OwnerID())
|
|
|
|
cmd.Printf("CreatedAt: %d\n", obj.CreationEpoch())
|
|
|
|
cmd.Printf("Size: %d\n", obj.PayloadSize())
|
|
|
|
common.PrintChecksum(cmd, "HomoHash", obj.PayloadHomomorphicHash)
|
|
|
|
common.PrintChecksum(cmd, "Checksum", obj.PayloadChecksum)
|
|
|
|
cmd.Printf("Type: %s\n", obj.Type())
|
|
|
|
|
|
|
|
cmd.Println("Attributes:")
|
|
|
|
for _, attr := range obj.Attributes() {
|
2023-07-06 12:36:41 +00:00
|
|
|
if attr.Key() == objectSDK.AttributeTimestamp {
|
2022-06-01 12:42:28 +00:00
|
|
|
cmd.Printf(" %s=%s (%s)\n",
|
|
|
|
attr.Key(),
|
|
|
|
attr.Value(),
|
|
|
|
common.PrettyPrintUnixTime(attr.Value()))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd.Printf(" %s=%s\n", attr.Key(), attr.Value())
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:31:36 +00:00
|
|
|
if signature := obj.Signature(); signature != nil {
|
|
|
|
cmd.Print("ID signature:\n")
|
|
|
|
|
2023-06-26 13:18:39 +00:00
|
|
|
// TODO(@carpawell): #468 implement and use another approach to avoid conversion
|
2022-11-25 11:31:36 +00:00
|
|
|
var sigV2 refs.Signature
|
|
|
|
signature.WriteToV2(&sigV2)
|
|
|
|
|
|
|
|
cmd.Printf(" public key: %s\n", hex.EncodeToString(sigV2.GetKey()))
|
|
|
|
cmd.Printf(" signature: %s\n", hex.EncodeToString(sigV2.GetSign()))
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:27:22 +00:00
|
|
|
if ecHeader := obj.ECHeader(); ecHeader != nil {
|
|
|
|
cmd.Print("EC header:\n")
|
|
|
|
|
|
|
|
cmd.Printf(" parent object ID: %s\n", ecHeader.Parent().EncodeToString())
|
|
|
|
cmd.Printf(" index: %d\n", ecHeader.Index())
|
|
|
|
cmd.Printf(" total: %d\n", ecHeader.Total())
|
|
|
|
cmd.Printf(" header length: %d\n", ecHeader.HeaderLength())
|
|
|
|
}
|
|
|
|
|
2022-06-01 12:42:28 +00:00
|
|
|
return printSplitHeader(cmd, obj)
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func printSplitHeader(cmd *cobra.Command, obj *objectSDK.Object) error {
|
2022-06-01 12:42:28 +00:00
|
|
|
if splitID := obj.SplitID(); splitID != nil {
|
|
|
|
cmd.Printf("Split ID: %s\n", splitID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if oid, ok := obj.ParentID(); ok {
|
|
|
|
cmd.Printf("Split ParentID: %s\n", oid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prev, ok := obj.PreviousID(); ok {
|
|
|
|
cmd.Printf("Split PreviousID: %s\n", prev)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range obj.Children() {
|
|
|
|
cmd.Printf("Split ChildID: %s\n", child.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := obj.Parent()
|
|
|
|
if parent != nil {
|
|
|
|
cmd.Print("\nSplit Parent Header:\n")
|
|
|
|
|
|
|
|
return printHeader(cmd, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|