Documentation
¶
Overview ¶
Package fs (file system) is an independent component to operate file and directory.
Package fs (file system) is an independent component to operate file and directory.
Package fs (file system) is an independent component to operate file and directory.
Index ¶
- Constants
- Variables
- func AdvisePageCache(f File, advice PageCacheAdvice) error
- func CleanupLeftoverTmp(fs FileSystem, partPath string)
- func CompareINode(srcPath, destPath string) error
- func MustClose(c Closer)
- func MustFlush(fs FileSystem, buffer []byte, name string, permission Mode)
- func MustFlushAtomic(fs FileSystem, buffer []byte, name string, permission Mode)
- func MustReadData(r Reader, offset int64, buff []byte)
- func MustWriteData(w SeqWriter, data []byte)
- func SetCached(f File, cached bool)
- func StatFile(f File) (os.FileInfo, error)
- func SyncAndDropCache(fd uintptr, offset int64, length int64) error
- func SyncDir(path string) error
- func SyncDirIfSupported(path string) error
- type Closer
- type DirEntry
- type File
- type FileSystem
- type FileSystemError
- type LocalFile
- func (file *LocalFile) Close() error
- func (file *LocalFile) IsLocked() bool
- func (file *LocalFile) Path() string
- func (file *LocalFile) Read(offset int64, buffer []byte) (int, error)
- func (file *LocalFile) Readv(offset int64, iov *[][]byte) (int, error)
- func (file *LocalFile) SequentialRead() SeqReader
- func (file *LocalFile) SequentialWrite() SeqWriter
- func (file *LocalFile) Size() (int64, error)
- func (file *LocalFile) Write(buffer []byte) (int, error)
- func (file *LocalFile) Writev(iov *[][]byte) (int, error)
- type LockOption
- type Mode
- type PageCacheAdvice
- type Reader
- type SeqReader
- type SeqWriter
- type Writer
Constants ¶
const (
IsNotExistError
)
FileSystemError code.
const OpenFileNamesMutable = true
OpenFileNamesMutable reports whether a file that is still open can be renamed or unlinked. POSIX file systems allow it: the open descriptor keeps referring to the same file.
Variables ¶
var ErrLockHeld = errors.New("the file is locked by another process")
ErrLockHeld matches, through errors.Is, the error of CreateLockFile when another open file holds the lock. Any other failure to open or lock the file does not match it.
var ErrNotLocalFile = errors.New("fs: not a local file")
ErrNotLocalFile reports an operation that needs a file of the local file system on another kind of File.
Functions ¶
func AdvisePageCache ¶ added in v0.12.0
func AdvisePageCache(f File, advice PageCacheAdvice) error
AdvisePageCache applies advice to the whole of f. It is a best-effort hint: files not backed by the local file system are left untouched.
func CleanupLeftoverTmp ¶ added in v0.10.3
func CleanupLeftoverTmp(fs FileSystem, partPath string)
CleanupLeftoverTmp removes any "<file>.tmp" sibling under partPath whose matching final "<file>" exists. A .tmp without its final indicates a crash between WriteAtomic's fsync(tmp) and rename — that case is left alone so the engine's existing panic-on-missing-metadata fires for operator intervention, per BanyanDB's canonical fail-fast contract.
Symlinks ending in ".tmp" are skipped: BanyanDB part directories never contain symlinks, so encountering one is unexpected and we do not want to follow it during cleanup.
func CompareINode ¶ added in v0.8.0
CompareINode compares the inode of two files.
func MustClose ¶ added in v0.6.0
func MustClose(c Closer)
MustClose closes c and panics if it cannot close.
func MustFlush ¶ added in v0.6.0
func MustFlush(fs FileSystem, buffer []byte, name string, permission Mode)
MustFlush flushes all data to one file and panics if it cannot write all data.
func MustFlushAtomic ¶ added in v0.10.3
func MustFlushAtomic(fs FileSystem, buffer []byte, name string, permission Mode)
MustFlushAtomic writes buffer to name via WriteAtomic and panics on error. Same panic-on-error semantics as MustFlush; use this when the caller needs crash-safety on the file (metadata.json, manifest.json, etc.).
func MustReadData ¶ added in v0.6.0
MustReadData reads data from r and panics if it cannot read all data.
func MustWriteData ¶ added in v0.6.0
MustWriteData writes data to w and panics if it cannot write all data.
func SetCached ¶ added in v0.12.0
SetCached controls whether sequential reads and writes on f keep its pages in the OS page cache. When cached is false they apply FADV_DONTNEED as data is streamed; when true the pages are left for later reads.
func StatFile ¶ added in v0.12.0
StatFile describes the open file f itself (fstat), not whatever its path names now, so os.SameFile can tell whether a path still names the file f was opened from. It reports ErrNotLocalFile for files not backed by the local file system.
func SyncAndDropCache ¶ added in v0.9.0
SyncAndDropCache syncs the file data to disk and then drops it from the page cache.
func SyncDir ¶ added in v0.12.0
SyncDir fsyncs the directory at path so that entries created, linked, or removed in it are durable. It is a no-op where the platform does not expose directory fsync.
func SyncDirIfSupported ¶ added in v0.12.0
SyncDirIfSupported is SyncDir for callers that run on any file system: a file system that cannot fsync a directory (EINVAL or ENOTSUP, as some FUSE and 9p mounts answer) is not an error, there is nothing more to do there. Any other failure wraps the underlying error, so errors.Is still finds os.ErrNotExist.
Types ¶
type Closer ¶ added in v0.6.0
type Closer interface {
// Returns the absolute path of the file.
Path() string
// Close File.
Close() error
}
Closer allows closing a file.
type DirEntry ¶ added in v0.6.0
type DirEntry interface {
// Name returns the name of the file or directory.
Name() string
// IsDir reports whether the entry describes a directory.
IsDir() bool
// Type returns the entry's type bits.
Type() iofs.FileMode
}
DirEntry is the interface that wraps the basic information about a file or directory.
type File ¶
type File interface {
Writer
Reader
// Vector Append mode, which supports appending consecutive buffers to the end of the file.
Writev(iov *[][]byte) (int, error)
// Reading contiguous regions of a file and dispersing them into discontinuous buffers.
Readv(offset int64, iov *[][]byte) (int, error)
// Get the file written data's size and return an error if the file does not exist. The unit of file size is Byte.
Size() (int64, error)
// Returns the absolute path of the file.
Path() string
// Close File.
Close() error
}
File operation interface.
func MustCreateFile ¶ added in v0.6.0
func MustCreateFile(fs FileSystem, path string, permission Mode, cached bool) File
MustCreateFile creates a new file with the specified name and permission. If cached is false, it will sync the file data to disk and then apply FADV_DONTNEED to tell the OS not to cache it.
type FileSystem ¶
type FileSystem interface {
// MkdirIfNotExist creates a new directory with the specified name and permission if it does not exist.
// If the directory exists, it will do nothing.
MkdirIfNotExist(path string, permission Mode)
// MkdirPanicIfExist creates a new directory with the specified name and permission if it does not exist.
// If the directory exists, it will panic.
MkdirPanicIfExist(path string, permission Mode)
// ReadDir reads the directory named by dirname and returns a list of directory entries sorted by filename.
ReadDir(dirname string) []DirEntry
// ReadDirLimit reads at most limit entries of dirname in directory order, or every entry when limit
// is not positive. Unlike ReadDir it returns failures as errors instead of panicking.
ReadDirLimit(dirname string, limit int) ([]DirEntry, error)
// MkdirAll creates path and any missing parents. Unlike MkdirIfNotExist it returns failures as
// errors instead of panicking.
MkdirAll(path string, permission Mode) error
// Lstat describes the named file without following a final symbolic link.
Lstat(name string) (iofs.FileInfo, error)
// Create and open the file by specified name and mode.
CreateFile(name string, permission Mode) (File, error)
// Create and open lock file by specified name and mode.
// With PreserveUntilLocked the file is emptied only once its lock is held.
CreateLockFile(name string, permission Mode, opts ...LockOption) (File, error)
// Open the file by specified name and mode.
OpenFile(name string) (File, error)
// Flush mode, which flushes all data to one file.
Write(buffer []byte, name string, permission Mode) (int, error)
// WriteAtomic writes buffer to a fresh ".tmp" sibling, fsyncs it, renames it
// over name, and fsyncs the parent directory. After a successful return the
// content is durable on a POSIX-compliant local filesystem (ext4, xfs, apfs,
// ntfs). On any error the .tmp file may be left behind for next-boot cleanup
// (when the matching final file exists) or operator intervention (when only
// the .tmp exists, indicating a crash before rename).
//
// Atomicity caveat: os.Rename is not guaranteed atomic on network-mounted
// filesystems (NFS, SMB, FUSE-based S3 gateways). BanyanDB targets local
// disks; do not run on a network FS without verifying the mount semantics.
WriteAtomic(buffer []byte, name string, permission Mode) (int, error)
// Read the entire file using streaming read.
Read(name string) ([]byte, error)
// Delete the file.
DeleteFile(name string) error
// Rename renames oldPath to newPath atomically.
Rename(oldPath, newPath string) error
// Delete the directory.
MustRMAll(path string)
// SyncPath the directory of file.
SyncPath(path string)
// MustGetFreeSpace returns the free space of the file system.
MustGetFreeSpace(path string) uint64
// MustGetTotalSpace returns the total space of the file system.
MustGetTotalSpace(path string) uint64
// CreateHardLink creates hard links in destPath for files in srcPath that pass the filter.
CreateHardLink(srcPath, destPath string, filter func(string) bool) error
// IsExist checks if the directory/file exists or not.
IsExist(path string) bool
}
FileSystem operation interface.
func NewLocalFileSystem ¶ added in v0.6.0
func NewLocalFileSystem() FileSystem
NewLocalFileSystem is used to create the Local File system.
func NewLocalFileSystemWithLogger ¶ added in v0.6.0
func NewLocalFileSystemWithLogger(parent *logger.Logger) FileSystem
NewLocalFileSystemWithLogger is used to create the Local File system with logger.
func NewLocalFileSystemWithLoggerAndLimit ¶ added in v0.9.0
func NewLocalFileSystemWithLoggerAndLimit(parent *logger.Logger, limit uint64) FileSystem
NewLocalFileSystemWithLoggerAndLimit is used to create the Local File system with logger and limit, limit is used to set the IO size.
type FileSystemError ¶ added in v0.6.0
FileSystemError implements the Error interface.
func (*FileSystemError) Error ¶ added in v0.6.0
func (err *FileSystemError) Error() string
func (*FileSystemError) Is ¶ added in v0.10.2
func (err *FileSystemError) Is(target error) bool
Is reports whether the error matches one of the standard io/fs sentinel errors, so callers can use errors.Is with iofs.ErrNotExist, iofs.ErrExist and iofs.ErrPermission regardless of the underlying OS error wrapping, and with ErrLockHeld for a lock another open file holds. The target is compared directly (no recursive errors.Is) to mirror the convention used by syscall.Errno.Is in the standard library.
type LocalFile ¶ added in v0.6.0
type LocalFile struct {
// contains filtered or unexported fields
}
LocalFile implements the File interface.
func (*LocalFile) IsLocked ¶ added in v0.12.0
IsLocked reports whether this file was acquired through CreateLockFile. It is used by ownership-bound storage adapters; callers cannot set the private marker on a LocalFile obtained through any other constructor.
func (*LocalFile) Readv ¶ added in v0.6.0
Readv is used to read contiguous regions of a file and disperse them into discontinuous buffers.
func (*LocalFile) SequentialRead ¶ added in v0.6.0
SequentialRead is used to read the entire file using streaming read. If cached is true, fadvise will not be applied.
func (*LocalFile) SequentialWrite ¶ added in v0.6.0
SequentialWrite supports appending consecutive buffers to the end of the file.
func (*LocalFile) Size ¶ added in v0.6.0
Size is used to get the file written data's size and return an error if the file does not exist. The unit of file size is Byte.
type LockOption ¶ added in v0.12.0
type LockOption func(*lockOptions)
LockOption adjusts CreateLockFile.
func PreserveUntilLocked ¶ added in v0.12.0
func PreserveUntilLocked() LockOption
PreserveUntilLocked makes CreateLockFile open an existing file without emptying it, without following a symbolic link or waiting for a FIFO, and refuse a file that is not a regular one: the file is emptied only once its lock is held, so a refused attempt leaves the holder's content.
type PageCacheAdvice ¶ added in v0.12.0
type PageCacheAdvice int
PageCacheAdvice is a hint about how a file's pages will be accessed. It is passed to the OS page cache through fadvise on Linux and ignored elsewhere.
const ( // PageCacheWillNeed asks the OS to read the whole file into the page // cache ahead of access. PageCacheWillNeed PageCacheAdvice = iota // PageCacheRandom disables readahead for a file read at scattered offsets. PageCacheRandom // PageCacheDontNeed drops the file's clean pages from the page cache. PageCacheDontNeed )
type Reader ¶ added in v0.6.0
type Reader interface {
// Read the entire file at a specified offset.
Read(offset int64, buffer []byte) (int, error)
// Read the entire file using sequential read.
// If skipFadvise is true, fadvise will not be applied.
SequentialRead() SeqReader
// Returns the absolute path of the file.
Path() string
// Close File.
Close() error
}
Reader allows reading data from a file.
type Writer ¶ added in v0.6.0
type Writer interface {
// Append mode, which adds new data to the end of a file.
Write(buffer []byte) (int, error)
// SequentialWrite mode, which supports appending consecutive buffers to the end of the file.
SequentialWrite() SeqWriter
// Returns the absolute path of the file.
Path() string
// Close File.
Close() error
}
Writer allows writing data to a file.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package remote provides an interface for interacting with various remote file systems such as S3, GCS, and distributed file systems for backup/restore operations.
|
Package remote provides an interface for interacting with various remote file systems such as S3, GCS, and distributed file systems for backup/restore operations. |
|
aws
Package aws provides an AWS S3 file system implementation.
|
Package aws provides an AWS S3 file system implementation. |
|
azure
Package azure provides an Azure Blob Storage implementation of the remote.FS interface.
|
Package azure provides an Azure Blob Storage implementation of the remote.FS interface. |
|
checksum
Package checksum provides functions for computing checksums algorithms and verifying.
|
Package checksum provides functions for computing checksums algorithms and verifying. |
|
config
Package config provides the configuration for the file system.
|
Package config provides the configuration for the file system. |
|
gcp
Package gcp provides the GCS file system.
|
Package gcp provides the GCS file system. |
|
local
Package local provides a local file system implementation.
|
Package local provides a local file system implementation. |