Skip to content

Commit bf09614

Browse files
committed
remove some unnecessary string conversions
Signed-off-by: Nicola Murino <[email protected]>
1 parent a4a33d4 commit bf09614

File tree

8 files changed

+11
-22
lines changed

8 files changed

+11
-22
lines changed

internal/common/actions.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/drakkan/sftpgo/v2/internal/httpclient"
3939
"github.com/drakkan/sftpgo/v2/internal/logger"
4040
"github.com/drakkan/sftpgo/v2/internal/plugin"
41-
"github.com/drakkan/sftpgo/v2/internal/util"
4241
)
4342

4443
var (
@@ -349,7 +348,7 @@ func notificationAsEnvVars(event *notifier.FsEvent) []string {
349348
if len(event.Metadata) > 0 {
350349
data, err := json.Marshal(event.Metadata)
351350
if err == nil {
352-
result = append(result, fmt.Sprintf("SFTPGO_ACTION_METADATA=%s", util.BytesToString(data)))
351+
result = append(result, fmt.Sprintf("SFTPGO_ACTION_METADATA=%s", data))
353352
}
354353
}
355354
return result

internal/common/dataretention.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ func (c *RetentionCheck) sendHookNotification(elapsed time.Duration, errCheck er
479479

480480
cmd := exec.CommandContext(ctx, Config.DataRetentionHook, args...)
481481
cmd.Env = append(env,
482-
fmt.Sprintf("SFTPGO_DATA_RETENTION_RESULT=%s", util.BytesToString(jsonData)))
482+
fmt.Sprintf("SFTPGO_DATA_RETENTION_RESULT=%s", jsonData))
483483
err := cmd.Run()
484484

485485
c.conn.Log(logger.LevelDebug, "notified result using command: %q, elapsed: %s err: %v",

internal/common/eventmanager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ func executeHTTPRuleAction(c dataprovider.EventActionHTTPConfig, params *EventPa
14891489
if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusNoContent {
14901490
if rb, err := io.ReadAll(io.LimitReader(resp.Body, 2048)); err == nil {
14911491
eventManagerLog(logger.LevelDebug, "error notification response from endpoint %q: %s",
1492-
endpoint, util.BytesToString(rb))
1492+
endpoint, rb)
14931493
}
14941494
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
14951495
}

internal/dataprovider/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func executeNotificationCommand(operation, executor, ip, objectType, objectName,
149149
fmt.Sprintf("SFTPGO_PROVIDER_IP=%s", ip),
150150
fmt.Sprintf("SFTPGO_PROVIDER_ROLE=%s", role),
151151
fmt.Sprintf("SFTPGO_PROVIDER_TIMESTAMP=%d", util.GetTimeAsMsSinceEpoch(time.Now())),
152-
fmt.Sprintf("SFTPGO_PROVIDER_OBJECT=%s", util.BytesToString(objectAsJSON)))
152+
fmt.Sprintf("SFTPGO_PROVIDER_OBJECT=%s", objectAsJSON))
153153

154154
startTime := time.Now()
155155
err := cmd.Run()

internal/dataprovider/dataprovider.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -4174,7 +4174,7 @@ func getPreLoginHookResponse(loginMethod, ip, protocol string, userAsJSON []byte
41744174

41754175
cmd := exec.CommandContext(ctx, config.PreLoginHook, args...)
41764176
cmd.Env = append(env,
4177-
fmt.Sprintf("SFTPGO_LOGIND_USER=%s", util.BytesToString(userAsJSON)),
4177+
fmt.Sprintf("SFTPGO_LOGIND_USER=%s", userAsJSON),
41784178
fmt.Sprintf("SFTPGO_LOGIND_METHOD=%s", loginMethod),
41794179
fmt.Sprintf("SFTPGO_LOGIND_IP=%s", ip),
41804180
fmt.Sprintf("SFTPGO_LOGIND_PROTOCOL=%s", protocol),
@@ -4221,7 +4221,7 @@ func executePreLoginHook(username, loginMethod, ip, protocol string, oidcTokenFi
42214221
recoveryCodes := u.Filters.RecoveryCodes
42224222
err = json.Unmarshal(out, &u)
42234223
if err != nil {
4224-
return u, fmt.Errorf("invalid pre-login hook response %q, error: %v", util.BytesToString(out), err)
4224+
return u, fmt.Errorf("invalid pre-login hook response %q, error: %v", out, err)
42254225
}
42264226
u.ID = userID
42274227
u.UsedQuotaSize = userUsedQuotaSize
@@ -4316,7 +4316,7 @@ func ExecutePostLoginHook(user *User, loginMethod, ip, protocol string, err erro
43164316

43174317
cmd := exec.CommandContext(ctx, config.PostLoginHook, args...)
43184318
cmd.Env = append(env,
4319-
fmt.Sprintf("SFTPGO_LOGIND_USER=%s", util.BytesToString(userAsJSON)),
4319+
fmt.Sprintf("SFTPGO_LOGIND_USER=%s", userAsJSON),
43204320
fmt.Sprintf("SFTPGO_LOGIND_IP=%s", ip),
43214321
fmt.Sprintf("SFTPGO_LOGIND_METHOD=%s", loginMethod),
43224322
fmt.Sprintf("SFTPGO_LOGIND_STATUS=%s", status),
@@ -4385,7 +4385,7 @@ func getExternalAuthResponse(username, password, pkey, keyboardInteractive, ip,
43854385
cmd := exec.CommandContext(ctx, config.ExternalAuthHook, args...)
43864386
cmd.Env = append(env,
43874387
fmt.Sprintf("SFTPGO_AUTHD_USERNAME=%s", username),
4388-
fmt.Sprintf("SFTPGO_AUTHD_USER=%s", util.BytesToString(userAsJSON)),
4388+
fmt.Sprintf("SFTPGO_AUTHD_USER=%s", userAsJSON),
43894389
fmt.Sprintf("SFTPGO_AUTHD_IP=%s", ip),
43904390
fmt.Sprintf("SFTPGO_AUTHD_PASSWORD=%s", password),
43914391
fmt.Sprintf("SFTPGO_AUTHD_PUBLIC_KEY=%s", pkey),

internal/logger/logger.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"os"
3030
"path/filepath"
3131
"time"
32-
"unsafe"
3332

3433
ftpserverlog "github.com/fclairamb/go-log"
3534
"github.com/rs/zerolog"
@@ -310,7 +309,7 @@ func (l *StdLoggerWrapper) Write(p []byte) (n int, err error) {
310309
p = p[0 : n-1]
311310
}
312311

313-
Log(LevelError, l.Sender, "", "%s", bytesToString(p))
312+
Log(LevelError, l.Sender, "", "%s", p)
314313
return
315314
}
316315

@@ -390,12 +389,3 @@ func (l *LeveledLogger) With(keysAndValues ...any) ftpserverlog.Logger {
390389
additionalKeyVals: append(l.additionalKeyVals, keysAndValues...),
391390
}
392391
}
393-
394-
func bytesToString(b []byte) string {
395-
// unsafe.SliceData relies on cap whereas we want to rely on len
396-
if len(b) == 0 {
397-
return ""
398-
}
399-
// https://github.com/golang/go/blob/4ed358b57efdad9ed710be7f4fc51495a7620ce2/src/strings/builder.go#L41
400-
return unsafe.String(unsafe.SliceData(b), len(b))
401-
}

internal/sftpd/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ func (c *Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.Serve
648648

649649
switch req.Type {
650650
case "subsystem":
651-
if util.BytesToString(req.Payload[4:]) == "sftp" {
651+
if bytes.Equal(req.Payload[4:], []byte("sftp")) {
652652
ok = true
653653
connection := &Connection{
654654
BaseConnection: common.NewBaseConnection(connID, common.ProtocolSFTP, conn.LocalAddr().String(),

internal/webdavd/file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ func (f *webDavFile) Patch(patches []webdav.Proppatch) ([]webdav.Propstat, error
451451
parsed, err := parseTime(util.BytesToString(p.InnerXML))
452452
if err != nil {
453453
f.Connection.Log(logger.LevelWarn, "unsupported last modification time: %q, err: %v",
454-
util.BytesToString(p.InnerXML), err)
454+
p.InnerXML, err)
455455
hasError = true
456456
continue
457457
}

0 commit comments

Comments
 (0)