From 1951cd7fd4225c66ea5120ed48aad19ec62fc9e2 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Mon, 6 Jun 2022 18:01:32 -0400 Subject: [PATCH 1/2] Add Timeout to SmbClientGetter Allow caller to configure a command execution context with a default timeout for all smbclient CLI operations. --- get_git.go | 2 +- get_smbclient.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/get_git.go b/get_git.go index b8afecc94..6f0c2a7a4 100644 --- a/get_git.go +++ b/get_git.go @@ -26,7 +26,7 @@ import ( type GitGetter struct { Detectors []Detector - // Timeout sets a deadline which all hg CLI operations should + // Timeout sets a deadline which all git CLI operations should // complete within. Defaults to zero which means no timeout. Timeout time.Duration } diff --git a/get_smbclient.go b/get_smbclient.go index 802cb9f2d..4946370a2 100644 --- a/get_smbclient.go +++ b/get_smbclient.go @@ -11,11 +11,17 @@ import ( "regexp" "strings" "syscall" + "time" ) // SmbClientGetter is a Getter implementation that will download a module from // a shared folder using smbclient cli. -type SmbClientGetter struct{} +type SmbClientGetter struct { + + // Timeout sets a deadline which all smb client CLI operations should + // complete within. Defaults to zero which means no timeout. + Timeout time.Duration +} func (g *SmbClientGetter) Mode(ctx context.Context, u *url.URL) (Mode, error) { if u.Host == "" || u.Path == "" { @@ -254,7 +260,15 @@ func (g *SmbClientGetter) isDirectory(args []string, object string) (bool, error } func (g *SmbClientGetter) runSmbClientCommand(dst string, args []string) (string, error) { - cmd := exec.Command("smbclient", args...) + ctx := context.Background() + + if g.Timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(context.Background(), g.Timeout) + defer cancel() + } + + cmd := exec.CommandContext(ctx, "smbclient", args...) if dst != "" { cmd.Dir = dst From d430c9ace1488516c4dae163bd4e5b8a653cf6c2 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Wed, 6 Jul 2022 16:14:57 -0400 Subject: [PATCH 2/2] Make smbclient timeout configurable --- get_smbclient.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/get_smbclient.go b/get_smbclient.go index 4946370a2..7d89fe573 100644 --- a/get_smbclient.go +++ b/get_smbclient.go @@ -9,18 +9,18 @@ import ( "os/exec" "path/filepath" "regexp" + "strconv" "strings" "syscall" - "time" ) // SmbClientGetter is a Getter implementation that will download a module from // a shared folder using smbclient cli. type SmbClientGetter struct { - // Timeout sets a deadline which all smb client CLI operations should - // complete within. Defaults to zero which means no timeout. - Timeout time.Duration + // Timeout in seconds sets a deadline which all smb client CLI operations should + // complete within. Defaults to zero which means to use the default client timeout of 20 seconds. + Timeout int } func (g *SmbClientGetter) Mode(ctx context.Context, u *url.URL) (Mode, error) { @@ -222,6 +222,10 @@ func (g *SmbClientGetter) smbclientCmdArgs(used *url.Userinfo, hostPath string, baseCmd = append(baseCmd, hostPath) baseCmd = append(baseCmd, "--directory") baseCmd = append(baseCmd, fileDir) + if g.Timeout > 0 { + baseCmd = append(baseCmd, "-t") + baseCmd = append(baseCmd, strconv.Itoa(g.Timeout)) + } return baseCmd } @@ -261,13 +265,6 @@ func (g *SmbClientGetter) isDirectory(args []string, object string) (bool, error func (g *SmbClientGetter) runSmbClientCommand(dst string, args []string) (string, error) { ctx := context.Background() - - if g.Timeout > 0 { - var cancel context.CancelFunc - ctx, cancel = context.WithTimeout(context.Background(), g.Timeout) - defer cancel() - } - cmd := exec.CommandContext(ctx, "smbclient", args...) if dst != "" {