2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-06-22 20:27:54 -04:00
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-22 16:20:22 -04:00
|
|
|
"bytes"
|
2021-03-29 20:00:55 -04:00
|
|
|
"encoding/binary"
|
2020-06-22 16:20:22 -04:00
|
|
|
"encoding/json"
|
2021-03-29 20:00:55 -04:00
|
|
|
"errors"
|
2020-06-22 16:20:22 -04:00
|
|
|
"fmt"
|
2020-06-12 23:04:01 -04:00
|
|
|
"io"
|
2021-03-29 20:00:55 -04:00
|
|
|
"io/ioutil"
|
2020-06-22 16:20:22 -04:00
|
|
|
"log"
|
2020-06-12 23:04:01 -04:00
|
|
|
"os"
|
2021-05-28 11:10:41 -04:00
|
|
|
"path/filepath"
|
2020-06-12 23:04:01 -04:00
|
|
|
|
|
|
|
"github.com/minio/cli"
|
|
|
|
"github.com/tinylib/msgp/msgp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Copyright = "MinIO, Inc."
|
|
|
|
app.Usage = "xl.meta to JSON"
|
2020-06-22 20:27:54 -04:00
|
|
|
app.HideVersion = true
|
|
|
|
app.CustomAppHelpTemplate = `NAME:
|
|
|
|
{{.Name}} - {{.Usage}}
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
{{.Name}} {{if .VisibleFlags}}[FLAGS]{{end}} METAFILES...
|
2021-05-28 11:10:41 -04:00
|
|
|
|
|
|
|
Multiple files can be added.
|
|
|
|
Wildcards are accepted: testdir/*.txt will compress all files in testdir ending with .txt
|
|
|
|
Directories can be wildcards as well. testdir/*/*.txt will match testdir/subdir/b.txt
|
|
|
|
|
2020-06-22 20:27:54 -04:00
|
|
|
{{if .VisibleFlags}}
|
|
|
|
GLOBAL FLAGS:
|
|
|
|
{{range .VisibleFlags}}{{.}}
|
|
|
|
{{end}}{{end}}
|
|
|
|
`
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
app.HideHelpCommand = true
|
|
|
|
|
|
|
|
app.Flags = []cli.Flag{
|
2020-06-22 16:20:22 -04:00
|
|
|
cli.BoolFlag{
|
|
|
|
Usage: "Print each file as a separate line without formatting",
|
|
|
|
Name: "ndjson",
|
2020-06-12 23:04:01 -04:00
|
|
|
},
|
2021-03-29 20:00:55 -04:00
|
|
|
cli.BoolFlag{
|
|
|
|
Usage: "Display inline data keys and sizes",
|
|
|
|
Name: "data",
|
|
|
|
},
|
2021-05-28 11:10:41 -04:00
|
|
|
cli.BoolFlag{
|
|
|
|
Usage: "Export inline data",
|
|
|
|
Name: "export",
|
|
|
|
},
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
app.Action = func(c *cli.Context) error {
|
2021-05-28 11:10:41 -04:00
|
|
|
args := c.Args()
|
|
|
|
if len(args) == 0 {
|
2021-01-05 23:08:35 -05:00
|
|
|
// If no args, assume xl.meta
|
2021-05-28 11:10:41 -04:00
|
|
|
args = []string{"xl.meta"}
|
|
|
|
}
|
|
|
|
var files []string
|
|
|
|
for _, pattern := range args {
|
|
|
|
found, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(found) == 0 {
|
|
|
|
return fmt.Errorf("unable to find file %v", pattern)
|
|
|
|
}
|
|
|
|
files = append(files, found...)
|
2020-06-22 20:27:54 -04:00
|
|
|
}
|
2021-01-05 23:08:35 -05:00
|
|
|
for _, file := range files {
|
2020-06-22 16:20:22 -04:00
|
|
|
var r io.Reader
|
|
|
|
switch file {
|
|
|
|
case "-":
|
|
|
|
r = os.Stdin
|
|
|
|
default:
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
r = f
|
|
|
|
}
|
|
|
|
|
2021-03-29 20:00:55 -04:00
|
|
|
b, err := ioutil.ReadAll(r)
|
2020-06-22 16:20:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-29 20:00:55 -04:00
|
|
|
b, _, minor, err := checkXL2V1(b)
|
2020-06-22 16:20:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-29 20:00:55 -04:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
var data xlMetaInlineData
|
|
|
|
switch minor {
|
|
|
|
case 0:
|
|
|
|
_, err = msgp.CopyToJSON(buf, bytes.NewBuffer(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-09 20:38:23 -04:00
|
|
|
case 1, 2:
|
2021-03-29 20:00:55 -04:00
|
|
|
v, b, err := msgp.ReadBytesZC(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-09 20:38:23 -04:00
|
|
|
if _, nbuf, err := msgp.ReadUint32Bytes(b); err == nil {
|
|
|
|
// Read metadata CRC (added in v2, ignore if not found)
|
|
|
|
b = nbuf
|
|
|
|
}
|
|
|
|
|
2021-03-29 20:00:55 -04:00
|
|
|
_, err = msgp.CopyToJSON(buf, bytes.NewBuffer(v))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data = b
|
|
|
|
default:
|
2021-08-30 11:38:11 -04:00
|
|
|
return fmt.Errorf("unknown metadata version %d", minor)
|
2021-03-29 20:00:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Bool("data") {
|
|
|
|
b, err := data.json()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf = bytes.NewBuffer(b)
|
2020-06-22 16:20:22 -04:00
|
|
|
}
|
2021-05-28 11:10:41 -04:00
|
|
|
if c.Bool("export") {
|
|
|
|
err := data.files(func(name string, data []byte) {
|
|
|
|
ioutil.WriteFile(fmt.Sprintf("%s-%s.data", file, name), data, os.ModePerm)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 16:20:22 -04:00
|
|
|
if c.Bool("ndjson") {
|
|
|
|
fmt.Println(buf.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var msi map[string]interface{}
|
2021-03-29 20:00:55 -04:00
|
|
|
dec := json.NewDecoder(buf)
|
2020-06-22 16:20:22 -04:00
|
|
|
// Use number to preserve integers.
|
|
|
|
dec.UseNumber()
|
|
|
|
err = dec.Decode(&msi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-29 20:00:55 -04:00
|
|
|
b, err = json.MarshalIndent(msi, "", " ")
|
2020-06-22 16:20:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(b))
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
2020-06-22 16:20:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
}
|
2021-03-29 20:00:55 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
// XL header specifies the format
|
|
|
|
xlHeader = [4]byte{'X', 'L', '2', ' '}
|
|
|
|
|
|
|
|
// Current version being written.
|
|
|
|
xlVersionCurrent [4]byte
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Breaking changes.
|
|
|
|
// Newer versions cannot be read by older software.
|
|
|
|
// This will prevent downgrades to incompatible versions.
|
|
|
|
xlVersionMajor = 1
|
|
|
|
|
|
|
|
// Non breaking changes.
|
|
|
|
// Bumping this is informational, but should be done
|
|
|
|
// if any change is made to the data stored, bumping this
|
|
|
|
// will allow to detect the exact version later.
|
|
|
|
xlVersionMinor = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
binary.LittleEndian.PutUint16(xlVersionCurrent[0:2], xlVersionMajor)
|
|
|
|
binary.LittleEndian.PutUint16(xlVersionCurrent[2:4], xlVersionMinor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkXL2V1 will check if the metadata has correct header and is a known major version.
|
|
|
|
// The remaining payload and versions are returned.
|
|
|
|
func checkXL2V1(buf []byte) (payload []byte, major, minor uint16, err error) {
|
|
|
|
if len(buf) <= 8 {
|
|
|
|
return payload, 0, 0, fmt.Errorf("xlMeta: no data")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf[:4], xlHeader[:]) {
|
|
|
|
return payload, 0, 0, fmt.Errorf("xlMeta: unknown XLv2 header, expected %v, got %v", xlHeader[:4], buf[:4])
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(buf[4:8], []byte("1 ")) {
|
|
|
|
// Set as 1,0.
|
|
|
|
major, minor = 1, 0
|
|
|
|
} else {
|
|
|
|
major, minor = binary.LittleEndian.Uint16(buf[4:6]), binary.LittleEndian.Uint16(buf[6:8])
|
|
|
|
}
|
|
|
|
if major > xlVersionMajor {
|
|
|
|
return buf[8:], major, minor, fmt.Errorf("xlMeta: unknown major version %d found", major)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf[8:], major, minor, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const xlMetaInlineDataVer = 1
|
|
|
|
|
|
|
|
type xlMetaInlineData []byte
|
|
|
|
|
|
|
|
// afterVersion returns the payload after the version, if any.
|
|
|
|
func (x xlMetaInlineData) afterVersion() []byte {
|
|
|
|
if len(x) == 0 {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return x[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// versionOK returns whether the version is ok.
|
|
|
|
func (x xlMetaInlineData) versionOK() bool {
|
|
|
|
if len(x) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return x[0] > 0 && x[0] <= xlMetaInlineDataVer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x xlMetaInlineData) json() ([]byte, error) {
|
|
|
|
if len(x) == 0 {
|
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
|
|
|
if !x.versionOK() {
|
|
|
|
return nil, errors.New("xlMetaInlineData: unknown version")
|
|
|
|
}
|
|
|
|
|
|
|
|
sz, buf, err := msgp.ReadMapHeaderBytes(x.afterVersion())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := []byte("{")
|
|
|
|
|
|
|
|
for i := uint32(0); i < sz; i++ {
|
|
|
|
var key, val []byte
|
|
|
|
key, buf, err = msgp.ReadMapKeyZC(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(key) == 0 {
|
|
|
|
return nil, fmt.Errorf("xlMetaInlineData: key %d is length 0", i)
|
|
|
|
}
|
|
|
|
// Skip data...
|
|
|
|
val, buf, err = msgp.ReadBytesZC(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
res = append(res, ',')
|
|
|
|
}
|
|
|
|
s := fmt.Sprintf(`"%s":%d`, string(key), len(val))
|
|
|
|
res = append(res, []byte(s)...)
|
|
|
|
}
|
|
|
|
res = append(res, '}')
|
|
|
|
return res, nil
|
|
|
|
}
|
2021-05-28 11:10:41 -04:00
|
|
|
|
|
|
|
// files returns files as callback.
|
|
|
|
func (x xlMetaInlineData) files(fn func(name string, data []byte)) error {
|
|
|
|
if len(x) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !x.versionOK() {
|
|
|
|
return errors.New("xlMetaInlineData: unknown version")
|
|
|
|
}
|
|
|
|
|
|
|
|
sz, buf, err := msgp.ReadMapHeaderBytes(x.afterVersion())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(0); i < sz; i++ {
|
|
|
|
var key, val []byte
|
|
|
|
key, buf, err = msgp.ReadMapKeyZC(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(key) == 0 {
|
|
|
|
return fmt.Errorf("xlMetaInlineData: key %d is length 0", i)
|
|
|
|
}
|
|
|
|
// Read data...
|
|
|
|
val, buf, err = msgp.ReadBytesZC(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Call back.
|
|
|
|
fn(string(key), val)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|