internal/file: refactoring

Updates #3045
This commit is contained in:
Hajime Hoshi 2024-07-31 11:10:34 +09:00
parent 47b8af554c
commit 95ad1b158c

View File

@ -43,7 +43,7 @@ func (f *FileEntryFS) Open(name string) (fs.File, error) {
} }
if name == "." { if name == "." {
return &dir{entry: f.rootEntry}, nil return &dir{dirEntry: f.rootEntry}, nil
} }
var chEntry chan js.Value var chEntry chan js.Value
@ -69,7 +69,7 @@ func (f *FileEntryFS) Open(name string) (fs.File, error) {
chEntry = make(chan js.Value) chEntry = make(chan js.Value)
f.rootEntry.Call("getDirectory", name, nil, cbSuccess, cbFailure) f.rootEntry.Call("getDirectory", name, nil, cbSuccess, cbFailure)
if entry := <-chEntry; entry.Truthy() { if entry := <-chEntry; entry.Truthy() {
return &dir{entry: entry}, nil return &dir{dirEntry: entry}, nil
} }
return nil, &fs.PathError{ return nil, &fs.PathError{
@ -109,7 +109,7 @@ func (f *file) ensureFile() js.Value {
func (f *file) Stat() (fs.FileInfo, error) { func (f *file) Stat() (fs.FileInfo, error) {
return &fileInfo{ return &fileInfo{
entry: f.entry, name: f.entry.Get("name").String(),
file: f.ensureFile(), file: f.ensureFile(),
}, nil }, nil
} }
@ -163,21 +163,21 @@ func (f *file) Close() error {
} }
type dir struct { type dir struct {
entry js.Value dirEntry js.Value
entries []js.Value fileEntries []js.Value
offset int offset int
} }
func (d *dir) Stat() (fs.FileInfo, error) { func (d *dir) Stat() (fs.FileInfo, error) {
return &fileInfo{ return &fileInfo{
entry: d.entry, name: d.dirEntry.Get("name").String(),
}, nil }, nil
} }
func (d *dir) Read(buf []byte) (int, error) { func (d *dir) Read(buf []byte) (int, error) {
return 0, &fs.PathError{ return 0, &fs.PathError{
Op: "read", Op: "read",
Path: d.entry.Get("name").String(), Path: d.dirEntry.Get("name").String(),
Err: errors.New("is a directory"), Err: errors.New("is a directory"),
} }
} }
@ -187,7 +187,7 @@ func (d *dir) Close() error {
} }
func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) { func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
if d.entries == nil { if d.fileEntries == nil {
ch := make(chan struct{}) ch := make(chan struct{})
var rec js.Func var rec js.Func
cb := js.FuncOf(func(this js.Value, args []js.Value) any { cb := js.FuncOf(func(this js.Value, args []js.Value) any {
@ -205,14 +205,14 @@ func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
if !ent.Get("isFile").Bool() && !ent.Get("isDirectory").Bool() { if !ent.Get("isFile").Bool() && !ent.Get("isDirectory").Bool() {
continue continue
} }
d.entries = append(d.entries, ent) d.fileEntries = append(d.fileEntries, ent)
} }
rec.Value.Call("call") rec.Value.Call("call")
return nil return nil
}) })
defer cb.Release() defer cb.Release()
reader := d.entry.Call("createReader") reader := d.dirEntry.Call("createReader")
rec = js.FuncOf(func(this js.Value, args []js.Value) any { rec = js.FuncOf(func(this js.Value, args []js.Value) any {
reader.Call("readEntries", cb) reader.Call("readEntries", cb)
return nil return nil
@ -223,7 +223,7 @@ func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
<-ch <-ch
} }
n := len(d.entries) - d.offset n := len(d.fileEntries) - d.offset
if n == 0 { if n == 0 {
if count <= 0 { if count <= 0 {
@ -238,11 +238,12 @@ func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
ents := make([]fs.DirEntry, n) ents := make([]fs.DirEntry, n)
for i := range ents { for i := range ents {
entry := d.fileEntries[d.offset+i]
fi := &fileInfo{ fi := &fileInfo{
entry: d.entries[d.offset+i], name: entry.Get("name").String(),
} }
if fi.entry.Get("isFile").Bool() { if entry.Get("isFile").Bool() {
fi.file = getFile(fi.entry) fi.file = getFile(entry)
} }
ents[i] = fs.FileInfoToDirEntry(fi) ents[i] = fs.FileInfoToDirEntry(fi)
} }
@ -252,12 +253,12 @@ func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
} }
type fileInfo struct { type fileInfo struct {
entry js.Value name string
file js.Value file js.Value
} }
func (f *fileInfo) Name() string { func (f *fileInfo) Name() string {
return f.entry.Get("name").String() return f.name
} }
func (f *fileInfo) Size() int64 { func (f *fileInfo) Size() int64 {