Debian Codegraph @

pathrs-lite/internal/fd/mount_linux.go

raw
1 // SPDX-License-Identifier: MPL-2.0
2
3 //go:build linux
4
5 // Copyright (C) 2024-2025 Aleksa Sarai <[email protected]>
6 // Copyright (C) 2024-2025 SUSE LLC
7 //
8 // This Source Code Form is subject to the terms of the Mozilla Public
9 // License, v. 2.0. If a copy of the MPL was not distributed with this
10 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
11
12 package fd
13
14 import (
15 "os"
16 "runtime"
17
18 "golang.org/x/sys/unix"
19 )
20
21 // Fsopen is an [Fd]-based wrapper around unix.Fsopen.
22 func Fsopen(fsName string, flags int) (*os.File, error) {
23 // Make sure we always set O_CLOEXEC.
24 flags |= unix.FSOPEN_CLOEXEC
25 fd, err := unix.Fsopen(fsName, flags)
26 if err != nil {
27 return nil, os.NewSyscallError("fsopen "+fsName, err)
28 }
29 return os.NewFile(uintptr(fd), "fscontext:"+fsName), nil
30 }
31
32 // Fsmount is an [Fd]-based wrapper around unix.Fsmount.
33 func Fsmount(ctx Fd, flags, mountAttrs int) (*os.File, error) {
34 // Make sure we always set O_CLOEXEC.
35 flags |= unix.FSMOUNT_CLOEXEC
36 fd, err := unix.Fsmount(int(ctx.Fd()), flags, mountAttrs)
37 if err != nil {
38 return nil, os.NewSyscallError("fsmount "+ctx.Name(), err)
39 }
40 return os.NewFile(uintptr(fd), "fsmount:"+ctx.Name()), nil
41 }
42
43 // OpenTree is an [Fd]-based wrapper around unix.OpenTree.
44 func OpenTree(dir Fd, path string, flags uint) (*os.File, error) {
45 dirFd, fullPath := prepareAt(dir, path)
46 // Make sure we always set O_CLOEXEC.
47 flags |= unix.OPEN_TREE_CLOEXEC
48 fd, err := unix.OpenTree(dirFd, path, flags)
49 if err != nil {
50 return nil, &os.PathError{Op: "open_tree", Path: fullPath, Err: err}
51 }
52 runtime.KeepAlive(dir)
53 return os.NewFile(uintptr(fd), fullPath), nil
54 }