Skip to content

Commit

Permalink
allow cat to take part by part numbers (#5026)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana authored Aug 26, 2024
1 parent efa1b39 commit 2e10255
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cmd/cat-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ var catFlags = []cli.Flag{
Name: "tail",
Usage: "tail number of bytes at ending of file",
},
cli.IntFlag{
Name: "part-number",
Usage: "download only a specific part number",
},
}

// Display contents of a file.
Expand Down Expand Up @@ -162,6 +166,7 @@ type catOpts struct {
timeRef time.Time
startO int64
tailO int64
partN int
isZip bool
stdinMode bool
}
Expand Down Expand Up @@ -197,6 +202,7 @@ func parseCatSyntax(ctx *cli.Context) catOpts {
o.isZip = ctx.Bool("zip")
o.startO = ctx.Int64("offset")
o.tailO = ctx.Int64("tail")
o.partN = ctx.Int("part-number")
if o.tailO != 0 && o.startO != 0 {
fatalIf(errInvalidArgument().Trace(), "You cannot specify both --tail and --offset")
}
Expand All @@ -209,6 +215,9 @@ func parseCatSyntax(ctx *cli.Context) catOpts {
if o.stdinMode && (o.isZip || o.startO != 0 || o.tailO != 0) {
fatalIf(errInvalidArgument().Trace(), "You cannot use --zip --tail or --offset with stdin")
}
if (o.tailO != 0 || o.startO != 0) && o.partN > 0 {
fatalIf(errInvalidArgument().Trace(), "You cannot use --part-number with --tail or --offset")
}

return o
}
Expand Down Expand Up @@ -248,18 +257,20 @@ func catURL(ctx context.Context, sourceURL string, encKeyDB map[string][]prefixS
o.startO = 0
}
}

if client.GetURL().Type == objectStorage {
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)
}
}
if o.partN != 0 {
size = int64(-1)
}
} else {
return err.Trace(sourceURL)
}
gopts := GetOptions{VersionID: versionID, Zip: o.isZip, RangeStart: o.startO}
gopts := GetOptions{VersionID: versionID, Zip: o.isZip, RangeStart: o.startO, PartNumber: o.partN}
if reader, err = getSourceStreamFromURL(ctx, sourceURL, encKeyDB, getSourceOpts{
GetOptions: gopts,
preserve: false,
Expand Down
1 change: 1 addition & 0 deletions cmd/client-s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ func (c *S3Client) Get(ctx context.Context, opts GetOptions) (io.ReadCloser, *Cl
o := minio.GetObjectOptions{
ServerSideEncryption: opts.SSE,
VersionID: opts.VersionID,
PartNumber: opts.PartNumber,
}
if opts.Zip {
o.Set("x-minio-extract", "true")
Expand Down
1 change: 1 addition & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type GetOptions struct {
VersionID string
Zip bool
RangeStart int64
PartNumber int
Preserve bool
}

Expand Down

0 comments on commit 2e10255

Please sign in to comment.