From 3e6dc02f8fb114ba06ef02f00ccb171a47744ff9 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Thu, 20 Jun 2024 07:46:44 -0700 Subject: [PATCH] Add actual inline data to JSON output in xl-meta (#19958) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the inlined data as base64 encoded field and try to add a string version if feasible. Example: ``` λ xl-meta -data xl.meta { "8e03504e-1123-4957-b272-7bc53eda0d55": { "bitrot_valid": true, "bytes": 58, "data_base64": "Z29sYW5nLm9yZy94L3N5cyB2MC4xNS4wIC8=", "data_string": "golang.org/x/sys v0.15.0 /" } ``` The string will have quotes, newlines escaped to produce valid JSON. If content isn't valid utf8 or the encoding otherwise fails, only the base64 data will be added. `-export` can still be used separately to extract the data as files (including bitrot). --- docs/debugging/xl-meta/main.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/debugging/xl-meta/main.go b/docs/debugging/xl-meta/main.go index c97a42720..12cfd5ef1 100644 --- a/docs/debugging/xl-meta/main.go +++ b/docs/debugging/xl-meta/main.go @@ -20,6 +20,7 @@ package main import ( "bytes" "crypto/md5" + "encoding/base64" "encoding/binary" "encoding/hex" "encoding/json" @@ -34,6 +35,7 @@ import ( "strconv" "strings" "time" + "unicode/utf8" "github.com/google/uuid" "github.com/klauspost/compress/zip" @@ -237,7 +239,7 @@ FLAGS: } if c.Bool("data") { - b, err := data.json() + b, err := data.json(true) if err != nil { return nil, err } @@ -562,7 +564,7 @@ func (x xlMetaInlineData) versionOK() bool { return x[0] > 0 && x[0] <= xlMetaInlineDataVer } -func (x xlMetaInlineData) json() ([]byte, error) { +func (x xlMetaInlineData) json(value bool) ([]byte, error) { if len(x) == 0 { return []byte("{}"), nil } @@ -607,6 +609,17 @@ func (x xlMetaInlineData) json() ([]byte, error) { } else { s += ", \"bitrot_valid\": false" } + if value { + if utf8.Valid(data) { + // Encode as JSON string. + b, err := json.Marshal(string(data)) + if err == nil { + s += `, "data_string": ` + string(b) + } + } + // Base64 encode. + s += `, "data_base64": "` + base64.StdEncoding.EncodeToString(data) + `"` + } s += "}" } res = append(res, []byte(s)...)