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.3.4
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.3.5
Choose a head ref
  • 8 commits
  • 5 files changed
  • 4 contributors

Commits on Aug 12, 2020

  1. Copy the full SHA
    0caf874 View commit details
  2. Merge pull request #257 from cksriprajittichai/fix-typos-in-readme-code

    Fix typos in README code samples
    0xmichalis authored Aug 12, 2020
    Copy the full SHA
    6400260 View commit details

Commits on Aug 22, 2020

  1. Fix amount of files read in UnionFile.Readdir

    To match behavior of os.File.Readdir, argument c in UnionFile.Readdir
    should control amount of files read, not the upper bound of buffered
    files.
    
    Fixes #259
    std0 committed Aug 22, 2020
    Copy the full SHA
    066c8b0 View commit details

Commits on Sep 2, 2020

  1. mem: add test for Write/Seek

    Signed-off-by: Yerden Zhumabekov <yerden.zhumabekov@gmail.com>
    yerden committed Sep 2, 2020
    Copy the full SHA
    9e9dd69 View commit details
  2. mem: fix code style in Seek

    Signed-off-by: Yerden Zhumabekov <yerden.zhumabekov@gmail.com>
    yerden committed Sep 2, 2020
    Copy the full SHA
    d2521ec View commit details
  3. mem: fix Write setting wrong offset

    Signed-off-by: Yerden Zhumabekov <yerden.zhumabekov@gmail.com>
    yerden committed Sep 2, 2020
    Copy the full SHA
    84f9c83 View commit details
  4. Merge pull request #263 from yerden/fix-mmap-fs-seeker

    mem: fix incorrect offset in Write
    0xmichalis authored Sep 2, 2020
    Copy the full SHA
    215b26b View commit details
  5. Merge pull request #260 from std0/union-readdir-fix

    Fix amount of files read in UnionFile.Readdir
    0xmichalis authored Sep 2, 2020
    Copy the full SHA
    27c9ee0 View commit details
Showing with 71 additions and 22 deletions.
  1. +3 −3 README.md
  2. +16 −4 composite_test.go
  3. +5 −5 mem/file.go
  4. +40 −0 mem/file_test.go
  5. +7 −10 unionFile.go
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -227,7 +227,7 @@ operation and a mock filesystem during testing or as needed.

```go
appfs := afero.NewOsFs()
appfs.MkdirAll("src/a", 0755))
appfs.MkdirAll("src/a", 0755)
```

## Memory Backed Storage
@@ -241,7 +241,7 @@ safely.

```go
mm := afero.NewMemMapFs()
mm.MkdirAll("src/a", 0755))
mm.MkdirAll("src/a", 0755)
```

#### InMemoryFile
@@ -306,7 +306,7 @@ Any Afero FileSystem can be used as an httpFs.

```go
httpFs := afero.NewHttpFs(<ExistingFS>)
fileserver := http.FileServer(httpFs.Dir(<PATH>)))
fileserver := http.FileServer(httpFs.Dir(<PATH>))
http.Handle("/", fileserver)
```

20 changes: 16 additions & 4 deletions composite_test.go
Original file line number Diff line number Diff line change
@@ -477,7 +477,8 @@ func TestUnionFileReaddirAskForTooMany(t *testing.T) {
base := &MemMapFs{}
overlay := &MemMapFs{}

for i := 0; i < 5; i++ {
const testFiles = 5
for i := 0; i < testFiles; i++ {
WriteFile(base, fmt.Sprintf("file%d.txt", i), []byte("afero"), 0777)
}

@@ -490,13 +491,24 @@ func TestUnionFileReaddirAskForTooMany(t *testing.T) {

defer f.Close()

names, err := f.Readdirnames(6)
// Read part of all files
wantNames := 3
names, err := f.Readdirnames(wantNames)
if err != nil {
t.Fatal(err)
}
if len(names) != wantNames {
t.Fatalf("got %d names %v, want %d", len(names), names, wantNames)
}

if len(names) != 5 {
t.Fatal(names)
// Try to read more files than remaining
wantNames = testFiles - len(names)
names, err = f.Readdirnames(wantNames + 1)
if err != nil {
t.Fatal(err)
}
if len(names) != wantNames {
t.Fatalf("got %d names %v, want %d", len(names), names, wantNames)
}

// End of directory
10 changes: 5 additions & 5 deletions mem/file.go
Original file line number Diff line number Diff line change
@@ -225,11 +225,11 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
return 0, ErrFileClosed
}
switch whence {
case 0:
case io.SeekStart:
atomic.StoreInt64(&f.at, offset)
case 1:
atomic.AddInt64(&f.at, int64(offset))
case 2:
case io.SeekCurrent:
atomic.AddInt64(&f.at, offset)
case io.SeekEnd:
atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
}
return f.at, nil
@@ -260,7 +260,7 @@ func (f *File) Write(b []byte) (n int, err error) {
}
setModTime(f.fileData, time.Now())

atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
atomic.AddInt64(&f.at, int64(n))
return
}

40 changes: 40 additions & 0 deletions mem/file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mem

import (
"bytes"
"io"
"testing"
"time"
@@ -205,3 +206,42 @@ func TestFileReadAtSeekOffset(t *testing.T) {
t.Fatal(err)
}
}

func TestFileWriteAndSeek(t *testing.T) {
fd := CreateFile("foo")
f := NewFileHandle(fd)

assert := func(expected bool, v ...interface{}) {
if !expected {
t.Helper()
t.Fatal(v...)
}
}

data4 := []byte{0, 1, 2, 3}
data20 := bytes.Repeat(data4, 5)
var off int64

for i := 0; i < 100; i++ {
// write 20 bytes
n, err := f.Write(data20)
assert(err == nil, err)
off += int64(n)
assert(n == len(data20), n)
assert(off == int64((i+1)*len(data20)), off)

// rewind to start and write 4 bytes there
cur, err := f.Seek(-off, io.SeekCurrent)
assert(err == nil, err)
assert(cur == 0, cur)

n, err = f.Write(data4)
assert(err == nil, err)
assert(n == len(data4), n)

// back at the end
cur, err = f.Seek(off-int64(n), io.SeekCurrent)
assert(err == nil, err)
assert(cur == off, cur, off)
}
}
17 changes: 7 additions & 10 deletions unionFile.go
Original file line number Diff line number Diff line change
@@ -186,25 +186,22 @@ func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) {
}
f.files = append(f.files, merged...)
}
files := f.files[f.off:]

if c <= 0 && len(f.files) == 0 {
return f.files, nil
if c <= 0 {
return files, nil
}

if f.off >= len(f.files) {
if len(files) == 0 {
return nil, io.EOF
}

if c <= 0 {
return f.files[f.off:], nil
}

if c > len(f.files) {
c = len(f.files)
if c > len(files) {
c = len(files)
}

defer func() { f.off += c }()
return f.files[f.off:c], nil
return files[:c], nil
}

func (f *UnionFile) Readdirnames(c int) ([]string, error) {