2020-06-12 18:35:12 +00:00
|
|
|
use ruffle_core::backend::storage::StorageBackend;
|
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
2021-05-09 19:35:53 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::path::PathBuf;
|
2020-06-12 18:35:12 +00:00
|
|
|
|
|
|
|
pub struct DiskStorageBackend {
|
2020-06-15 18:42:27 +00:00
|
|
|
base_path: PathBuf,
|
2021-05-09 19:35:53 +00:00
|
|
|
shared_objects_path: PathBuf,
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiskStorageBackend {
|
2020-12-21 16:16:44 +00:00
|
|
|
pub fn new() -> Self {
|
2021-05-09 19:35:53 +00:00
|
|
|
let base_path = dirs::data_local_dir().unwrap().join("ruffle");
|
|
|
|
let shared_objects_path = base_path.join("SharedObjects");
|
2020-06-12 18:35:12 +00:00
|
|
|
|
|
|
|
// Create a base dir if one doesn't exist yet
|
2021-05-09 19:35:53 +00:00
|
|
|
if !shared_objects_path.exists() {
|
2020-06-12 18:35:12 +00:00
|
|
|
log::info!("Creating storage dir");
|
2020-06-15 18:42:27 +00:00
|
|
|
if let Err(r) = fs::create_dir_all(&base_path) {
|
2020-06-12 18:35:12 +00:00
|
|
|
log::warn!("Unable to create storage dir {}", r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 19:35:53 +00:00
|
|
|
DiskStorageBackend {
|
|
|
|
base_path,
|
|
|
|
shared_objects_path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_shared_object_path(&self, name: &str) -> PathBuf {
|
|
|
|
self.shared_objects_path.join(format!("{}.sol", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_back_compat_shared_object_path(&self, name: &str) -> PathBuf {
|
|
|
|
// Backwards compatibility with pre-05/09/2021:
|
|
|
|
// Search for data in old location, without .sol extension and # prefix.
|
|
|
|
// Remove this code eventually.
|
|
|
|
self.base_path.join(name.replacen("/#", "/", 1))
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StorageBackend for DiskStorageBackend {
|
2021-05-04 01:22:01 +00:00
|
|
|
fn get(&self, name: &str) -> Option<Vec<u8>> {
|
2021-05-09 19:35:53 +00:00
|
|
|
let path = self.get_shared_object_path(name);
|
|
|
|
match std::fs::read(path) {
|
|
|
|
Ok(data) => Some(data),
|
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
|
|
|
// Backwards compatibility with pre-05/09/2021:
|
|
|
|
// Search for data in old location, without .sol extension.
|
|
|
|
// Remove this eventually.
|
|
|
|
let path = self.get_back_compat_shared_object_path(name);
|
|
|
|
match std::fs::read(path) {
|
|
|
|
Ok(data) => Some(data),
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("Unable to read file {:?}", e);
|
|
|
|
None
|
|
|
|
}
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 19:35:53 +00:00
|
|
|
Err(e) => {
|
|
|
|
log::warn!("Unable to read file {:?}", e);
|
2020-06-12 18:35:12 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 01:22:01 +00:00
|
|
|
fn put(&mut self, name: &str, value: &[u8]) -> bool {
|
2021-05-09 19:35:53 +00:00
|
|
|
let full_path = self.get_shared_object_path(name);
|
2020-12-21 16:16:44 +00:00
|
|
|
if let Some(parent_dir) = full_path.parent() {
|
|
|
|
if !parent_dir.exists() {
|
|
|
|
if let Err(r) = fs::create_dir_all(&parent_dir) {
|
|
|
|
log::warn!("Unable to create storage dir {}", r);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 18:35:12 +00:00
|
|
|
|
2020-06-15 18:42:27 +00:00
|
|
|
match File::create(full_path) {
|
2020-06-12 18:35:12 +00:00
|
|
|
Ok(mut file) => {
|
2021-05-04 01:22:01 +00:00
|
|
|
if let Err(r) = file.write_all(&value) {
|
2020-06-15 18:18:48 +00:00
|
|
|
log::warn!("Unable to write file content {:?}", r);
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 18:18:48 +00:00
|
|
|
Err(r) => {
|
|
|
|
log::warn!("Unable to save file {:?}", r);
|
|
|
|
false
|
2020-06-17 17:58:57 +00:00
|
|
|
}
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
2020-06-15 18:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 18:42:27 +00:00
|
|
|
fn remove_key(&mut self, name: &str) {
|
2021-05-09 19:35:53 +00:00
|
|
|
let full_path = self.get_shared_object_path(name);
|
2020-06-17 17:58:57 +00:00
|
|
|
let _ = fs::remove_file(full_path);
|
2020-06-12 18:35:12 +00:00
|
|
|
}
|
|
|
|
}
|