From cd0ea1cb567dca81baeeaf8054eb86545cbdc7c0 Mon Sep 17 00:00:00 2001 From: Gianmaria Del Monte <39946305+gmgigi96@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:36:45 +0100 Subject: [PATCH] Added listversions command (#4301) * Add the listversions command in the command line * Rework the response body close() * add changelog --------- Co-authored-by: Fabrizio Furano --- changelog/unreleased/list-versions.md | 3 ++ cmd/reva/listversions.go | 67 +++++++++++++++++++++++++++ cmd/reva/main.go | 1 + pkg/eosclient/eosgrpc/eoshttp.go | 14 ++++-- 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 changelog/unreleased/list-versions.md create mode 100644 cmd/reva/listversions.go diff --git a/changelog/unreleased/list-versions.md b/changelog/unreleased/list-versions.md new file mode 100644 index 0000000000..b1235c0d70 --- /dev/null +++ b/changelog/unreleased/list-versions.md @@ -0,0 +1,3 @@ +Enhancement: Added listversions command + +https://github.com/cs3org/reva/pull/4301 \ No newline at end of file diff --git a/cmd/reva/listversions.go b/cmd/reva/listversions.go new file mode 100644 index 0000000000..9c14fea1a8 --- /dev/null +++ b/cmd/reva/listversions.go @@ -0,0 +1,67 @@ +// Copyright 2018-2023 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package main + +import ( + "fmt" + "io" + + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" + "github.com/pkg/errors" +) + +func listVersionsCommand() *command { + cmd := newCommand("listversions") + cmd.Description = func() string { return "list the versions of a file" } + cmd.Usage = func() string { return "Usage: listversions " } + + cmd.Action = func(w ...io.Writer) error { + if cmd.NArg() < 1 { + return errors.New("Invalid arguments: " + cmd.Usage()) + } + + fn := cmd.Args()[0] + client, err := getClient() + if err != nil { + return err + } + + ref := &provider.Reference{Path: fn} + req := &provider.ListFileVersionsRequest{Ref: ref} + + ctx := getAuthContext() + res, err := client.ListFileVersions(ctx, req) + if err != nil { + return err + } + + if res.Status.Code != rpc.Code_CODE_OK { + return formatError(res.Status) + } + + vers := res.Versions + for _, info := range vers { + fmt.Printf("Key: %s Size: %d mtime:%d\n", info.Key, info.Size, info.Mtime) + } + + return nil + } + return cmd +} diff --git a/cmd/reva/main.go b/cmd/reva/main.go index 9b1c599959..15e90b5b8f 100644 --- a/cmd/reva/main.go +++ b/cmd/reva/main.go @@ -49,6 +49,7 @@ var ( loginCommand(), whoamiCommand(), lsCommand(), + listVersionsCommand(), statCommand(), uploadCommand(), downloadCommand(), diff --git a/pkg/eosclient/eosgrpc/eoshttp.go b/pkg/eosclient/eosgrpc/eoshttp.go index 72be501c1c..93e85aa56d 100644 --- a/pkg/eosclient/eosgrpc/eoshttp.go +++ b/pkg/eosclient/eosgrpc/eoshttp.go @@ -390,9 +390,6 @@ func (c *EOSHTTPClient) PUTFile(ctx context.Context, remoteuser string, auth eos log.Debug().Str("func", "PUTFile").Msg("sending req") resp, err := c.doReq(req, remoteuser) - if resp != nil { - resp.Body.Close() - } // Let's support redirections... and if we retry we retry at the same FST if resp != nil && resp.StatusCode == 307 { @@ -424,6 +421,9 @@ func (c *EOSHTTPClient) PUTFile(ctx context.Context, remoteuser string, auth eos log.Debug().Str("func", "PUTFile").Str("location", loc.String()).Msg("redirection") nredirs++ + if (resp != nil) && (resp.Body != nil) { + resp.Body.Close() + } resp = nil err = nil continue @@ -432,12 +432,17 @@ func (c *EOSHTTPClient) PUTFile(ctx context.Context, remoteuser string, auth eos // And get an error code (if error) that is worth propagating e := c.getRespError(resp, err) if e != nil { + if (resp != nil) && (resp.Body != nil) { + resp.Body.Close() + } if os.IsTimeout(e) { ntries++ log.Warn().Str("func", "PUTFile").Str("url", finalurl).Str("err", e.Error()).Int("try", ntries).Msg("recoverable network timeout") continue } + log.Error().Str("func", "PUTFile").Str("url", finalurl).Str("err", e.Error()).Msg("") + return e } @@ -446,6 +451,9 @@ func (c *EOSHTTPClient) PUTFile(ctx context.Context, remoteuser string, auth eos return errtypes.NotFound(fmt.Sprintf("url: %s", finalurl)) } + if (resp != nil) && (resp.Body != nil) { + resp.Body.Close() + } return nil } }