Skip to content

Commit

Permalink
fix with FtpPwdPath.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzlichtbezirk committed Apr 10, 2023
1 parent 98921b8 commit e78e556
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
3 changes: 1 addition & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ func (c *Cache[K, T]) Remove(key K) (ok bool) {
}

func (c *Cache[K, T]) Enum(f func(K, T) bool) {
var s = make([]kvcell[K, T], len(c.s))
c.mux.Lock()
copy(s, c.s)
var s = append([]kvcell[K, T]{}, c.s...) // make non-nil copy
c.mux.Unlock()

for _, cell := range s {
Expand Down
41 changes: 24 additions & 17 deletions ftpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"net/url"
"path"
"strings"
"sync"
"time"

Expand All @@ -19,24 +20,34 @@ var (
)

var (
pwdmap map[string]string
pwdmap = map[string]string{}
pwdmux sync.RWMutex
)

func PwdCache(ftpaddr string, conn *ftp.ServerConn) (pwd string) {
var ok bool
// FtpPwdPath return path from given URL concatenated with FTP
// current directory. It's used cache to avoid extra calls to
// FTP-server to get current directory for every call.
func FtpPwdPath(u *url.URL, conn *ftp.ServerConn) (fpath string) {
var ftpaddr = (&url.URL{
Scheme: u.Scheme,
User: u.User,
Host: u.Host,
}).String()

pwdmux.RLock()
pwd, ok = pwdmap[ftpaddr]
var pwd, ok = pwdmap[ftpaddr]
pwdmux.RUnlock()
if ok {
return
if !ok {
var err error
if pwd, err = conn.CurrentDir(); err == nil {
pwdmux.Lock()
pwdmap[ftpaddr] = pwd
pwdmux.Unlock()
}
}
var err error
if pwd, err = conn.CurrentDir(); err == nil {
pwdmux.Lock()
pwdmap[ftpaddr] = pwd
pwdmux.Unlock()
return
fpath = path.Join(pwd, u.Path)
if strings.HasPrefix(fpath, "/") {
fpath = fpath[1:]
}
return
}
Expand Down Expand Up @@ -104,11 +115,7 @@ func (ff *FtpFile) Open(ftppath string) (err error) {
if ff.conn, err = ftp.Dial(u.Host, ftp.DialWithTimeout(cfg.DialTimeout)); err != nil {
return
}
ff.path = path.Join(PwdCache((&url.URL{
Scheme: u.Scheme,
User: u.User,
Host: u.Host,
}).String(), ff.conn), u.Path)
ff.path = FtpPwdPath(u, ff.conn)
var pass, _ = u.User.Password()
if err = ff.conn.Login(u.User.Username(), pass); err != nil {
return
Expand Down
3 changes: 1 addition & 2 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ func (pl *Profiles) ReadYaml(fname string) (err error) {
var prf = pl.NewProfile("admin", "dag qus fly in the sky")
prf.ID = 1
// set hidden files array to default predefined list
prf.Hidden = make([]string, len(DefHidden))
copy(prf.Hidden, DefHidden)
prf.Hidden = append([]string{}, DefHidden...)
// set default "home" share
prf.Shares = []DiskPath{
{CPhome, CatNames[CPhome]},
Expand Down
15 changes: 5 additions & 10 deletions opendisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,10 @@ func StatFile(syspath string) (fi fs.FileInfo, err error) {
if err = conn.Login(u.User.Username(), pass); err != nil {
return
}
var path = path.Join(PwdCache((&url.URL{
Scheme: u.Scheme,
User: u.User,
Host: u.Host,
}).String(), conn), u.Path)
var path = FtpPwdPath(u, conn)
if strings.HasPrefix(path, "/") {
path = path[1:]
}
var ent *ftp.Entry
if ent, err = conn.GetEntry(path); err != nil {
return
Expand Down Expand Up @@ -223,11 +222,7 @@ func ReadDir(dir string) (ret []fs.FileInfo, err error) {
if err = conn.Login(u.User.Username(), pass); err != nil {
return
}
var path = path.Join(PwdCache((&url.URL{
Scheme: u.Scheme,
User: u.User,
Host: u.Host,
}).String(), conn), u.Path)
var path = FtpPwdPath(u, conn)
var entries []*ftp.Entry
if entries, err = conn.List(path); err != nil {
return
Expand Down
9 changes: 3 additions & 6 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ func (prf *Profile) FindLocal() {
// ScanLocal scans paths from local roots list.
func (prf *Profile) ScanLocal(session *Session) (ret []any, err error) {
prf.mux.RLock()
var vfiles = make([]DiskPath, len(prf.Roots))
copy(vfiles, prf.Roots)
var vfiles = append([]DiskPath{}, prf.Roots...) // make non-nil copy
prf.mux.RUnlock()

var dp DirProp
Expand All @@ -323,8 +322,7 @@ func (prf *Profile) ScanLocal(session *Session) (ret []any, err error) {
// ScanRemote scans paths at network destination.
func (prf *Profile) ScanRemote(session *Session) (ret []any, err error) {
prf.mux.RLock()
var vfiles = make([]DiskPath, len(prf.Remote))
copy(vfiles, prf.Remote)
var vfiles = append([]DiskPath{}, prf.Remote...) // make non-nil copy
prf.mux.RUnlock()

var dp DirProp
Expand All @@ -346,8 +344,7 @@ func (prf *Profile) ScanRemote(session *Session) (ret []any, err error) {
// ScanShares scans actual shares from shares list.
func (prf *Profile) ScanShares(session *Session) (ret []any, err error) {
prf.mux.RLock()
var vfiles = make([]DiskPath, len(prf.Shares))
copy(vfiles, prf.Shares)
var vfiles = append([]DiskPath{}, prf.Shares...) // make non-nil copy
prf.mux.RUnlock()

var dp DirProp
Expand Down

0 comments on commit e78e556

Please sign in to comment.