Overview

The Path Parser provides comprehensive path resolution and management for FreeWorld OS. It handles virtual drives, UNC paths, path normalization, and drive management, providing Windows-compatible path handling.

Features

  • Virtual Drive Management: Register and manage virtual drives (C:, D:, etc.)
  • UNC Path Support: Parse and handle UNC paths (\\server\share\path)
  • Path Normalization: Resolve .. and . components
  • Path Resolution: Convert relative paths to absolute paths
  • Path Component Splitting: Extract directory, filename, extension
  • Drive Information: Query drive details and existence
  • Path Validation: Check if paths exist, are directories, or files

Virtual Drives

The Path Parser supports Windows-style virtual drives:

  • C: - Root filesystem (maps to /)
  • D: - Secondary drive (maps to /mnt/d)
  • Additional drives can be registered dynamically

Drive Management Functions

  • registerDrive(letter, path) - Register a virtual drive
  • unregisterDrive(letter) - Unregister a virtual drive
  • getDrivePath(letter) - Get path for a drive letter
  • getAllDrives() - Get all registered drives
  • getDriveInfo(letter) - Get detailed drive information

Path Operations

Path Parsing

  • parsePath(path, currentDir) - Parse and normalize a path
  • normalizePath(path) - Normalize path (resolve .. and .)
  • joinPaths(...paths) - Join multiple path components
  • resolvePath(processId, path) - Resolve path using process's current directory
  • resolveFullPath(path) - Resolve all symbolic links and relative components

Path Component Extraction

  • getDirectoryName(path) - Get directory portion of path
  • getFileName(path) - Get filename from path
  • getExtension(path) - Get file extension
  • splitPath(path) - Split path into all components (root, dir, base, name, ext)

Path Validation

  • isAbsolutePath(path) - Check if path is absolute
  • isUNCPath(path) - Check if path is UNC
  • pathExists(path) - Check if path exists
  • isDirectory(path) - Check if path is a directory
  • isFile(path) - Check if path is a file
  • getRootPath(path) - Get root path (drive or UNC server/share)

UNC Path Support

The Path Parser supports UNC (Universal Naming Convention) paths for network shares:

// Parse UNC path
const unc = pathParser.parseUNCPath('\\\\server\\share\\path\\file.txt');
// Returns: { server: 'server', share: 'share', path: 'path\\file.txt' }

UNC paths are automatically detected and parsed separately from local paths.

Current Directory Management

The Path Parser tracks current directory per process:

  • setCurrentDirectory(processId, path) - Set current directory for process
  • getCurrentDirectory(processId) - Get current directory for process

Usage Example

const PathParser = require('./system/path');

const pathParser = new PathParser();

// Register a new drive
pathParser.registerDrive('E:', '/mnt/e');

// Parse a path with virtual drive
const resolved = pathParser.parsePath('C:\\Users\\test\\file.txt');
// Returns: '/Users/test/file.txt'

// Parse UNC path
const unc = pathParser.parseUNCPath('\\\\server\\share\\file.txt');
// Returns: { server: 'server', share: 'share', path: 'file.txt' }

// Get path components
const parts = pathParser.splitPath('/usr/bin/node');
// Returns: { root: '/', dir: '/usr/bin', base: 'node', name: 'node', ext: '' }

// Check if path exists
if (pathParser.pathExists('/usr/bin/node')) {
    console.log('Node.js found');
}

Integration

The Path Parser integrates with:

  • Object Manager: Used for file path resolution
  • Shell: Used for command path resolution
  • ShellExecute: Used for executable path resolution
  • Filesystem: Used for all filesystem operations

Related Documentation