Documentation
¶
Overview ¶
Package osfs provides helpers for converting between Unix-style absfs paths and native OS paths. All absfs filesystems use Unix-style forward slash paths for consistency and composability. The osfs package provides conversion helpers for interacting with native filesystem APIs.
Path Conventions ¶
absfs paths use Unix-style conventions regardless of the host OS:
- Forward slashes: /foo/bar
- Drive letters (Windows): /c/Users/foo (lowercase)
- UNC paths (Windows): //server/share/path
- No drive = current drive: /foo resolves using cwd's drive
Windows Examples ¶
FromNative("C:\\Users\\foo") → "/c/Users/foo"
FromNative("\\\\server\\share") → "//server/share"
ToNative("/c/Users/foo") → "C:\\Users\\foo"
ToNative("//server/share") → "\\\\server\\share"
Unix Examples ¶
On Unix systems, ToNative and FromNative are effectively no-ops since Unix already uses forward slashes. Drive letter functions return empty strings and pass through paths unchanged.
Index ¶
- func FromNative(path string) string
- func GetDrive(path string) string
- func IsReservedName(name string) bool
- func IsUNC(path string) bool
- func JoinDrive(drive, path string) string
- func JoinUNC(server, share, path string) string
- func SetDrive(path, drive string) string
- func SplitDrive(path string) (drive, rest string)
- func SplitUNC(path string) (server, share, rest string)
- func StripDrive(path string) string
- func ToNative(path string) string
- func ValidatePath(path string) error
- type File
- func (f *File) Close() error
- func (f *File) Name() string
- func (f *File) Read(p []byte) (int, error)
- func (f *File) ReadAt(b []byte, off int64) (n int, err error)
- func (f *File) ReadDir(n int) ([]fs.DirEntry, error)
- func (f *File) Readdir(n int) ([]os.FileInfo, error)
- func (f *File) Readdirnames(n int) ([]string, error)
- func (f *File) Seek(offset int64, whence int) (ret int64, err error)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Sync() error
- func (f *File) Truncate(size int64) error
- func (f *File) Write(p []byte) (int, error)
- func (f *File) WriteAt(b []byte, off int64) (n int, err error)
- func (f *File) WriteString(s string) (n int, err error)
- type FileSystem
- func (fs *FileSystem) Chdir(name string) error
- func (fs *FileSystem) Chmod(name string, mode os.FileMode) error
- func (fs *FileSystem) Chown(name string, uid, gid int) error
- func (fs *FileSystem) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (fs *FileSystem) Create(name string) (absfs.File, error)
- func (fs *FileSystem) Getwd() (dir string, err error)
- func (fs *FileSystem) Lchown(name string, uid, gid int) error
- func (fs *FileSystem) Lstat(name string) (os.FileInfo, error)
- func (fs *FileSystem) Mkdir(name string, perm os.FileMode) error
- func (fs *FileSystem) MkdirAll(name string, perm os.FileMode) error
- func (fs *FileSystem) Open(name string) (absfs.File, error)
- func (fs *FileSystem) OpenFile(name string, flag int, perm os.FileMode) (absfs.File, error)
- func (fs *FileSystem) ReadDir(name string) ([]fs.DirEntry, error)
- func (fs *FileSystem) ReadFile(name string) ([]byte, error)
- func (fs *FileSystem) Readlink(name string) (string, error)
- func (fs *FileSystem) Remove(name string) error
- func (fs *FileSystem) RemoveAll(name string) error
- func (fs *FileSystem) Rename(oldpath, newpath string) error
- func (fs *FileSystem) Stat(name string) (os.FileInfo, error)
- func (fs *FileSystem) Sub(dir string) (fs.FS, error)
- func (fs *FileSystem) Symlink(oldname, newname string) error
- func (fs *FileSystem) TempDir() string
- func (fs *FileSystem) Truncate(name string, size int64) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromNative ¶
FromNative converts an OS-native path to a Unix-style absfs path.
On Windows:
- "C:\foo\bar" → "/c/foo/bar"
- "\\server\share\path" → "//server/share/path"
- "\foo\bar" → "/foo/bar"
On Unix: returns the path unchanged (no-op).
func GetDrive ¶
GetDrive returns just the drive letter from a Unix-style absfs path. Returns empty string if path has no drive letter.
Examples:
GetDrive("/c/foo") → "c"
GetDrive("/foo") → ""
GetDrive("//server/share") → ""
func IsReservedName ¶
IsReservedName returns true if name is a reserved device name on Windows. Always returns false on Unix.
Reserved names (case-insensitive): CON, PRN, AUX, NUL, COM1-COM9, LPT1-LPT9
Examples:
IsReservedName("CON") → true (Windows), false (Unix)
IsReservedName("con.txt") → true (Windows), false (Unix)
IsReservedName("config") → false
func IsUNC ¶
IsUNC returns true if the path is a UNC-style path (//server/share).
Examples:
IsUNC("//server/share") → true
IsUNC("//server/share/path") → true
IsUNC("/c/foo") → false
IsUNC("/foo") → false
func JoinDrive ¶
JoinDrive combines a drive letter with a path to create a Unix-style absfs path.
Examples:
JoinDrive("c", "/foo") → "/c/foo"
JoinDrive("C", "/foo") → "/c/foo" // normalized to lowercase
JoinDrive("", "/foo") → "/foo" // no drive
JoinDrive("c", "foo") → "/c/foo" // ensures leading slash
func JoinUNC ¶
JoinUNC creates a UNC path from server, share, and path components.
Examples:
JoinUNC("server", "share", "/foo") → "//server/share/foo"
JoinUNC("server", "share", "/") → "//server/share"
JoinUNC("server", "share", "") → "//server/share"
func SetDrive ¶
SetDrive returns the path with its drive letter changed (or added). If the path already has a drive, it is replaced. If not, one is added.
Examples:
SetDrive("/c/foo", "d") → "/d/foo"
SetDrive("/foo", "c") → "/c/foo"
SetDrive("foo", "c") → "/c/foo"
func SplitDrive ¶
SplitDrive extracts the drive letter from a Unix-style absfs path. Returns the drive letter (lowercase, without colon) and the remaining path.
Examples:
SplitDrive("/c/foo") → ("c", "/foo")
SplitDrive("/C/foo") → ("c", "/foo") // normalized to lowercase
SplitDrive("/foo") → ("", "/foo") // no drive
SplitDrive("//server/share") → ("", "//server/share") // UNC, no drive
SplitDrive("foo/bar") → ("", "foo/bar") // relative path
func SplitUNC ¶
SplitUNC splits a UNC path into server, share, and remaining path components. Returns empty strings if the path is not a valid UNC path.
Examples:
SplitUNC("//server/share/foo/bar") → ("server", "share", "/foo/bar")
SplitUNC("//server/share") → ("server", "share", "/")
SplitUNC("/c/foo") → ("", "", "") // not UNC
func StripDrive ¶
StripDrive removes the drive prefix from a Unix-style absfs path.
Examples:
StripDrive("/c/foo") → "/foo"
StripDrive("/foo") → "/foo" // no change
StripDrive("//server/share/path") → "//server/share/path" // UNC unchanged
func ToNative ¶
ToNative converts a Unix-style absfs path to an OS-native path.
On Windows:
- "/c/foo/bar" → "C:\foo\bar"
- "//server/share/path" → "\\server\share\path"
- "/foo/bar" → "\foo\bar" (no drive, relative to current drive root)
On Unix: returns the path unchanged (no-op).
func ValidatePath ¶
ValidatePath checks if a path is valid for the current OS. Returns nil if valid, or an error describing the issue.
On Windows, checks for:
- Reserved device names (CON, PRN, NUL, etc.)
- Invalid characters (< > : " | ? *)
- Trailing spaces or periods in path components
On Unix: most paths are valid, only checks for null bytes.
Types ¶
type File ¶
type File struct {
// contains filtered or unexported fields
}
func (*File) Name ¶
Name returns the file name in Unix-style format. On Windows, this includes the drive letter (e.g., "/c/Users/foo/file.txt").
func (*File) ReadDir ¶
ReadDir reads the contents of the directory and returns a slice of up to n DirEntry values in directory order. This delegates to the underlying os.File.ReadDir which uses high-performance native OS directory reading APIs.
If n > 0, ReadDir returns at most n entries. In this case, if ReadDir returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF.
If n <= 0, ReadDir returns all entries from the directory in a single slice. In this case, if ReadDir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error.
type FileSystem ¶
type FileSystem struct {
// contains filtered or unexported fields
}
FileSystem implements absfs.SymlinkFileSystem using the host OS filesystem. All paths are Unix-style (forward slashes) for consistency with absfs conventions. On Windows, drive letters are represented as /c/, /d/, etc.
func NewFS ¶
func NewFS() (*FileSystem, error)
NewFS creates a new FileSystem rooted at the OS current working directory. The cwd is stored in Unix-style format.
func (*FileSystem) Chdir ¶
func (fs *FileSystem) Chdir(name string) error
Chdir changes the current working directory. The name can be a Unix-style path (e.g., "/c/Users" on Windows).
func (*FileSystem) Chmod ¶
func (fs *FileSystem) Chmod(name string, mode os.FileMode) error
Chmod changes the mode of the named file.
func (*FileSystem) Chown ¶
func (fs *FileSystem) Chown(name string, uid, gid int) error
Chown changes the owner and group ids of the named file.
func (*FileSystem) Create ¶
func (fs *FileSystem) Create(name string) (absfs.File, error)
Create creates or truncates the named file.
func (*FileSystem) Getwd ¶
func (fs *FileSystem) Getwd() (dir string, err error)
Getwd returns the current working directory in Unix-style format. On Windows, this includes the drive letter (e.g., "/c/Users/foo").
func (*FileSystem) Lchown ¶
func (fs *FileSystem) Lchown(name string, uid, gid int) error
Lchown changes the owner and group ids of a symlink.
func (*FileSystem) Lstat ¶
func (fs *FileSystem) Lstat(name string) (os.FileInfo, error)
Lstat returns file information without following symlinks.
func (*FileSystem) Mkdir ¶
func (fs *FileSystem) Mkdir(name string, perm os.FileMode) error
Mkdir creates a directory with the specified permissions.
func (*FileSystem) MkdirAll ¶
func (fs *FileSystem) MkdirAll(name string, perm os.FileMode) error
MkdirAll creates a directory and all necessary parents.
func (*FileSystem) Open ¶
func (fs *FileSystem) Open(name string) (absfs.File, error)
Open opens the named file for reading.
func (*FileSystem) ReadDir ¶
func (fs *FileSystem) ReadDir(name string) ([]fs.DirEntry, error)
ReadDir reads the named directory and returns a list of directory entries. Uses platform-specific optimizations for high-performance directory reading: - Linux: syscall.Getdents with 32KB buffer (vs default 8KB) - macOS: os.ReadDir (uses getattrlistbulk internally) - Windows: FindFirstFileEx with optimizations - Other platforms: os.ReadDir fallback
func (*FileSystem) ReadFile ¶
func (fs *FileSystem) ReadFile(name string) ([]byte, error)
ReadFile reads the named file and returns its contents. Uses os.ReadFile for optimized file reading with proper size pre-allocation.
func (*FileSystem) Readlink ¶
func (fs *FileSystem) Readlink(name string) (string, error)
Readlink returns the symlink target in Unix-style format.
func (*FileSystem) Remove ¶
func (fs *FileSystem) Remove(name string) error
Remove removes the named file or empty directory.
func (*FileSystem) RemoveAll ¶
func (fs *FileSystem) RemoveAll(name string) error
RemoveAll removes a file or directory and any children.
func (*FileSystem) Rename ¶
func (fs *FileSystem) Rename(oldpath, newpath string) error
Rename renames (moves) a file or directory.
func (*FileSystem) Stat ¶
func (fs *FileSystem) Stat(name string) (os.FileInfo, error)
Stat returns file information for the named file.
func (*FileSystem) Sub ¶
func (fs *FileSystem) Sub(dir string) (fs.FS, error)
Sub returns an fs.FS corresponding to the subtree rooted at dir. The directory must exist and be a valid directory.
func (*FileSystem) Symlink ¶
func (fs *FileSystem) Symlink(oldname, newname string) error
Symlink creates a symbolic link. The oldname (target) is stored exactly as given - it can be relative or absolute. Only the newname (link location) is converted to a native path.
func (*FileSystem) TempDir ¶
func (fs *FileSystem) TempDir() string
TempDir returns the OS temp directory in Unix-style format.