Skip to content

vfs.vfs_local #

Local Filesystem Implementation (vfs_local)

The LocalVFS implementation provides a direct passthrough to the operating system's filesystem. It implements all VFS operations by delegating to the corresponding OS filesystem operations.

Features

  • Native filesystem access with full POSIX compliance
  • Preserves file permissions and metadata
  • Efficient direct access to local files
  • Support for all VFS operations including symlinks
  • Path-based access relative to root directory

Implementation Details

Structure

vfs_local/
├── factory.v               # VFS factory implementation
├── vfs_implementation.v    # Core VFS interface implementation
├── vfs_local.v            # LocalVFS type definition
├── model_fsentry.v        # FSEntry implementation
└── vfs_implementation_test.v  # Implementation tests

Key Components

  • LocalVFS: Main implementation struct that handles filesystem operations
  • LocalFSEntry: Implementation of FSEntry interface for local filesystem entries
  • factory.v: Provides new_local_vfs() for creating instances

Error Handling

The implementation provides detailed error messages including:- Path validation

  • Permission checks
  • File existence verification
  • Type checking (file/directory/symlink)

Usage

import vfs

fn main() ! {
    // Create a new local VFS instance rooted at /tmp/test
    mut fs := vfs.new_vfs('local', '/tmp/test')!

    // Basic file operations
    fs.file_create('example.txt')!
    fs.file_write('example.txt', 'Hello from LocalVFS'.bytes())!

    // Read file contents
    content := fs.file_read('example.txt')!
    println(content.bytestr())

    // Directory operations
    fs.dir_create('subdir')!
    fs.file_create('subdir/nested.txt')!

    // List directory contents
    entries := fs.dir_list('subdir')!
    for entry in entries {
        println('Found: ${entry.get_path()}')
    }

    // Symlink operations
    fs.link_create('example.txt', 'link.txt')!
    target := fs.link_read('link.txt')!
    println('Link target: ${target}')

    // Clean up
    fs.destroy()!
}

Limitations

  • Operations are restricted to the root directory specified during creation
  • Symlink support depends on OS capabilities
  • File permissions follow OS user context

Implementation Notes

  1. Path Handling:
  • All paths are made relative to the VFS root
  • Absolute paths are converted to relative
  • Parent directory (..) references are resolved
  1. Error Cases:
  • Non-existent files/directories
  • Permission denied
  • Invalid operations (e.g., reading directory as file)
  • Path traversal attempts
  1. Metadata:
  • Preserves OS file metadata
  • Maps OS attributes to VFS metadata structure
  • Maintains creation/modification times

Testing

The implementation includes comprehensive tests covering:- Basic file operations

  • Directory manipulation
  • Symlink handling
  • Error conditions
  • Edge cases

Run tests with:

v test vfs/vfs_local/

fn new_local_vfs #

fn new_local_vfs(root_path string) !vfs.VFSImplementation

Create a new LocalVFS instance

fn (LocalFSEntry) is_dir #

fn (self &LocalFSEntry) is_dir() bool

is_dir returns true if the entry is a directory

fn (LocalFSEntry) is_file #

fn (self &LocalFSEntry) is_file() bool

is_file returns true if the entry is a file

struct LocalVFS #

struct LocalVFS {
mut:
	root_path string
}

LocalVFS implements vfs.VFSImplementation for local filesystem

fn (LocalVFS) copy #

fn (myvfs LocalVFS) copy(src_path string, dst_path string) !vfs.FSEntry

fn (LocalVFS) delete #

fn (myvfs LocalVFS) delete(path string) !

Generic delete operation that handles all types

fn (LocalVFS) destroy #

fn (mut myvfs LocalVFS) destroy() !

Destroy the vfscore by removing all its contents

fn (LocalVFS) dir_create #

fn (myvfs LocalVFS) dir_create(path string) !vfs.FSEntry

Directory operations with improved error handling

fn (LocalVFS) dir_delete #

fn (myvfs LocalVFS) dir_delete(path string) !

fn (LocalVFS) dir_list #

fn (myvfs LocalVFS) dir_list(path string) ![]vfs.FSEntry

fn (LocalVFS) exists #

fn (myvfs LocalVFS) exists(path string) bool

Common operations with improved error handling

fn (LocalVFS) file_concatenate #

fn (myvfs LocalVFS) file_concatenate(path string, data []u8) !

File concatenate operation - appends data to a file

fn (LocalVFS) file_create #

fn (myvfs LocalVFS) file_create(path string) !vfs.FSEntry

File operations with improved error handling and TOCTOU protection

fn (LocalVFS) file_delete #

fn (myvfs LocalVFS) file_delete(path string) !

fn (LocalVFS) file_read #

fn (myvfs LocalVFS) file_read(path string) ![]u8

fn (LocalVFS) file_write #

fn (myvfs LocalVFS) file_write(path string, data []u8) !

fn (LocalVFS) get #

fn (myvfs LocalVFS) get(path string) !vfs.FSEntry

fn (LocalVFS) get_path #

fn (myvfs LocalVFS) get_path(entry &vfs.FSEntry) !string

Get path of an FSEntry

fn (LocalVFS) move #

fn (myvfs LocalVFS) move(src_path string, dst_path string) !vfs.FSEntry

fn (LocalVFS) print #

fn (myvfs LocalVFS) print() !

Print information about the VFS

fn (LocalVFS) rename #

fn (myvfs LocalVFS) rename(old_path string, new_path string) !vfs.FSEntry

fn (LocalVFS) root_get #

fn (myvfs LocalVFS) root_get() !vfs.FSEntry

Basic operations