(&self, input: &serde_json::Value)
| 332 | } |
| 333 | |
| 334 | async fn search_files(&self, input: &serde_json::Value) -> AppResult<String> { |
| 335 | let pattern_str = input["pattern"] |
| 336 | .as_str() |
| 337 | .ok_or_else(|| AppError::Validation("Missing 'pattern' field".to_string()))?; |
| 338 | let path_str = input["path"] |
| 339 | .as_str() |
| 340 | .ok_or_else(|| AppError::Validation("Missing 'path' field".to_string()))?; |
| 341 | let safe_path = self.validate_path(path_str)?; |
| 342 | let sandbox_canonical = self.sandbox.canonicalize().map_err(AppError::from)?; |
| 343 | let regex = Regex::new(pattern_str) |
| 344 | .map_err(|error| AppError::Validation(format!("Invalid regex: {error}")))?; |
| 345 | |
| 346 | let mut results = Vec::new(); |
| 347 | let walker = WalkDir::new(&safe_path) |
| 348 | .into_iter() |
| 349 | .filter_entry(|e| { |
| 350 | if e.file_type().is_dir() { |
| 351 | if let Some(name) = e.file_name().to_str() { |
| 352 | return !should_skip_dir(name); |
| 353 | } |
| 354 | } |
| 355 | true |
| 356 | }); |
| 357 | |
| 358 | for entry_result in walker { |
| 359 | let entry = match entry_result { |
| 360 | Ok(entry) => entry, |
| 361 | Err(_) => continue, |
| 362 | }; |
| 363 | if !entry.file_type().is_file() { |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | let canonical = match entry.path().canonicalize() { |
| 368 | Ok(canonical) => canonical, |
| 369 | Err(_) => continue, |
| 370 | }; |
| 371 | if !canonical.starts_with(&sandbox_canonical) { |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | let content = match tokio::fs::read_to_string(&canonical).await { |
| 376 | Ok(content) => content, |
| 377 | Err(_) => continue, |
| 378 | }; |
| 379 | |
| 380 | for (line_index, line) in content.lines().enumerate() { |
| 381 | if regex.is_match(line) { |
| 382 | let rel = canonical |
| 383 | .strip_prefix(&sandbox_canonical) |
| 384 | .map_err(|e| AppError::Internal(format!("strip_prefix failed: {e}")))?; |
| 385 | results.push(format!("{}:{}: {}", rel.display(), line_index + 1, line)); |
| 386 | if results.len() >= 100 { |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 |
no test coverage detected