Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: spf13/afero
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.9.2
Choose a base ref
...
head repository: spf13/afero
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.9.3
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Nov 14, 2022

  1. Fix concurrency issue in MemMapFs.Mkdir/MkdirAll

    * The backing map is protected by a RWMutex
    * This commit double checks for the existence of the directory inside the write lock to avoid potential data races when multiple goroutines tries to create
    the same directory.
    
    Fixes #361
    Fixes #298
    bep committed Nov 14, 2022
    Copy the full SHA
    a800a9d View commit details
Showing with 89 additions and 0 deletions.
  1. +5 −0 memmap.go
  2. +84 −0 memmap_test.go
5 changes: 5 additions & 0 deletions memmap.go
Original file line number Diff line number Diff line change
@@ -142,6 +142,11 @@ func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error {
}

m.mu.Lock()
// Dobule check that it doesn't exist.
if _, ok := m.getData()[name]; ok {
m.mu.Unlock()
return &os.PathError{Op: "mkdir", Path: name, Err: ErrFileExists}
}
item := mem.CreateDir(name)
mem.SetMode(item, os.ModeDir|perm)
m.getData()[name] = item
84 changes: 84 additions & 0 deletions memmap_test.go
Original file line number Diff line number Diff line change
@@ -3,9 +3,12 @@ package afero
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
)
@@ -692,3 +695,84 @@ func TestMemFsLstatIfPossible(t *testing.T) {
t.Fatalf("Function indicated lstat was called. This should never be true.")
}
}

func TestMemMapFsConfurrentMkdir(t *testing.T) {
const dir = "test_dir"
const n = 1000
mfs := NewMemMapFs().(*MemMapFs)

allFilePaths := make([]string, 0, n)

// run concurrency test
var wg sync.WaitGroup
for i := 0; i < n; i++ {
fp := filepath.Join(
dir,
fmt.Sprintf("%02d", n%10),
fmt.Sprintf("%d.txt", i),
)
allFilePaths = append(allFilePaths, fp)

wg.Add(1)
go func() {
defer wg.Done()

if err := mfs.MkdirAll(filepath.Dir(fp), 0755); err != nil {
t.Error(err)
}

wt, err := mfs.Create(fp)
if err != nil {
t.Error(err)
}
defer func() {
if err := wt.Close(); err != nil {
t.Error(err)
}
}()

// write 30 bytes
for j := 0; j < 10; j++ {
_, err := wt.Write([]byte("000"))
if err != nil {
t.Error(err)
}
}
}()
}
wg.Wait()

// Test1: find all files by full path access
for _, fp := range allFilePaths {
info, err := mfs.Stat(fp)
if err != nil {
t.Error(err)
}

if info.Size() != 30 {
t.Errorf("file size should be 30, but got %d", info.Size())
}

}

// Test2: find all files by walk
foundFiles := make([]string, 0, n)
wErr := Walk(mfs, dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
t.Error(err)
}
if info.IsDir() {
return nil // skip dir
}
if strings.HasSuffix(info.Name(), ".txt") {
foundFiles = append(foundFiles, path)
}
return nil
})
if wErr != nil {
t.Error(wErr)
}
if len(foundFiles) != n {
t.Errorf("found %d files, but expect %d", len(foundFiles), n)
}
}