CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-fuse-python

Python bindings for FUSE (Filesystem in USErspace) enabling custom userspace filesystems

Pending
Overview
Eval results
Files

file-management.mddocs/

File and Directory Management

Operations for creating, removing, and modifying files and directories. These operations handle filesystem structure changes including file creation, deletion, linking, and renaming.

Capabilities

File Creation and Removal

Create and remove files and directories in the filesystem.

def mknod(self, path, mode, dev):
    """
    Create a file node (regular file, device file, etc.).
    
    Args:
        path (str): Path where to create the file
        mode (int): File type and permissions (stat.S_IFREG | 0o644)
        dev (int): Device number (for device files, usually 0)
        
    Returns:
        int: 0 on success, negative errno on error
    """

def create(self, path, mode, fi):
    """
    Create and open a file atomically.
    
    Args:
        path (str): Path where to create the file
        mode (int): File permissions
        fi (FuseFileInfo): File info structure
        
    Returns:
        int: 0 on success, negative errno on error
    """

def mkdir(self, path, mode):
    """
    Create a directory.
    
    Args:
        path (str): Path where to create directory
        mode (int): Directory permissions (typically 0o755)
        
    Returns:
        int: 0 on success, negative errno on error
    """

def unlink(self, path):
    """
    Remove a file.
    
    Args:
        path (str): Path to file to remove
        
    Returns:
        int: 0 on success, negative errno on error
    """

def rmdir(self, path):
    """
    Remove a directory (must be empty).
    
    Args:
        path (str): Path to directory to remove
        
    Returns:
        int: 0 on success, negative errno on error
    """

Usage Example:

def mknod(self, path, mode, dev):
    if path in self.files:
        return -errno.EEXIST
    
    # Create regular file
    if stat.S_ISREG(mode):
        self.files[path] = b''
        return 0
    else:
        return -errno.EPERM  # Only support regular files

def mkdir(self, path, mode):
    if path in self.dirs:
        return -errno.EEXIST
    
    # Check parent exists
    parent = os.path.dirname(path)
    if parent != '/' and parent not in self.dirs:
        return -errno.ENOENT
    
    self.dirs[path] = {}
    return 0

def unlink(self, path):
    if path not in self.files:
        return -errno.ENOENT
    
    del self.files[path]
    return 0

Linking Operations

Create and manage file links including symbolic and hard links.

def symlink(self, target, path):
    """
    Create a symbolic link.
    
    Args:
        target (str): Target path the symlink points to
        path (str): Path where to create the symlink
        
    Returns:
        int: 0 on success, negative errno on error
    """

def readlink(self, path):
    """
    Read the target of a symbolic link.
    
    Args:
        path (str): Path to symbolic link
        
    Returns:
        str: Target path that the symlink points to
        int: Negative errno on error (e.g., -errno.ENOENT, -errno.EINVAL)
    """

def link(self, target, path):
    """
    Create a hard link.
    
    Args:
        target (str): Existing file path
        path (str): New link path
        
    Returns:
        int: 0 on success, negative errno on error
    """

Usage Example:

def symlink(self, target, path):
    if path in self.files or path in self.links:
        return -errno.EEXIST
    
    self.links[path] = target
    return 0

def readlink(self, path):
    if path not in self.links:
        return -errno.ENOENT
    
    return self.links[path]

def link(self, target, path):
    if target not in self.files:
        return -errno.ENOENT
    
    if path in self.files:
        return -errno.EEXIST
    
    # Create hard link by sharing content
    self.files[path] = self.files[target]
    return 0

File Movement and Renaming

Move and rename files and directories within the filesystem.

def rename(self, old_path, new_path):
    """
    Rename/move a file or directory.
    
    Args:
        old_path (str): Current path
        new_path (str): New path
        
    Returns:
        int: 0 on success, negative errno on error
    """

Usage Example:

def rename(self, old_path, new_path):
    # Check if source exists
    if old_path not in self.files and old_path not in self.dirs:
        return -errno.ENOENT
    
    # Check if destination parent exists
    parent = os.path.dirname(new_path)
    if parent != '/' and parent not in self.dirs:
        return -errno.ENOENT
    
    # Move file
    if old_path in self.files:
        if new_path in self.dirs:
            return -errno.EISDIR
        
        self.files[new_path] = self.files[old_path]
        del self.files[old_path]
    
    # Move directory
    elif old_path in self.dirs:
        if new_path in self.files:
            return -errno.ENOTDIR
        
        self.dirs[new_path] = self.dirs[old_path]
        del self.dirs[old_path]
    
    return 0

File Handle Operations

Extended file operations that work with file handles for efficiency.

def fgetattr(self, path, fh):
    """
    Get file attributes using file handle.
    
    Args:
        path (str): Path to file
        fh (int): File handle from open()
        
    Returns:
        Stat: File attributes object
        int: Negative errno on error
    """

def ftruncate(self, path, size, fh):
    """
    Truncate file using file handle.
    
    Args:
        path (str): Path to file
        size (int): New file size
        fh (int): File handle from open()
        
    Returns:
        int: 0 on success, negative errno on error
    """

Usage Example:

def fgetattr(self, path, fh):
    # Use file handle for more efficient attribute retrieval
    return self.getattr(path)

def ftruncate(self, path, size, fh):
    if path not in self.files:
        return -errno.ENOENT
    
    if size < len(self.files[path]):
        self.files[path] = self.files[path][:size]
    else:
        self.files[path] += b'\0' * (size - len(self.files[path]))
    
    return 0

Advanced File Operations

File Access Control

def access(self, path, mode):
    """
    Check file access permissions.
    
    Args:
        path (str): Path to file or directory
        mode (int): Access mode to check (R_OK, W_OK, X_OK, F_OK)
        
    Returns:
        int: 0 if access granted, negative errno on error
    """

Usage Example:

def access(self, path, mode):
    if path not in self.files and path not in self.dirs:
        return -errno.ENOENT
    
    # Simple permission check (in real implementation, check actual permissions)
    if mode & os.W_OK and path.startswith('/readonly'):
        return -errno.EACCES
    
    return 0

Block Mapping

def bmap(self, path, blocksize, idx):
    """
    Map file block to filesystem block (for direct I/O).
    
    Args:
        path (str): Path to file
        blocksize (int): Block size in bytes
        idx (int): Block index in file
        
    Returns:
        int: Physical block number, or negative errno on error
    """

Error Handling

File management operations commonly return these errors:

  • ENOENT: File or directory not found
  • EEXIST: File or directory already exists
  • ENOTDIR: Component in path is not a directory
  • EISDIR: Is a directory (when file expected)
  • ENOTEMPTY: Directory not empty (for rmdir)
  • EXDEV: Cross-device link (for link operations)
  • EACCES: Permission denied
  • EPERM: Operation not permitted
  • ENOSPC: No space left on device

Best Practices

  • Always check parent directory existence before creating files/directories
  • Ensure proper cleanup of resources when operations fail
  • Handle atomic operations (like create) properly to avoid partial states
  • Consider filesystem-specific constraints (maximum filename length, etc.)
  • Implement proper reference counting for hard links
  • Validate paths and handle edge cases (empty paths, root directory operations)

Install with Tessl CLI

npx tessl i tessl/pypi-fuse-python

docs

configuration.md

core-operations.md

data-structures.md

extended-features.md

file-management.md

index.md

tile.json