Adding Permissions To MapFS Part 1
I decided to try and commit to golang, because I was feeling brave.
First step was identifying a github issue for a specific problem: The test.MapFS package does not enforce file permissions. I'd found this problem while working on Andrew, and John Arundel kindly identified a github issue tracking the problem.
"Hah", I thought, "This'll be easy."
There's no hubris quite like the hubrjis of estimating while never having working on a problem like the one before you.
Those wiser than me have noted that the moment of beginning is the moment of greatest ignorance.
First I compiled golang. Then I figured out how to source that same build repeatedly. It turned out that
vs code was being weird about interacting with the custom build, so I had to add "go.goroot": "/Users/gwyn/Developer/go/bin"
to settings.json
Unfortunately, settings.json is global to all instances of vs code, so I have to either remember to turn it off or have mostly broken package imports in other projectsfor a few weeks.
I also wrote a .env file to set up my command line environment
; cat ./src/gwyn.env
PATH=/Users/gwyn/Developer/go/bin:$PATH
It wouldn't be hard to automatically activate it when I enter into ~/Developer/go
but I'm manually sourcing it. I'm l a z y.
I started with adding a test into go/src/testing/fstest/mapfs_test.go
func TestMapFSFileSystemPermissions(t *testing.T) {
m := MapFS{
"path/to/a.txt": &MapFile{Mode: 0o000},
}
_, err := m.Open("path/to/a.txt") // Use Open instead of Stat
want := fs.ErrPermission
if !errors.Is(err, fs.ErrPermission) {
t.Errorf("MapFS should not open a file with permissions 0o000 want:\n%s\ngot:\n%v\n", want, err)
}
}
All I'm trying to achieve is trying to open a file without permissions and getting an error.
So how do you check file system permissions anyway? Normally this'd be enforced by the kernel (the open system call returns an EACCES error if permission is denied, but a mapFS is just a hashmap that contains a bunch of a type called a MapFile
The kernel source for open() shows that it mostly dumps the work to a second function, after checking for whether it needs a special type to represent the size of a big file. The kernel shows that it populates a struct tracking file permissions, but I can't quite figure out from the kernel source precisely what else is happening.
So, I need to figure out how to verify if a string like "644" and validate it against a set of permissions. Fun!