mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-25 09:00:27 +00:00
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 <nicola.murino@gmail.com>
This commit is contained in:
parent
6ba1198c47
commit
bd5eb03d9c
4 changed files with 11 additions and 21 deletions
|
@ -21,6 +21,7 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
@ -1795,12 +1796,12 @@ type DirListerAt struct {
|
||||||
lister vfs.DirLister
|
lister vfs.DirLister
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add adds the given os.FileInfo to the internal cache
|
// Prepend adds the given os.FileInfo as first element of the internal cache
|
||||||
func (l *DirListerAt) Add(fi os.FileInfo) {
|
func (l *DirListerAt) Prepend(fi os.FileInfo) {
|
||||||
l.mu.Lock()
|
l.mu.Lock()
|
||||||
defer l.mu.Unlock()
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
l.info = append(l.info, fi)
|
l.info = slices.Insert(l.info, 0, fi)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListAt implements sftp.ListerAt
|
// ListAt implements sftp.ListerAt
|
||||||
|
@ -1813,10 +1814,10 @@ func (l *DirListerAt) ListAt(f []os.FileInfo, _ int64) (int, error) {
|
||||||
}
|
}
|
||||||
if len(f) <= len(l.info) {
|
if len(f) <= len(l.info) {
|
||||||
files := make([]os.FileInfo, 0, len(f))
|
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])
|
files = append(files, l.info[idx])
|
||||||
if len(files) == len(f) {
|
if len(files) == len(f) {
|
||||||
l.info = l.info[:idx]
|
l.info = l.info[idx+1:]
|
||||||
n := copy(f, files)
|
n := copy(f, files)
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
@ -1838,9 +1839,7 @@ func (l *DirListerAt) Next(limit int) ([]os.FileInfo, error) {
|
||||||
}
|
}
|
||||||
files = l.user.FilterListDir(files, l.virtualPath)
|
files = l.user.FilterListDir(files, l.virtualPath)
|
||||||
if len(l.info) > 0 {
|
if len(l.info) > 0 {
|
||||||
for _, fi := range l.info {
|
files = slices.Concat(l.info, files)
|
||||||
files = util.PrependFileInfo(files, fi)
|
|
||||||
}
|
|
||||||
l.info = nil
|
l.info = nil
|
||||||
}
|
}
|
||||||
if err != nil || len(files) > 0 {
|
if err != nil || len(files) > 0 {
|
||||||
|
|
|
@ -1134,8 +1134,8 @@ func TestListerAt(t *testing.T) {
|
||||||
require.Equal(t, 0, n)
|
require.Equal(t, 0, n)
|
||||||
lister, err = conn.ListDir("/")
|
lister, err = conn.ListDir("/")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
lister.Add(vfs.NewFileInfo("..", true, 0, time.Unix(0, 0), false))
|
lister.Prepend(vfs.NewFileInfo("..", true, 0, time.Unix(0, 0), false))
|
||||||
lister.Add(vfs.NewFileInfo(".", true, 0, time.Unix(0, 0), false))
|
lister.Prepend(vfs.NewFileInfo(".", true, 0, time.Unix(0, 0), false))
|
||||||
files = make([]os.FileInfo, 1)
|
files = make([]os.FileInfo, 1)
|
||||||
n, err = lister.ListAt(files, 0)
|
n, err = lister.ListAt(files, 0)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
|
@ -221,9 +221,9 @@ func (c *Connection) Filelist(request *sftp.Request) (sftp.ListerAt, error) {
|
||||||
}
|
}
|
||||||
modTime := time.Unix(0, 0)
|
modTime := time.Unix(0, 0)
|
||||||
if request.Filepath != "/" {
|
if request.Filepath != "/" {
|
||||||
lister.Add(vfs.NewFileInfo("..", true, 0, modTime, false))
|
lister.Prepend(vfs.NewFileInfo("..", true, 0, modTime, false))
|
||||||
}
|
}
|
||||||
lister.Add(vfs.NewFileInfo(".", true, 0, modTime, false))
|
lister.Prepend(vfs.NewFileInfo(".", true, 0, modTime, false))
|
||||||
return lister, nil
|
return lister, nil
|
||||||
case "Stat":
|
case "Stat":
|
||||||
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(request.Filepath)) {
|
if !c.User.HasPerm(dataprovider.PermListItems, path.Dir(request.Filepath)) {
|
||||||
|
|
|
@ -809,15 +809,6 @@ func GetRedactedURL(rawurl string) string {
|
||||||
return u.Redacted()
|
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:
|
// GetTLSVersion returns the TLS version for integer:
|
||||||
// - 12 means TLS 1.2
|
// - 12 means TLS 1.2
|
||||||
// - 13 means TLS 1.3
|
// - 13 means TLS 1.3
|
||||||
|
|
Loading…
Reference in a new issue