1use fuser::{
4 Errno, FileType, INodeNo, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry, ReplyWrite,
5};
6use std::path::PathBuf;
7use tracing::{info, warn};
8
9use super::{GENERATION, RagFs, TTL, reply_file_bytes};
10use crate::inode::{
11 APPROVE_FILE_INO, CLEANUP_FILE_INO, DEDUPE_FILE_INO, ORGANIZE_FILE_INO, PENDING_DIR_INO,
12 RAGFS_DIR_INO, REJECT_FILE_INO, SEMANTIC_DIR_INO, SIMILAR_OPS_FILE_INO,
13};
14
15impl RagFs {
16 pub(crate) fn lookup_semantic_dir(&self, name_str: &str, reply: ReplyEntry) {
17 match name_str {
18 ".organize" => {
19 let attr = self.make_attr(ORGANIZE_FILE_INO, FileType::RegularFile, 0);
20 reply.entry(&TTL, &attr, GENERATION);
21 }
22 ".similar" => {
23 let content = self
24 .runtime
25 .block_on(self.semantic_manager.get_similar_json());
26 let attr = self.make_attr(
27 SIMILAR_OPS_FILE_INO,
28 FileType::RegularFile,
29 content.len() as u64,
30 );
31 let mut cache = self.runtime.block_on(self.content_cache.write());
32 cache.insert(SIMILAR_OPS_FILE_INO, content);
33 reply.entry(&TTL, &attr, GENERATION);
34 }
35 ".cleanup" => {
36 let content = self
37 .runtime
38 .block_on(self.semantic_manager.get_cleanup_json());
39 let attr = self.make_attr(
40 CLEANUP_FILE_INO,
41 FileType::RegularFile,
42 content.len() as u64,
43 );
44 let mut cache = self.runtime.block_on(self.content_cache.write());
45 cache.insert(CLEANUP_FILE_INO, content);
46 reply.entry(&TTL, &attr, GENERATION);
47 }
48 ".dedupe" => {
49 let content = self
50 .runtime
51 .block_on(self.semantic_manager.get_dedupe_json());
52 let attr =
53 self.make_attr(DEDUPE_FILE_INO, FileType::RegularFile, content.len() as u64);
54 let mut cache = self.runtime.block_on(self.content_cache.write());
55 cache.insert(DEDUPE_FILE_INO, content);
56 reply.entry(&TTL, &attr, GENERATION);
57 }
58 ".pending" => {
59 let attr = self.make_attr(PENDING_DIR_INO, FileType::Directory, 0);
60 reply.entry(&TTL, &attr, GENERATION);
61 }
62 ".approve" => {
63 let attr = self.make_attr(APPROVE_FILE_INO, FileType::RegularFile, 0);
64 reply.entry(&TTL, &attr, GENERATION);
65 }
66 ".reject" => {
67 let attr = self.make_attr(REJECT_FILE_INO, FileType::RegularFile, 0);
68 reply.entry(&TTL, &attr, GENERATION);
69 }
70 _ => reply.error(Errno::ENOENT),
71 }
72 }
73
74 pub(crate) fn lookup_pending_dir(&self, name_str: &str, reply: ReplyEntry) {
75 let plan_ids = self
76 .runtime
77 .block_on(self.semantic_manager.get_pending_plan_ids());
78 if plan_ids.contains(&name_str.to_string()) {
79 let content = self
80 .runtime
81 .block_on(self.semantic_manager.get_plan_json(name_str));
82 let mut inodes = self.runtime.block_on(self.inodes.write());
83 let ino = inodes.get_or_create_query_result(PENDING_DIR_INO, name_str.to_string());
84 let attr = self.make_attr(ino, FileType::RegularFile, content.len() as u64);
85 let mut cache = self.runtime.block_on(self.content_cache.write());
86 cache.insert(ino, content);
87 reply.entry(&TTL, &attr, GENERATION);
88 return;
89 }
90 reply.error(Errno::ENOENT);
91 }
92
93 pub(crate) fn getattr_semantic(&self, ino: u64, reply: ReplyAttr) {
94 match ino {
95 SEMANTIC_DIR_INO | PENDING_DIR_INO => {
96 let attr = self.make_attr(ino, FileType::Directory, 0);
97 reply.attr(&TTL, &attr);
98 }
99 SIMILAR_OPS_FILE_INO => {
100 let content = self
101 .runtime
102 .block_on(self.semantic_manager.get_similar_json());
103 let size = content.len() as u64;
104 let mut cache = self.runtime.block_on(self.content_cache.write());
105 cache.insert(SIMILAR_OPS_FILE_INO, content);
106 let attr = self.make_attr(ino, FileType::RegularFile, size);
107 reply.attr(&TTL, &attr);
108 }
109 CLEANUP_FILE_INO => {
110 let content = self
111 .runtime
112 .block_on(self.semantic_manager.get_cleanup_json());
113 let size = content.len() as u64;
114 let mut cache = self.runtime.block_on(self.content_cache.write());
115 cache.insert(CLEANUP_FILE_INO, content);
116 let attr = self.make_attr(ino, FileType::RegularFile, size);
117 reply.attr(&TTL, &attr);
118 }
119 DEDUPE_FILE_INO => {
120 let content = self
121 .runtime
122 .block_on(self.semantic_manager.get_dedupe_json());
123 let size = content.len() as u64;
124 let mut cache = self.runtime.block_on(self.content_cache.write());
125 cache.insert(DEDUPE_FILE_INO, content);
126 let attr = self.make_attr(ino, FileType::RegularFile, size);
127 reply.attr(&TTL, &attr);
128 }
129 ORGANIZE_FILE_INO | APPROVE_FILE_INO | REJECT_FILE_INO => {
130 let attr = self.make_attr(ino, FileType::RegularFile, 0);
131 reply.attr(&TTL, &attr);
132 }
133 _ => reply.error(Errno::ENOENT),
134 }
135 }
136
137 pub(crate) fn read_semantic(&self, ino: u64, offset: u64, size: u32, reply: ReplyData) {
138 match ino {
139 SIMILAR_OPS_FILE_INO => {
140 let content = self
141 .runtime
142 .block_on(self.semantic_manager.get_similar_json());
143 reply_file_bytes(reply, &content, offset, size);
144 }
145 CLEANUP_FILE_INO => {
146 let content = self
147 .runtime
148 .block_on(self.semantic_manager.get_cleanup_json());
149 reply_file_bytes(reply, &content, offset, size);
150 }
151 DEDUPE_FILE_INO => {
152 let content = self
153 .runtime
154 .block_on(self.semantic_manager.get_dedupe_json());
155 reply_file_bytes(reply, &content, offset, size);
156 }
157 ORGANIZE_FILE_INO | APPROVE_FILE_INO | REJECT_FILE_INO => {
158 reply.data(&[]);
159 }
160 _ => reply.error(Errno::ENOENT),
161 }
162 }
163
164 pub(crate) fn readdir_semantic(&self, ino: u64, offset: u64, mut reply: ReplyDirectory) {
165 match ino {
166 SEMANTIC_DIR_INO => {
167 let entries = [
168 (SEMANTIC_DIR_INO, FileType::Directory, "."),
169 (RAGFS_DIR_INO, FileType::Directory, ".."),
170 (ORGANIZE_FILE_INO, FileType::RegularFile, ".organize"),
171 (SIMILAR_OPS_FILE_INO, FileType::RegularFile, ".similar"),
172 (CLEANUP_FILE_INO, FileType::RegularFile, ".cleanup"),
173 (DEDUPE_FILE_INO, FileType::RegularFile, ".dedupe"),
174 (PENDING_DIR_INO, FileType::Directory, ".pending"),
175 (APPROVE_FILE_INO, FileType::RegularFile, ".approve"),
176 (REJECT_FILE_INO, FileType::RegularFile, ".reject"),
177 ];
178
179 for (i, (ino, kind, name)) in entries.iter().enumerate().skip(offset as usize) {
180 if reply.add(INodeNo(*ino), (i + 1) as u64, *kind, name) {
181 break;
182 }
183 }
184 reply.ok();
185 }
186 PENDING_DIR_INO => {
187 let mut entries: Vec<(u64, FileType, String)> = vec![
188 (PENDING_DIR_INO, FileType::Directory, ".".to_string()),
189 (SEMANTIC_DIR_INO, FileType::Directory, "..".to_string()),
190 ];
191
192 let plan_ids = self
193 .runtime
194 .block_on(self.semantic_manager.get_pending_plan_ids());
195 for plan_id in plan_ids {
196 let mut inodes = self.runtime.block_on(self.inodes.write());
197 let entry_ino =
198 inodes.get_or_create_query_result(PENDING_DIR_INO, plan_id.clone());
199 entries.push((entry_ino, FileType::RegularFile, plan_id));
200 }
201
202 for (i, (ino, kind, name)) in entries.iter().enumerate().skip(offset as usize) {
203 if reply.add(INodeNo(*ino), (i + 1) as u64, *kind, name) {
204 break;
205 }
206 }
207 reply.ok();
208 }
209 _ => reply.error(Errno::ENOENT),
210 }
211 }
212
213 pub(crate) fn write_semantic(&self, ino: u64, data: &[u8], reply: ReplyWrite) {
214 if ino == ORGANIZE_FILE_INO {
215 let data_str = String::from_utf8_lossy(data).to_string();
216 let semantic_manager = self.semantic_manager.clone();
217 let result = self.runtime.block_on(async move {
218 match serde_json::from_str::<crate::semantic::OrganizeRequest>(&data_str) {
219 Ok(request) => semantic_manager.create_organize_plan(request).await,
220 Err(e) => Err(format!("Invalid OrganizeRequest JSON: {e}")),
221 }
222 });
223 match result {
224 Ok(plan) => info!("Created organization plan: {}", plan.id),
225 Err(e) => warn!("Failed to create organization plan: {}", e),
226 }
227 reply.written(data.len() as u32);
228 return;
229 }
230 if ino == SIMILAR_OPS_FILE_INO {
231 let data_str = String::from_utf8_lossy(data).trim().to_string();
232 let path = PathBuf::from(&data_str);
233 let semantic_manager = self.semantic_manager.clone();
234 let result = self
235 .runtime
236 .block_on(async move { semantic_manager.find_similar(&path).await });
237 match result {
238 Ok(r) => info!("Found {} similar files to {}", r.similar.len(), data_str),
239 Err(e) => warn!("Failed to find similar files: {}", e),
240 }
241 reply.written(data.len() as u32);
242 return;
243 }
244 if ino == APPROVE_FILE_INO {
245 let data_str = String::from_utf8_lossy(data).trim().to_string();
246 if let Ok(plan_id) = uuid::Uuid::parse_str(&data_str) {
247 let semantic_manager = self.semantic_manager.clone();
248 let result = self
249 .runtime
250 .block_on(async move { semantic_manager.approve_plan(plan_id).await });
251 match result {
252 Ok(plan) => info!("Approved plan: {}", plan.id),
253 Err(e) => warn!("Failed to approve plan: {}", e),
254 }
255 } else {
256 warn!("Invalid plan ID for approve: {}", data_str);
257 }
258 reply.written(data.len() as u32);
259 return;
260 }
261 if ino == REJECT_FILE_INO {
262 let data_str = String::from_utf8_lossy(data).trim().to_string();
263 if let Ok(plan_id) = uuid::Uuid::parse_str(&data_str) {
264 let semantic_manager = self.semantic_manager.clone();
265 let result = self
266 .runtime
267 .block_on(async move { semantic_manager.reject_plan(plan_id).await });
268 match result {
269 Ok(plan) => info!("Rejected plan: {}", plan.id),
270 Err(e) => warn!("Failed to reject plan: {}", e),
271 }
272 } else {
273 warn!("Invalid plan ID for reject: {}", data_str);
274 }
275 reply.written(data.len() as u32);
276 }
277 }
278}