osfs

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 26 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package osfs 提供 XOps v2 句柄安全的三平台本地文件系统、旁路文件锁、Windows DACL/超长路径 与受控本地 AF_UNIX 套接字原语(公开契约 4,ADR-007 / INV-A1..A5)。

Index

Constants

View Source
const (
	WinAttrDirectory, WinAttrDevice, WinAttrReparsePoint                         uint32 = 0x10, 0x40, 0x400
	WinACEFlagInheritOnly, WinACETypeAllow, WinACETypeDeny                       uint8  = 0x08, 0x00, 0x01
	WinAccessWriteData, WinAccessAppendData, WinAccessWriteEA                    uint32 = 0x02, 0x04, 0x10
	WinAccessCreatePipeInstance                                                  uint32 = 0x04 // WinSDK FILE_CREATE_PIPE_INSTANCE(命名管道抢注权限)
	WinAccessDeleteChild                                                         uint32 = 0x40 // WinSDK winnt.h FILE_DELETE_CHILD(目录删除子项权限)
	WinAccessWriteAttrs, WinAccessDelete, WinAccessWriteDAC, WinAccessWriteOwner uint32 = 0x100, 0x10000, 0x40000, 0x80000
	WinAccessGenericAll, WinAccessGenericWrite                                   uint32 = 0x10000000, 0x40000000
)

Win32 DACL 与文件属性位常量(跨平台纯数学副本)。

View Source
const Win32LongPathThreshold = 248

Win32LongPathThreshold 是触发 Win32 扩展长路径前缀(\\?\ 与 \\?\UNC\)的 UTF-16 码元阈值(INV-A4)。

Variables

本包导出的规范化哨兵错误(供调用方以 errors.Is 精确判定)。

Functions

func AtomicReplaceWithSidecarLock

func AtomicReplaceWithSidecarLock(ctx context.Context, root ports.RootHandle, dest pathalg.LocalRootedDest, perm fs.FileMode, fn WriteFileFunc) error

AtomicReplaceWithSidecarLock 持有同目录旁路独占锁 <dest>.lock(绝不锁目标文件)并原子替换 dest(ADR-007); 完全通过 ports.RootHandle 接口委派,零具体类型断言,同时兼容真实 osfs、仿真器及包装适配器。

func DialWindowsNamedPipeVerified

func DialWindowsNamedPipeVerified(ctx context.Context, _ string) (net.Conn, error)

DialWindowsNamedPipeVerified 在非 Windows 平台上固定返回 ports.ErrUnsupported。

func EnsureDir

func EnsureDir(dir pathalg.LocalDir, perm fs.FileMode) error

EnsureDir 跨平台安全确保本地根目录 dir 存在且末段非符号链接: Unix 逐段使用 Mkdirat + O_DIRECTORY|O_NOFOLLOW 建目录(默认 0700);Windows 当 perm&0o077==0 时附带 Owner-Only DACL。

func EnsureOwnerOnlyDir

func EnsureOwnerOnlyDir(dir pathalg.LocalDir) error

EnsureOwnerOnlyDir 跨平台确保目录 dir 存在且仅所有者可访问(Unix 0700 + 非软链;Windows Owner-Only DACL)。

func EvaluateWindowsObjectACL

func EvaluateWindowsObjectACL(in WindowsACLInput) security.Verdict

EvaluateWindowsObjectACL 以失败关闭交半格语义评估 Windows 对象属性与 DACL 安全性(INV-A4 / INV-E1)。

func IsExtendedOrDeviceWindowsPath

func IsExtendedOrDeviceWindowsPath(path string) bool

IsExtendedOrDeviceWindowsPath 报告路径是否已携带 \\?\、\\.\ 或 \??\ Win32 设备/扩展前缀。

func MaxUnixSocketPathBytes

func MaxUnixSocketPathBytes() int

MaxUnixSocketPathBytes 返回当前平台 sockaddr_un.sun_path 最大路径字节数(不含结尾 NUL)。

func NormalizeWin32LongPath

func NormalizeWin32LongPath(p string) string

NormalizeWin32LongPath 在有效路径长度 >= 248 UTF-16 码元时补齐 \\?\ 或 \\?\UNC\ 前缀(INV-A4)。

func OpenRegular

OpenRegular / CreateExclusive / OpenLockedContext / AtomicReplace 为 ports.RootHandle 提供包级显式作用域原语(ADR-007)。

func RemoveLockedFileSafely

func RemoveLockedFileSafely(root ports.RootHandle, lockDest pathalg.LocalRootedDest) (bool, error)

RemoveLockedFileSafely 按三平台安全删锁协议清理已退役的单进程实例锁文件: 持独占锁截断清空刷盘 -> 解锁关句柄 -> Remove(忽略 ErrNotFound),消除 Windows 共享冲突。

func SidecarLockDest

func SidecarLockDest(dest pathalg.LocalRootedDest) (pathalg.LocalRootedDest, error)

SidecarLockDest 为目标文件构造同目录旁路锁路径 <name>.lock(ADR-007)。

func UTF16CodeUnitLen

func UTF16CodeUnitLen(s string) (n int)

UTF16CodeUnitLen 计算字符串在 UTF-16 编码下的精确码元数量(辅助平面码点计 2 码元)。

func WithSidecarLock

func WithSidecarLock(ctx context.Context, root ports.RootHandle, target pathalg.LocalRootedDest, mode ports.LockMode, perm fs.FileMode, fn LockedFileFunc) error

WithSidecarLock 锁定 target 同目录旁路锁文件 <target>.lock 并同步执行回调(ADR-007)。

func WithSocketSlot

func WithSocketSlot(ctx context.Context, path ControlSocketPath, opts SocketSlotOptions, fn func(slot *SocketSlot) error) (retErr error)

WithSocketSlot 在目录锁保护下独占绑定本地 AF_UNIX 套接字并在同步闭包内借出 SocketSlot; 退出时无条件按 INV-A3 固定顺序「加目录锁 -> 身份校验 -> unlink -> close -> 解锁」释放。

Types

type ControlSocketPath

type ControlSocketPath struct {
	// contains filtered or unexported fields
}

ControlSocketPath 表示经平台安全规则与 sun_path 上限校验的本地 AF_UNIX 套接字路径。 Unix 允许 OpenSSH ControlPath 含 ':'(如 cm-alice@host:22),Windows 除盘符前缀外严禁 ':'。

func BuildManagedMuxSocketPath

func BuildManagedMuxSocketPath(dir pathalg.LocalDir, percentC string) (ControlSocketPath, error)

BuildManagedMuxSocketPath 基于经校验的沙箱目录与 OpenSSH %C 40 位小写十六进制摘要构造自管套接字路径。

func ControlSocketPathFromSource

func ControlSocketPathFromSource(src pathalg.LocalSourcePath) (ControlSocketPath, error)

ControlSocketPathFromSource 将经 pathalg 校验的本地源路径转换为当前平台安全控制套接字路径; 兼容 Unix 下 OpenSSH %r@%h:%p 含 ':' 文件名,并在 Windows 上剥离 \\?\ 前缀后执行 sun_path 校验。

func ValidateControlSocketPath

func ValidateControlSocketPath(raw string) (ControlSocketPath, error)

ValidateControlSocketPath 按当前运行平台规则校验并构造不可变 ControlSocketPath。

func ValidateUnixControlSocketPath

func ValidateUnixControlSocketPath(raw string, maxBytes int) (ControlSocketPath, error)

ValidateUnixControlSocketPath 按 Unix 规则校验套接字路径:允许文件名含 ':',拒绝 '..'、尾随分隔符/'.' 与超长 sun_path。

func ValidateWindowsControlSocketPath

func ValidateWindowsControlSocketPath(raw string, maxBytes int) (ControlSocketPath, error)

ValidateWindowsControlSocketPath 按 Windows/NTFS 规则严格校验套接字路径:除盘符前缀外严禁 ':'、前导分隔符与尾随分隔符/'.'。

func (ControlSocketPath) Dir

func (p ControlSocketPath) Dir() string

func (ControlSocketPath) Equal

func (ControlSocketPath) IsValid

func (p ControlSocketPath) IsValid() bool

func (ControlSocketPath) IsZero

func (p ControlSocketPath) IsZero() bool

func (ControlSocketPath) Name

func (p ControlSocketPath) Name() string

func (ControlSocketPath) NativePath

func (p ControlSocketPath) NativePath() string

func (ControlSocketPath) String

func (p ControlSocketPath) String() string

type LockedFileFunc

type LockedFileFunc func(f ports.LockedFile) error

type OpenRegularFunc

type OpenRegularFunc func(r ports.BoundedReadHandle, size int64) error

OpenRegularFunc / WriteFileFunc / LockedFileFunc 定义包级作用域闭包原语的借用回调契约。

type OperatorFS

type OperatorFS struct {
	// contains filtered or unexported fields
}

OperatorFS 是面向 CLI/Playbook DirectOperatorProfile 的人工显式本地只读源适配器, 仅实现 ports.OperatorReadFS(物理上绝不注入 mcpruntime,零值失败关闭)。

func NewOperatorReadFS

func NewOperatorReadFS() *OperatorFS

NewOperatorReadFS 构造可用的操作员只读源文件适配器实例(ports.OperatorReadFS 生产实现)。

func (*OperatorFS) WithOperatorRead

func (o *OperatorFS) WithOperatorRead(ctx context.Context, path pathalg.LocalSourcePath, policy pathalg.SymlinkPolicy, fn func(r ports.BoundedReadHandle, size int64) error) error

WithOperatorRead 按 policy 管控符号链接行为打开只读常规文件,并在同步闭包内借出随机只读句柄与字节长度; 回调退出后立即使借出句柄失效并关闭底层文件句柄。

type ParsedWindowsACE

type ParsedWindowsACE struct {
	ACEType, ACEFlags    uint8
	Mask                 uint32
	SIDValid, SIDTrusted bool
}

ParsedWindowsACE 表示从 Windows DACL 解码出的单条 ACE 纯数学视图。

type PeerCred

type PeerCred struct {
	// contains filtered or unexported fields
}

PeerCred 表示经内核查询到的本地 AF_UNIX 连接对端身份凭据快照;零值恒无效。

func DialUnixVerified

func DialUnixVerified(ctx context.Context, path ControlSocketPath) (*net.UnixConn, PeerCred, error)

DialUnixVerified 连接至经安全核验的本地 AF_UNIX 控制套接字并强制核验对端用户身份(fail-closed)。

func QueryUnixPeerCred

func QueryUnixPeerCred(c *net.UnixConn) (PeerCred, error)

QueryUnixPeerCred / VerifyUnixPeerCurrentUser 通过内核套接字选项查询或强校验对端身份(fail-closed)。

func VerifyUnixPeerCurrentUser

func VerifyUnixPeerCurrentUser(c *net.UnixConn) (PeerCred, error)

func (PeerCred) Equal

func (c PeerCred) Equal(o PeerCred) bool

func (PeerCred) GID

func (c PeerCred) GID() (uint32, bool)

func (PeerCred) HasGID

func (c PeerCred) HasGID() bool

func (PeerCred) HasPID

func (c PeerCred) HasPID() bool

func (PeerCred) HasUID

func (c PeerCred) HasUID() bool

func (PeerCred) IsValid

func (c PeerCred) IsValid() bool

func (PeerCred) IsZero

func (c PeerCred) IsZero() bool

func (PeerCred) PID

func (c PeerCred) PID() (uint32, bool)

func (PeerCred) UID

func (c PeerCred) UID() (uint32, bool)

type ScopedFS

type ScopedFS struct {
	// contains filtered or unexported fields
}

ScopedFS 是基于 Go *os.Root 的沙箱本地文件系统适配器,仅实现 ports.ScopedRootFS(零值失败关闭)。

func NewScopedRootFS

func NewScopedRootFS() *ScopedFS

NewScopedRootFS 构造可用的沙箱本地文件系统实例(ports.ScopedRootFS 生产实现)。

func (*ScopedFS) EnsureRoot

func (s *ScopedFS) EnsureRoot(dir pathalg.LocalDir, perm fs.FileMode) error

EnsureRoot 实现 ports.ScopedRootFS.EnsureRoot,在 WithRoot 前安全引导沙箱根目录存在。

func (*ScopedFS) WithRoot

func (s *ScopedFS) WithRoot(dir pathalg.LocalDir, fn func(root ports.RootHandle) error) (retErr error)

WithRoot 打开绑定至 dir 的 *os.Root 沙箱句柄并在同步闭包 fn 内借出;退出前无条件失效并关闭。

type SocketProbeStatus

type SocketProbeStatus uint8

SocketProbeStatus 描述 ProbeStaleUnixSocket 的探活分类结果;零值 SocketProbeUnknown 严格失败关闭。

const (
	SocketProbeUnknown   SocketProbeStatus = iota // 探测未完成或异常(零值失败关闭)
	SocketProbeMissing                            // 目标路径不存在
	SocketProbeNotSocket                          // 存在但非本地 AF_UNIX 套接字
	SocketProbeLive                               // 存在且活跃监听进程响应连接
	SocketProbeStale                              // 确认为套接字且连接被明确拒绝(可安全清理)
)

func ProbeStaleUnixSocket

func ProbeStaleUnixSocket(ctx context.Context, path ControlSocketPath) (SocketProbeStatus, error)

ProbeStaleUnixSocket 对本地 AF_UNIX 套接字执行 fail-closed 存活/陈旧探测。

func PruneStaleUnixSocket

func PruneStaleUnixSocket(ctx context.Context, path ControlSocketPath) (SocketProbeStatus, error)

PruneStaleUnixSocket 在目录锁内复核并安全清理已确证过期的本地 AF_UNIX 套接字;父目录缺失时零副作用返回 Missing。

func (SocketProbeStatus) IsValid

func (s SocketProbeStatus) IsValid() bool

IsValid 报告 s 是否为已判明的有效探测状态(零值 SocketProbeUnknown 返回 false)。

func (SocketProbeStatus) String

func (s SocketProbeStatus) String() string

String 返回 SocketProbeStatus 的可读名称。

type SocketSlot

type SocketSlot struct {
	// contains filtered or unexported fields
}

SocketSlot 表示 WithSocketSlot 在同步作用域内借出的独占本地 AF_UNIX 监听槽位(INV-A3)。

func (*SocketSlot) AcceptVerified

func (s *SocketSlot) AcceptVerified() (*net.UnixConn, PeerCred, error)

AcceptVerified 接受本地 AF_UNIX 连接并强制核验对端用户身份(失败立即关连并返回 ErrUnauthorizedPeer)。

func (*SocketSlot) IsActive

func (s *SocketSlot) IsActive() bool

func (*SocketSlot) Listener

func (s *SocketSlot) Listener() net.Listener

func (*SocketSlot) Path

func (s *SocketSlot) Path() ControlSocketPath

type SocketSlotOptions

type SocketSlotOptions struct {
	ReplaceStale bool // 在目录锁内确证为 SocketProbeStale 后允许清理接管
}

SocketSlotOptions 控制 WithSocketSlot 绑定本地 AF_UNIX 套接字时的冲突处置策略。

type WindowsACLInput

type WindowsACLInput struct {
	Mode                                           WindowsACLMode
	Attrs                                          uint32
	ExpectDir, AllowReparse, OwnerTrusted, HasDACL bool
	ACEs                                           []ParsedWindowsACE
}

WindowsACLInput 封装待评估 Windows 对象的属性与解析后 DACL 信息。

type WindowsACLMode

type WindowsACLMode uint8

WindowsACLMode 区分 Windows DACL 安全评估策略;零值 WindowsACLInvalid 严格失败关闭。

const (
	WindowsACLInvalid         WindowsACLMode = iota // 无效评估模式(零值失败关闭)
	WindowsACLOwnerOnly                             // 仅受信任所有者/系统主体可访问
	WindowsACLWriteRestricted                       // 严禁非受信任主体具备写/删/改权位
	WindowsACLPipeServerSafe                        // 命名管道服务端:阻断非受信任主体抢注实例/改权限,放行合法双工 FILE_WRITE_DATA
)

type WriteFileFunc

type WriteFileFunc func(w io.Writer) error

Directories

Path Synopsis
internal
winpipe
Package winpipe 为 core/osfs 提供私有 Windows 命名管道全双工重叠 I/O 连接封装(ADR-007 / P4-T4)。
Package winpipe 为 core/osfs 提供私有 Windows 命名管道全双工重叠 I/O 连接封装(ADR-007 / P4-T4)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL