Trouble Creating Directory for Portable Database #8719
-
Hello! I'm new to Tauri and I'm really enjoying it, but I have some questions regarding filesystem management. After reading through the discussions and documentation, I haven't been able to find a solution to my problem. I am currently developing an application that utilizes a portable database, specifically SQLite. This database will store information about the various users who can use the application, meaning that each user will utilize a separate database, making it portable. However, I am encountering issues when attempting to create a directory to store these databases. When I build and test my application, it crashes. In my const ACCOUNT_PATH: &str = "./.accounts"; Then, within my fn main() -> Result<()> {
if !Path::new(ACCOUNT_PATH).exists() {
fs::create_dir(ACCOUNT_PATH)
.expect("Failed to create directory");
}
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
...
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Ok(())
} Additionally, in my {
"allowlist": {
"all": true,
"fs": {
"all": true,
"scope": ["**"]
},
"os": {
"all": true
},
"path": {
"all": true
}
}
} The above setup functions correctly in my development environment; however, upon building my Linux Failed to create directory: Os { code: 30, kind: ReadOnlyFilesystem, message: "Read-only file system" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace Similarly, the macOS version encounters the same issue. Could someone please help me understand what I might be doing incorrectly? Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm surprised the .deb works to be honest, it should be read-only too. Really the only bundle type that's reliably writable is the nsis installer on windows in perUser installmode. Just to be safe i assume that .deb is read-only below too. So, if you plan to support .deb, .dmg, .msi, or setup.exe (nsis) in perMachine installmode you have to use a "well known" location to store the file instead of next to the app. For example the documents dir, or application specific data folders (f.e. https://tauri.app/v1/api/js/path#appdatadir). If you only care about .appimage you could read the On macOS, even though .app is just a simple directory i think macos makes it read-only once the app is running, i could be totally wrong about that. If the user moves that file into the /Applications dir (i think that's what users typically do?) it will be read-only though. tldr, depending on the app's current working dir is super risky and rarely the right thing to do. |
Beta Was this translation helpful? Give feedback.
-
So, what is the way to proceed in order to create the directory correctly to save my databases? Should I do something like this in my "fs": {
"all": true,
"scope": ["$APPDATA/", "$APPDATA/**"]
} And define my path variable as Forgive my ignorance, but it would help me a lot if you could show me the best way to deal with this case, as this is my first project with Tauri and native applications. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
The scope setting is only relevant for the javascript/webview side so let's ignore that for now.
Tauri also does not change the behavior of std::fs (it couldn't do that anyway) so
"$APPDATA/.accounts"
means that it will try to create a$APPDATA
(literally) directory in the current working dir.To get the correct dir you'll have to use https://docs.rs/tauri/latest/tauri/struct.PathResolver.html#method.app_data_dir, for this you need access to an App/AppHandle which typically means to move the code into tauri's setup hook: