Upload FastSeekWpf/MainWindow.xaml.cs
Browse files- FastSeekWpf/MainWindow.xaml.cs +117 -77
FastSeekWpf/MainWindow.xaml.cs
CHANGED
|
@@ -51,6 +51,7 @@ public partial class MainWindow : Window, INotifyPropertyChanged
|
|
| 51 |
|
| 52 |
public MainWindow()
|
| 53 |
{
|
|
|
|
| 54 |
InitializeComponent();
|
| 55 |
DataContext = this;
|
| 56 |
_excludedDirs = CacheManager.LoadExclusions();
|
|
@@ -79,132 +80,171 @@ public partial class MainWindow : Window, INotifyPropertyChanged
|
|
| 79 |
|
| 80 |
int darkMode = 1;
|
| 81 |
Win32Api.DwmSetWindowAttribute(hwnd, Win32Api.DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkMode, Marshal.SizeOf(darkMode));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
-
catch { /* DWM not available on older Windows */ }
|
| 84 |
}
|
| 85 |
|
| 86 |
private void Window_Loaded(object sender, RoutedEventArgs e)
|
| 87 |
{
|
|
|
|
| 88 |
if (_initialized) return;
|
| 89 |
_initialized = true;
|
| 90 |
|
|
|
|
| 91 |
Task.Run(async () => await InitializeAsync());
|
| 92 |
}
|
| 93 |
|
| 94 |
private async Task InitializeAsync()
|
| 95 |
{
|
| 96 |
-
|
| 97 |
-
|
| 98 |
{
|
| 99 |
-
|
| 100 |
-
{
|
| 101 |
-
StatusText.Text = "No NTFS drives found. Run as Administrator.";
|
| 102 |
-
StatusBar.Visibility = Visibility.Visible;
|
| 103 |
-
});
|
| 104 |
-
return;
|
| 105 |
-
}
|
| 106 |
|
| 107 |
-
|
| 108 |
-
bool needFullScan = true;
|
| 109 |
-
var cache = CacheManager.LoadCache();
|
| 110 |
-
if (cache != null)
|
| 111 |
-
{
|
| 112 |
-
lock (_indexLock)
|
| 113 |
{
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
}
|
| 116 |
-
Debug.WriteLine($"Loaded cache: {_index.Count} files");
|
| 117 |
|
| 118 |
-
//
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
{
|
| 123 |
-
|
| 124 |
-
|
| 125 |
{
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
{
|
| 128 |
-
|
| 129 |
-
watcher.Drain();
|
| 130 |
-
lock (_indexLock)
|
| 131 |
{
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
}
|
| 135 |
-
_watchers.Add(watcher);
|
| 136 |
}
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
{
|
| 139 |
-
|
| 140 |
-
deltaOk = false;
|
| 141 |
-
break;
|
| 142 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
}
|
| 145 |
|
| 146 |
-
if (
|
| 147 |
{
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
while (_eventQueue.TryDequeue(out var evt))
|
| 151 |
{
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
}
|
| 154 |
-
}
|
| 155 |
-
else
|
| 156 |
-
{
|
| 157 |
-
CacheManager.ClearCache();
|
| 158 |
-
_watchers.Clear();
|
| 159 |
-
_eventQueue.Clear();
|
| 160 |
-
}
|
| 161 |
-
}
|
| 162 |
|
| 163 |
-
|
| 164 |
-
{
|
| 165 |
-
lock (_indexLock)
|
| 166 |
-
{
|
| 167 |
-
_index = new IndexStore();
|
| 168 |
foreach (var drive in _drives)
|
| 169 |
{
|
| 170 |
try
|
| 171 |
{
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
Debug.WriteLine($"Scanned {drive.Letter}: {scan.Records.Count} files");
|
| 177 |
}
|
| 178 |
catch (Exception ex)
|
| 179 |
{
|
| 180 |
-
|
| 181 |
}
|
| 182 |
}
|
| 183 |
-
_index.CompleteIndex();
|
| 184 |
-
CacheManager.SaveCache(_index);
|
| 185 |
}
|
| 186 |
|
| 187 |
-
//
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
{
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
_ = watcher.RunAsync(CancellationToken.None);
|
| 195 |
-
}
|
| 196 |
-
catch { /* ignore */ }
|
| 197 |
-
}
|
| 198 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
_ = Task.Run(() => LiveUpdateLoop(_cts.Token));
|
| 203 |
-
|
| 204 |
Dispatcher.Invoke(() =>
|
| 205 |
{
|
| 206 |
-
|
| 207 |
-
StatusBar.Visibility = Visibility.Collapsed;
|
| 208 |
});
|
| 209 |
}
|
| 210 |
|
|
|
|
| 51 |
|
| 52 |
public MainWindow()
|
| 53 |
{
|
| 54 |
+
Logger.Log("MainWindow constructor");
|
| 55 |
InitializeComponent();
|
| 56 |
DataContext = this;
|
| 57 |
_excludedDirs = CacheManager.LoadExclusions();
|
|
|
|
| 80 |
|
| 81 |
int darkMode = 1;
|
| 82 |
Win32Api.DwmSetWindowAttribute(hwnd, Win32Api.DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkMode, Marshal.SizeOf(darkMode));
|
| 83 |
+
Logger.Log("DWM rounded corners + dark mode enabled.");
|
| 84 |
+
}
|
| 85 |
+
catch (Exception ex)
|
| 86 |
+
{
|
| 87 |
+
Logger.Log($"DWM setup failed: {ex.Message}");
|
| 88 |
}
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
private void Window_Loaded(object sender, RoutedEventArgs e)
|
| 92 |
{
|
| 93 |
+
Logger.Log("Window_Loaded");
|
| 94 |
if (_initialized) return;
|
| 95 |
_initialized = true;
|
| 96 |
|
| 97 |
+
CenterWindow();
|
| 98 |
Task.Run(async () => await InitializeAsync());
|
| 99 |
}
|
| 100 |
|
| 101 |
private async Task InitializeAsync()
|
| 102 |
{
|
| 103 |
+
Logger.Log("InitializeAsync start");
|
| 104 |
+
try
|
| 105 |
{
|
| 106 |
+
_drives = DriveDiscovery.GetNtfsDrives();
|
| 107 |
+
Logger.Log($"Found {_drives.Count} NTFS drives");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
if (_drives.Count == 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
{
|
| 111 |
+
Dispatcher.Invoke(() =>
|
| 112 |
+
{
|
| 113 |
+
StatusText.Text = "No NTFS drives found. Run as Administrator.";
|
| 114 |
+
StatusBar.Visibility = Visibility.Visible;
|
| 115 |
+
});
|
| 116 |
+
ShowError("No NTFS drives found. Make sure you are running as Administrator.");
|
| 117 |
+
return;
|
| 118 |
}
|
|
|
|
| 119 |
|
| 120 |
+
// Try cache first
|
| 121 |
+
bool needFullScan = true;
|
| 122 |
+
var cache = CacheManager.LoadCache();
|
| 123 |
+
if (cache != null)
|
| 124 |
{
|
| 125 |
+
Logger.Log($"Cache loaded: {cache.Entries.Count} entries");
|
| 126 |
+
lock (_indexLock)
|
| 127 |
{
|
| 128 |
+
_index = IndexStore.FromCache(cache);
|
| 129 |
+
}
|
| 130 |
+
Logger.Log($"Loaded cache: {_index.Count} files");
|
| 131 |
+
|
| 132 |
+
// Delta catch-up
|
| 133 |
+
var checkpoints = new List<JournalCheckpoint>(cache.Checkpoints);
|
| 134 |
+
bool deltaOk = true;
|
| 135 |
+
foreach (var drive in _drives)
|
| 136 |
+
{
|
| 137 |
+
var cp = checkpoints.FirstOrDefault(c => c.DriveLetter == drive.Letter);
|
| 138 |
+
if (cp != null)
|
| 139 |
{
|
| 140 |
+
try
|
|
|
|
|
|
|
| 141 |
{
|
| 142 |
+
var watcher = UsnWatcher.CreateForCheckpoint(drive, evt => _eventQueue.Enqueue(evt), cp);
|
| 143 |
+
watcher.Drain();
|
| 144 |
+
lock (_indexLock)
|
| 145 |
+
{
|
| 146 |
+
_index.Checkpoints.RemoveAll(c => c.DriveLetter == drive.Letter);
|
| 147 |
+
_index.Checkpoints.Add(watcher.Checkpoint());
|
| 148 |
+
}
|
| 149 |
+
_watchers.Add(watcher);
|
| 150 |
+
Logger.Log($"Delta catch-up OK for {drive.Letter}");
|
| 151 |
+
}
|
| 152 |
+
catch (Exception ex)
|
| 153 |
+
{
|
| 154 |
+
Logger.Log($"Delta catch-up failed: {ex.Message}");
|
| 155 |
+
deltaOk = false;
|
| 156 |
+
break;
|
| 157 |
}
|
|
|
|
| 158 |
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
if (deltaOk)
|
| 162 |
+
{
|
| 163 |
+
needFullScan = false;
|
| 164 |
+
// Apply delta events
|
| 165 |
+
while (_eventQueue.TryDequeue(out var evt))
|
| 166 |
{
|
| 167 |
+
ApplyEvent(evt);
|
|
|
|
|
|
|
| 168 |
}
|
| 169 |
+
Logger.Log("Delta events applied.");
|
| 170 |
+
}
|
| 171 |
+
else
|
| 172 |
+
{
|
| 173 |
+
Logger.Log("Delta failed, clearing cache.");
|
| 174 |
+
CacheManager.ClearCache();
|
| 175 |
+
_watchers.Clear();
|
| 176 |
+
_eventQueue.Clear();
|
| 177 |
}
|
| 178 |
}
|
| 179 |
|
| 180 |
+
if (needFullScan)
|
| 181 |
{
|
| 182 |
+
Logger.Log("Starting full MFT scan...");
|
| 183 |
+
lock (_indexLock)
|
|
|
|
| 184 |
{
|
| 185 |
+
_index = new IndexStore();
|
| 186 |
+
foreach (var drive in _drives)
|
| 187 |
+
{
|
| 188 |
+
try
|
| 189 |
+
{
|
| 190 |
+
Logger.Log($"Scanning {drive.Letter}...");
|
| 191 |
+
using var reader = new MftReader(drive);
|
| 192 |
+
var scan = reader.ScanDirect();
|
| 193 |
+
if (scan == null) scan = reader.Scan();
|
| 194 |
+
_index.PopulateFromScan(scan, drive.Root);
|
| 195 |
+
Logger.Log($"Scanned {drive.Letter}: {scan.Records.Count} files");
|
| 196 |
+
}
|
| 197 |
+
catch (Exception ex)
|
| 198 |
+
{
|
| 199 |
+
Logger.Log($"Scan failed for {drive.Letter}: {ex}");
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
_index.CompleteIndex();
|
| 203 |
+
Logger.Log($"Full scan complete. Total indexed: {_index.Count}");
|
| 204 |
+
CacheManager.SaveCache(_index);
|
| 205 |
+
Logger.Log("Cache saved.");
|
| 206 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
// Set up USN watchers for live updates
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
foreach (var drive in _drives)
|
| 210 |
{
|
| 211 |
try
|
| 212 |
{
|
| 213 |
+
var watcher = new UsnWatcher(drive, evt => _eventQueue.Enqueue(evt), null);
|
| 214 |
+
_watchers.Add(watcher);
|
| 215 |
+
_ = watcher.RunAsync(CancellationToken.None);
|
| 216 |
+
Logger.Log($"USN watcher started for {drive.Letter}");
|
|
|
|
| 217 |
}
|
| 218 |
catch (Exception ex)
|
| 219 |
{
|
| 220 |
+
Logger.Log($"USN watcher failed for {drive.Letter}: {ex.Message}");
|
| 221 |
}
|
| 222 |
}
|
|
|
|
|
|
|
| 223 |
}
|
| 224 |
|
| 225 |
+
// Start live update processor
|
| 226 |
+
_cts = new CancellationTokenSource();
|
| 227 |
+
_ = Task.Run(() => LiveUpdateLoop(_cts.Token));
|
| 228 |
+
|
| 229 |
+
Dispatcher.Invoke(() =>
|
| 230 |
{
|
| 231 |
+
StatusText.Text = $"{_index.Count} files indexed";
|
| 232 |
+
StatusBar.Visibility = Visibility.Collapsed;
|
| 233 |
+
});
|
| 234 |
+
Logger.Log("InitializeAsync done.");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
}
|
| 236 |
+
catch (Exception ex)
|
| 237 |
+
{
|
| 238 |
+
Logger.Log($"InitializeAsync fatal: {ex}");
|
| 239 |
+
ShowError($"Initialization failed:\n{ex.Message}\n\nCheck log at %LOCALAPPDATA%\\FastSeek\\log.txt");
|
| 240 |
+
}
|
| 241 |
+
}
|
| 242 |
|
| 243 |
+
private void ShowError(string message)
|
| 244 |
+
{
|
|
|
|
|
|
|
| 245 |
Dispatcher.Invoke(() =>
|
| 246 |
{
|
| 247 |
+
MessageBox.Show(message, "FastSeek Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
|
| 248 |
});
|
| 249 |
}
|
| 250 |
|