diff --git a/memory.go b/memory.go index f9646b2..ac326ea 100644 --- a/memory.go +++ b/memory.go @@ -378,10 +378,19 @@ func (f *FakeFile) pwrite(b []byte, off int64) (n int, err error) { } n = copy(f.data.buff[off:], b) - //f.data.dyrtyPages = append(f.data.dyrtyPages, interval{from: off, to: off + int64(n)}) + f.appendDirtyPage(off, off+int64(n)) return n, nil } +func (f *FakeFile) appendDirtyPage(from int64, to int64) { + if !f.data.fs.trackDirtyPages { + return + } + + // TODO: want to add some optimization to less allocation (e.g. for situation then we write sequentially) + f.data.dyrtyPages = append(f.data.dyrtyPages, interval{from: from, to: to}) +} + func (f *FakeFile) WriteString(s string) (n int, err error) { b := unsafe.Slice(unsafe.StringData(s), len(s)) return f.Write(b) diff --git a/memory_fs.go b/memory_fs.go index c51f262..e1e7fe9 100644 --- a/memory_fs.go +++ b/memory_fs.go @@ -14,8 +14,9 @@ import ( ) type FakeFS struct { - inodes map[string]*mockData - workDir string + inodes map[string]*mockData + workDir string + trackDirtyPages bool } var _ FS = &FakeFS{} @@ -36,6 +37,10 @@ func NewMemoryFs() *FakeFS { return fs } +func (f *FakeFS) TrackDirtyPages() { + f.trackDirtyPages = true +} + func (f *FakeFS) Create(path string) (*File, error) { return f.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) } @@ -219,7 +224,7 @@ func (f *FakeFS) ReadDir(name string) ([]os.DirEntry, error) { defer fp.Close() dirs, err := fp.ReadDir(-1) - slices.SortFunc(dirs, func (a os.DirEntry, b os.DirEntry) int { return cmp.Compare(a.Name(), b.Name())}) + slices.SortFunc(dirs, func(a os.DirEntry, b os.DirEntry) int { return cmp.Compare(a.Name(), b.Name()) }) return dirs, err }