1use fuser::{
4 Errno, FileType, INodeNo, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry, ReplyWrite,
5};
6use tracing::{info, warn};
7
8use super::{GENERATION, RagFs, TTL, reply_file_bytes};
9use crate::inode::{
10 FIRST_REAL_INO, HISTORY_FILE_INO, RAGFS_DIR_INO, SAFETY_DIR_INO, TRASH_DIR_INO, UNDO_FILE_INO,
11};
12
13impl RagFs {
14 pub(crate) fn lookup_safety_dir(&self, name_str: &str, reply: ReplyEntry) {
15 match name_str {
16 ".trash" => {
17 let attr = self.make_attr(TRASH_DIR_INO, FileType::Directory, 0);
18 reply.entry(&TTL, &attr, GENERATION);
19 }
20 ".history" => {
21 let content = self.safety_manager.get_history_json(Some(100));
22 let attr = self.make_attr(
23 HISTORY_FILE_INO,
24 FileType::RegularFile,
25 content.len() as u64,
26 );
27 let mut cache = self.runtime.block_on(self.content_cache.write());
28 cache.insert(HISTORY_FILE_INO, content);
29 reply.entry(&TTL, &attr, GENERATION);
30 }
31 ".undo" => {
32 let attr = self.make_attr(UNDO_FILE_INO, FileType::RegularFile, 0);
33 reply.entry(&TTL, &attr, GENERATION);
34 }
35 _ => reply.error(Errno::ENOENT),
36 }
37 }
38
39 pub(crate) fn getattr_safety(&self, ino: u64, reply: ReplyAttr) {
40 match ino {
41 SAFETY_DIR_INO | TRASH_DIR_INO => {
42 let attr = self.make_attr(ino, FileType::Directory, 0);
43 reply.attr(&TTL, &attr);
44 }
45 HISTORY_FILE_INO => {
46 let content = self.safety_manager.get_history_json(Some(100));
47 let size = content.len() as u64;
48 let mut cache = self.runtime.block_on(self.content_cache.write());
49 cache.insert(HISTORY_FILE_INO, content);
50 let attr = self.make_attr(ino, FileType::RegularFile, size);
51 reply.attr(&TTL, &attr);
52 }
53 UNDO_FILE_INO => {
54 let attr = self.make_attr(ino, FileType::RegularFile, 0);
55 reply.attr(&TTL, &attr);
56 }
57 _ => reply.error(Errno::ENOENT),
58 }
59 }
60
61 pub(crate) fn read_safety(&self, ino: u64, offset: u64, size: u32, reply: ReplyData) {
62 match ino {
63 HISTORY_FILE_INO => {
64 let content = self.safety_manager.get_history_json(Some(100));
65 reply_file_bytes(reply, &content, offset, size);
66 }
67 UNDO_FILE_INO => {
68 reply.data(&[]);
69 }
70 _ => reply.error(Errno::ENOENT),
71 }
72 }
73
74 pub(crate) fn readdir_safety(&self, ino: u64, offset: u64, mut reply: ReplyDirectory) {
75 match ino {
76 SAFETY_DIR_INO => {
77 let entries = [
78 (SAFETY_DIR_INO, FileType::Directory, "."),
79 (RAGFS_DIR_INO, FileType::Directory, ".."),
80 (TRASH_DIR_INO, FileType::Directory, ".trash"),
81 (HISTORY_FILE_INO, FileType::RegularFile, ".history"),
82 (UNDO_FILE_INO, FileType::RegularFile, ".undo"),
83 ];
84
85 for (i, (ino, kind, name)) in entries.iter().enumerate().skip(offset as usize) {
86 if reply.add(INodeNo(*ino), (i + 1) as u64, *kind, name) {
87 break;
88 }
89 }
90 reply.ok();
91 }
92 TRASH_DIR_INO => {
93 let mut entries: Vec<(u64, FileType, String)> = vec![
94 (TRASH_DIR_INO, FileType::Directory, ".".to_string()),
95 (SAFETY_DIR_INO, FileType::Directory, "..".to_string()),
96 ];
97
98 let trash = self.runtime.block_on(self.safety_manager.list_trash());
99 for entry in trash {
100 let entry_ino =
101 FIRST_REAL_INO + 500_000 + (entry.id.as_u128() % 100_000) as u64;
102 entries.push((entry_ino, FileType::RegularFile, entry.id.to_string()));
103 }
104
105 for (i, (ino, kind, name)) in entries.iter().enumerate().skip(offset as usize) {
106 if reply.add(INodeNo(*ino), (i + 1) as u64, *kind, name) {
107 break;
108 }
109 }
110 reply.ok();
111 }
112 _ => reply.error(Errno::ENOENT),
113 }
114 }
115
116 pub(crate) fn write_safety(&self, _ino: u64, data: &[u8], reply: ReplyWrite) {
117 let data_str = String::from_utf8_lossy(data).trim().to_string();
118 if let Ok(operation_id) = uuid::Uuid::parse_str(&data_str) {
119 let safety_manager = self.safety_manager.clone();
120 let result = self
121 .runtime
122 .block_on(async move { safety_manager.undo(operation_id).await });
123 match result {
124 Ok(msg) => info!("Undo successful: {}", msg),
125 Err(e) => warn!("Undo failed: {}", e),
126 }
127 } else {
128 warn!("Invalid operation ID for undo: {}", data_str);
129 }
130 reply.written(data.len() as u32);
131 }
132}