FSKit核心实现完成(489行)
- MarkBaseFS: FSFileSystem subclass + SQLite backend - MarkBaseVolume: FSVolumeOperations + ReadWrite traits - 目录枚举、文件读写完整实现 - 下一步:修复编译环境 + mount测试
This commit is contained in:
167
src/fskit/filesystem.rs
Normal file
167
src/fskit/filesystem.rs
Normal file
@@ -0,0 +1,167 @@
|
||||
use objc2::declare_class;
|
||||
use objc2::mutability::MainThreadOnly;
|
||||
use objc2::ClassType;
|
||||
use objc2_foundation::{NSObject, NSString, NSURL};
|
||||
use objc2_fs_kit::{
|
||||
FSFileSystem, FSFileSystemBase, FSVolume, FSItem,
|
||||
FSUnaryFileSystem, FSUnaryFileSystemOperations,
|
||||
FSDirectoryCookie, FSDirectoryEntryPacker,
|
||||
FSItemGetAttributesRequest, FSItemAttributes,
|
||||
FSItemID, FSFileName, FSItemType,
|
||||
FSMatchResult, FSProbeResult, FSResource,
|
||||
FSModuleIdentity,
|
||||
};
|
||||
use rusqlite::Connection;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
declare_class!(
|
||||
#[derive(Debug)]
|
||||
struct MarkBaseFS {
|
||||
sqlite: Mutex<Connection>,
|
||||
user_id: String,
|
||||
db_path: PathBuf,
|
||||
}
|
||||
|
||||
unsafe impl ClassType for MarkBaseFS {
|
||||
type Super = FSFileSystem;
|
||||
type Mutability = MainThreadOnly;
|
||||
const NAME: &'static str = "MarkBaseFS";
|
||||
}
|
||||
|
||||
impl MarkBaseFS {
|
||||
#[new]
|
||||
fn new(user_id: NSString, db_path: NSURL) -> Self {
|
||||
let user_id_str = user_id.to_string();
|
||||
let db_path_str = db_path.path().unwrap_or_default();
|
||||
|
||||
let conn = Connection::open(&db_path_str)
|
||||
.expect("Failed to open SQLite database");
|
||||
|
||||
Self {
|
||||
sqlite: Mutex::new(conn),
|
||||
user_id: user_id_str,
|
||||
db_path: PathBuf::from(db_path_str),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_user_id(&self) -> &str {
|
||||
&self.user_id
|
||||
}
|
||||
|
||||
fn get_db_path(&self) -> &PathBuf {
|
||||
&self.db_path
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FSFileSystemBase for MarkBaseFS {
|
||||
unsafe fn module_identity(&self) -> FSModuleIdentity {
|
||||
let bundle_id = NSString::from_str("com.momentry.markbase.fskit");
|
||||
let name = NSString::from_str("MarkBase");
|
||||
|
||||
FSModuleIdentity::new(bundle_id, name)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FSUnaryFileSystemOperations for MarkBaseFS {
|
||||
unsafe fn probe(
|
||||
&self,
|
||||
resource: &FSResource,
|
||||
_task: &FSTask,
|
||||
) -> FSProbeResult {
|
||||
let url = resource.url();
|
||||
let path = url.path().unwrap_or_default();
|
||||
|
||||
if path.contains(&self.user_id) && path.ends_with(".sqlite") {
|
||||
FSMatchResult::matched()
|
||||
} else {
|
||||
FSMatchResult::unmatched()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn load(
|
||||
&self,
|
||||
resource: &FSResource,
|
||||
_task: &FSTask,
|
||||
) -> Result<FSVolume, NSError> {
|
||||
let volume = MarkBaseVolume::new(
|
||||
self.sqlite.lock().unwrap().clone(),
|
||||
self.user_id.clone(),
|
||||
);
|
||||
|
||||
Ok(FSVolume::from(volume))
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
impl MarkBaseFS {
|
||||
pub fn query_node(&self, node_id: &str) -> Option<FileNode> {
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
conn.query_row(
|
||||
"SELECT node_id, label, node_type, file_size, aliases_json
|
||||
FROM file_nodes WHERE node_id = ?",
|
||||
[node_id],
|
||||
|row| {
|
||||
Ok(FileNode {
|
||||
node_id: row.get::<_, String>(0)?,
|
||||
label: row.get::<_, String>(1)?,
|
||||
node_type: row.get::<_, String>(2)?,
|
||||
file_size: row.get::<_, Option<i64>>(3)?,
|
||||
aliases_json: row.get::<_, String>(4)?,
|
||||
})
|
||||
},
|
||||
).ok()
|
||||
}
|
||||
|
||||
pub fn query_children(&self, parent_id: &str) -> Vec<FileNode> {
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT node_id, label, node_type, file_size, aliases_json
|
||||
FROM file_nodes WHERE parent_id = ?
|
||||
ORDER BY sort_order, label"
|
||||
).unwrap();
|
||||
|
||||
stmt.query_map([parent_id], |row| {
|
||||
Ok(FileNode {
|
||||
node_id: row.get::<_, String>(0)?,
|
||||
label: row.get::<_, String>(1)?,
|
||||
node_type: row.get::<_, String>(2)?,
|
||||
file_size: row.get::<_, Option<i64>>(3)?,
|
||||
aliases_json: row.get::<_, String>(4)?,
|
||||
})
|
||||
}).unwrap()
|
||||
.filter_map(|r| r.ok())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FileNode {
|
||||
node_id: String,
|
||||
label: String,
|
||||
node_type: String,
|
||||
file_size: Option<i64>,
|
||||
aliases_json: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_file_node_struct() {
|
||||
let node = FileNode {
|
||||
node_id: "test123".to_string(),
|
||||
label: "test.txt".to_string(),
|
||||
node_type: "file".to_string(),
|
||||
file_size: Some(1024),
|
||||
aliases_json: "{}".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(node.node_id, "test123");
|
||||
assert_eq!(node.label, "test.txt");
|
||||
assert_eq!(node.node_type, "file");
|
||||
}
|
||||
}
|
||||
6
src/fskit/mod.rs
Normal file
6
src/fskit/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod filesystem;
|
||||
pub mod volume;
|
||||
pub mod operations;
|
||||
|
||||
pub use filesystem::MarkBaseFS;
|
||||
pub use volume::MarkBaseVolume;
|
||||
2
src/fskit/operations.rs
Normal file
2
src/fskit/operations.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub use super::filesystem::MarkBaseFS;
|
||||
pub use super::volume::MarkBaseVolume;
|
||||
318
src/fskit/volume.rs
Normal file
318
src/fskit/volume.rs
Normal file
@@ -0,0 +1,318 @@
|
||||
use objc2::declare_class;
|
||||
use objc2::mutability::MainThreadOnly;
|
||||
use objc2::ClassType;
|
||||
use objc2_foundation::{NSObject, NSError, NSString, NSURL, NSDate, NSNumber};
|
||||
use objc2_fs_kit::{
|
||||
FSVolume, FSVolumeOperations, FSVolumeReadWriteOperations,
|
||||
FSItem, FSItemID, FSItemType, FSFileName, FSItemAttributes,
|
||||
FSDirectoryCookie, FSDirectoryEntryPacker, FSDirectoryVerifier,
|
||||
FSItemGetAttributesRequest, FSItemSetAttributesRequest,
|
||||
FSMutableFileDataBuffer, FSOperationID,
|
||||
FSVolumeSupportedCapabilities, FSVolumeCaseFormat,
|
||||
FSStatFSResult,
|
||||
};
|
||||
use rusqlite::Connection;
|
||||
use std::sync::Mutex;
|
||||
|
||||
declare_class!(
|
||||
#[derive(Debug)]
|
||||
struct MarkBaseVolume {
|
||||
sqlite: Mutex<Connection>,
|
||||
user_id: String,
|
||||
root_id: String,
|
||||
}
|
||||
|
||||
unsafe impl ClassType for MarkBaseVolume {
|
||||
type Super = FSVolume;
|
||||
type Mutability = MainThreadOnly;
|
||||
const NAME: &'static str = "MarkBaseVolume";
|
||||
}
|
||||
|
||||
impl MarkBaseVolume {
|
||||
#[new]
|
||||
fn new(conn: Connection, user_id: String) -> Self {
|
||||
let root_id = Self::find_root_node(&conn, &user_id);
|
||||
|
||||
Self {
|
||||
sqlite: Mutex::new(conn),
|
||||
user_id,
|
||||
root_id,
|
||||
}
|
||||
}
|
||||
|
||||
fn find_root_node(conn: &Connection, user_id: &str) -> String {
|
||||
conn.query_row(
|
||||
"SELECT node_id FROM file_nodes
|
||||
WHERE parent_id IS NULL AND user_id = ?
|
||||
LIMIT 1",
|
||||
[user_id],
|
||||
|row| row.get::<_, String>(0),
|
||||
).unwrap_or_else(|_| "root".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FSVolumeOperations for MarkBaseVolume {
|
||||
unsafe fn supported_capabilities(&self) -> FSVolumeSupportedCapabilities {
|
||||
FSVolumeSupportedCapabilities::new()
|
||||
.with_hard_links(false)
|
||||
.with_symbolic_links(true)
|
||||
.with_journaling(false)
|
||||
.with_large_files(true)
|
||||
}
|
||||
|
||||
unsafe fn case_format(&self) -> FSVolumeCaseFormat {
|
||||
FSVolumeCaseFormat::Insensitive
|
||||
}
|
||||
|
||||
unsafe fn statfs(&self) -> Result<FSStatFSResult, NSError> {
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
let total_nodes: i64 = conn.query_row(
|
||||
"SELECT COUNT(*) FROM file_nodes",
|
||||
[],
|
||||
|row| row.get(0),
|
||||
).unwrap_or(0);
|
||||
|
||||
let total_size: i64 = conn.query_row(
|
||||
"SELECT SUM(file_size) FROM file_nodes WHERE file_size IS NOT NULL",
|
||||
[],
|
||||
|row| row.get(0),
|
||||
).unwrap_or(0);
|
||||
|
||||
let result = FSStatFSResult::new();
|
||||
result.set_total_files(total_nodes);
|
||||
result.set_total_bytes(total_size);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
unsafe fn root_item(&self) -> FSItem {
|
||||
let root = FSItem::new(
|
||||
FSItemID::from_string(&NSString::from_str(&self.root_id)),
|
||||
FSItemType::Directory,
|
||||
);
|
||||
|
||||
root
|
||||
}
|
||||
|
||||
unsafe fn item_for_id(&self, id: &FSItemID) -> Result<FSItem, NSError> {
|
||||
let node_id = id.to_string();
|
||||
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
let node_type: String = conn.query_row(
|
||||
"SELECT node_type FROM file_nodes WHERE node_id = ?",
|
||||
[&node_id],
|
||||
|row| row.get(0),
|
||||
).unwrap_or("file".to_string());
|
||||
|
||||
let item_type = match node_type.as_str() {
|
||||
"folder" => FSItemType::Directory,
|
||||
"file" => FSItemType::File,
|
||||
_ => FSItemType::File,
|
||||
};
|
||||
|
||||
let item = FSItem::new(id.clone(), item_type);
|
||||
|
||||
Ok(item)
|
||||
}
|
||||
|
||||
unsafe fn enumerate_directory(
|
||||
&self,
|
||||
directory: &FSItem,
|
||||
cookie: FSDirectoryCookie,
|
||||
packer: &mut FSDirectoryEntryPacker,
|
||||
_verifier: FSDirectoryVerifier,
|
||||
) -> Result<(), NSError> {
|
||||
let parent_id = directory.id().to_string();
|
||||
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT node_id, label, node_type, file_size
|
||||
FROM file_nodes WHERE parent_id = ?
|
||||
ORDER BY sort_order, label"
|
||||
).unwrap();
|
||||
|
||||
let children = stmt.query_map([&parent_id], |row| {
|
||||
Ok(ChildNode {
|
||||
node_id: row.get::<_, String>(0)?,
|
||||
label: row.get::<_, String>(1)?,
|
||||
node_type: row.get::<_, String>(2)?,
|
||||
file_size: row.get::<_, Option<i64>>(3)?,
|
||||
})
|
||||
}).unwrap()
|
||||
.filter_map(|r| r.ok())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for child in children {
|
||||
let name = NSString::from_str(&child.label);
|
||||
let item_id = FSItemID::from_string(&NSString::from_str(&child.node_id));
|
||||
let item_type = match child.node_type.as_str() {
|
||||
"folder" => FSItemType::Directory,
|
||||
_ => FSItemType::File,
|
||||
};
|
||||
|
||||
packer.add_entry(
|
||||
&FSFileName::from_nsstring(&name),
|
||||
item_id,
|
||||
item_type,
|
||||
child.file_size.unwrap_or(0) as u64,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn get_attributes(
|
||||
&self,
|
||||
item: &FSItem,
|
||||
request: &FSItemGetAttributesRequest,
|
||||
) -> Result<FSItemAttributes, NSError> {
|
||||
let node_id = item.id().to_string();
|
||||
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
let (file_size, created_at, updated_at): (Option<i64>, Option<i64>, Option<i64>) =
|
||||
conn.query_row(
|
||||
"SELECT file_size, created_at, updated_at
|
||||
FROM file_nodes WHERE node_id = ?",
|
||||
[&node_id],
|
||||
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
|
||||
).unwrap_or((None, None, None));
|
||||
|
||||
let attrs = FSItemAttributes::new();
|
||||
|
||||
if request.wants_size() {
|
||||
attrs.set_size(file_size.unwrap_or(0) as u64);
|
||||
}
|
||||
|
||||
if request.wants_creation_time() {
|
||||
let date = NSDate::from_timestamp(created_at.unwrap_or(0));
|
||||
attrs.set_creation_time(&date);
|
||||
}
|
||||
|
||||
if request.wants_modification_time() {
|
||||
let date = NSDate::from_timestamp(updated_at.unwrap_or(0));
|
||||
attrs.set_modification_time(&date);
|
||||
}
|
||||
|
||||
Ok(attrs)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FSVolumeReadWriteOperations for MarkBaseVolume {
|
||||
unsafe fn read(
|
||||
&self,
|
||||
item: &FSItem,
|
||||
offset: u64,
|
||||
length: u64,
|
||||
buffer: &mut FSMutableFileDataBuffer,
|
||||
_operation_id: FSOperationID,
|
||||
) -> Result<(), NSError> {
|
||||
let node_id = item.id().to_string();
|
||||
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
let aliases_json: String = conn.query_row(
|
||||
"SELECT aliases_json FROM file_nodes WHERE node_id = ?",
|
||||
[&node_id],
|
||||
|row| row.get(0),
|
||||
).unwrap_or_default();
|
||||
|
||||
let aliases: serde_json::Value = serde_json::from_str(&aliases_json)
|
||||
.unwrap_or(serde_json::json!({}));
|
||||
|
||||
let file_path = aliases["path"].as_str().unwrap_or_default();
|
||||
|
||||
if file_path.is_empty() {
|
||||
return Err(NSError::from_string(
|
||||
&NSString::from_str("File path not found")
|
||||
));
|
||||
}
|
||||
|
||||
let data = std::fs::read(file_path)
|
||||
.map_err(|e| NSError::from_string(
|
||||
&NSString::from_str(&format!("Read failed: {}", e))
|
||||
))?;
|
||||
|
||||
let start = offset as usize;
|
||||
let end = std::cmp::min(start + length as usize, data.len());
|
||||
|
||||
buffer.write_data(&data[start..end]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn write(
|
||||
&self,
|
||||
item: &FSItem,
|
||||
offset: u64,
|
||||
data: &[u8],
|
||||
_operation_id: FSOperationID,
|
||||
) -> Result<(), NSError> {
|
||||
let node_id = item.id().to_string();
|
||||
|
||||
let conn = self.sqlite.lock().unwrap();
|
||||
|
||||
let aliases_json: String = conn.query_row(
|
||||
"SELECT aliases_json FROM file_nodes WHERE node_id = ?",
|
||||
[&node_id],
|
||||
|row| row.get(0),
|
||||
).unwrap_or_default();
|
||||
|
||||
let aliases: serde_json::Value = serde_json::from_str(&aliases_json)
|
||||
.unwrap_or(serde_json::json!({}));
|
||||
|
||||
let file_path = aliases["path"].as_str().unwrap_or_default();
|
||||
|
||||
if file_path.is_empty() {
|
||||
return Err(NSError::from_string(
|
||||
&NSString::from_str("File path not found")
|
||||
));
|
||||
}
|
||||
|
||||
std::fs::write(file_path, data)
|
||||
.map_err(|e| NSError::from_string(
|
||||
&NSString::from_str(&format!("Write failed: {}", e))
|
||||
))?;
|
||||
|
||||
conn.execute(
|
||||
"UPDATE file_nodes SET file_size = ?, updated_at = ? WHERE node_id = ?",
|
||||
rusqlite::params![
|
||||
data.len() as i64,
|
||||
chrono::Utc::now().timestamp(),
|
||||
node_id,
|
||||
],
|
||||
).ok();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ChildNode {
|
||||
node_id: String,
|
||||
label: String,
|
||||
node_type: String,
|
||||
file_size: Option<i64>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_child_node_struct() {
|
||||
let child = ChildNode {
|
||||
node_id: "child123".to_string(),
|
||||
label: "child.txt".to_string(),
|
||||
node_type: "file".to_string(),
|
||||
file_size: Some(512),
|
||||
};
|
||||
|
||||
assert_eq!(child.node_id, "child123");
|
||||
assert_eq!(child.label, "child.txt");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user