remove some unnecessary string conversions

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2025-01-04 11:58:37 +01:00
parent a4a33d4407
commit bf0961458c
8 changed files with 11 additions and 22 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -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)
}

View File

@@ -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()

View File

@@ -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),

View File

@@ -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))
}

View File

@@ -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(),

View File

@@ -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
}