Skip to content

Commit

Permalink
Close methods only unregister fs once
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Nov 15, 2023
1 parent f75ac30 commit 0ca98d5
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 80 deletions.
4 changes: 4 additions & 0 deletions dropboxfs/dropboxfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ func (dbfs *fileSystem) Remove(filePath string) error {
}

func (dbfs *fileSystem) Close() error {
if dbfs.id == "" {
return nil // already closed
}
fs.Unregister(dbfs)
dbfs.id = ""
return nil
}
7 changes: 6 additions & 1 deletion ftpfs/ftpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ func (f *fileSystem) Remove(filePath string) error {
}

func (f *fileSystem) Close() error {
if f.conn == nil {
return nil // already closed
}
fs.Unregister(f)
return f.conn.Quit()
err := f.conn.Quit()
f.conn = nil
return err
}
27 changes: 20 additions & 7 deletions memfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ func NewMemFileSystem(separator string, rootFiles ...MemFile) (*MemFileSystem, e
}

func (fs *MemFileSystem) Close() error {
fs.mtx.Lock()
if fs.id == "" {
fs.mtx.Unlock()
return nil // already closed
}
fs.id = ""
clear(fs.root.Dir)
fs.mtx.Unlock() // Unlock before Unregister to avoid deadlock
Unregister(fs)
fs.Clear()
return nil
}

Expand Down Expand Up @@ -250,17 +257,23 @@ func (fs *MemFileSystem) WithID(id string) *MemFileSystem {
if id == "" {
panic("empty id")
}
Unregister(fs)
fs.id = id
Register(fs)
if id != fs.id {
if fs.id != "" {
Unregister(fs)
}
fs.id = id
Register(fs)
}
return fs
}

// This method is not thread-safe!
func (fs *MemFileSystem) WithVolume(volume string) *MemFileSystem {
Unregister(fs)
fs.volume = volume
Register(fs)
if volume != fs.volume {
Unregister(fs)
fs.volume = volume
Register(fs)
}
return fs
}

Expand Down
4 changes: 4 additions & 0 deletions s3fs/s3fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ func (s *fileSystem) Watch(filePath string, onEvent func(fs.File, fs.Event)) (ca
}

func (s *fileSystem) Close() error {
if s.client == nil {
return nil // already closed
}
fs.Unregister(s)
s.client = nil
return nil
}
6 changes: 5 additions & 1 deletion sftpfs/sftpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ func (f *fileSystem) Remove(filePath string) error {
}

func (f *fileSystem) Close() error {
if f.client == nil {
return nil // already closed
}
fs.Unregister(f)
return f.client.Close()
f.client = nil
return nil
}
Loading

0 comments on commit 0ca98d5

Please sign in to comment.