1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use crate::sync::SyncEngine;
use crate::transport::Transport;
use anyhow::Result;
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::PathBuf;
use std::sync::mpsc::{channel, RecvTimeoutError};
use std::time::{Duration, Instant};
use tokio::signal;
#[cfg(test)]
use crate::cli::SymlinkMode;
#[cfg(test)]
use crate::integrity::ChecksumType;
pub struct WatchMode<T: Transport> {
engine: SyncEngine<T>,
source: PathBuf,
destination: PathBuf,
debounce: Duration,
}
impl<T: Transport + 'static> WatchMode<T> {
pub fn new(
engine: SyncEngine<T>,
source: PathBuf,
destination: PathBuf,
debounce: Duration,
) -> Self {
Self {
engine,
source,
destination,
debounce,
}
}
pub async fn watch(&self) -> Result<()> {
// Initial sync
tracing::info!("Running initial sync...");
self.engine.sync(&self.source, &self.destination).await?;
// Set up file watcher
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = notify::recommended_watcher(tx)?;
watcher.watch(&self.source, RecursiveMode::Recursive)?;
println!(
"\nđ Watching {} for changes (Ctrl+C to stop)...\n",
self.source.display()
);
// Event loop with debouncing
let mut pending_changes = Vec::new();
let mut last_sync = Instant::now();
// Set up Ctrl+C handler
let ctrl_c = signal::ctrl_c();
tokio::pin!(ctrl_c);
loop {
// Check for Ctrl+C
tokio::select! {
_ = &mut ctrl_c => {
println!("\nâšī¸ Stopping watch mode...");
break;
}
_ = tokio::time::sleep(Duration::from_millis(10)) => {
// Continue to check file events
}
}
// Process file system events
match rx.recv_timeout(Duration::from_millis(100)) {
Ok(Ok(event)) => {
// Filter out events we don't care about
if self.should_sync_event(&event) {
pending_changes.push(event);
}
}
Ok(Err(e)) => {
tracing::error!("Watch error: {}", e);
// Force sync on error to ensure consistency
pending_changes.push(Event::new(notify::EventKind::Other));
}
Err(RecvTimeoutError::Timeout) => {
// Check if we should sync (debounce timeout reached)
if !pending_changes.is_empty() && last_sync.elapsed() >= self.debounce {
tracing::info!("Detected {} changes, syncing...", pending_changes.len());
println!("đ Changes detected, syncing...");
match self.engine.sync(&self.source, &self.destination).await {
Ok(_) => {
println!("â Sync complete\n");
}
Err(e) => {
eprintln!("â Sync failed: {}\n", e);
}
}
pending_changes.clear();
last_sync = Instant::now();
}
}
Err(RecvTimeoutError::Disconnected) => {
tracing::error!("File watcher disconnected unexpectedly");
eprintln!("â File watcher stopped. Exiting.");
break;
}
}
}
Ok(())
}
fn should_sync_event(&self, event: &Event) -> bool {
use notify::EventKind;
match event.kind {
// File created, modified, or removed
EventKind::Create(_)
| EventKind::Modify(_)
| EventKind::Remove(_)
| EventKind::Other => true,
// Ignore metadata-only changes (access time, etc.)
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transport::local::LocalTransport;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_watch_mode_creation() {
let temp = TempDir::new().unwrap();
let source = temp.path().join("src");
let destination = temp.path().join("dst");
fs::create_dir_all(&source).unwrap();
fs::create_dir_all(&destination).unwrap();
let transport = LocalTransport::new();
let engine = SyncEngine::new(
transport,
false, // dry_run
false, // diff_mode
false, // delete
50, // delete_threshold
false, // trash
false, // force_delete
true, // quiet
10, // parallel
100, // max_errors
None, // min_size
None, // max_size
crate::filter::FilterEngine::new(), // filter_engine
None, // bwlimit
false, // resume
10, // checkpoint_files
100, // checkpoint_bytes
false, // json
ChecksumType::None, // verification_mode
false, // verify_on_write
SymlinkMode::Preserve, // symlink_mode
false, // preserve_xattrs
false, // preserve_hardlinks
false, // preserve_acls
false, // preserve_flags
false, // per_file_progress
false, // ignore_times
false, // size_only
false, // checksum
false, // update_only
false, // ignore_existing
false, // use_cache
false, // clear_cache
false, // checksum_db
false, // clear_checksum_db
false, // prune_checksum_db
false, // dest_is_remote
false, // perf
);
let watch_mode = WatchMode::new(
engine,
source.clone(),
destination.clone(),
Duration::from_millis(500),
);
assert_eq!(watch_mode.source, source);
assert_eq!(watch_mode.destination, destination);
assert_eq!(watch_mode.debounce, Duration::from_millis(500));
}
#[test]
fn test_should_sync_event() {
use notify::{Event, EventKind};
let temp = TempDir::new().unwrap();
let source = temp.path().join("src");
let destination = temp.path().join("dst");
fs::create_dir_all(&source).unwrap();
fs::create_dir_all(&destination).unwrap();
let transport = LocalTransport::new();
let engine = SyncEngine::new(
transport,
false, // dry_run
false, // diff_mode
false, // delete
50, // delete_threshold
false, // trash
false, // force_delete
true,
10,
100, // max_errors
None,
None,
crate::filter::FilterEngine::new(),
None,
false,
10,
100,
false,
ChecksumType::None,
false,
SymlinkMode::Preserve,
false,
false,
false,
false, // preserve_flags
false, // per_file_progress
false, // ignore_times
false, // size_only
false, // checksum
false, // update_only
false, // ignore_existing
false, // use_cache
false, // clear_cache
false, // checksum_db
false, // clear_checksum_db
false, // prune_checksum_db
false, // dest_is_remote
false, // perf
);
let watch_mode = WatchMode::new(engine, source, destination, Duration::from_millis(500));
// Should sync on create, modify, remove
let create_event = Event::new(EventKind::Create(notify::event::CreateKind::File));
assert!(watch_mode.should_sync_event(&create_event));
let modify_event = Event::new(EventKind::Modify(notify::event::ModifyKind::Data(
notify::event::DataChange::Any,
)));
assert!(watch_mode.should_sync_event(&modify_event));
let remove_event = Event::new(EventKind::Remove(notify::event::RemoveKind::File));
assert!(watch_mode.should_sync_event(&remove_event));
// Should not sync on access events
let access_event = Event::new(EventKind::Access(notify::event::AccessKind::Read));
assert!(!watch_mode.should_sync_event(&access_event));
}
}