Skip to main content

AirLibrary/Indexing/Scan/
ScanFile.rs

1//! # ScanFile
2//!
3//! ## File: Indexing/Scan/ScanFile.rs
4//!
5//! ## Role in Air Architecture
6//!
7//! Provides individual file scanning functionality for the File Indexer
8//! service, handling reading, metadata extraction, and categorization of files
9//! for indexing.
10//!
11//! ## Primary Responsibility
12//!
13//! Scan individual files to extract metadata, content, and prepare them for
14//! indexing operations.
15//!
16//! ## Secondary Responsibilities
17//!
18//! - File access validation and permission checking
19//! - Encoding detection for text files
20//! - Language detection for code files
21//! - File size validation
22//! - Symbolic link detection
23//!
24//! ## Dependencies
25//!
26//! **External Crates:**
27//! - `tokio` - Async file I/O operations
28//! - `sha2` - Checksum calculation for file integrity
29//!
30//! **Internal Modules:**
31//! - `crate::Result` - Error handling type
32//! - `crate::AirError` - Error types
33//! - `crate::Configuration::IndexingConfig` - Indexing configuration
34//! - `super::super::State::CreateState` - State structure definitions
35//! - `super::Process::ProcessContent` - Content processing operations
36//!
37//! ## Dependents
38//!
39//! - `Indexing::Scan::ScanDirectory` - Batch file processing
40//! - `Indexing::Watch::WatchFile` - Individual file change handling
41//! - `Indexing::mod::FileIndexer` - Main file indexer implementation
42//!
43//! ## VSCode Pattern Reference
44//!
45//! Inspired by VSCode's file scanning in
46//! `src/vs/workbench/services/files/`
47//!
48//! ## Security Considerations
49//!
50//! - Path canonicalization before access
51//! - File size limits enforced
52//! - Timeout protection for I/O operations
53//! - Permission checking before reads
54//!
55//! ## Performance Considerations
56//!
57//! - Asynchronous file reading
58//! - Batch processing operations
59//! - Memory-efficient streaming for large files
60//! - Cached metadata when available
61//!
62//! ## Error Handling Strategy
63//!
64//! File scanning returns Results with detailed error messages about
65//! why a file cannot be scanned or accessed. Errors are logged and
66//! individual file failures don't halt batch operations.
67//!
68//! ## Thread Safety
69//!
70//! File scanning operations are designed for parallel execution and
71use std::{
72	path::PathBuf,
73	time::{Duration, Instant},
74};
75
76/// produce results that can be safely merged into shared state.
77use crate::dev_log;
78use crate::{
79	AirError,
80	Configuration::IndexingConfig,
81	Indexing::{
82		Process::{
83			ExtractSymbols::ExtractSymbols,
84			ProcessContent::{DetectEncoding, DetectLanguage, DetectMimeType},
85		},
86		State::CreateState::{FileMetadata, SymbolInfo},
87	},
88	Result,
89};
90
91/// Index a single file internally with comprehensive validation
92///
93/// This function is called by parallel tasks during directory scanning
94/// and includes:
95/// - File metadata extraction
96/// - Size validation
97/// - SHA-256 checksum calculation
98/// - Encoding detection
99/// - MIME type detection
100/// - Language detection
101/// - Symbol extraction for code files
102pub async fn IndexFileInternal(
103	file_path:&PathBuf,
104	config:&IndexingConfig,
105	_patterns:&[String],
106) -> Result<(FileMetadata, Vec<SymbolInfo>)> {
107	let start_time = Instant::now();
108
109	// Get file metadata with error handling
110	let metadata = std::fs::metadata(file_path)
111		.map_err(|e| AirError::FileSystem(format!("Failed to get file metadata: {}", e)))?;
112
113	// Get modified time
114	let modified = metadata
115		.modified()
116		.map_err(|e| AirError::FileSystem(format!("Failed to get modification time: {}", e)))?;
117
118	let modified_time = chrono::DateTime::<chrono::Utc>::from(modified);
119
120	// Check if file size exceeds limit
121	let file_size = metadata.len();
122	if file_size > config.MaxFileSizeMb as u64 * 1024 * 1024 {
123		return Err(AirError::FileSystem(format!(
124			"File size {} exceeds limit {} MB",
125			file_size, config.MaxFileSizeMb
126		)));
127	}
128
129	// File read with timeout protection
130	let content = tokio::time::timeout(Duration::from_secs(30), tokio::fs::read(file_path))
131		.await
132		.map_err(|_| AirError::FileSystem(format!("Timeout reading file: {} (30s limit)", file_path.display())))?
133		.map_err(|e| AirError::FileSystem(format!("Failed to read file: {}", e)))?;
134
135	// Check for symbolic link
136	let is_symlink = std::fs::symlink_metadata(file_path)
137		.map(|m| m.file_type().is_symlink())
138		.unwrap_or(false);
139
140	// Calculate SHA-256 checksum
141	let checksum = CalculateChecksum(&content);
142
143	// Detect file encoding
144	let encoding = DetectEncoding(&content);
145
146	// Detect MIME type
147	let mime_type = DetectMimeType(file_path, &content);
148
149	// Detect programming language
150	let language = DetectLanguage(file_path);
151
152	// Count lines for text files
153	let line_count = if mime_type.starts_with("text/") {
154		Some(content.iter().filter(|&&b| b == b'\n').count() as u32 + 1)
155	} else {
156		None
157	};
158
159	// Extract symbols from code for VSCode Outline View
160	let symbols = if let Some(lang) = &language {
161		ExtractSymbols(file_path, &content, lang).await?
162	} else {
163		Vec::new()
164	};
165
166	let permissions = GetPermissionsString(&metadata);
167
168	let elapsed = start_time.elapsed();
169
170	dev_log!(
171		"indexing",
172		"indexed {} in {}ms ({} symbols)",
173		file_path.display(),
174		elapsed.as_millis(),
175		symbols.len()
176	);
177
178	Ok((
179		FileMetadata {
180			path:file_path.clone(),
181			size:file_size,
182			modified:modified_time,
183			mime_type,
184			language,
185			line_count,
186			checksum,
187			is_symlink,
188			permissions,
189			encoding,
190			indexed_at:chrono::Utc::now(),
191			symbol_count:symbols.len() as u32,
192		},
193		symbols,
194	))
195}
196
197/// Validate file access and permissions before scanning
198pub async fn ValidateFileAccess(file_path:&PathBuf) -> bool {
199	tokio::task::spawn_blocking({
200		let file_path = file_path.to_path_buf();
201		move || {
202			// Try to read file metadata
203			let can_access = std::fs::metadata(&file_path).is_ok();
204			if can_access {
205				// Try to open file for reading
206				std::fs::File::open(&file_path).is_ok()
207			} else {
208				false
209			}
210		}
211	})
212	.await
213	.unwrap_or(false)
214}
215
216/// Calculate SHA-256 checksum for file content
217pub fn CalculateChecksum(content:&[u8]) -> String {
218	use sha2::{Digest, Sha256};
219	let mut hasher = Sha256::new();
220	hasher.update(content);
221	format!("{:x}", hasher.finalize())
222}
223
224/// Get file permissions as string
225#[cfg(unix)]
226pub fn GetPermissionsString(metadata:&std::fs::Metadata) -> String {
227	use std::os::unix::fs::PermissionsExt;
228	let mode = metadata.permissions().mode();
229	let mut perms = String::new();
230	// Read permission
231	perms.push(if mode & 0o400 != 0 { 'r' } else { '-' });
232	// Write permission
233	perms.push(if mode & 0o200 != 0 { 'w' } else { '-' });
234	// Execute permission
235	perms.push(if mode & 0o100 != 0 { 'x' } else { '-' });
236	// Group permissions
237	perms.push(if mode & 0o040 != 0 { 'r' } else { '-' });
238	perms.push(if mode & 0o020 != 0 { 'w' } else { '-' });
239	perms.push(if mode & 0o010 != 0 { 'x' } else { '-' });
240	// Other permissions
241	perms.push(if mode & 0o004 != 0 { 'r' } else { '-' });
242	perms.push(if mode & 0o002 != 0 { 'w' } else { '-' });
243	perms.push(if mode & 0o001 != 0 { 'x' } else { '-' });
244	perms
245}
246
247/// Get file permissions as string for non-Unix systems
248#[cfg(not(unix))]
249pub fn GetPermissionsString(_metadata:&std::fs::Metadata) -> String { "--------".to_string() }
250
251/// Scan file and return just the metadata (without symbols)
252pub async fn ScanFileMetadata(file_path:&PathBuf) -> Result<FileMetadata> {
253	let metadata = std::fs::metadata(file_path)
254		.map_err(|e| AirError::FileSystem(format!("Failed to get file metadata: {}", e)))?;
255
256	let modified = metadata
257		.modified()
258		.map_err(|e| AirError::FileSystem(format!("Failed to get modification time: {}", e)))?;
259
260	let modified_time = chrono::DateTime::<chrono::Utc>::from(modified);
261
262	Ok(FileMetadata {
263		path:file_path.clone(),
264		size:metadata.len(),
265		modified:modified_time,
266		mime_type:"application/octet-stream".to_string(),
267		language:None,
268		line_count:None,
269		checksum:String::new(),
270		is_symlink:metadata.file_type().is_symlink(),
271		permissions:GetPermissionsString(&metadata),
272		encoding:None,
273		indexed_at:chrono::Utc::now(),
274		symbol_count:0,
275	})
276}
277
278/// Check if file has been modified since last indexed
279pub fn FileModifiedSince(file_path:&PathBuf, last_indexed:chrono::DateTime<chrono::Utc>) -> Result<bool> {
280	let metadata = std::fs::metadata(file_path)
281		.map_err(|e| AirError::FileSystem(format!("Failed to get file metadata: {}", e)))?;
282
283	let modified = metadata
284		.modified()
285		.map_err(|e| AirError::FileSystem(format!("Failed to get modification time: {}", e)))?;
286
287	let modified_time = chrono::DateTime::<chrono::Utc>::from(modified);
288
289	Ok(modified_time > last_indexed)
290}
291
292/// Get file size with error handling
293pub async fn GetFileSize(file_path:&PathBuf) -> Result<u64> {
294	tokio::task::spawn_blocking({
295		let file_path = file_path.to_path_buf();
296		move || {
297			let metadata = std::fs::metadata(&file_path)
298				.map_err(|e| AirError::FileSystem(format!("Failed to get file metadata: {}", e)))?;
299			Ok(metadata.len())
300		}
301	})
302	.await?
303}
304
305/// Check if file is text-based (likely to be code or documentation)
306pub fn IsTextFile(metadata:&FileMetadata) -> bool {
307	metadata.mime_type.starts_with("text/")
308		|| metadata.mime_type.contains("json")
309		|| metadata.mime_type.contains("xml")
310		|| metadata.mime_type.contains("yaml")
311		|| metadata.mime_type.contains("toml")
312		|| metadata.language.is_some()
313}
314
315/// Check if file is binary (not suitable for indexing)
316pub fn IsBinaryFile(metadata:&FileMetadata) -> bool {
317	!IsTextFile(metadata)
318		|| metadata.mime_type == "application/octet-stream"
319		|| metadata.mime_type == "application/zip"
320		|| metadata.mime_type == "application/x-tar"
321		|| metadata.mime_type == "application/x-gzip"
322		|| metadata.mime_type == "application/x-bzip2"
323}