Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memfs vs osfs behavior differences #412

Open
shadowspore opened this issue Dec 24, 2023 · 0 comments
Open

memfs vs osfs behavior differences #412

shadowspore opened this issue Dec 24, 2023 · 0 comments

Comments

@shadowspore
Copy link

I've noticed a behavior difference between memfs and real os filesystem.
Related issue: #152
Related commit: ec3a311
It says that returning io.ErrUnexpectedEOF is "in line with how the OS File works".
However, it seems that this is not true?
ping @bep

Test case to reproduce (tested on linux 6.6.7 ext4):

package filebase

import (
	"os"
	"testing"

	"github.com/spf13/afero"
	"github.com/stretchr/testify/require"
)

func TestAferoMemfs(t *testing.T) {
	memfs := afero.NewMemMapFs()
	osfs := afero.NewOsFs()

	// Create files.
	fmem, err := memfs.OpenFile("/tmp/foo.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600)
	require.NoError(t, err)

	freal, err := osfs.OpenFile("/tmp/foo.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600)
	require.NoError(t, err)

	// Write data and close files.
	{
		_, err = fmem.WriteString("abc")
		require.NoError(t, err)

		_, err = freal.WriteString("abc")
		require.NoError(t, err)

		require.NoError(t, fmem.Close())
		require.NoError(t, freal.Close())
	}

	// Open files again.
	fmem, err = memfs.OpenFile("/tmp/foo.txt", os.O_RDWR, 0o600)
	require.NoError(t, err)

	freal, err = osfs.OpenFile("/tmp/foo.txt", os.O_RDWR, 0o600)
	require.NoError(t, err)

	// Read data and truncate.
	{
		bufMem := make([]byte, 5)
		nMem, errMem := fmem.Read(bufMem)

		bufReal := make([]byte, 5)
		nReal, errReal := freal.Read(bufReal)

		require.Equal(t, errMem, errReal)
		require.Equal(t, nMem, nReal)
		require.Equal(t, bufMem, bufReal)

		require.NoError(t, fmem.Truncate(0))
		require.NoError(t, freal.Truncate(0))
	}

	// Try to read data again.
	{
		bufMem := make([]byte, 5)
		nMem, errMem := fmem.Read(bufMem)

		bufReal := make([]byte, 5)
		nReal, errReal := freal.Read(bufReal)

		require.Equal(t, errMem, errReal) // <- UnexpectedEOF vs EOF
		require.Equal(t, nMem, nReal)
		require.Equal(t, bufMem, bufReal)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant