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 · Plugins and security · all subjects

fs/api

29 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

File stat, open, copyFile, and readDir functions overview

stat() retrieves file/directory metadata and follows symlinks. open() opens a file with customizable modes (read, write, append, truncate, create, createNew). copyFile() copies files between paths with separate baseDirs. readDir() recursively lists directory contents.

File System baseDir option usage

All File System APIs accept an optional baseDir argument that serves as the working directory for the operation. Example: await readFile('avatars/tauri.png', { baseDir: BaseDirectory.Home }) reads ~/avatars/tauri.png.

File System create function

The create() function creates a file and returns a file handle. If the file already exists, it is truncated. Always call file.close() when done. Example: const file = await create('foo/bar.txt', { baseDir: BaseDirectory.AppData }); await file.write(new TextEncoder().encode('Hello world')); await file.close();

File System writeTextFile and writeFile functions

writeTextFile() writes text content to a file. writeFile() writes binary content as Uint8Array. Both functions truncate existing files by default. Example text: await writeTextFile('config.json', JSON.stringify({notifications: true}), {baseDir: BaseDirectory.AppConfig}). Example binary: await writeFile('config', new Uint8Array(), {baseDir: BaseDirectory.AppConfig});

File System open function with read mode

open() with {read: true} opens a file in read-only mode (default). Example: 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 function with write mode

open() with {write: true} opens a file in write-only mode. By default, file.write() truncates the file. Example: const file = await open('foo/bar.txt', {write: true, baseDir: BaseDirectory.AppData}); await file.write(new TextEncoder().encode('Hello world')); await file.close();

File System open function with append mode

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

File System open function with truncate option

open() with {truncate: true} truncates an existing file to length 0. Requires {write: true}. The truncate option can be used alongside append option when overwriting an existing file with multiple write() calls. Example: 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 function with createNew option

open() with {createNew: true} creates a file only if it doesn't already exist. Requires {write: true}. If file exists, it won't be created. Example: const file = await open('foo/bar.txt', {write: true, createNew: true, baseDir: BaseDirectory.AppData}); await file.write(new TextEncoder().encode('world')); await file.close();

File System readTextFile function

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

File System readTextFileLines for streaming large files

readTextFileLines() allows streaming reading of large text files line by line. Example: const lines = await readTextFileLines('app.logs', {baseDir: BaseDirectory.AppLog}); for await (const line of lines) { console.log(line); }

File System readFile for binary data

readFile() reads binary file content as Uint8Array. Example: const icon = await readFile('icon.png', {baseDir: BaseDirectory.Resources});

File System remove function for files

remove() deletes a file. Returns error if file doesn't exist. Example: await remove('user.db', {baseDir: BaseDirectory.AppLocalData});

File System copyFile function

copyFile() copies a file from source to destination. Each path requires its own separate baseDir. Example: await copyFile('user.db', 'user.db.bk', {fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp}); copies <app-local-data>/user.db to $TMPDIR/user.db.bk

File System exists function

exists() checks if a file or directory exists. Example: const tokenExists = await exists('token', {baseDir: BaseDirectory.AppLocalData});

File System stat and lstat functions for metadata

stat() and lstat() retrieve file metadata. stat() follows symbolic links (returns error if target not in scope). lstat() does not follow symbolic links and returns info about the link itself. Example: const metadata = await stat('app.db', {baseDir: BaseDirectory.AppLocalData});

File System rename function

rename() changes file name from source to destination path. Each path requires its own separate baseDir. Example: await rename('user.db.bk', 'user.db', {fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp}); renames <app-local-data>/user.db.bk to $TMPDIR/user.db

File System truncate function

truncate() truncates or extends a file to specified length (default 0). Example truncate to 0: await truncate('my_file.txt', 0, {baseDir: BaseDirectory.AppLocalData}). Example truncate to 7 bytes: await truncate('file.txt', 7, {baseDir: BaseDirectory.AppLocalData}); after writing 'Hello World' produces 'Hello W'.

File System mkdir function for directory creation

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

File System readDir function for directory listing

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

File System remove function for directories

remove() deletes a directory. For non-empty directories, set {recursive: true}. Example empty: await remove('images', {baseDir: BaseDirectory.AppLocalData}). Example recursive: await remove('images', {baseDir: BaseDirectory.AppLocalData, recursive: true});

File System watch function with debounce

watch() monitors file/directory changes with debounce (events fire after delay). Example: await watch('app.log', (event) => {console.log('app.log event', event);}, {baseDir: BaseDirectory.AppLog, delayMs: 500});

File System watchImmediate function for real-time monitoring

watchImmediate() monitors file/directory changes with immediate notification. Example: await watchImmediate('logs', (event) => {console.log('logs directory event', event);}, {baseDir: BaseDirectory.AppLog, recursive: true});

File System watch recursive option for subdirectories

By default, directory watching is not recursive. To recursively watch all subdirectories for changes, set {recursive: true} in watch or watchImmediate options.

File System scope variables for common paths

Scope entries can use 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. Each maps to a system directory path.

File System dot-file access on Unix systems

For accessing Unix dot-files (.gitignore) or dot-folders (.ssh), either specify the full path (/home/user/.ssh/example) or add a glob after the dot-folder component (/home/user/.ssh/*). If this doesn't work, use requireLiteralLeadingDot: false in tauri.conf.json plugins.fs configuration.

File System requireLiteralLeadingDot configuration

In src-tauri/tauri.conf.json, set "requireLiteralLeadingDot": false in plugins.fs configuration to treat any path component as valid path literal, allowing access to dot-files and dot-folders without glob patterns.

File System API function renames

In tauri-plugin-fs: Dir alias removed, use BaseDirectory. createDir renamed to mkdir. readBinaryFile renamed to readFile. removeDir and removeFile replaced with remove. renameFile replaced with rename. writeBinaryFile renamed to writeFile. FileEntry, FsBinaryFileOption, FsDirOptions, FsOptions, FsTextFileOption, BinaryFileContents interfaces removed.

New fs.exists API in 1.1.0

Tauri 1.1.0 adds an exists API in the fs module.

Give your agent this brain