檔案系統
存取檔案系統。
支援平台
此外掛程式需要至少 1.77.2 的 Rust 版本
平台 | 層級 | 註解 |
---|---|---|
windows | 透過 MSI 或 NSIS 以 | |
linux | 沒有 | |
macos | 沒有 | |
android | | 預設情況下,存取權限僅限於應用程式資料夾 |
ios | | 預設情況下,存取權限僅限於應用程式資料夾 |
設定
安裝 fs 外掛程式以開始使用。
使用專案的套件管理器新增依賴項
npm run tauri add fs
yarn run tauri add fs
pnpm tauri add fs
deno task tauri add fs
bun tauri add fs
cargo tauri add fs
-
在
src-tauri
資料夾中執行以下命令,將外掛程式新增至Cargo.toml
中的專案依賴項cargo add tauri-plugin-fs -
修改
lib.rs
以初始化外掛程式src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_fs::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用您偏好的 JavaScript 套件管理器安裝 JavaScript Guest 綁定
npm install @tauri-apps/plugin-fsyarn add @tauri-apps/plugin-fspnpm add @tauri-apps/plugin-fsdeno add npm:@tauri-apps/plugin-fsbun add @tauri-apps/plugin-fs
組態
Android
當使用 audio、cache、documents、downloads、picture、public 或 video 目錄時,您的應用程式必須具有外部儲存空間的存取權限。
在 gen/android/app/src/main/AndroidManifest.xml
檔案中的 manifest
標籤中包含下列權限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
iOS
Apple 要求應用程式開發人員指定核准的 API 使用理由,以增強使用者隱私。
您必須在 src-tauri/gen/apple
資料夾中建立 PrivacyInfo.xcprivacy
檔案,其中包含必要的 NSPrivacyAccessedAPICategoryFileTimestamp 鍵和 C617.1 建議理由。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"> <dict> <key>NSPrivacyAccessedAPITypes</key> <array> <dict> <key>NSPrivacyAccessedAPIType</key> <string>NSPrivacyAccessedAPICategoryFileTimestamp</string> <key>NSPrivacyAccessedAPITypeReasons</key> <array> <string>C617.1</string> </array> </dict> </array> </dict></plist>
用法
fs 外掛程式在 JavaScript 和 Rust 中均可用。
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';// when using `"withGlobalTauri": true`, you may use// const { exists, BaseDirectory } = window.__TAURI__.fs;
// Check if the `$APPDATA/avatar.png` file existsawait exists('avatar.png', { baseDir: BaseDirectory.AppData });
use tauri_plugin_fs::FsExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_fs::init()) .setup(|app| { // allowed the given directory let scope = app.fs_scope(); scope.allow_directory("/path/to/directory", false); dbg!(scope.allowed());
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
安全性
此模組可防止路徑遍歷,不允許使用父目錄存取器(即不允許使用「/usr/path/to/../file」或「../path/to/file」路徑)。使用此 API 存取的路徑必須相對於 基本目錄 之一,或使用 路徑 API 建立。
有關更多資訊,請參閱 @tauri-apps/plugin-fs - 安全性。
路徑
檔案系統外掛程式提供兩種操作路徑的方式:基本目錄 和 路徑 API。
-
基本目錄
每個 API 都有一個選項參數,可讓您定義一個 baseDir,作為操作的工作目錄。
import { readFile } from '@tauri-apps/plugin-fs';const contents = await readFile('avatars/tauri.png', {baseDir: BaseDirectory.Home,});在上面的範例中,由於我們使用的是 Home 基本目錄,因此讀取了 ~/avatars/tauri.png 檔案。
-
路徑 API
或者,您可以使用路徑 API 來執行路徑操作。
import { readFile } from '@tauri-apps/plugin-fs';import * as path from '@tauri-apps/api/path';const home = await path.homeDir();const contents = await readFile(await path.join(home, 'avatars/tauri.png'));
檔案
建立
建立檔案並傳回其句柄。如果檔案已存在,則會截斷檔案。
import { create, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await create('foo/bar.txt', { baseDir: BaseDirectory.AppData });await file.write(new TextEncoder().encode('Hello world'));await file.close();
寫入
此外掛程式提供用於寫入文字和二進制檔案的獨立 API,以提高效能。
-
文字檔案
import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = JSON.stringify({ notifications: true });await writeTextFile('config.json', contents, {baseDir: BaseDirectory.AppConfig,}); -
二進制檔案
import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = new Uint8Array(); // fill a byte arrayawait writeFile('config', contents, {baseDir: BaseDirectory.AppConfig,});
開啟
開啟檔案並傳回其句柄。透過此 API,您可以更精確地控制檔案的開啟方式(唯讀模式、唯寫模式、附加而非覆寫、僅在檔案不存在時建立等等)。
-
唯讀
這是預設模式。
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';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(); -
唯寫
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('Hello world'));await file.close();預設情況下,檔案會在任何
file.write()
呼叫時被截斷。請參閱以下範例,以了解如何附加到現有內容。 -
附加
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {append: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();請注意,
{ append: true }
與{ write: true, append: true }
具有相同的效果。 -
截斷
當設定
truncate
選項且檔案已存在時,檔案將被截斷為長度 0。import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,truncate: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();此選項需要將
write
設定為true
。如果您想使用多個
file.write()
呼叫來重寫現有檔案,則可以將其與append
選項一起使用。 -
建立
預設情況下,
open
API 僅開啟現有檔案。若要在檔案不存在時建立檔案,並在檔案存在時開啟檔案,請將create
設定為true
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,create: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();為了建立檔案,也必須將
write
或append
設定為true
。若要在檔案已存在時失敗,請參閱
createNew
。 -
createNew
createNew
的運作方式與create
類似,但如果檔案不存在,則操作會失敗。import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open('foo/bar.txt', {write: true,createNew: true,baseDir: BaseDirectory.AppData,});await file.write(new TextEncoder().encode('world'));await file.close();為了建立檔案,也必須將
write
設定為true
。
讀取
此外掛程式提供用於讀取文字和二進制檔案的獨立 API,以提高效能。
-
文字檔案
import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';const configToml = await readTextFile('config.toml', {baseDir: BaseDirectory.AppConfig,});如果檔案很大,您可以使用
readTextFileLines
API 串流其行。import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';const lines = await readTextFileLines('app.logs', {baseDir: BaseDirectory.AppLog,});for await (const line of lines) {console.log(line);} -
二進制檔案
import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';const icon = await readFile('icon.png', {baseDir: BaseDirectory.Resources,});
移除
呼叫 remove()
以刪除檔案。如果檔案不存在,則會傳回錯誤。
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('user.db', { baseDir: BaseDirectory.AppLocalData });
複製
copyFile
函數接受來源和目標路徑。請注意,您必須分別配置每個基本目錄。
import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';await copyFile('user.db', 'user.db.bk', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp,});
在上面的範例中,<app-local-data>/user.db 檔案被複製到 $TMPDIR/user.db.bk。
存在
使用 exists()
函數檢查檔案是否存在
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';const tokenExists = await exists('token', { baseDir: BaseDirectory.AppLocalData,});
元數據
可以使用 stat
和 lstat
函數檢索檔案元數據。stat
跟隨符號連結(如果符號連結指向的實際檔案不在範圍內,則傳回錯誤),而 lstat
不跟隨符號連結,而是傳回符號連結本身的資訊。
import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';const metadata = await stat('app.db', { baseDir: BaseDirectory.AppLocalData,});
重新命名
rename
函數接受來源和目標路徑。請注意,您必須分別配置每個基本目錄。
import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';await rename('user.db.bk', 'user.db', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp,});
在上面的範例中,<app-local-data>/user.db.bk 檔案被重新命名為 $TMPDIR/user.db。
截斷
截斷或擴展指定的檔案,使其達到指定的檔案長度(預設為 0)。
- 截斷為 0 長度
import { truncate } from '@tauri-apps/plugin-fs';await truncate('my_file.txt', 0, { baseDir: BaseDirectory.AppLocalData });
- 截斷為特定長度
import { truncate, readTextFile, writeTextFile, BaseDirectory,} from '@tauri-apps/plugin-fs';
const filePath = 'file.txt';await writeTextFile(filePath, 'Hello World', { baseDir: BaseDirectory.AppLocalData,});await truncate(filePath, 7, { baseDir: BaseDirectory.AppLocalData,});const data = await readTextFile(filePath, { baseDir: BaseDirectory.AppLocalData,});console.log(data); // "Hello W"
目錄
建立
若要建立目錄,請呼叫 mkdir
函數
import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';await mkdir('images', { baseDir: BaseDirectory.AppLocalData,});
讀取
readDir
函數會遞迴列出目錄的項目
import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });
移除
呼叫 remove()
以刪除目錄。如果目錄不存在,則會傳回錯誤。
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('images', { baseDir: BaseDirectory.AppLocalData });
如果目錄不是空的,則必須將 recursive
選項設定為 true
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('images', { baseDir: BaseDirectory.AppLocalData, recursive: true,});
存在
使用 exists()
函數檢查目錄是否存在
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';const tokenExists = await exists('images', { baseDir: BaseDirectory.AppLocalData,});
元數據
可以使用 stat
和 lstat
函數檢索目錄元數據。stat
跟隨符號連結(如果符號連結指向的實際檔案不在範圍內,則傳回錯誤),而 lstat
不跟隨符號連結,而是傳回符號連結本身的資訊。
import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';const metadata = await stat('databases', { baseDir: BaseDirectory.AppLocalData,});
監看變更
若要監看目錄或檔案的變更,請使用 watch
或 watchImmediate
函數。
-
watch
watch
經過防抖處理,因此僅會在特定延遲後發出事件import { watch, BaseDirectory } from '@tauri-apps/plugin-fs';await watch('app.log',(event) => {console.log('app.log event', event);},{baseDir: BaseDirectory.AppLog,delayMs: 500,}); -
watchImmediate
watchImmediate
會立即通知監聽器事件import { watchImmediate, BaseDirectory } from '@tauri-apps/plugin-fs';await watchImmediate('logs',(event) => {console.log('logs directory event', event);},{baseDir: BaseDirectory.AppLog,recursive: true,});
預設情況下,目錄上的監看操作不是遞迴的。將 recursive
選項設定為 true
,以遞迴監看所有子目錄的變更。
權限
預設情況下,所有潛在危險的外掛程式命令和範圍都被封鎖且無法存取。您必須修改 capabilities
組態中的權限才能啟用這些。
有關更多資訊,請參閱功能概觀,以及逐步指南以使用外掛程式權限。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ "fs:default", { "identifier": "fs:allow-exists", "allow": [{ "path": "$APPDATA/*" }] } ]}
預設權限
這組權限描述了 fs
外掛程式預設啟用或拒絕的檔案系統存取類型。
已授與的權限
此預設權限集啟用對應用程式特定目錄(AppConfig、AppData、AppLocalData、AppCache、AppLog)以及在其中建立的所有檔案和子目錄的讀取存取權限。這些目錄的位置取決於執行應用程式的作業系統。
一般來說,這些目錄需要在執行階段由應用程式手動建立,然後才能存取其中的檔案或資料夾。
因此,也允許透過 mkdir
命令建立所有這些資料夾。
拒絕的權限
此預設權限集預設會防止存取 Tauri 應用程式的重要組件。在 Windows 上,網頁檢視資料夾存取會被拒絕。
此預設權限集中包含的權限
create-app-specific-dirs
read-app-specific-dirs-recursive
deny-default
權限表
識別碼 | 描述 |
---|---|
|
這允許完整遞迴讀取應用程式的所有資料夾、檔案和子目錄。 |
|
這允許完整遞迴寫入應用程式的所有資料夾、檔案和子目錄。 |
|
這允許非遞迴讀取應用程式資料夾。 |
|
這允許非遞迴寫入應用程式資料夾。 |
|
這允許完整遞迴讀取應用程式資料夾的metadata(包含檔案列表和統計資訊)。 |
|
這允許非遞迴讀取應用程式資料夾的metadata(包含檔案列表和統計資訊)。 |
|
此範圍允許遞迴存取應用程式的所有資料夾,包含子目錄和檔案。 |
|
此範圍允許存取應用程式資料夾中頂層目錄的所有檔案和列表內容。 |
|
此範圍允許列出應用程式目錄中的所有檔案和資料夾。 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
這允許完整遞迴讀取完整的 |
|
這允許完整遞迴寫入完整的 |
|
這允許非遞迴讀取 |
|
這允許非遞迴寫入 |
|
這允許完整遞迴讀取 |
|
這允許非遞迴讀取 |
|
此範圍允許遞迴存取完整的 |
|
此範圍允許存取 |
|
此範圍允許列出 |
|
啟用 copy_file 命令,而無需任何預先設定的範圍。 |
|
拒絕 copy_file 命令,而無需任何預先設定的範圍。 |
|
啟用 create 命令,而無需任何預先設定的範圍。 |
|
拒絕 create 命令,而無需任何預先設定的範圍。 |
|
啟用 exists 命令,而無需任何預先設定的範圍。 |
|
拒絕 exists 命令,而無需任何預先設定的範圍。 |
|
啟用 fstat 命令,而無需任何預先設定的範圍。 |
|
拒絕 fstat 命令,而無需任何預先設定的範圍。 |
|
啟用 ftruncate 命令,而無需任何預先設定的範圍。 |
|
拒絕 ftruncate 命令,而無需任何預先設定的範圍。 |
|
啟用 lstat 命令,而無需任何預先設定的範圍。 |
|
拒絕 lstat 命令,而無需任何預先設定的範圍。 |
|
啟用 mkdir 命令,而無需任何預先設定的範圍。 |
|
拒絕 mkdir 命令,而無需任何預先設定的範圍。 |
|
啟用 open 命令,而無需任何預先設定的範圍。 |
|
拒絕 open 命令,而無需任何預先設定的範圍。 |
|
啟用 read 命令,而無需任何預先設定的範圍。 |
|
拒絕 read 命令,而無需任何預先設定的範圍。 |
|
啟用 read_dir 命令,而無需任何預先設定的範圍。 |
|
拒絕 read_dir 命令,而無需任何預先設定的範圍。 |
|
啟用 read_file 命令,而無需任何預先設定的範圍。 |
|
拒絕 read_file 命令,而無需任何預先設定的範圍。 |
|
啟用 read_text_file 命令,而無需任何預先設定的範圍。 |
|
拒絕 read_text_file 命令,而無需任何預先設定的範圍。 |
|
啟用 read_text_file_lines 命令,而無需任何預先設定的範圍。 |
|
拒絕未經預先設定範圍的 read_text_file_lines 命令。 |
|
啟用未經預先設定範圍的 read_text_file_lines_next 命令。 |
|
拒絕未經預先設定範圍的 read_text_file_lines_next 命令。 |
|
啟用未經預先設定範圍的 remove 命令。 |
|
拒絕未經預先設定範圍的 remove 命令。 |
|
啟用未經預先設定範圍的 rename 命令。 |
|
拒絕未經預先設定範圍的 rename 命令。 |
|
啟用未經預先設定範圍的 seek 命令。 |
|
拒絕未經預先設定範圍的 seek 命令。 |
|
啟用未經預先設定範圍的 size 命令。 |
|
拒絕未經預先設定範圍的 size 命令。 |
|
啟用未經預先設定範圍的 stat 命令。 |
|
拒絕未經預先設定範圍的 stat 命令。 |
|
啟用未經預先設定範圍的 truncate 命令。 |
|
拒絕未經預先設定範圍的 truncate 命令。 |
|
啟用未經預先設定範圍的 unwatch 命令。 |
|
拒絕未經預先設定範圍的 unwatch 命令。 |
|
啟用未經預先設定範圍的 watch 命令。 |
|
拒絕未經預先設定範圍的 watch 命令。 |
|
啟用未經預先設定範圍的 write 命令。 |
|
拒絕未經預先設定範圍的 write 命令。 |
|
啟用未經預先設定範圍的 write_file 命令。 |
|
拒絕未經預先設定範圍的 write_file 命令。 |
|
啟用未經預先設定範圍的 write_text_file 命令。 |
|
拒絕未經預先設定範圍的 write_text_file 命令。 |
|
此權限允許建立應用程式特定的目錄。 |
|
這會預設拒絕存取危險的 Tauri 相關檔案和資料夾。 |
|
這會拒絕讀取 Linux 上 |
|
這會拒絕讀取 Windows 上 |
|
這會啟用所有讀取相關的命令,而無需任何預先設定的可存取路徑。 |
|
此權限允許對應用程式特定的基本目錄進行遞迴讀取功能。 |
|
這會啟用目錄讀取和檔案metadata相關的命令,而無需任何預先設定的可存取路徑。 |
|
這會啟用檔案讀取相關的命令,而無需任何預先設定的可存取路徑。 |
|
這會啟用所有索引或metadata相關的命令,而無需任何預先設定的可存取路徑。 |
|
一個空的權限,您可以使用它來修改全域範圍。 |
|
這會啟用所有寫入相關的命令,而無需任何預先設定的可存取路徑。 |
|
這會啟用所有檔案寫入相關的命令,而無需任何預先設定的可存取路徑。 |
範圍
此插件權限包含用於定義允許或明確拒絕的路徑的範圍。有關範圍的更多資訊,請參閱命令範圍。
每個 allow
或 deny
範圍都必須包含一個陣列,列出所有應允許或拒絕的路徑。範圍條目採用 { path: string }
格式。
範圍條目可以使用 $<path>
變數來參考常見的系統路徑,例如 home 目錄、應用程式資源目錄和組態目錄。下表列出了您可以參考的所有常見路徑
路徑 | 變數 |
---|---|
appConfigDir | $APPCONFIG |
appDataDir | $APPDATA |
appLocalDataDir | $APPLOCALDATA |
appcacheDir | $APPCACHE |
applogDir | $APPLOG |
audioDir | $AUDIO |
cacheDir | $CACHE |
configDir | $CONFIG |
dataDir | $DATA |
localDataDir | $LOCALDATA |
desktopDir | $DESKTOP |
documentDir | $DOCUMENT |
downloadDir | $DOWNLOAD |
executableDir | $EXE |
fontDir | $FONT |
homeDir | $HOME |
pictureDir | $PICTURE |
publicDir | $PUBLIC |
runtimeDir | $RUNTIME |
templateDir | $TEMPLATE |
videoDir | $VIDEO |
resourceDir | $RESOURCE |
tempDir | $TEMP |
範例
- 全域範圍
若要將範圍應用於任何 fs
命令,請使用 fs:scope
權限
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "fs:scope", "allow": [{ "path": "$APPDATA" }, { "path": "$APPDATA/**" }] } ]}
若要將範圍應用於特定的 fs
命令,請使用物件形式的權限 { "identifier": string, "allow"?: [], "deny"?: [] }
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "fs:allow-rename", "allow": [{ "path": "$HOME/**" }] }, { "identifier": "fs:allow-rename", "deny": [{ "path": "$HOME/.config/**" }] }, { "identifier": "fs:allow-exists", "allow": [{ "path": "$APPDATA/*" }] } ]}
© 2025 Tauri Contributors。CC-BY / MIT