new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Tauri · all subjects

file system plugin

61 notes in this subject, read out of this brain and free to use. This is page 1 of 2.

File System remove directory

Remove a directory: `await remove('images', { baseDir: BaseDirectory.AppLocalData });` Returns an error if the directory does not exist.

File System remove non-empty directory

Remove a non-empty directory with recursive option: `await remove('images', { baseDir: BaseDirectory.AppLocalData, recursive: true });`

File System plugin JavaScript import

Import File System functions with: `import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';`. With `withGlobalTauri: true`, use `const { exists, BaseDirectory } = window.__TAURI__.fs;`

iOS File System privacy manifest requirement

iOS apps must include a `PrivacyInfo.xcprivacy` file in `src-tauri/gen/apple` containing the `NSPrivacyAccessedAPICategoryFileTimestamp` key with the reason code `C617.1` to declare file timestamp access per Apple's privacy requirements effective May 1, 2024.

File System readDir lists directory contents recursively

List directory contents recursively: `const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });`

File System base directory option

All File System APIs accept an optional `baseDir` parameter that specifies the working directory for operations. Example: `await exists('avatar.png', { baseDir: BaseDirectory.AppData });` reads from `$APPDATA/avatar.png`.

File System Rust scope configuration example

In Rust, configure File System scope with: `let scope = app.fs_scope(); scope.allow_directory("/path/to/directory", false); dbg!(scope.allowed());`

File System create operation

The `create()` function creates a file and returns its handle. If the file already exists, it is truncated. Always call `file.close()` after operations are complete.

File System write operations

The File System plugin provides separate APIs for performance: `writeTextFile()` for text files and `writeFile()` for binary files.

File System open read-only mode example

Open a file in read-only mode (default): `const file = await open('foo/bar.txt', { read: true, baseDir: BaseDirectory.AppData }); const stat = await file.stat(); const buf = new Uint8Array(stat.size); await file.read(buf); const textContents = new TextDecoder().decode(buf); await file.close();`

File System open append mode

Open a file in append mode: `const file = await open('foo/bar.txt', { append: true, baseDir: BaseDirectory.AppData }); await file.write(new TextEncoder().encode('world')); await file.close();` Setting `{ append: true }` has the same effect as `{ write: true, append: true }`.

File System open truncate mode

The `truncate` option truncates the file to length 0 if the file already exists and write is true: `const file = await open('foo/bar.txt', { write: true, truncate: true, baseDir: BaseDirectory.AppData }); await file.write(new TextEncoder().encode('world')); await file.close();`

File System open create mode

Set `create: true` to create a file if it does not exist or open it if it does. The `write` or `append` flag must also be true. Example: `const file = await open('foo/bar.txt', { write: true, create: true, baseDir: BaseDirectory.AppData });`

File System open createNew mode

The `createNew` option is like `create` but does not create the file if it already exists. The `write` flag must be true. Example: `const file = await open('foo/bar.txt', { write: true, createNew: true, baseDir: BaseDirectory.AppData });`

File System readTextFile operation

Read a text file: `const configToml = await readTextFile('config.toml', { baseDir: BaseDirectory.AppConfig });`

File System readTextFileLines for streaming

For large files, use `readTextFileLines()` for streaming line-by-line reads: `const lines = await readTextFileLines('app.logs', { baseDir: BaseDirectory.AppLog }); for await (const line of lines) { console.log(line); }`

File System readFile binary operation

Read a binary file: `const icon = await readFile('icon.png', { baseDir: BaseDirectory.Resources });`

File System remove operation

Delete a file: `await remove('user.db', { baseDir: BaseDirectory.AppLocalData });` Returns an error if the file does not exist.

File System copyFile operation with separate base directories

Copy a file specifying separate base directories for source and destination: `await copyFile('user.db', 'user.db.bk', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp });` This copies `<app-local-data>/user.db` to `$TMPDIR/user.db.bk`.

File System exists check

Check if a file exists: `const tokenExists = await exists('token', { baseDir: BaseDirectory.AppLocalData });`

File System stat vs lstat for metadata

Use `stat()` to get file metadata; it follows symlinks and returns an error if the target is not permitted. Use `lstat()` to get symlink metadata without following it.

File System stat metadata example

Get file metadata: `const metadata = await stat('app.db', { baseDir: BaseDirectory.AppLocalData });`

File System rename operation with separate base directories

Rename a file specifying separate base directories: `await rename('user.db.bk', 'user.db', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp });` This renames `<app-local-data>/user.db.bk` to `$TMPDIR/user.db`.

File System truncate to zero length

Truncate a file to zero length: `await truncate('my_file.txt', 0, { baseDir: BaseDirectory.AppLocalData });`

File System truncate to specific length

Truncate a file to a specific length: `await writeTextFile('file.txt', 'Hello World', { baseDir: BaseDirectory.AppLocalData }); await truncate('file.txt', 7, { baseDir: BaseDirectory.AppLocalData }); const data = await readTextFile('file.txt', { baseDir: BaseDirectory.AppLocalData }); console.log(data); // "Hello W"`

File System mkdir directory creation

Create a directory: `await mkdir('images', { baseDir: BaseDirectory.AppLocalData });`

File System watch function with debounce

The `watch()` function monitors file/directory changes with debouncing; events are emitted after a delay: `await watch('app.log', (event) => { console.log('app.log event', event); }, { baseDir: BaseDirectory.AppLog, delayMs: 500 });`

File System watchImmediate function

The `watchImmediate()` function notifies listeners immediately without debouncing: `await watchImmediate('logs', (event) => { console.log('logs directory event', event); }, { baseDir: BaseDirectory.AppLog, recursive: true });`

File System watch recursive option

By default, watch operations are not recursive. Set `recursive: true` to recursively monitor all subdirectories.

File System permissions enabled via capabilities

By default, all potentially dangerous File System plugin commands are blocked. Enable them by modifying the `permissions` in the `capabilities` configuration.

File System scope path variables

Scope entries support path variables: $APPCONFIG, $APPDATA, $APPLOCALDATA, $APPCACHE, $APPLOG, $AUDIO, $CACHE, $CONFIG, $DATA, $LOCALDATA, $DESKTOP, $DOCUMENT, $DOWNLOAD, $EXE, $FONT, $HOME, $PICTURE, $PUBLIC, $RUNTIME, $TEMPLATE, $VIDEO, $RESOURCE, $TEMP.

File System global scope configuration example

Apply scope to all fs commands using `fs:scope` permission: `{ "identifier": "fs:scope", "allow": [{ "path": "$APPDATA" }, { "path": "$APPDATA/**/*" }] }`

File System command-specific scope configuration

Apply scope to specific fs commands using object format: `{ "identifier": "fs:allow-rename", "allow": [{ "path": "$HOME/**/*" }], "deny": [{ "path": "$HOME/.config/**/*" }] }` allows rename on `$HOME/**/*` but denies on `$HOME/.config/**/*`.

File System deny scope takes precedence

The `deny` scope takes precedence over `allow` scope. If a path is denied by any scope, it is blocked at runtime even if permitted by another scope.

File System dot file and dot folder path access

To access Unix-based dot files or dot folders, specify the complete path (e.g., `/home/user/.ssh/example`) or add a glob wildcard after the dot component (e.g., `/home/user/.ssh/*`).

File System requireLiteralLeadingDot configuration

Configure the plugin to treat any path component as a valid path literal by setting `requireLiteralLeadingDot: false` in `src-tauri/tauri.conf.json` under the `plugins.fs` section.

File System plugin installation command

Install the fs plugin using the command: `npm run tauri add fs`, `yarn run tauri add fs`, `pnpm tauri add fs`, `deno task tauri add fs`, `bun tauri add fs`, or `cargo tauri add fs` depending on your package manager.

File System plugin Rust initialization

Initialize the fs plugin in `src-tauri/src/lib.rs` by adding `.plugin(tauri_plugin_fs::init())` to the tauri::Builder::default() chain.

File System plugin JavaScript bindings

Install the JavaScript Guest bindings for the fs plugin using: `npm install @tauri-apps/plugin-fs`, `yarn add @tauri-apps/plugin-fs`, `pnpm add @tauri-apps/plugin-fs`, `deno add npm:@tauri-apps/plugin-fs`, or `bun add @tauri-apps/plugin-fs`.

Android external storage permissions for File System plugin

When using audio, cache, documents, downloads, picture, public, or video directories on Android, include these permissions in the `manifest` tag of `gen/android/app/src/main/AndroidManifest.xml`: `<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>` and `<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />`.

iOS PrivacyInfo.xcprivacy file for File System plugin

Create a `PrivacyInfo.xcprivacy` file in `src-tauri/gen/apple` folder with NSPrivacyAccessedAPICategoryFileTimestamp key and reason C617.1 for iOS file system access. The file must be a valid plist XML with NSPrivacyAccessedAPITypes array containing the required privacy declaration.

File System plugin prevents path traversal attacks

The File System plugin prevents path traversal by not allowing parent directory accessors. Paths like "/usr/path/to/../file" or "../path/to/file" are not allowed. Paths must be either relative to a base directory or created with the path API.

File System plugin base directories scope variables

The fs plugin supports the following path scope variables: $APPCONFIG (appConfigDir), $APPDATA (appDataDir), $APPLOCALDATA (appLocalDataDir), $APPCACHE (appcacheDir), $APPLOG (applogDir), $AUDIO (audioDir), $CACHE (cacheDir), $CONFIG (configDir), $DATA (dataDir), $LOCALDATA (localDataDir), $DESKTOP (desktopDir), $DOCUMENT (documentDir), $DOWNLOAD (downloadDir), $EXE (executableDir), $FONT (fontDir), $HOME (homeDir), $PICTURE (pictureDir), $PUBLIC (publicDir), $RUNTIME (runtimeDir), $TEMPLATE (templateDir), $VIDEO (videoDir), $RESOURCE (resourceDir), $TEMP (tempDir).

File System plugin deny takes precedence over allow in scopes

In File System plugin permission scopes, `deny` takes precedence over `allow`. If a path is denied by a scope, it will be blocked at runtime even if it is allowed by another scope.

File System plugin watch feature flag requirement

The watch and watchImmediate functions in the File System plugin require the `watch` feature flag to be enabled in `src-tauri/Cargo.toml`: `tauri-plugin-fs = { version = "2.0.0", features = ["watch"] }`.

File System plugin dotfile access configuration

To access dotfiles (e.g., `.gitignore`) or dotfolders (e.g., `.ssh`) on Unix-based systems with the File System plugin, specify either the full path `/home/user/.ssh/example` or the glob after the dotfolder path component `/home/user/.ssh/*`. Alternatively, set `requireLiteralLeadingDot: false` in `src-tauri/tauri.conf.json` under `plugins.fs` to treat any component as a valid path literal.

File System plugin file.close() requirement

Always call `file.close()` when done manipulating files obtained from the `create()` or `open()` APIs in the File System plugin to properly release file handles.

File System plugin write modes: text vs binary

The File System plugin offers separate APIs for writing text and binary files for performance optimization. Use `writeTextFile()` for text files and `writeFile()` for binary files (Uint8Array).

File System plugin read modes: text vs binary

The File System plugin offers separate APIs for reading text and binary files for performance. Use `readTextFile()` for text files, `readTextFileLines()` for streaming large text files line-by-line, and `readFile()` for binary files (returns Uint8Array).

File System plugin open() file options

The `open()` function in File System plugin accepts these options: `read` (boolean, default true for read-only), `write` (boolean), `append` (boolean), `truncate` (boolean, requires write=true), `create` (boolean, requires write or append=true), and `createNew` (boolean, requires write=true and fails if file exists).

File System plugin copyFile requires separate base directories

The `copyFile()` function in the File System plugin takes `fromPathBaseDir` and `toPathBaseDir` options. You must configure each base directory separately, as they can be different.

File System plugin rename requires separate base directories

The `rename()` function in the File System plugin takes `fromPathBaseDir` and `toPathBaseDir` options. You must configure each base directory separately, as they can be different.

File System plugin stat vs lstat functions

The `stat()` function follows symlinks and returns an error if the actual file is not allowed by the scope. The `lstat()` function does not follow symlinks and returns information about the symlink itself.

File System plugin truncate function

The `truncate()` function in the File System plugin truncates or extends a file to reach a specified length. It defaults to 0 if no length is specified.

File System plugin remove() with recursive option

The `remove()` function in the File System plugin can delete both files and directories. To delete a non-empty directory, set the `recursive` option to `true`.

File System plugin readDir function

The `readDir()` function in the File System plugin recursively lists the entries of a directory and returns an array of directory entries.

File System plugin watch vs watchImmediate

The File System plugin provides two watch functions: `watch()` is debounced and only emits events after a delay (configurable with `delayMs` option), while `watchImmediate()` immediately notifies listeners of file system events.

File System plugin recursive watch option

By default, watch operations on a directory in the File System plugin are not recursive. Set the `recursive` option to `true` to recursively watch for changes on all sub-directories.

Rust-side file manipulation with std::fs or tokio::fs

For file manipulation through Rust in a Tauri application, use traditional Rust libraries like std::fs, tokio::fs, or others instead of the fs plugin API, which is primarily for frontend JavaScript access.

tauri::scope::fs module in Tauri 2.0

Tauri 2.0 added `tauri::scope::fs` module. The old `tauri::scope::FsScope`, `tauri::scope::GlobPattern`, and `tauri::scope::FsScopeEvent` were removed and replaced with `tauri::scope::fs::Scope`, `tauri::scope::fs::Pattern`, and `tauri::scope::fs::Event` respectively.

Give your agent this brain