Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/fix-fs-android-file-may-crash-for-content-uri.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fs: patch:bug
fs-js: patch:bug
---

Fixes a crash that occurs when opening a file from an Android Content URI due to missing permissions or the file not existing.
5 changes: 3 additions & 2 deletions plugins/fs/android/src/main/java/FsPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// Copyright 2019-2026 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -45,7 +45,7 @@ class FsPlugin(private val activity: Activity): Plugin(activity) {
if (args.uri.startsWith(app.tauri.TAURI_ASSETS_DIRECTORY_URI)) {
val path = args.uri.substring(app.tauri.TAURI_ASSETS_DIRECTORY_URI.length)
try {
val fd = activity.assets.openFd(path).parcelFileDescriptor?.detachFd()
val fd = activity.assets.openFd(path).parcelFileDescriptor.detachFd()
res.put("fd", fd)
} catch (e: IOException) {
// if the asset is compressed, we cannot open a file descriptor directly
Expand All @@ -64,6 +64,7 @@ class FsPlugin(private val activity: Activity): Plugin(activity) {
Uri.parse(args.uri),
args.mode
)?.parcelFileDescriptor?.detachFd()
if (fd == null) throw IOException("No file or permission")
res.put("fd", fd)
}

Expand Down
12 changes: 4 additions & 8 deletions plugins/fs/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,10 @@ impl<R: Runtime> Fs<R> {
mode: mode.into(),
},
)?;
if let Some(fd) = result.fd {
Ok(unsafe {
use std::os::fd::FromRawFd;
std::fs::File::from_raw_fd(fd)
})
} else {
unimplemented!()
}
Ok(unsafe {
use std::os::fd::FromRawFd;
std::fs::File::from_raw_fd(result.fd)
})
}
}
}
2 changes: 1 addition & 1 deletion plugins/fs/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pub struct GetFileDescriptorPayload {
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetFileDescriptorResponse {
pub fd: Option<i32>,
pub fd: i32,
}