From 4660c2e859d6c64a834f3114b3e40e2a85da195c Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Thu, 25 Jul 2024 18:39:02 +0200 Subject: [PATCH] replace hand-written slice utilities with methods from slices package SFTPGo depends on Go 1.22 so we can use slices package Signed-off-by: Nicola Murino --- internal/common/connection.go | 11 +++++------ internal/util/util.go | 9 --------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/internal/common/connection.go b/internal/common/connection.go index e7f9c9a8..713e386a 100644 --- a/internal/common/connection.go +++ b/internal/common/connection.go @@ -21,6 +21,7 @@ import ( "io/fs" "os" "path" + "slices" "strings" "sync" "sync/atomic" @@ -1827,7 +1828,7 @@ func (l *DirListerAt) Add(fi os.FileInfo) { l.mu.Lock() defer l.mu.Unlock() - l.info = append(l.info, fi) + l.info = slices.Insert(l.info, 0, fi) } // ListAt implements sftp.ListerAt @@ -1840,10 +1841,10 @@ func (l *DirListerAt) ListAt(f []os.FileInfo, _ int64) (int, error) { } if len(f) <= len(l.info) { files := make([]os.FileInfo, 0, len(f)) - for idx := len(l.info) - 1; idx >= 0; idx-- { + for idx := range l.info { files = append(files, l.info[idx]) if len(files) == len(f) { - l.info = l.info[:idx] + l.info = l.info[idx+1:] n := copy(f, files) return n, nil } @@ -1865,9 +1866,7 @@ func (l *DirListerAt) Next(limit int) ([]os.FileInfo, error) { } files = l.user.FilterListDir(files, l.virtualPath) if len(l.info) > 0 { - for _, fi := range l.info { - files = util.PrependFileInfo(files, fi) - } + files = slices.Concat(l.info, files) l.info = nil } if err != nil || len(files) > 0 { diff --git a/internal/util/util.go b/internal/util/util.go index 54dd0bcd..0274d97d 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -809,15 +809,6 @@ func GetRedactedURL(rawurl string) string { return u.Redacted() } -// PrependFileInfo prepends a file info to a slice in an efficient way. -// We, optimistically, assume that the slice has enough capacity -func PrependFileInfo(files []os.FileInfo, info os.FileInfo) []os.FileInfo { - files = append(files, nil) - copy(files[1:], files) - files[0] = info - return files -} - // GetTLSVersion returns the TLS version for integer: // - 12 means TLS 1.2 // - 13 means TLS 1.3