From bf0961458cffc811cd37c9335ba14cb5f61aecd4 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sat, 4 Jan 2025 11:58:37 +0100 Subject: [PATCH] remove some unnecessary string conversions Signed-off-by: Nicola Murino --- internal/common/actions.go | 3 +-- internal/common/dataretention.go | 2 +- internal/common/eventmanager.go | 2 +- internal/dataprovider/actions.go | 2 +- internal/dataprovider/dataprovider.go | 8 ++++---- internal/logger/logger.go | 12 +----------- internal/sftpd/server.go | 2 +- internal/webdavd/file.go | 2 +- 8 files changed, 11 insertions(+), 22 deletions(-) diff --git a/internal/common/actions.go b/internal/common/actions.go index 1da74b8a..d6fd04b0 100644 --- a/internal/common/actions.go +++ b/internal/common/actions.go @@ -38,7 +38,6 @@ import ( "github.com/drakkan/sftpgo/v2/internal/httpclient" "github.com/drakkan/sftpgo/v2/internal/logger" "github.com/drakkan/sftpgo/v2/internal/plugin" - "github.com/drakkan/sftpgo/v2/internal/util" ) var ( @@ -349,7 +348,7 @@ func notificationAsEnvVars(event *notifier.FsEvent) []string { if len(event.Metadata) > 0 { data, err := json.Marshal(event.Metadata) if err == nil { - result = append(result, fmt.Sprintf("SFTPGO_ACTION_METADATA=%s", util.BytesToString(data))) + result = append(result, fmt.Sprintf("SFTPGO_ACTION_METADATA=%s", data)) } } return result diff --git a/internal/common/dataretention.go b/internal/common/dataretention.go index d97369c2..f99f7fdf 100644 --- a/internal/common/dataretention.go +++ b/internal/common/dataretention.go @@ -479,7 +479,7 @@ func (c *RetentionCheck) sendHookNotification(elapsed time.Duration, errCheck er cmd := exec.CommandContext(ctx, Config.DataRetentionHook, args...) cmd.Env = append(env, - fmt.Sprintf("SFTPGO_DATA_RETENTION_RESULT=%s", util.BytesToString(jsonData))) + fmt.Sprintf("SFTPGO_DATA_RETENTION_RESULT=%s", jsonData)) err := cmd.Run() c.conn.Log(logger.LevelDebug, "notified result using command: %q, elapsed: %s err: %v", diff --git a/internal/common/eventmanager.go b/internal/common/eventmanager.go index 4db788e2..8a650046 100644 --- a/internal/common/eventmanager.go +++ b/internal/common/eventmanager.go @@ -1489,7 +1489,7 @@ func executeHTTPRuleAction(c dataprovider.EventActionHTTPConfig, params *EventPa if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusNoContent { if rb, err := io.ReadAll(io.LimitReader(resp.Body, 2048)); err == nil { eventManagerLog(logger.LevelDebug, "error notification response from endpoint %q: %s", - endpoint, util.BytesToString(rb)) + endpoint, rb) } return fmt.Errorf("unexpected status code: %d", resp.StatusCode) } diff --git a/internal/dataprovider/actions.go b/internal/dataprovider/actions.go index b511ffb0..b61e5b0a 100644 --- a/internal/dataprovider/actions.go +++ b/internal/dataprovider/actions.go @@ -149,7 +149,7 @@ func executeNotificationCommand(operation, executor, ip, objectType, objectName, fmt.Sprintf("SFTPGO_PROVIDER_IP=%s", ip), fmt.Sprintf("SFTPGO_PROVIDER_ROLE=%s", role), fmt.Sprintf("SFTPGO_PROVIDER_TIMESTAMP=%d", util.GetTimeAsMsSinceEpoch(time.Now())), - fmt.Sprintf("SFTPGO_PROVIDER_OBJECT=%s", util.BytesToString(objectAsJSON))) + fmt.Sprintf("SFTPGO_PROVIDER_OBJECT=%s", objectAsJSON)) startTime := time.Now() err := cmd.Run() diff --git a/internal/dataprovider/dataprovider.go b/internal/dataprovider/dataprovider.go index 5b9b9555..4cd72bde 100644 --- a/internal/dataprovider/dataprovider.go +++ b/internal/dataprovider/dataprovider.go @@ -4174,7 +4174,7 @@ func getPreLoginHookResponse(loginMethod, ip, protocol string, userAsJSON []byte cmd := exec.CommandContext(ctx, config.PreLoginHook, args...) cmd.Env = append(env, - fmt.Sprintf("SFTPGO_LOGIND_USER=%s", util.BytesToString(userAsJSON)), + fmt.Sprintf("SFTPGO_LOGIND_USER=%s", userAsJSON), fmt.Sprintf("SFTPGO_LOGIND_METHOD=%s", loginMethod), fmt.Sprintf("SFTPGO_LOGIND_IP=%s", ip), fmt.Sprintf("SFTPGO_LOGIND_PROTOCOL=%s", protocol), @@ -4221,7 +4221,7 @@ func executePreLoginHook(username, loginMethod, ip, protocol string, oidcTokenFi recoveryCodes := u.Filters.RecoveryCodes err = json.Unmarshal(out, &u) if err != nil { - return u, fmt.Errorf("invalid pre-login hook response %q, error: %v", util.BytesToString(out), err) + return u, fmt.Errorf("invalid pre-login hook response %q, error: %v", out, err) } u.ID = userID u.UsedQuotaSize = userUsedQuotaSize @@ -4316,7 +4316,7 @@ func ExecutePostLoginHook(user *User, loginMethod, ip, protocol string, err erro cmd := exec.CommandContext(ctx, config.PostLoginHook, args...) cmd.Env = append(env, - fmt.Sprintf("SFTPGO_LOGIND_USER=%s", util.BytesToString(userAsJSON)), + fmt.Sprintf("SFTPGO_LOGIND_USER=%s", userAsJSON), fmt.Sprintf("SFTPGO_LOGIND_IP=%s", ip), fmt.Sprintf("SFTPGO_LOGIND_METHOD=%s", loginMethod), fmt.Sprintf("SFTPGO_LOGIND_STATUS=%s", status), @@ -4385,7 +4385,7 @@ func getExternalAuthResponse(username, password, pkey, keyboardInteractive, ip, cmd := exec.CommandContext(ctx, config.ExternalAuthHook, args...) cmd.Env = append(env, fmt.Sprintf("SFTPGO_AUTHD_USERNAME=%s", username), - fmt.Sprintf("SFTPGO_AUTHD_USER=%s", util.BytesToString(userAsJSON)), + fmt.Sprintf("SFTPGO_AUTHD_USER=%s", userAsJSON), fmt.Sprintf("SFTPGO_AUTHD_IP=%s", ip), fmt.Sprintf("SFTPGO_AUTHD_PASSWORD=%s", password), fmt.Sprintf("SFTPGO_AUTHD_PUBLIC_KEY=%s", pkey), diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 629236dd..f0c21d16 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -29,7 +29,6 @@ import ( "os" "path/filepath" "time" - "unsafe" ftpserverlog "github.com/fclairamb/go-log" "github.com/rs/zerolog" @@ -310,7 +309,7 @@ func (l *StdLoggerWrapper) Write(p []byte) (n int, err error) { p = p[0 : n-1] } - Log(LevelError, l.Sender, "", "%s", bytesToString(p)) + Log(LevelError, l.Sender, "", "%s", p) return } @@ -390,12 +389,3 @@ func (l *LeveledLogger) With(keysAndValues ...any) ftpserverlog.Logger { additionalKeyVals: append(l.additionalKeyVals, keysAndValues...), } } - -func bytesToString(b []byte) string { - // unsafe.SliceData relies on cap whereas we want to rely on len - if len(b) == 0 { - return "" - } - // https://github.com/golang/go/blob/4ed358b57efdad9ed710be7f4fc51495a7620ce2/src/strings/builder.go#L41 - return unsafe.String(unsafe.SliceData(b), len(b)) -} diff --git a/internal/sftpd/server.go b/internal/sftpd/server.go index e88454ea..61f798d7 100644 --- a/internal/sftpd/server.go +++ b/internal/sftpd/server.go @@ -648,7 +648,7 @@ func (c *Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.Serve switch req.Type { case "subsystem": - if util.BytesToString(req.Payload[4:]) == "sftp" { + if bytes.Equal(req.Payload[4:], []byte("sftp")) { ok = true connection := &Connection{ BaseConnection: common.NewBaseConnection(connID, common.ProtocolSFTP, conn.LocalAddr().String(), diff --git a/internal/webdavd/file.go b/internal/webdavd/file.go index 32820bf5..be08c554 100644 --- a/internal/webdavd/file.go +++ b/internal/webdavd/file.go @@ -451,7 +451,7 @@ func (f *webDavFile) Patch(patches []webdav.Proppatch) ([]webdav.Propstat, error parsed, err := parseTime(util.BytesToString(p.InnerXML)) if err != nil { f.Connection.Log(logger.LevelWarn, "unsupported last modification time: %q, err: %v", - util.BytesToString(p.InnerXML), err) + p.InnerXML, err) hasError = true continue }