Struct PrivateStorage

Source
pub struct PrivateStorage<'a, R: Runtime>(/* private fields */);
Expand description

API of file storage intended for the app’s use only.

§Examples

fn example(app: &tauri::AppHandle) {
    use tauri_plugin_android_fs::AndroidFsExt;
 
    let api = app.android_fs();
    let private_storage = api.private_storage();
}

Implementations§

Source§

impl<'a, R: Runtime> PrivateStorage<'a, R>

Source

pub fn resolve_path(&self, dir: PrivateDir) -> Result<PathBuf>

Get the absolute path of the specified directory.
App can fully manage entries within this directory without any permission via std::fs.

These files will be deleted when the app is uninstalled and may also be deleted at the user’s initialising request.
When using PrivateDir::Cache, the system will automatically delete files in this directory as disk space is needed elsewhere on the device.

The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

§Examples
use tauri_plugin_android_fs::{AndroidFs, AndroidFsExt, PrivateDir, PrivateStorage};
 
fn example(app: tauri::AppHandle) {
    let api = app.android_fs().private_storage();
 
    let dir_path = api.resolve_path(PrivateDir::Data).unwrap();
    let file_path = dir_path.join("2025-2-12/data.txt");
     
    // Write file
    std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
    std::fs::write(&file_path, "aaaa").unwrap();
 
    // Read file
    let _ = std::fs::read_to_string(&file_path).unwrap();
 
    // Remove file
    std::fs::remove_file(&file_path).unwrap();
}
§Support

All.

Source

pub fn resolve_path_with( &self, dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<PathBuf>

Get the absolute path of the specified relative path and base directory.
App can fully manage entries of this path without any permission via std::fs.

See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn resolve_uri(&self, dir: PrivateDir) -> Result<FileUri>

Source

pub fn resolve_uri_with( &self, dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<FileUri>

Source

pub fn write( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, contents: impl AsRef<[u8]>, ) -> Result<()>

Writes a slice as the entire contents of a file.

This function will create a file if it does not exist, and will entirely replace its contents if it does.
Recursively create parent directories if they are missing.

This internally uses PrivateStorage::resolve_path , std::fs::create_dir_all, and std::fs::write.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn open_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File>

Open a file in read-only mode.

If you only need to read the entire file contents, consider using PrivateStorage::read or PrivateStorage::read_to_string instead.

This internally uses PrivateStorage::resolve_path and std::fs::File::open.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn create_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File>

Opens a file in write-only mode.
This function will create a file if it does not exist, and will truncate it if it does.

If you only need to write the contents, consider using PrivateStorage::write instead.

This internally uses PrivateStorage::resolve_path and std::fs::File::create.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn create_new_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<File>

Creates a new file in read-write mode; error if the file exists.

This internally uses PrivateStorage::resolve_path and std::fs::File::create_new.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn read( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<Vec<u8>>

Reads the entire contents of a file into a bytes vector.

If you need std::fs::File, use PrivateStorage::open_file insted.

This internally uses PrivateStorage::resolve_path and std::fs::read.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn read_to_string( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<String>

Reads the entire contents of a file into a string.

If you need std::fs::File, use PrivateStorage::open_file insted.

This internally uses PrivateStorage::resolve_path and std::fs::read_to_string.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn read_dir( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<ReadDir>

Returns an iterator over the entries within a directory.

This internally uses PrivateStorage::resolve_path and std::fs::read_dir.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn remove_file( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<()>

Removes a file from the filesystem.

This internally uses PrivateStorage::resolve_path and std::fs::remove_file.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn remove_dir( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<()>

Removes an empty directory.
If you want to remove a directory that is not empty, as well as all of its contents recursively, consider using PrivateStorage::remove_dir_all instead.

This internally uses PrivateStorage::resolve_path and std::fs::remove_dir.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn remove_dir_all( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<()>

Removes a directory at this path, after removing all its contents. Use carefully!

This internally uses PrivateStorage::resolve_path and std::fs::remove_dir_all.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn exists( &self, base_dir: PrivateDir, relative_path: impl AsRef<str>, ) -> Result<bool>

Returns Ok(true) if the path points at an existing entity.

This internally uses PrivateStorage::resolve_path and std::fs::exists.
See PrivateStorage::resolve_path for details.

§Support

All.

Source

pub fn metadata( &self, base_dir: PrivateDir, relative_path: Option<&str>, ) -> Result<Metadata>

Queries the file system to get information about a file, directory.

This internally uses PrivateStorage::resolve_path and std::fs::metadata.
See PrivateStorage::resolve_path for details.

§Support

All.

Auto Trait Implementations§

§

impl<'a, R> Freeze for PrivateStorage<'a, R>

§

impl<'a, R> RefUnwindSafe for PrivateStorage<'a, R>

§

impl<'a, R> Send for PrivateStorage<'a, R>

§

impl<'a, R> Sync for PrivateStorage<'a, R>

§

impl<'a, R> Unpin for PrivateStorage<'a, R>

§

impl<'a, R> UnwindSafe for PrivateStorage<'a, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T