Make delete more synchronous

This commit is contained in:
Dane Everitt 2020-09-30 21:47:42 -07:00
parent 9b7c0fb7f3
commit ee460686d6
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -182,7 +182,7 @@ func (fs *Filesystem) Chown(path string) error {
if err != nil { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }
if fs.isTest { if fs.isTest {
return nil return nil
} }
@ -321,6 +321,7 @@ func (fs *Filesystem) Copy(p string) error {
// Deletes a file or folder from the system. Prevents the user from accidentally // Deletes a file or folder from the system. Prevents the user from accidentally
// (or maliciously) removing their root server data directory. // (or maliciously) removing their root server data directory.
func (fs *Filesystem) Delete(p string) error { func (fs *Filesystem) Delete(p string) error {
wg := sync.WaitGroup{}
// This is one of the few (only?) places in the codebase where we're explicitly not using // This is one of the few (only?) places in the codebase where we're explicitly not using
// the SafePath functionality when working with user provided input. If we did, you would // the SafePath functionality when working with user provided input. If we did, you would
// not be able to delete a file that is a symlink pointing to a location outside of the data // not be able to delete a file that is a symlink pointing to a location outside of the data
@ -348,14 +349,18 @@ func (fs *Filesystem) Delete(p string) error {
if !st.IsDir() { if !st.IsDir() {
fs.addDisk(-st.Size()) fs.addDisk(-st.Size())
} else { } else {
go func(st os.FileInfo, resolved string) { wg.Add(1)
go func(wg *sync.WaitGroup, st os.FileInfo, resolved string) {
defer wg.Done()
if s, err := fs.DirectorySize(resolved); err == nil { if s, err := fs.DirectorySize(resolved); err == nil {
fs.addDisk(-s) fs.addDisk(-s)
} }
}(st, resolved) }(&wg, st, resolved)
} }
} }
wg.Wait()
return os.RemoveAll(resolved) return os.RemoveAll(resolved)
} }