Add mc cat --tail and --offset (#4149)
Allow range requests for cat operations, either specifying tail or an absolute offset. If tail is longer then fail, the entire file is listed.
This commit is contained in:
+75
-26
@@ -47,6 +47,14 @@ var catFlags = []cli.Flag{
|
||||
Name: "zip",
|
||||
Usage: "Extract from remote zip file (MinIO server source only)",
|
||||
},
|
||||
cli.Int64Flag{
|
||||
Name: "offset",
|
||||
Usage: "Start offset",
|
||||
},
|
||||
cli.Int64Flag{
|
||||
Name: "tail",
|
||||
Usage: "Tail number of bytes at ending of file",
|
||||
},
|
||||
}
|
||||
|
||||
// Display contents of a file.
|
||||
@@ -143,40 +151,69 @@ func (s prettyStdout) Write(input []byte) (int, error) {
|
||||
return inputLen, nil
|
||||
}
|
||||
|
||||
// parseCatSyntax performs command-line input validation for cat command.
|
||||
func parseCatSyntax(ctx *cli.Context) (args []string, versionID string, timeRef time.Time) {
|
||||
args = ctx.Args()
|
||||
type catOpts struct {
|
||||
args []string
|
||||
versionID string
|
||||
timeRef time.Time
|
||||
startO int64
|
||||
tailO int64
|
||||
isZip bool
|
||||
stdinMode bool
|
||||
}
|
||||
|
||||
versionID = ctx.String("version-id")
|
||||
// parseCatSyntax performs command-line input validation for cat command.
|
||||
func parseCatSyntax(ctx *cli.Context) catOpts {
|
||||
var o catOpts
|
||||
o.args = ctx.Args()
|
||||
|
||||
o.versionID = ctx.String("version-id")
|
||||
rewind := ctx.String("rewind")
|
||||
|
||||
if versionID != "" && rewind != "" {
|
||||
if o.versionID != "" && rewind != "" {
|
||||
fatalIf(errInvalidArgument().Trace(), "You cannot specify --version-id and --rewind at the same time")
|
||||
}
|
||||
|
||||
if versionID != "" && len(args) != 1 {
|
||||
if o.versionID != "" && len(o.args) != 1 {
|
||||
fatalIf(errInvalidArgument().Trace(), "You need to pass at least one argument if --version-id is specified")
|
||||
}
|
||||
|
||||
for _, arg := range args {
|
||||
for _, arg := range o.args {
|
||||
if strings.HasPrefix(arg, "-") && len(arg) > 1 {
|
||||
fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Unknown flag `%s` passed.", arg))
|
||||
}
|
||||
}
|
||||
|
||||
timeRef = parseRewindFlag(rewind)
|
||||
return
|
||||
o.stdinMode = len(o.args) == 0
|
||||
|
||||
o.timeRef = parseRewindFlag(rewind)
|
||||
o.isZip = ctx.Bool("zip")
|
||||
o.startO = ctx.Int64("offset")
|
||||
o.tailO = ctx.Int64("tail")
|
||||
if o.tailO != 0 && o.startO != 0 {
|
||||
fatalIf(errInvalidArgument().Trace(), "You cannot specify both --tail and --offset")
|
||||
}
|
||||
if o.tailO < 0 || o.startO < 0 {
|
||||
fatalIf(errInvalidArgument().Trace(), "You cannot specify negative --tail or --offset")
|
||||
}
|
||||
if o.isZip && (o.tailO != 0 || o.startO != 0) {
|
||||
fatalIf(errInvalidArgument().Trace(), "You cannot combine --zip with --tail or --offset")
|
||||
}
|
||||
if o.stdinMode && (o.isZip || o.startO != 0 || o.tailO != 0) {
|
||||
fatalIf(errInvalidArgument().Trace(), "You cannot use --zip --tail or --offset with stdin")
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
// catURL displays contents of a URL to stdout.
|
||||
func catURL(ctx context.Context, sourceURL, sourceVersion string, timeRef time.Time, encKeyDB map[string][]prefixSSEPair, isZip bool) *probe.Error {
|
||||
func catURL(ctx context.Context, sourceURL string, encKeyDB map[string][]prefixSSEPair, o catOpts) *probe.Error {
|
||||
var reader io.ReadCloser
|
||||
size := int64(-1)
|
||||
switch sourceURL {
|
||||
case "-":
|
||||
reader = os.Stdin
|
||||
default:
|
||||
versionID := sourceVersion
|
||||
versionID := o.versionID
|
||||
var err *probe.Error
|
||||
// Try to stat the object, the purpose is to:
|
||||
// 1. extract the size of S3 object so we can check if the size of the
|
||||
@@ -184,17 +221,34 @@ func catURL(ctx context.Context, sourceURL, sourceVersion string, timeRef time.T
|
||||
// are ignored since some of them have zero size though they
|
||||
// have contents like files under /proc.
|
||||
// 2. extract the version ID if rewind flag is passed
|
||||
if client, content, err := url2Stat(ctx, sourceURL, sourceVersion, false, encKeyDB, timeRef, isZip); err == nil {
|
||||
if sourceVersion == "" {
|
||||
if client, content, err := url2Stat(ctx, sourceURL, o.versionID, false, encKeyDB, o.timeRef, o.isZip); err == nil {
|
||||
if o.versionID == "" {
|
||||
versionID = content.VersionID
|
||||
}
|
||||
if o.tailO > 0 && content.Size > 0 {
|
||||
o.startO = content.Size - o.tailO
|
||||
if o.startO < 0 {
|
||||
// Return all.
|
||||
o.startO = 0
|
||||
}
|
||||
}
|
||||
|
||||
if client.GetURL().Type == objectStorage {
|
||||
size = content.Size
|
||||
size = content.Size - o.startO
|
||||
if size < 0 {
|
||||
err := probe.NewError(fmt.Errorf("specified offset (%d) bigger than file (%d)", o.startO, content.Size))
|
||||
return err.Trace(sourceURL)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return err.Trace(sourceURL)
|
||||
}
|
||||
if reader, err = getSourceStreamFromURL(ctx, sourceURL, versionID, encKeyDB, isZip); err != nil {
|
||||
gopts := GetOptions{VersionID: versionID, Zip: o.isZip, RangeStart: o.startO}
|
||||
if reader, err = getSourceStreamFromURL(ctx, sourceURL, encKeyDB, getSourceOpts{
|
||||
GetOptions: gopts,
|
||||
fetchStat: false,
|
||||
preserve: false,
|
||||
}); err != nil {
|
||||
return err.Trace(sourceURL)
|
||||
}
|
||||
defer reader.Close()
|
||||
@@ -257,35 +311,30 @@ func mainCat(cliCtx *cli.Context) error {
|
||||
fatalIf(err, "Unable to parse encryption keys.")
|
||||
|
||||
// check 'cat' cli arguments.
|
||||
args, versionID, rewind := parseCatSyntax(cliCtx)
|
||||
o := parseCatSyntax(cliCtx)
|
||||
|
||||
// Set command flags from context.
|
||||
stdinMode := false
|
||||
if len(args) == 0 {
|
||||
stdinMode = true
|
||||
}
|
||||
isZip := cliCtx.Bool("zip")
|
||||
|
||||
// handle std input data.
|
||||
if stdinMode {
|
||||
if o.stdinMode {
|
||||
fatalIf(catOut(os.Stdin, -1).Trace(), "Unable to read from standard input.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// if Args contain `-`, we need to preserve its order specially.
|
||||
if len(args) > 0 && args[0] == "-" {
|
||||
if len(o.args) > 0 && o.args[0] == "-" {
|
||||
for i, arg := range os.Args {
|
||||
if arg == "cat" {
|
||||
// Overwrite cliCtx.Args with os.Args.
|
||||
args = os.Args[i+1:]
|
||||
o.args = os.Args[i+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert arguments to URLs: expand alias, fix format.
|
||||
for _, url := range args {
|
||||
fatalIf(catURL(ctx, url, versionID, rewind, encKeyDB, isZip).Trace(url), "Unable to read from `"+url+"`.")
|
||||
for _, url := range o.args {
|
||||
fatalIf(catURL(ctx, url, encKeyDB, o).Trace(url), "Unable to read from `"+url+"`.")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -435,6 +435,14 @@ func (f *fsClient) Get(ctx context.Context, opts GetOptions) (io.ReadCloser, *pr
|
||||
err := f.toClientError(e, f.PathURL.Path)
|
||||
return nil, err.Trace(f.PathURL.Path)
|
||||
}
|
||||
if opts.RangeStart != 0 {
|
||||
_, e := fileData.Seek(opts.RangeStart, os.SEEK_SET)
|
||||
if e != nil {
|
||||
err := f.toClientError(e, f.PathURL.Path)
|
||||
return nil, err.Trace(f.PathURL.Path)
|
||||
}
|
||||
}
|
||||
|
||||
return fileData, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -829,6 +829,12 @@ func (c *S3Client) Get(ctx context.Context, opts GetOptions) (io.ReadCloser, *pr
|
||||
if opts.Zip {
|
||||
o.Set("x-minio-extract", "true")
|
||||
}
|
||||
if opts.RangeStart != 0 {
|
||||
err := o.SetRange(opts.RangeStart, 0)
|
||||
if err != nil {
|
||||
return nil, probe.NewError(err)
|
||||
}
|
||||
}
|
||||
|
||||
reader, e := c.api.GetObject(ctx, bucket, object, o)
|
||||
if e != nil {
|
||||
|
||||
+4
-3
@@ -45,9 +45,10 @@ const (
|
||||
|
||||
// GetOptions holds options of the GET operation
|
||||
type GetOptions struct {
|
||||
SSE encrypt.ServerSide
|
||||
VersionID string
|
||||
Zip bool
|
||||
SSE encrypt.ServerSide
|
||||
VersionID string
|
||||
Zip bool
|
||||
RangeStart int64
|
||||
}
|
||||
|
||||
// PutOptions holds options for PUT operation
|
||||
|
||||
+29
-10
@@ -159,18 +159,29 @@ func getSourceStreamMetadataFromURL(ctx context.Context, aliasedURL, versionID s
|
||||
}
|
||||
versionID = content.VersionID
|
||||
}
|
||||
sseKey := getSSE(aliasedURL, encKeyDB[alias])
|
||||
return getSourceStream(ctx, alias, urlStrFull, versionID, true, sseKey, false, false)
|
||||
return getSourceStream(ctx, alias, urlStrFull, getSourceOpts{
|
||||
GetOptions: GetOptions{
|
||||
SSE: getSSE(aliasedURL, encKeyDB[alias]),
|
||||
VersionID: versionID,
|
||||
Zip: false,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type getSourceOpts struct {
|
||||
GetOptions
|
||||
fetchStat bool
|
||||
preserve bool
|
||||
}
|
||||
|
||||
// getSourceStreamFromURL gets a reader from URL.
|
||||
func getSourceStreamFromURL(ctx context.Context, urlStr, versionID string, encKeyDB map[string][]prefixSSEPair, isZip bool) (reader io.ReadCloser, err *probe.Error) {
|
||||
func getSourceStreamFromURL(ctx context.Context, urlStr string, encKeyDB map[string][]prefixSSEPair, opts getSourceOpts) (reader io.ReadCloser, err *probe.Error) {
|
||||
alias, urlStrFull, _, err := expandAlias(urlStr)
|
||||
if err != nil {
|
||||
return nil, err.Trace(urlStr)
|
||||
}
|
||||
sse := getSSE(urlStr, encKeyDB[alias])
|
||||
reader, _, err = getSourceStream(ctx, alias, urlStrFull, versionID, false, sse, false, isZip)
|
||||
opts.SSE = getSSE(urlStr, encKeyDB[alias])
|
||||
reader, _, err = getSourceStream(ctx, alias, urlStrFull, opts)
|
||||
return reader, err
|
||||
}
|
||||
|
||||
@@ -222,18 +233,18 @@ func isReadAt(reader io.Reader) (ok bool) {
|
||||
}
|
||||
|
||||
// getSourceStream gets a reader from URL.
|
||||
func getSourceStream(ctx context.Context, alias, urlStr, versionID string, fetchStat bool, sse encrypt.ServerSide, preserve, isZip bool) (reader io.ReadCloser, metadata map[string]string, err *probe.Error) {
|
||||
func getSourceStream(ctx context.Context, alias, urlStr string, opts getSourceOpts) (reader io.ReadCloser, metadata map[string]string, err *probe.Error) {
|
||||
sourceClnt, err := newClientFromAlias(alias, urlStr)
|
||||
if err != nil {
|
||||
return nil, nil, err.Trace(alias, urlStr)
|
||||
}
|
||||
reader, err = sourceClnt.Get(ctx, GetOptions{SSE: sse, VersionID: versionID, Zip: isZip})
|
||||
reader, err = sourceClnt.Get(ctx, opts.GetOptions)
|
||||
if err != nil {
|
||||
return nil, nil, err.Trace(alias, urlStr)
|
||||
}
|
||||
|
||||
metadata = make(map[string]string)
|
||||
if fetchStat {
|
||||
if opts.fetchStat {
|
||||
var st *ClientContent
|
||||
mo, mok := reader.(*minio.Object)
|
||||
if mok {
|
||||
@@ -253,7 +264,7 @@ func getSourceStream(ctx context.Context, alias, urlStr, versionID string, fetch
|
||||
}
|
||||
st.ETag = oinfo.ETag
|
||||
} else {
|
||||
st, err = sourceClnt.Stat(ctx, StatOptions{preserve: preserve, sse: sse})
|
||||
st, err = sourceClnt.Stat(ctx, StatOptions{preserve: opts.preserve, sse: opts.SSE})
|
||||
if err != nil {
|
||||
return nil, nil, err.Trace(alias, urlStr)
|
||||
}
|
||||
@@ -539,7 +550,15 @@ func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader,
|
||||
|
||||
var reader io.ReadCloser
|
||||
// Proceed with regular stream copy.
|
||||
reader, metadata, err = getSourceStream(ctx, sourceAlias, sourceURL.String(), sourceVersion, true, srcSSE, preserve, isZip)
|
||||
reader, metadata, err = getSourceStream(ctx, sourceAlias, sourceURL.String(), getSourceOpts{
|
||||
GetOptions: GetOptions{
|
||||
VersionID: sourceVersion,
|
||||
SSE: srcSSE,
|
||||
Zip: isZip,
|
||||
},
|
||||
fetchStat: true,
|
||||
preserve: preserve,
|
||||
})
|
||||
if err != nil {
|
||||
return urls.WithError(err.Trace(sourceURL.String()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user