File System API provides separate text and binary file operations
The file system plugin provides separate APIs for text and binary file operations for performance optimization. Text files use `readTextFile()` and `writeTextFile()`. Binary files use `readFile()` and `writeFile()`. Large text files can use `readTextFileLines()` for streaming line-by-line.
BaseDirectory option for file operations
All file system API methods accept a baseDir option that serves as the working directory. Available BaseDirectory values include: AppData, AppConfig, AppLocalData, AppCache, AppLog, Home, Temp, Desktop, Document, Download, and Resources. Example: `await readFile('avatar.png', { baseDir: BaseDirectory.AppData })`.
File open modes in file system plugin
The open() API supports these modes: read-only (default), write-only, append (can be combined with write), truncate (requires write: true, cuts file to length 0), create (creates file if missing, requires write or append), and createNew (creates only if file doesn't exist, requires write: true).
File handle must be closed after operations
Always call `file.close()` after completing file operations when using the file handle returned by `create()` or `open()` methods. Failing to close the file handle can lead to resource leaks.
File system watch function with debounce
The `watch()` function monitors file/directory changes with debouncing, meaning events are only emitted after a delay period with no activity. Accepts delayMs option to control the debounce delay. Requires the 'watch' feature in Cargo.toml: `tauri-plugin-fs = { version = "2.0.0", features = ["watch"] }`.
File system watchImmediate function for immediate notification
The `watchImmediate()` function notifies the event listener immediately when file/directory changes occur, without debouncing. Set `recursive: true` in options to monitor all subdirectories.
Directory removal requires recursive option for non-empty directories
The `remove()` function deletes files or directories. For non-empty directories, the `recursive: true` option must be set: `await remove('images', { baseDir: BaseDirectory.AppLocalData, recursive: true })`.
copyFile accepts separate base directories for source and destination
The `copyFile()` function accepts `fromPathBaseDir` and `toPathBaseDir` options to specify different base directories for source and destination paths. Example: `await copyFile('user.db', 'user.db.bk', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp })`.
rename function accepts separate base directories
The `rename()` function accepts `fromPathBaseDir` and `toPathBaseDir` options to specify different base directories for source and destination paths separately.
readDir function recursively lists directory contents
The `readDir()` function recursively lists the contents of a directory. It returns entries for all files and subdirectories within the specified directory.
stat vs lstat for file metadata
The `stat()` function follows symbolic links and returns metadata of the target file, throwing an error if the target is not in scope. The `lstat()` function does not follow symbolic links and returns metadata of the symbolic link itself.
truncate function usage with default and custom lengths
The `truncate()` function truncates or extends a file to a specified length (default 0). Example with length 0: `await truncate('my_file.txt', 0)`. Example with custom length: `await truncate('file.txt', 7)` truncates to 7 characters.
Rust std::fs and tokio::fs for backend file operations
For backend file and directory manipulation in Rust, use standard Rust libraries like std::fs or tokio::fs rather than the Tauri file system plugin. The plugin is designed for frontend access, with Rust backend methods limited to permission management.