Documentation
¶
Overview ¶
Package osfsutil 提供 core/osfs 使用的平台无关纯辅助、错误归一化、路径与目录项校验工具。 本包属于 core/osfs 四层最小特权隔离架构中的零特权辅助层(LayerCoreOSFSInternal), 严禁持有裸 OS 句柄、文件锁、Windows ACL 系统调用或 net 原语(受控原语分别严格收口于 core/osfs、winpipe 与 unixipc)。
Index ¶
- Constants
- func AtomicReplaceWithSidecarLock(ctx context.Context, root ports.RootHandle, dest pathalg.LocalRootedDest, ...) error
- func BuildAppendFrame(line []byte) ([]byte, error)
- func CheckAFUnixReparseAttrs(path string, attrs, reparseTag, expectedTag uint32, errNotSocket error) error
- func CheckChtimesLstatInfo(rel string, lst fs.FileInfo) error
- func CheckLockArgs(activeDestErr error, mode ports.LockMode, fnNil bool) error
- func CheckLstatInfo(rel string, lst fs.FileInfo, wantDir bool) error
- func CheckOperatorReadArgs(ctx context.Context, valid bool, path pathalg.LocalSourcePath, ...) error
- func CheckPrepareSocketStatus(nativePath string, status uint8, replaceStale bool, ...) error
- func CheckReadSizeLimit(dest string, size, maxBytes int64) error
- func CheckRootCB(root ports.RootHandle, fnNil bool) error
- func CheckSocketDialTarget(nativePath string, status uint8, inspectErr, errNotSocket error) error
- func CheckSocketSlotEntry(ctx context.Context, pathValid, fnNil bool, errInvalid error) error
- func CheckSocketWithinRoot(within bool, err error, target string) error
- func CheckStaleProbeOutcome(nativePath string, probed uint8, pErr, errSlotOccupied error) error
- func CheckUnixPrivateOwnerAndMode(uid uint32, euid int, mode fs.FileMode, errInsecure error) error
- func CheckWin32DirAttrs(path string, attrs uint32, errInsecure error) error
- func CheckWin32HandleAttrs(path string, attrs uint32, expectDir, rejectReparse bool) error
- func CollectDirEntries(raw []fs.DirEntry) ([]ports.LocalDirEntry, error)
- func CollectMissingDirChain(target string, checkExisting func(cur string) (exists bool, err error), ...) (string, []string, error)
- func CopyChunkAt(w io.WriteSeeker, offset int64, r io.Reader, length int64) (int64, error)
- func EffectiveSocketFilePerm(filePerm fs.FileMode) fs.FileMode
- func ErrField(s error, f string) error
- func ErrFieldNil(s error, f string) error
- func ErrNullDeviceTypeMismatch(sentinel error, expected string, actual uint64) error
- func ErrSocketPathTooLong(errTooLong error, size, limit int) error
- func ErrStageDirAccessDenied(errInsecure error, stageDir string) error
- func ErrWrongKind(p string, wantDir bool) error
- func EvaluateSingleWindowsACE(mode uint8, expectDir bool, aceType, aceFlags uint8, mask uint32, ...) security.Verdict
- func FileInfoToLocalFileInfo(name string, fi fs.FileInfo) (ports.LocalFileInfo, error)
- func HasForbiddenControlPathRune(s string) bool
- func HasInvalidSocketTail(p string) bool
- func IsASCIIAlphaByte(b byte) bool
- func IsActiveHandle(active *atomic.Bool, nonNil bool) bool
- func IsAlreadyNormalizedError(err error) bool
- func IsExtendedOrDeviceWindowsPath(path string) bool
- func IsLowerHex40(s string) bool
- func IsPhysicalPathContained(physTarget, physRoot string, caseInsensitive bool) bool
- func IsRawPathWithinRoot(targetPath, rootPath string, absFn func(string) (string, error), ...) (bool, error)
- func IsSafeWin32ObjectAttrs(attrs uint32, expectDir, allowReparse bool) bool
- func IsUnmappableWin32LinkTarget(raw string) bool
- func IsValidRFC3339Time(t time.Time) bool
- func IsValidWindowsACLHeader(mode uint8, attrs uint32, expectDir, allowReparse, ownerTrusted, hasDACL bool) bool
- func IsValidWindowsLeaf(leaf string) bool
- func IsWin32Sep(b byte) bool
- func JoinAbsPhysicalRaw(raw string, cwdFn func() (string, error), isWindows bool) (string, error)
- func JoinManagedMuxPath(nativeDir, percentC string) string
- func MakeTempDestInSameDir(dest pathalg.LocalRootedDest) (pathalg.LocalRootedDest, error)
- func MapErrTable(path string, err error, tbl []ErrMapPair) error
- func MapOSError(err error) error
- func MapProbeEOF(n int, err error) error
- func MapSocketLinkErr(target string, err error, isExist bool, errSlotOccupied error) error
- func NeedsDualParentSync(oldDest, newDest pathalg.LocalRootedDest) bool
- func NormalizeWin32LongPathWithResolver(path string, fullPath func(string) (string, error), ...) string
- func NormalizeWindowsNamedPipePath(raw string) (string, error)
- func OrZero[T any](ok bool, v T) T
- func ParseUnixControlSocketPath(raw string, maxBytes int, errInvalid, errTooLong error) (dir, name, native string, err error)
- func ParseWindowsControlSocketPath(raw string, maxBytes int, errInvalid, errTooLong error) (dir, name, native string, err error)
- func PickIf[T any](ok bool, yes, no T) T
- func PrepareSocketSlot[ID any](nativePath string, replaceStale bool, inspect func() (ID, uint8, error), ...) error
- func ProbeSocketState[ID any](ctx context.Context, defaultTimeout time.Duration, ...) (uint8, error)
- func PruneStaleSocketState[ID any](ctx context.Context, dir string, inspect func() (ID, uint8, error), ...) (uint8, error)
- func RandomHexToken(n int) (string, error)
- func ReadAllBoundedZeroing(r io.Reader, sizeHint, maxBytes int64, dest string) ([]byte, error)
- func ReadAtBounded(r ports.BoundedReadHandle, p []byte, off, maxBytes int64, limited bool, ...) (int, error)
- func ReadBounded(r ports.BoundedReadHandle, p []byte, remaining *int64, limited bool, ...) (int, error)
- func RelSegmentsUnderRoot(rootDir, targetDir string, isWindows bool, escapeErr error) ([]string, error)
- func ReleaseSocketSlot(withDirLock func(context.Context, string, bool, func() error) error, ...) error
- func RemoveLockedFileSafely(root ports.RootHandle, lockDest pathalg.LocalRootedDest) (bool, error)
- func ResolvePhysicalPathWith(absPath string, ops PhysicalPathOps) (string, error)
- func ResolveRawPhysicalPath(raw string, absFn func(string) (string, error), ops PhysicalPathOps) (string, error)
- func ResolveSocketSlotConfig(filePerm fs.FileMode, peerPolicy uint8, ...) (fs.FileMode, uint8, string, error)
- func ResolveSocketSlotOptions(perm fs.FileMode, policy uint8, errInsecure error) (fs.FileMode, uint8, error)
- func RetryLockBackoff(ctx context.Context, tryFn func() (bool, error)) error
- func SanitizeFilePerm(p, fb fs.FileMode) fs.FileMode
- func ShouldRejectOperatorSymlink(policy pathalg.SymlinkPolicy) (bool, error)
- func SiblingDest(dest pathalg.LocalRootedDest, name string) (pathalg.LocalRootedDest, error)
- func SidecarLockDest(dest pathalg.LocalRootedDest) (pathalg.LocalRootedDest, error)
- func SocketPeerPolicyString(policy uint8) string
- func SocketProbeStatusString(status uint8) string
- func StripWin32ExtendedPrefix(p string) string
- func StripWin32LongPathPrefix(p string) string
- func UTF16CodeUnitLen(s string) (n int)
- func ValidateChtimesArgs(atime, mtime time.Time) error
- func ValidateConfineRootDir(dir pathalg.LocalDir, errInvalid error) error
- func ValidateCreateSymlinkTarget(link pathalg.LocalRootedDest, target string) error
- func ValidateRawPhysicalPath(raw string, isWindows bool) error
- func ValidateSocketCtxPath(ctx context.Context, pathValid bool, errInvalid error) error
- func ValidateSocketPeerUID(valid, hasUID bool, actual, expected uint32, errUnauthorized error) error
- func ValidateTruncateSize(size int64) error
- func ValidateWriteChunkArgs(r io.Reader, offset, length int64, perm fs.FileMode) error
- func WithSidecarLock(ctx context.Context, root ports.RootHandle, target pathalg.LocalRootedDest, ...) error
- func WrapOp(s, c error, op string) error
- func WrapOpenTarget(s, c error, tgt string) error
- func WrapS(s, c error) error
- func WrapSyscallErr(err error, call string) error
- func WrapWinACLError(sentinel, cause, errInsecure error, path string) error
- func WriteAndSyncClose(w SyncCloseWriter, frame []byte) error
- func WriteBorrowed(w io.Writer, p []byte, active *atomic.Bool) (int, error)
- func WriteChunkSyncClose(f SyncCloseWriteSeeker, offset int64, r io.Reader, length int64) (int64, error)
- func Zero[T any]() (z T)
- type BorrowedReader
- type BorrowedWriter
- type ErrMapPair
- type LockedFile
- type LockedFileFunc
- type PhysicalNodeProbe
- type PhysicalPathOps
- type SyncCloseWriteSeeker
- type SyncCloseWriter
- type SyncTruncateStatFile
- type WriteFileFunc
Constants ¶
const ( DefaultFilePerm fs.FileMode = 0o600 DefaultDirPerm fs.FileMode = 0o700 )
DefaultFilePerm 与 DefaultDirPerm 为跨平台默认文件与目录权限掩码。
const ( SocketProbeUnknownCode uint8 = iota SocketProbeMissingCode SocketProbeNotSocketCode SocketProbeLiveCode SocketProbeStaleCode SocketProbeMinValidCode = SocketProbeMissingCode SocketProbeMaxValidCode = SocketProbeStaleCode )
SocketProbeStatus 底层连续编码与有效边界常量,由父包 osfs.SocketProbeStatus 单源派生。
const ( SocketPeerPolicyUnsetCode uint8 = iota // 零值默认,强制归一为最严 SocketPeerOwnerOnly SocketPeerOwnerOnlyCode // 强制内核级同 UID(Unix)/ 同 SID(Windows)对端校验 SocketPeerFilesystemPermCode // 显式放权:委托内核 VFS 文件权限(仅 Unix 非属主掩码 StreamLocal) SocketPeerPolicyMaxKnownCode = SocketPeerFilesystemPermCode )
SocketPeerPolicy 底层连续编码与有效边界常量,由父包 osfs.SocketPeerPolicy 单源派生。
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
WinAccessDeleteChild uint32 = 0x40
WinAccessWriteAttrs, WinAccessDelete, WinAccessWriteDAC, WinAccessWriteOwner uint32 = 0x100, 0x10000, 0x40000, 0x80000
WinAccessGenericAll, WinAccessGenericWrite uint32 = 0x10000000, 0x40000000
)
Win32 DACL 与文件属性位常量(跨平台纯数学副本)。
const ( ACLModeInvalid uint8 = iota ACLModeOwnerOnly ACLModeWriteRestricted ACLModePipeServerSafe ACLModeMinValid = ACLModeOwnerOnly ACLModeMaxValid = ACLModePipeServerSafe )
WindowsACLMode 底层连续编码与有效边界常量,由父包 osfs.WindowsACLMode 单源派生。
const MaxSymlinkHops = 40
MaxSymlinkHops 定义逐分量解析物理路径时允许展开的符号链接/重解析点最大跳数(对齐 POSIX SYMLOOP_MAX / Linux MAXSYMLINKS)。
const Win32LongPathThreshold = 248
Win32LongPathThreshold 是触发 Win32 扩展长路径前缀(\\?\ 与 \\?\UNC\)的 UTF-16 码元阈值(INV-A4)。
Variables ¶
This section is empty.
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)。
func BuildAppendFrame ¶
BuildAppendFrame 校验追加行不含内嵌 '\r'/'\n' 并复制追加结尾 '\n'。
func CheckAFUnixReparseAttrs ¶
func CheckAFUnixReparseAttrs(path string, attrs, reparseTag, expectedTag uint32, errNotSocket error) error
CheckAFUnixReparseAttrs 校验 Windows 对象句柄为非目录的 AF_UNIX 重解析点。
func CheckChtimesLstatInfo ¶
CheckChtimesLstatInfo 校验 Chtimes 目标非符号链接且为常规文件或目录。
func CheckLockArgs ¶
CheckLockArgs 校验文件加锁操作的目标路径活性、回调与锁模式。
func CheckLstatInfo ¶
CheckLstatInfo 校验已获取的 Lstat 元信息非符号链接且与期望对象类型一致。
func CheckOperatorReadArgs ¶
func CheckOperatorReadArgs(ctx context.Context, valid bool, path pathalg.LocalSourcePath, policy pathalg.SymlinkPolicy, fnNil bool) error
CheckOperatorReadArgs 校验 WithOperatorRead 的上下文、回调、有效性与路径/软链策略。
func CheckPrepareSocketStatus ¶
func CheckPrepareSocketStatus(nativePath string, status uint8, replaceStale bool, errNotSocket, errSlotOccupied error) error
CheckPrepareSocketStatus 判定已存在路径在绑定套接字槽位前的冲突处置状态。
func CheckReadSizeLimit ¶
CheckReadSizeLimit 校验文件大小不超过 ReadLimits 上限。
func CheckRootCB ¶
func CheckRootCB(root ports.RootHandle, fnNil bool) error
CheckRootCB 校验包级原语传入的 RootHandle 与回调函数均非 nil。
func CheckSocketDialTarget ¶
CheckSocketDialTarget 核验拨号前对本地 AF_UNIX 目标路径的探测分类,缺失或非套接字一律失败关闭。
func CheckSocketSlotEntry ¶
CheckSocketSlotEntry 统一校验 WithSocketSlot 入口的回调非空、上下文与控制路径有效性。
func CheckSocketWithinRoot ¶
CheckSocketWithinRoot 核验物理路径包含判定结果,越界时 fail-closed 返回同时匹配 ErrPermissionDenied 与 ErrSymlinkRejected 的错误。
func CheckStaleProbeOutcome ¶
CheckStaleProbeOutcome 核验陈旧套接字探活结论;上下文取消原样返回,非 Stale 失败关闭返回 ErrSlotOccupied。
func CheckUnixPrivateOwnerAndMode ¶
CheckUnixPrivateOwnerAndMode 校验已打开 Unix 私密文件的属主 UID 与非属主权限位(mode & 0o077 == 0)。
func CheckWin32DirAttrs ¶
CheckWin32DirAttrs 校验 Windows 目录句柄属性非重解析点且为目录。
func CheckWin32HandleAttrs ¶
CheckWin32HandleAttrs 校验 Windows 句柄文件属性是否满足重解析点、非设备及目录/文件类型约束。
func CollectDirEntries ¶
func CollectDirEntries(raw []fs.DirEntry) ([]ports.LocalDirEntry, error)
CollectDirEntries 将目录项切片校验并按名称升序排序,自动跳过并发消失条目。
func CollectMissingDirChain ¶
func CollectMissingDirChain( target string, checkExisting func(cur string) (exists bool, err error), splitParent func(cur string) (parent, item string), onRootMissing func(cur string) error, ) (string, []string, error)
CollectMissingDirChain 沿父目录逐级向上探测直至遇到已存在目录,返回基准祖先目录与待创建子路径/分量序列。
func CopyChunkAt ¶
CopyChunkAt 在 offset 处向目标可寻址文件写入精确 length 字节,遇流提前结束返回 io.ErrUnexpectedEOF。
func EffectiveSocketFilePerm ¶
EffectiveSocketFilePerm 返回归一后的有效本地套接字权限位(零值回退 0o600,含非法特殊权限位返回 0)。
func ErrNullDeviceTypeMismatch ¶
ErrNullDeviceTypeMismatch 构造平台空设备非字符设备类型校验失败的结构化错误。
func ErrSocketPathTooLong ¶
ErrSocketPathTooLong 构造本地套接字路径超出 sun_path 上限的结构化错误。
func ErrStageDirAccessDenied ¶
ErrStageDirAccessDenied 构造私有暂存目录属主/权限核验失败错误。
func EvaluateSingleWindowsACE ¶
func EvaluateSingleWindowsACE(mode uint8, expectDir bool, aceType, aceFlags uint8, mask uint32, sidValid, sidTrusted bool) security.Verdict
EvaluateSingleWindowsACE 以失败关闭交半格语义评估单条 Windows DACL ACE 安全性。
func FileInfoToLocalFileInfo ¶
FileInfoToLocalFileInfo 将 fs.FileInfo 转换为不可变 ports.LocalFileInfo。
func HasForbiddenControlPathRune ¶
HasForbiddenControlPathRune 检测路径中是否含非法 UTF-8、控制字符、行/段分隔符或 Bidi/Cf 格式字符。
func HasInvalidSocketTail ¶
HasInvalidSocketTail 报告套接字路径末尾是否为空、'.'、'..' 或尾随斜杠。
func IsActiveHandle ¶
IsActiveHandle 判断受借用句柄的原子活性哨兵是否处于有效态。
func IsAlreadyNormalizedError ¶
IsAlreadyNormalizedError 报告 err 是否已携带规范 ports 或 context 哨兵错误。
func IsExtendedOrDeviceWindowsPath ¶
IsExtendedOrDeviceWindowsPath 报告路径是否已携带 \\?\、\\.\ 或 \??\ Win32 设备/扩展前缀。
func IsLowerHex40 ¶
IsLowerHex40 报告 s 是否为 40 位小写十六进制字符串(OpenSSH %C 摘要格式)。
func IsPhysicalPathContained ¶
IsPhysicalPathContained 判定已规范物理路径 physTarget 是否等于或位于物理根目录 physRoot 内(严格段边界匹配); 正确处理 "/"、"c:/" 及 UNC 根,并在 caseInsensitive == true(windows || darwin)时折叠大小写。
func IsRawPathWithinRoot ¶
func IsRawPathWithinRoot(targetPath, rootPath string, absFn func(string) (string, error), ops PhysicalPathOps, caseInsensitive bool) (bool, error)
IsRawPathWithinRoot 对目标路径与根路径分别执行抗悬空软链物理展开,再按段边界判定包含关系。
func IsSafeWin32ObjectAttrs ¶
IsSafeWin32ObjectAttrs 校验 Windows 对象属性非设备、符合期望目录/文件类型及重解析点策略。
func IsUnmappableWin32LinkTarget ¶
IsUnmappableWin32LinkTarget 报告剥离 \\?\ / \??\ 前缀并小写化后的 Windows 重解析目标 是否为空或无法词法映射的 NT 对象命名空间目标(Volume{GUID}、GLOBALROOT\、\Device\、Device\、\\.\ 等)。
func IsValidRFC3339Time ¶
IsValidRFC3339Time 校验时间戳非零且 UTC 年份落在 [1, 9999] 内。
func IsValidWindowsACLHeader ¶
func IsValidWindowsACLHeader(mode uint8, attrs uint32, expectDir, allowReparse, ownerTrusted, hasDACL bool) bool
IsValidWindowsACLHeader 校验 Windows 对象 ACL 评估前置头条件(模式、属主、DACL 存在性与对象属性)。
func IsValidWindowsLeaf ¶
IsValidWindowsLeaf 校验单层 Windows 叶文件名不含路径分隔符、冒号或父目录穿越段。
func JoinAbsPhysicalRaw ¶
JoinAbsPhysicalRaw 在不执行词法 Clean 的前提下将原始路径归一为绝对路径(保留符号链接后的 "." / ".." 交由逐段展开处理): 绝对路径(含 Windows 扩展/设备前缀 \\?\ / \??\ / \\.\、盘符根路径与 UNC 路径)原样返回; Windows 单分隔符根相对路径(如 \a\..\b)拼接至 cwd 卷前缀;其余相对路径直接以原生分隔符拼接至 cwd。
func JoinManagedMuxPath ¶
JoinManagedMuxPath 在目录 nativeDir 下按平台分隔符拼接自管控制套接字路径 cm-<percentC>.sock。
func MakeTempDestInSameDir ¶
func MakeTempDestInSameDir(dest pathalg.LocalRootedDest) (pathalg.LocalRootedDest, error)
MakeTempDestInSameDir 在 dest 同级目录下构造带随机后缀的临时文件路径。
func MapErrTable ¶
func MapErrTable(path string, err error, tbl []ErrMapPair) error
MapErrTable 按错误映射表将 err 映射为带路径上下文的 ports 哨兵错误,未命中返回 nil。
func MapOSError ¶
MapOSError 将标准库 OS/FS 错误归一化包装为 ports 规范哨兵错误并保留底层错误链。
func MapProbeEOF ¶
MapProbeEOF 将上限边界外 1 字节探测结果映射为 ErrLimitExceeded、底层错误或 io.EOF。
func MapSocketLinkErr ¶
MapSocketLinkErr 映射原子 linkat 发布套接字时的冲突或 OS 错误。
func NeedsDualParentSync ¶
func NeedsDualParentSync(oldDest, newDest pathalg.LocalRootedDest) bool
NeedsDualParentSync 判断重命名源与目标是否位于不同父目录从而需要双向同步父目录。
func NormalizeWin32LongPathWithResolver ¶
func NormalizeWin32LongPathWithResolver(path string, fullPath func(string) (string, error), getwd func() (string, error)) string
NormalizeWin32LongPathWithResolver 在有效路径长度 >= 248 UTF-16 码元时补齐 \\?\ 或 \\?\UNC\ 前缀(INV-A4)。
func NormalizeWindowsNamedPipePath ¶
NormalizeWindowsNamedPipePath 校验并归一化 Windows 本地命名管道路径(\\.\pipe\<name>)。
func ParseUnixControlSocketPath ¶
func ParseUnixControlSocketPath(raw string, maxBytes int, errInvalid, errTooLong error) (dir, name, native string, err error)
ParseUnixControlSocketPath 按 Unix 规则解析并校验套接字路径:允许文件名含 ':',拒绝 '..' 与超长 sun_path。
func ParseWindowsControlSocketPath ¶
func ParseWindowsControlSocketPath(raw string, maxBytes int, errInvalid, errTooLong error) (dir, name, native string, err error)
ParseWindowsControlSocketPath 按 Windows/NTFS 规则解析并校验套接字路径:除盘符前缀外严禁 ':' 与非法分量。
func PrepareSocketSlot ¶
func PrepareSocketSlot[ID any]( nativePath string, replaceStale bool, inspect func() (ID, uint8, error), probe func() (uint8, error), unlink func(ID) error, errNotSocket, errSlotOccupied error, ) error
PrepareSocketSlot 在目录锁内校验目标路径已有条目并按 replaceStale 策略探活清理陈旧套接字(零 OS 原语依赖)。
func ProbeSocketState ¶
func ProbeSocketState[ID any]( ctx context.Context, defaultTimeout time.Duration, inspect func() (ID, uint8, error), dial func(context.Context) error, isConnRefused func(error) bool, sameID func(ID, ID) bool, ) (uint8, error)
ProbeSocketState 编排本地 AF_UNIX 套接字 fail-closed 存活/陈旧探活状态机(前后身份复核防 TOCTOU)。
func PruneStaleSocketState ¶
func PruneStaleSocketState[ID any]( ctx context.Context, dir string, inspect func() (ID, uint8, error), withDirLock func(context.Context, string, bool, func() error) error, probe func() (uint8, error), unlink func(ID) error, ) (uint8, error)
PruneStaleSocketState 在目录锁内复核并安全清理陈旧本地 AF_UNIX 套接字,父目录缺失时零副作用返回 Missing。
func RandomHexToken ¶
RandomHexToken 生成 n 字节随机数据的 2n 位小写十六进制令牌。
func ReadAllBoundedZeroing ¶
ReadAllBoundedZeroing 在 maxBytes 字节上限内读取私密流 r 的全部内容; 在切片扩容、超限或发生任意读取错误时均对所有中间缓冲执行 clear() 零化,防止机密字节残余堆内存。
func ReadAtBounded ¶
func ReadAtBounded(r ports.BoundedReadHandle, p []byte, off, maxBytes int64, limited bool, active *atomic.Bool) (int, error)
ReadAtBounded 在作用域活性与字节上限约束下执行并发安全随机偏移读。
func ReadBounded ¶
func ReadBounded(r ports.BoundedReadHandle, p []byte, remaining *int64, limited bool, active *atomic.Bool) (int, error)
ReadBounded 在作用域活性与字节上限约束下执行顺序有界读取,越限探测返回 ports.ErrLimitExceeded。
func RelSegmentsUnderRoot ¶
func RelSegmentsUnderRoot(rootDir, targetDir string, isWindows bool, escapeErr error) ([]string, error)
RelSegmentsUnderRoot 计算 targetDir 相对于非根绝对沙箱目录 rootDir 的逐段相对路径分量(无 OS/filepath 依赖):
- 当 targetDir 等于 rootDir 时返回 (nil, nil);
- 当任一方非绝对、为文件系统根、含 "."/".." 分量或 targetDir 逃逸出 rootDir 时,返回包装 escapeErr 的错误;
- Windows 模式按大小写不敏感比对盘符与各段,自动剥离 \\?\ 长路径前缀。
func ReleaseSocketSlot ¶
func ReleaseSocketSlot( withDirLock func(context.Context, string, bool, func() error) error, dir string, unlinkFn func() error, closeFn func() error, isRetryable func(error) bool, ) error
ReleaseSocketSlot 按 INV-A3 固定顺序编排「加目录锁 -> unlink -> close -> 可重试 unlink -> 忽略已消失目录锁」。
func RemoveLockedFileSafely ¶
func RemoveLockedFileSafely(root ports.RootHandle, lockDest pathalg.LocalRootedDest) (bool, error)
RemoveLockedFileSafely 按三平台安全删锁协议清理已退役的单进程实例锁文件: 持独占锁截断清空刷盘 -> 解锁关句柄 -> Remove(忽略 ErrNotFound),消除 Windows 共享冲突。
func ResolvePhysicalPathWith ¶
func ResolvePhysicalPathWith(absPath string, ops PhysicalPathOps) (string, error)
ResolvePhysicalPathWith 自根向叶逐分量推进并展开所有已存在及悬空符号链接/重解析点(上限 MaxSymlinkHops=40 跳), 维护 missingDepth 精确追踪悬空尾部与软链内 "..",剥离 \\?\ / \??\ / \\?\UNC\ 前缀, 拒绝 NUL 字节与 Windows 盘符相对路径,彻底阻断悬空祖先软链绕过沙箱互斥防线。
func ResolveRawPhysicalPath ¶
func ResolveRawPhysicalPath(raw string, absFn func(string) (string, error), ops PhysicalPathOps) (string, error)
ResolveRawPhysicalPath 组合原始路径前置校验、abs 回调与 ResolvePhysicalPathWith 逐分量物理展开。
func ResolveSocketSlotConfig ¶
func ResolveSocketSlotConfig( filePerm fs.FileMode, peerPolicy uint8, confineOpt algebraic.Option[pathalg.LocalDir], toNative func(pathalg.LocalPathKind, string) (string, error), errInvalid, errInsecure error, ) (fs.FileMode, uint8, string, error)
ResolveSocketSlotConfig 统一校验并归一套接字权限位、对端策略与可选内核沙箱根目录(零 OS 原语依赖)。
func ResolveSocketSlotOptions ¶
func ResolveSocketSlotOptions(perm fs.FileMode, policy uint8, errInsecure error) (fs.FileMode, uint8, error)
ResolveSocketSlotOptions 校验并归一化本地 AF_UNIX 套接字权限位与对端接入策略:
- 零值 perm 归一为 DefaultFilePerm(0o600);含高位特殊权限位一律拒绝;
- 零值 policy 归一为 SocketPeerOwnerOnlyCode;未知枚举值一律拒绝;
- 当有效策略为 SocketPeerOwnerOnlyCode 且 filePerm&0o077 != 0 时,fail-closed 返回以 ports.ErrInvalidArgument 包装 errInsecure 的结构化错误,消除权限开放却按同 UID 拦截的矛盾。
func RetryLockBackoff ¶
RetryLockBackoff 在 ctx 取消控制下以有界指数退避同步重试非阻塞加锁回调(零后台协程)。
func SanitizeFilePerm ¶
SanitizeFilePerm 剥离特殊权限位,若权限位为零则回退至 fb。
func ShouldRejectOperatorSymlink ¶
func ShouldRejectOperatorSymlink(policy pathalg.SymlinkPolicy) (bool, error)
ShouldRejectOperatorSymlink 按 SymlinkPolicy 判定只读操作是否必须拒绝跟随符号链接/重解析点。
func SiblingDest ¶
func SiblingDest(dest pathalg.LocalRootedDest, name string) (pathalg.LocalRootedDest, error)
SiblingDest 在 dest 同级父目录下拼接新文件名分量 name。
func SidecarLockDest ¶
func SidecarLockDest(dest pathalg.LocalRootedDest) (pathalg.LocalRootedDest, error)
SidecarLockDest 为目标文件构造同目录旁路锁路径 <name>.lock(ADR-007)。
func SocketPeerPolicyString ¶
SocketPeerPolicyString 将本地套接字对端策略底层码转换为规范小写名称。
func SocketProbeStatusString ¶
SocketProbeStatusString 将套接字探活状态枚举底层码转换为规范小写名称。
func StripWin32ExtendedPrefix ¶
StripWin32ExtendedPrefix 剥离 Win32 扩展长路径及 NT 对象前缀(\\?\、\??\、\\?\UNC\、\??\UNC\)。
func StripWin32LongPathPrefix ¶
StripWin32LongPathPrefix 剥离 Win32 \\?\ 与 \\?\UNC\ 扩展前缀。
func UTF16CodeUnitLen ¶
UTF16CodeUnitLen 计算字符串在 UTF-16 编码下的精确码元数量(辅助平面码点计 2 码元)。
func ValidateChtimesArgs ¶
ValidateChtimesArgs 校验 Chtimes 时间参数合法性(拒绝零值与越界年份)。
func ValidateConfineRootDir ¶
ValidateConfineRootDir 校验可选 Unix 套接字沙箱根目录:必须为有效、绝对且非文件系统根级目录(拒绝 "/"、"C:/"、"." 与相对路径)。
func ValidateCreateSymlinkTarget ¶
func ValidateCreateSymlinkTarget(link pathalg.LocalRootedDest, target string) error
ValidateCreateSymlinkTarget 校验相对符号链接目标不逃逸沙箱深度。
func ValidateRawPhysicalPath ¶
ValidateRawPhysicalPath 在调用 filepath.Abs 之前执行平台无关前置校验: 拒绝空路径、NUL 字节与 Windows 盘符相对路径(如 "C:foo"),避免 OS GetFullPathName 静默拼入驱动器工作目录。
func ValidateSocketCtxPath ¶
ValidateSocketCtxPath 校验套接字操作的上下文与路径有效性。
func ValidateSocketPeerUID ¶
func ValidateSocketPeerUID(valid, hasUID bool, actual, expected uint32, errUnauthorized error) error
ValidateSocketPeerUID 核验 Unix 本地套接字对端 UID 存在且等于当前进程有效 UID。
func ValidateTruncateSize ¶
ValidateTruncateSize 校验 Truncate 目标长度非负。
func ValidateWriteChunkArgs ¶
ValidateWriteChunkArgs 校验 WriteChunk 的输入流、偏移量、长度防溢出与标准权限位。
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 WrapOpenTarget ¶
WrapOpenTarget 以哨兵 s 包装目标路径 tgt 打开失败的错误。
func WrapSyscallErr ¶
WrapSyscallErr 以 osfs.syscall_failed 包装系统调用错误,并按 MapOSError 归一常见 OS 哨兵。
func WrapWinACLError ¶
WrapWinACLError 按调用方指定哨兵与默认权限哨兵包装 Windows DACL 解析或评估失败错误。
func WriteAndSyncClose ¶
func WriteAndSyncClose(w SyncCloseWriter, frame []byte) error
WriteAndSyncClose 向流写入完整帧、执行 Sync 与 Close 并归一化返回错误。
func WriteBorrowed ¶
WriteBorrowed 在作用域活性哨兵保护下执行受借用写入。
func WriteChunkSyncClose ¶
func WriteChunkSyncClose(f SyncCloseWriteSeeker, offset int64, r io.Reader, length int64) (int64, error)
WriteChunkSyncClose 执行定长分片写入、Sync 刷盘与关闭,并优先返回写入故障或合并的刷盘/关闭错误。
Types ¶
type BorrowedReader ¶
type BorrowedReader struct {
// contains filtered or unexported fields
}
BorrowedReader 在同步作用域关闭后拒绝一切读取,并对顺序读与随机读施加溢出安全字节上限。
func NewBorrowedReader ¶
func NewBorrowedReader(r ports.BoundedReadHandle, maxBytes int64, limited bool, active *atomic.Bool) *BorrowedReader
NewBorrowedReader 构造绑定活性哨兵与可选字节上限的借用只读句柄包装器。
type BorrowedWriter ¶
type BorrowedWriter struct {
// contains filtered or unexported fields
}
BorrowedWriter 在同步作用域关闭后拒绝一切写入。
func NewBorrowedWriter ¶
func NewBorrowedWriter(w io.Writer, active *atomic.Bool) *BorrowedWriter
NewBorrowedWriter 构造绑定活性哨兵的借用写入句柄包装器。
type ErrMapPair ¶
ErrMapPair 将底层系统错误映射至目标 ports 哨兵错误。
type LockedFile ¶
type LockedFile struct {
// contains filtered or unexported fields
}
LockedFile 为同步作用域内借出的持锁文件句柄实现 ports.LockedFile,并在作用域结束后拒绝一切读写。
func NewLockedFile ¶
func NewLockedFile(f SyncTruncateStatFile, name string, active *atomic.Bool) *LockedFile
NewLockedFile 构造绑定活性哨兵的持锁文件句柄适配器。
func (*LockedFile) Stat ¶
func (l *LockedFile) Stat() (ports.LocalFileInfo, error)
func (*LockedFile) Sync ¶
func (l *LockedFile) Sync() error
func (*LockedFile) Truncate ¶
func (l *LockedFile) Truncate(size int64) error
type LockedFileFunc ¶
type LockedFileFunc func(f ports.LockedFile) error
LockedFileFunc / WriteFileFunc 定义旁路锁与原子替换辅助函数的借用回调契约。
type PhysicalNodeProbe ¶
type PhysicalNodeProbe struct {
Exists bool
IsLink bool
LinkTarget string // 仅 IsLink == true 时有效
Canonical string // 仅 Exists && !IsLink 时可选返回平台规范路径(如 macOS firmlink / Windows GetFinalPathNameByHandle)
}
PhysicalNodeProbe 描述单次无跟随路径节点探测的纯数据结论(供跨平台物理路径展开算法消费)。
type PhysicalPathOps ¶
type PhysicalPathOps struct {
IsWindows bool
Probe func(candidate string) (PhysicalNodeProbe, error)
}
PhysicalPathOps 封装物理路径解析算法所需的平台特性开关与无跟随探测回调(0 os、0 filepath 依赖)。
type SyncCloseWriteSeeker ¶
type SyncCloseWriteSeeker interface {
io.WriteSeeker
Sync() error
Close() error
}
SyncCloseWriteSeeker 抽象支持定位写入、刷盘与关闭的分片写入文件流。
type SyncCloseWriter ¶
SyncCloseWriter 抽象支持写入、刷盘与关闭的文件流。
type SyncTruncateStatFile ¶
type SyncTruncateStatFile interface {
io.ReadWriteSeeker
Sync() error
Truncate(size int64) error
Stat() (fs.FileInfo, error)
}
SyncTruncateStatFile 抽象底层持锁文件所需的最小接口,使 LockedFile 适配器与具体 *os.File 解耦。