Avoid race condition when listing a directory
This commit is contained in:
parent
d3cbf96c57
commit
2220eb049c
|
@ -521,14 +521,14 @@ func (fs *Filesystem) ListDirectory(p string) ([]*Stat, error) {
|
||||||
// You must initialize the output of this directory as a non-nil value otherwise
|
// You must initialize the output of this directory as a non-nil value otherwise
|
||||||
// when it is marshaled into a JSON object you'll just get 'null' back, which will
|
// when it is marshaled into a JSON object you'll just get 'null' back, which will
|
||||||
// break the panel badly.
|
// break the panel badly.
|
||||||
out := make([]*Stat, 0)
|
out := make([]*Stat, len(files))
|
||||||
|
|
||||||
// Iterate over all of the files and directories returned and perform an async process
|
// Iterate over all of the files and directories returned and perform an async process
|
||||||
// to get the mime-type for them all.
|
// to get the mime-type for them all.
|
||||||
for _, file := range files {
|
for i, file := range files {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
go func(f os.FileInfo) {
|
go func(idx int, f os.FileInfo) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
var m = "inode/directory"
|
var m = "inode/directory"
|
||||||
|
@ -536,11 +536,11 @@ func (fs *Filesystem) ListDirectory(p string) ([]*Stat, error) {
|
||||||
m, _, _ = mimetype.DetectFile(filepath.Join(cleaned, f.Name()))
|
m, _, _ = mimetype.DetectFile(filepath.Join(cleaned, f.Name()))
|
||||||
}
|
}
|
||||||
|
|
||||||
out = append(out, &Stat{
|
out[idx] = &Stat{
|
||||||
Info: f,
|
Info: f,
|
||||||
Mimetype: m,
|
Mimetype: m,
|
||||||
})
|
}
|
||||||
}(file)
|
}(i, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user