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

MemMapFs clears out non chmod mode bits when writing file #392

Open
sridgeway7 opened this issue Jun 22, 2023 · 0 comments
Open

MemMapFs clears out non chmod mode bits when writing file #392

sridgeway7 opened this issue Jun 22, 2023 · 0 comments

Comments

@sridgeway7
Copy link

sridgeway7 commented Jun 22, 2023

I recently update a project from v1.3.3 to v1.8.2 and it looks like file mode is not preserved when creating files using MemMapFs.

I'm trying to write a file with os.ModeSocket to a MemMapFs instance in a test. After writing the file, I can see that os.ModeSocket is not set. It looks like this is cause by this line which mutate perm at the top of MemMapFs.OpenFile:

afero/memmap.go

Lines 223 to 224 in 45ef346

func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
perm &= chmodBits

Which was changed here: b598fbb

This effectively zeroes out any perm bits that are not the chmodBits which make it impossible to create irregular files. It looks like the handling for preserving previous bits while only honoring chmodBits is already in MemMapFs.Chmod. So maybe a better approach is to not modify perm, set the mode as passed on creation, and then call Chmod if chmod is true?:

	if os.IsNotExist(err) && (flag&os.O_CREATE > 0) {
		file, err = m.Create(name)
		if err == nil {
			err = file.setFileMode(perm & ^chmodBits)
		}

		chmod = true
	}

	/* ... */

	if chmod {
		return file, m.Chmod(name, perm)
	}
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