Skip to content

Commit

Permalink
Next iteration
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Nov 7, 2024
1 parent bc9cee9 commit 97bc779
Show file tree
Hide file tree
Showing 12 changed files with 1,065 additions and 310 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
go.etcd.io/etcd/etcdutl/v3 v3.5.16
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.28.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/mod v0.21.0
golang.org/x/sync v0.8.0
golang.org/x/sys v0.26.0
Expand Down Expand Up @@ -257,7 +258,6 @@ require (
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/term v0.25.0 // indirect
Expand Down
52 changes: 40 additions & 12 deletions internal/os/unix/dirfd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,40 @@ import (
"golang.org/x/sys/unix"
)

// Opens the path with the given name by using the openat2 syscall.
// An open Linux-native handle to some path on the file system.
type LinuxPath interface {
Path

// Stats this path using the fstatat(path, "", AT_EMPTY_PATH) syscall.
StatSelf() (*FileInfo, error)
}

var _ LinuxPath = (*PathFD)(nil)

// Stats this path using the fstatat(path, "", AT_EMPTY_PATH) syscall.
func (p *PathFD) StatSelf() (*FileInfo, error) {
return p.UnwrapDir().StatSelf()
}

var _ LinuxPath = (*DirFD)(nil)

// Stats this path using the fstatat(path, "", AT_EMPTY_PATH) syscall.
func (d *DirFD) StatSelf() (*FileInfo, error) {
return d.StatAt("", unix.AT_EMPTY_PATH)
}

// Opens the path with the given name.
// The path is opened relative to the receiver, using the openat2 syscall.
//
// Note that, in contrast to [os.Open] and [os.OpenFile], the returned
// descriptor is not put into non-blocking mode automatically. Callers may
// decide if they want this by setting the [syscall.O_NONBLOCK] flag.
//
// Available since Linux 5.6 (April 2020).
//
// https://www.man7.org/linux/man-pages/man2/openat2.2.html
// https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=fddb5d430ad9fa91b49b1d34d0202ffe2fa0e179
func (d *DirFD) Open2(name string, how unix.OpenHow) (*DirFDEntry, error) {
func (d *DirFD) Open2(name string, how unix.OpenHow) (*PathFD, error) {
var opened int
if err := openAt2Support.guard(func() error {
return syscallControl(d, func(fd uintptr) (err error) {
Expand All @@ -45,13 +73,13 @@ func (d *DirFD) Open2(name string, how unix.OpenHow) (*DirFDEntry, error) {
if err == nil {
return nil
}
return &os.PathError{Op: "openat2", Path: name, Err: err}
return pathErr("openat2", d, name, err)
})
}); err != nil {
return nil, err
}

return (*DirFDEntry)(os.NewFile(uintptr(opened), name)), nil
return (*PathFD)(os.NewFile(uintptr(opened), name)), nil
}

// Opens the directory with the given name by using the openat2 syscall.
Expand All @@ -60,7 +88,7 @@ func (d *DirFD) Open2(name string, how unix.OpenHow) (*DirFDEntry, error) {
func (d *DirFD) OpenDir2(name string, how unix.OpenHow) (*DirFD, error) {
how.Flags |= unix.O_DIRECTORY
f, err := d.Open2(name, how)
return f.AsDir(), err
return f.UnwrapDir(), err
}

var openAt2Support = runtimeSupport{test: func() error {
Expand All @@ -75,7 +103,9 @@ var openAt2Support = runtimeSupport{test: func() error {
return nil
}}

// Stats the path with the given name by using the statx syscall.
// Stats the path with the given name.
// The path is interpreted relative to the receiver, using the statx syscall.
//
// Available since Linux 4.11 (May 2017).
//
// https://www.man7.org/linux/man-pages/man2/statx.2.html
Expand All @@ -84,7 +114,7 @@ func (d *DirFD) StatX(name string, flags, mask int) (*FileInfoX, error) {
const requiredMask = unix.STATX_MODE | unix.STATX_TYPE | unix.STATX_SIZE | unix.STATX_MTIME

info := FileInfoX{Path: name}
err := d.RawStatX(name, flags, mask|requiredMask, &info.StatX)
err := d.NativeStatX(name, flags, mask|requiredMask, &info.StatX)
if err != nil {
return nil, err
}
Expand All @@ -99,11 +129,9 @@ func (d *DirFD) StatX(name string, flags, mask int) (*FileInfoX, error) {
return &info, nil
}

// Stats the path with the given name by using the statx syscall, returning the
// raw stats. Available since Linux 4.11 (May 2017).
//
// https://www.man7.org/linux/man-pages/man2/statx.2.html
func (d *DirFD) RawStatX(name string, flags, mask int, stat *StatX) error {
// Stats the path with the given name, just like [DirFD.StatX], but returning
// stats in the native format of the operating system.
func (d *DirFD) NativeStatX(name string, flags, mask int, stat *StatX) error {
return statxSupport.guard(func() error {
return syscallControl(d, func(fd uintptr) error {
err := unix.Statx(int(fd), name, flags, mask, (*unix.Statx_t)(stat))
Expand Down
47 changes: 47 additions & 0 deletions internal/os/unix/dirfd_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2024 k0s authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package unix_test

import (
"testing"

osunix "github.com/k0sproject/k0s/internal/os/unix"
"golang.org/x/sys/unix"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPathFD_StatSelf(t *testing.T) {
dirPath := t.TempDir()

p, err := osunix.OpenPath(dirPath, unix.O_PATH, 0)
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, p.Close()) })

// An O_PATH descriptor cannot read anything.
_, err = p.UnwrapDir().Readdirnames(1)
assert.ErrorIs(t, err, unix.EBADF)

// Verify that the fstatat syscall works for O_PATH file descriptors.
// It's not documented in the Linux man pages, just fstat is.
// See open(2).
stat, err := p.StatSelf()
if assert.NoError(t, err) {
assert.True(t, stat.IsDir())
}
}
Loading

0 comments on commit 97bc779

Please sign in to comment.