sysfs: adds chmod (#1135)

This adds `FS.Chmod` and implements it for `GOOS=js`. This function
isn't defined in WASI snapshot01, but it is in `wasi-filesystem`, e.g.
`change-file-permissions-at`.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2023-02-17 20:55:03 +09:00
committed by GitHub
parent add6458c99
commit e2ebce5d23
13 changed files with 181 additions and 14 deletions

View File

@@ -76,6 +76,45 @@ func TestDirFS_MkDir(t *testing.T) {
err := testFS.Mkdir(filePath, fs.ModeDir)
require.Equal(t, syscall.ENOENT, err)
})
// Remove the path so that we can test creating it with perms.
require.NoError(t, os.Remove(realPath))
// Setting mode only applies to files on windows
if runtime.GOOS != "windows" {
t.Run("dir", func(t *testing.T) {
require.NoError(t, os.Mkdir(realPath, 0o444))
defer os.RemoveAll(realPath)
testChmod(t, testFS, name)
})
}
t.Run("file", func(t *testing.T) {
require.NoError(t, os.WriteFile(realPath, nil, 0o444))
defer os.RemoveAll(realPath)
testChmod(t, testFS, name)
})
}
func testChmod(t *testing.T, testFS FS, path string) {
// Test base case, using 0o444 not 0o400 for read-back on windows.
requireMode(t, testFS, path, 0o444)
// Test adding write, using 0o666 not 0o600 for read-back on windows.
require.NoError(t, testFS.Chmod(path, 0o666))
requireMode(t, testFS, path, 0o666)
if runtime.GOOS != "windows" {
// Test clearing group and world, setting owner read+execute.
require.NoError(t, testFS.Chmod(path, 0o500))
requireMode(t, testFS, path, 0o500)
}
}
func requireMode(t *testing.T, testFS FS, path string, mode fs.FileMode) {
stat, err := StatPath(testFS, path)
require.NoError(t, err)
require.Equal(t, mode, stat.Mode()&fs.ModePerm)
}
func TestDirFS_Rename(t *testing.T) {
@@ -371,7 +410,7 @@ func TestDirFS_Utimes(t *testing.T) {
testUtimes(t, tmpDir, testFS)
}
func TestDirFS_Open(t *testing.T) {
func TestDirFS_OpenFile(t *testing.T) {
tmpDir := t.TempDir()
// Create a subdirectory, so we can test reads outside the FS root.
@@ -466,9 +505,9 @@ func TestDirFS_Truncate(t *testing.T) {
require.NoError(t, os.Remove(realPath))
})
t.Run("negative", func(t *testing.T) {
require.NoError(t, os.WriteFile(realPath, []byte{}, 0o600))
require.NoError(t, os.WriteFile(realPath, []byte{}, 0o600))
t.Run("negative", func(t *testing.T) {
err := testFS.Truncate(name, -1)
require.Equal(t, syscall.EINVAL, err)
})