import Foundation import SQLite3 // MarkBaseFS FSKit Probe Tool // Checks database integrity and returns probe result to fskitd func probeMarkBaseFS() -> Bool { print("=== MarkBaseFS Probe Tool ===") print("Date: \(Date())") print("") // Step 1: Check database file exists let dbPath = "/Users/accusys/markbase/data/users/warren.sqlite" print("Checking database file: \(dbPath)") if !FileManager.default.fileExists(atPath: dbPath) { print(" ❌ Database file NOT found") return false } print(" ✅ Database file found") // Step 2: Check database integrity print("Checking database integrity...") var db: OpaquePointer? if sqlite3_open(dbPath, &db) != SQLITE_OK { print(" ❌ Failed to open database") return false } print(" ✅ Database opened successfully") // Step 3: Check file_nodes table exists print("Checking file_nodes table...") let query = "SELECT name FROM sqlite_master WHERE type='table' AND name='file_nodes'" var stmt: OpaquePointer? if sqlite3_prepare_v2(db, query, -1, &stmt, nil) != SQLITE_OK { print(" ❌ Failed to prepare query") sqlite3_close(db) return false } if sqlite3_step(stmt) == SQLITE_ROW { let tableName = String(cString: sqlite3_column_text(stmt, 0)) print(" ✅ Table found: \(tableName)") sqlite3_finalize(stmt) sqlite3_close(db) // Step 4: Check data count print("Checking data count...") var db2: OpaquePointer? if sqlite3_open(dbPath, &db2) == SQLITE_OK { let countQuery = "SELECT COUNT(*) FROM file_nodes" var stmt2: OpaquePointer? if sqlite3_prepare_v2(db2, countQuery, -1, &stmt2, nil) == SQLITE_OK { if sqlite3_step(stmt2) == SQLITE_ROW { let count = sqlite3_column_int(stmt2, 0) print(" ✅ Found \(count) nodes in database") sqlite3_finalize(stmt2) sqlite3_close(db2) return count > 0 } sqlite3_finalize(stmt2) } sqlite3_close(db2) } return true } print(" ❌ file_nodes table NOT found") sqlite3_finalize(stmt) sqlite3_close(db) return false } // Main entry point print("Arguments: \(CommandLine.arguments)") print("") // Handle probe request if CommandLine.arguments.contains("-p") { print("Probe request received (-p flag)") print("") let result = probeMarkBaseFS() print("") print("=== Probe Result ===") print("Result: \(result ? "YES" : "NO")") // Return result to fskitd (stdout) // fskitd expects "YES" or "NO" print(result ? "YES" : "NO") exit(result ? 0 : 1) } // Handle other requests if CommandLine.arguments.count > 1 { print("Unknown command: \(CommandLine.arguments[1])") print("Supported commands: -p (probe)") exit(1) } print("No arguments provided") print("Usage: MarkBaseFSProbe -p") exit(1)