Skip to content

Commit b710350

Browse files
author
OkaYu
committed
fix(fs): update getting file descriptor from android content uri
1 parent 69a9d57 commit b710350

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fs: patch:bug
3+
fs-js: patch:bug
4+
---
5+
6+
Fixes a crash that occurs when opening a file from an Android Content URI due to missing permissions or the file not existing.

plugins/fs/android/src/main/java/FsPlugin.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
1+
// Copyright 2019-2026 Tauri Programme within The Commons Conservancy
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

@@ -45,7 +45,7 @@ class FsPlugin(private val activity: Activity): Plugin(activity) {
4545
if (args.uri.startsWith(app.tauri.TAURI_ASSETS_DIRECTORY_URI)) {
4646
val path = args.uri.substring(app.tauri.TAURI_ASSETS_DIRECTORY_URI.length)
4747
try {
48-
val fd = activity.assets.openFd(path).parcelFileDescriptor?.detachFd()
48+
val fd = activity.assets.openFd(path).parcelFileDescriptor.detachFd()
4949
res.put("fd", fd)
5050
} catch (e: IOException) {
5151
// if the asset is compressed, we cannot open a file descriptor directly
@@ -64,6 +64,7 @@ class FsPlugin(private val activity: Activity): Plugin(activity) {
6464
Uri.parse(args.uri),
6565
args.mode
6666
)?.parcelFileDescriptor?.detachFd()
67+
if (fd == null) throw IOException("No file or permission")
6768
res.put("fd", fd)
6869
}
6970

plugins/fs/src/mobile.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,10 @@ impl<R: Runtime> Fs<R> {
8383
mode: mode.into(),
8484
},
8585
)?;
86-
if let Some(fd) = result.fd {
87-
Ok(unsafe {
88-
use std::os::fd::FromRawFd;
89-
std::fs::File::from_raw_fd(fd)
90-
})
91-
} else {
92-
unimplemented!()
93-
}
86+
Ok(unsafe {
87+
use std::os::fd::FromRawFd;
88+
std::fs::File::from_raw_fd(result.fd)
89+
})
9490
}
9591
}
9692
}

plugins/fs/src/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ pub struct GetFileDescriptorPayload {
1414
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
1515
#[serde(rename_all = "camelCase")]
1616
pub struct GetFileDescriptorResponse {
17-
pub fd: Option<i32>,
17+
pub fd: i32,
1818
}

0 commit comments

Comments
 (0)