Documentation
¶
Overview ¶
Package filefs provides a fixed-size file-backed filesystem implementation that conforms to the absfs.FileSystem interface.
filefs is designed for critical systems where storage availability must be guaranteed regardless of host disk pressure. It pre-allocates a single file of fixed size on the host filesystem, then manages that space internally using block allocation. Once created, the backing file's size never changes, so even if other processes fill the host disk completely, filefs continues to operate within its pre-allocated space.
This is analogous to how operating systems use disk partitions to isolate critical storage from user activity, but without requiring kernel-level partition management.
Usage ¶
// Create a 64 MB filesystem backed by a single file
fs, err := filefs.New("/var/lib/myapp/storage.fs", 64*1024*1024)
if err != nil {
log.Fatal(err)
}
defer fs.Close()
// Use like any absfs.FileSystem
f, _ := fs.Create("/config/app.json")
f.Write([]byte(`{"key": "value"}`))
f.Close()
// Sync persists metadata (data writes go directly to the backing file)
fs.Sync()
Persistence ¶
File data is written directly to the backing file on every Write call. Metadata (directory tree, file sizes, block mappings) is persisted on Sync() and Close(). After a crash, reopening the file recovers the last synced state.
Thread Safety ¶
FileSystem is safe for concurrent use by multiple goroutines.
Index ¶
- 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) (int, 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) (int64, 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) (int, error)
- func (f *File) WriteString(s string) (int, error)
- type FileByteStore
- func (s *FileByteStore) ReadAt(ino uint64, p []byte, off int64) (int, error)
- func (s *FileByteStore) Remove(ino uint64) error
- func (s *FileByteStore) Stat(ino uint64) (int64, error)
- func (s *FileByteStore) Truncate(ino uint64, size int64) error
- func (s *FileByteStore) WriteAt(ino uint64, p []byte, off int64) (int, error)
- type FileSystem
- func (fs *FileSystem) Capacity() int64
- 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) Close() error
- func (fs *FileSystem) Create(name string) (absfs.File, error)
- func (fs *FileSystem) Free() int64
- func (fs *FileSystem) Getwd() (string, 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) Sync() 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 ¶
This section is empty.
Types ¶
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents an open file in the file-backed filesystem.
type FileByteStore ¶
type FileByteStore struct {
// contains filtered or unexported fields
}
FileByteStore implements inode.ByteStore using a fixed-size backing file.
File data is stored in fixed-size blocks within the backing file. Each inode's data is mapped to an ordered list of block indices. Blocks are allocated from a bitmap-managed free pool.
Thread Safety: All methods are safe for concurrent use. A mutex protects all state mutations and file I/O to ensure consistency.
func (*FileByteStore) Remove ¶
func (s *FileByteStore) Remove(ino uint64) error
Remove deletes all data associated with the inode.
func (*FileByteStore) Stat ¶
func (s *FileByteStore) Stat(ino uint64) (int64, error)
Stat returns the current logical size of the inode's data.
type FileSystem ¶
type FileSystem struct {
Umask os.FileMode
Tempdir string
// contains filtered or unexported fields
}
FileSystem represents a fixed-size file-backed filesystem.
func New ¶
func New(filepath string, size int64) (*FileSystem, error)
New creates or opens a filefs filesystem at the given path.
If the file does not exist, it is created and formatted with the given size. If the file already exists, it is opened and the existing filesystem is loaded; the size parameter is ignored in this case.
The size must be at least 1 MB.
func (*FileSystem) Capacity ¶
func (fs *FileSystem) Capacity() int64
Capacity returns the total data capacity in bytes.
func (*FileSystem) Chdir ¶
func (fs *FileSystem) Chdir(name string) error
func (*FileSystem) Close ¶
func (fs *FileSystem) Close() error
Close syncs metadata and closes the backing file.
func (*FileSystem) Free ¶
func (fs *FileSystem) Free() int64
Free returns the number of free bytes available in the filesystem.
func (*FileSystem) Getwd ¶
func (fs *FileSystem) Getwd() (string, error)
func (*FileSystem) Remove ¶
func (fs *FileSystem) Remove(name string) error
func (*FileSystem) RemoveAll ¶
func (fs *FileSystem) RemoveAll(name string) error
func (*FileSystem) Rename ¶
func (fs *FileSystem) Rename(oldpath, newpath string) error
func (*FileSystem) Symlink ¶
func (fs *FileSystem) Symlink(oldname, newname string) error
func (*FileSystem) Sync ¶
func (fs *FileSystem) Sync() error
Sync persists the filesystem metadata to the backing file. File data is already written on each Write call; Sync saves the directory tree, block mappings, and allocation bitmap.
func (*FileSystem) TempDir ¶
func (fs *FileSystem) TempDir() string