msy 0.4.5

Modern musl rsync alternative - Fast, parallel file synchronization
Documentation
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use std::fs;
use std::io::{Read, Seek, SeekFrom, Write};
use std::process::Command;
use tempfile::TempDir;

fn sy_bin() -> String {
	env!("CARGO_BIN_EXE_sy").to_string()
}

fn setup_test_dir(_name: &str) -> (TempDir, TempDir) {
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();

	// Create git repo in source for .gitignore support
	Command::new("git").args(["init"]).current_dir(source.path()).output().unwrap();

	(source, dest)
}

#[test]
fn test_basic_sync() {
	let (source, dest) = setup_test_dir("basic");

	// Create test files
	fs::write(source.path().join("file1.txt"), "content1").unwrap();
	fs::write(source.path().join("file2.txt"), "content2").unwrap();

	// Run sync
	let output = Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert!(output.status.success());
	assert!(dest.path().join("file1.txt").exists());
	assert!(dest.path().join("file2.txt").exists());
	assert_eq!(fs::read_to_string(dest.path().join("file1.txt")).unwrap(), "content1");
}

#[test]
fn test_dry_run() {
	let (source, dest) = setup_test_dir("dry_run");

	fs::write(source.path().join("file.txt"), "content").unwrap();

	// Run dry-run
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--dry-run"])
		.output()
		.unwrap();

	assert!(output.status.success());
	assert!(!dest.path().join("file.txt").exists());

	let stdout = String::from_utf8_lossy(&output.stdout);
	assert!(stdout.contains("Dry-run"));
}

#[test]
fn test_delete_mode() {
	let (source, dest) = setup_test_dir("delete");

	fs::write(source.path().join("keep.txt"), "keep").unwrap();
	fs::write(dest.path().join("keep.txt"), "keep").unwrap();
	fs::write(dest.path().join("delete.txt"), "delete").unwrap();

	// Run with --delete
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--delete"])
		.output()
		.unwrap();

	assert!(output.status.success());
	assert!(dest.path().join("keep.txt").exists());
	assert!(!dest.path().join("delete.txt").exists());
}

#[test]
fn test_gitignore_support() {
	let (source, dest) = setup_test_dir("gitignore");

	// Create .gitignore
	fs::write(source.path().join(".gitignore"), "*.log\n").unwrap();
	fs::write(source.path().join("keep.txt"), "keep").unwrap();
	fs::write(source.path().join("ignore.log"), "ignore").unwrap();

	// Run sync with --gitignore flag to respect .gitignore patterns
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--gitignore"])
		.output()
		.unwrap();

	assert!(output.status.success());
	assert!(dest.path().join("keep.txt").exists());
	assert!(!dest.path().join("ignore.log").exists());
	assert!(dest.path().join(".gitignore").exists());
}

#[test]
fn test_nested_directories() {
	let (source, dest) = setup_test_dir("nested");

	// Create nested structure
	fs::create_dir_all(source.path().join("dir1/dir2/dir3")).unwrap();
	fs::write(source.path().join("dir1/file1.txt"), "content1").unwrap();
	fs::write(source.path().join("dir1/dir2/file2.txt"), "content2").unwrap();
	fs::write(source.path().join("dir1/dir2/dir3/file3.txt"), "content3").unwrap();

	// Run sync
	let output = Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert!(output.status.success());
	assert!(dest.path().join("dir1/file1.txt").exists());
	assert!(dest.path().join("dir1/dir2/file2.txt").exists());
	assert!(dest.path().join("dir1/dir2/dir3/file3.txt").exists());
}

#[test]
fn test_update_existing_files() {
	let (source, dest) = setup_test_dir("update");

	// Initial sync
	fs::write(source.path().join("file.txt"), "v1").unwrap();
	Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert_eq!(fs::read_to_string(dest.path().join("file.txt")).unwrap(), "v1");

	// Wait to ensure mtime changes (mtime has 1s tolerance)
	std::thread::sleep(std::time::Duration::from_secs(2));

	// Update source file
	fs::write(source.path().join("file.txt"), "v2").unwrap();

	// Sync again
	let output = Command::new(sy_bin())
		.args(["-v", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());
	assert_eq!(fs::read_to_string(dest.path().join("file.txt")).unwrap(), "v2");

	let stdout = String::from_utf8_lossy(&output.stdout);
	assert!(stdout.contains("Files updated:     1"));
}

#[test]
fn test_skip_unchanged_files() {
	let (source, dest) = setup_test_dir("skip");

	fs::write(source.path().join("file.txt"), "content").unwrap();

	// First sync (exclude .git to get predictable file counts)
	Command::new(sy_bin())
		.args(["-v", &format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--exclude-vcs"])
		.output()
		.unwrap();

	// Second sync (should skip)
	let output = Command::new(sy_bin())
		.args(["-v", &format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--exclude-vcs"])
		.output()
		.unwrap();

	assert!(output.status.success());
	let stdout = String::from_utf8_lossy(&output.stdout);
	assert!(stdout.contains("Files skipped:     1"));
}

#[test]
fn test_quiet_mode() {
	let (source, dest) = setup_test_dir("quiet");

	fs::write(source.path().join("file.txt"), "content").unwrap();

	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--quiet"])
		.output()
		.unwrap();

	assert!(output.status.success());
	let stdout = String::from_utf8_lossy(&output.stdout);
	// Should have minimal output in quiet mode
	assert!(!stdout.contains("sy v"));
}

#[test]
fn test_error_source_not_exists() {
	let dest = TempDir::new().unwrap();

	let output = Command::new(sy_bin()).args(["/nonexistent/path", dest.path().to_str().unwrap()]).output().unwrap();

	assert!(!output.status.success());
	let stderr = String::from_utf8_lossy(&output.stderr);
	assert!(stderr.contains("does not exist"));
}

#[tokio::test]
async fn test_single_file_sync() {
	let temp = TempDir::new().unwrap();
	let file_path = temp.path().join("file.txt");
	fs::write(&file_path, "test content for single file").unwrap();

	let dest_file = temp.path().join("dest.txt");

	let output = Command::new(sy_bin()).args([file_path.to_str().unwrap(), dest_file.to_str().unwrap()]).output().unwrap();

	assert!(
		output.status.success(),
		"stdout: {}, stderr: {}",
		String::from_utf8_lossy(&output.stdout),
		String::from_utf8_lossy(&output.stderr)
	);
	assert!(dest_file.exists());
	assert_eq!(fs::read_to_string(&dest_file).unwrap(), "test content for single file");
}

#[test]
fn test_git_directory_excluded() {
	let (source, dest) = setup_test_dir("git_exclude");

	// Git repo already initialized by setup
	// Add a file in .git
	fs::write(source.path().join(".git/config"), "test").unwrap();
	fs::write(source.path().join("file.txt"), "content").unwrap();

	// Use --exclude-vcs to exclude .git directory
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--exclude-vcs"])
		.output()
		.unwrap();

	assert!(output.status.success());
	assert!(dest.path().join("file.txt").exists());
	assert!(!dest.path().join(".git").exists());
}

#[test]
fn test_update_shows_correct_stats() {
	let (source, dest) = setup_test_dir("update_stats");

	// Create initial files
	fs::write(source.path().join("file1.txt"), "initial content v1").unwrap();
	fs::write(source.path().join("file2.txt"), "initial content v2").unwrap();

	// Initial sync (exclude .git for predictable file counts)
	let output = Command::new(sy_bin())
		.args(["-v", &format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--exclude-vcs"])
		.output()
		.unwrap();

	assert!(output.status.success());
	let stdout = String::from_utf8_lossy(&output.stdout);
	assert!(stdout.contains("Files created:     2"));

	// Wait to ensure mtime changes (1s tolerance)
	std::thread::sleep(std::time::Duration::from_secs(2));

	// Modify files
	fs::write(source.path().join("file1.txt"), "updated content v1").unwrap();
	fs::write(source.path().join("file2.txt"), "updated content v2").unwrap();

	// Sync again - should show updates
	let output = Command::new(sy_bin())
		.args(["-v", &format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--exclude-vcs"])
		.output()
		.unwrap();

	assert!(output.status.success());
	let stdout = String::from_utf8_lossy(&output.stdout);

	// Verify update stats
	assert!(stdout.contains("Files updated:     2"));
	assert!(stdout.contains("Files skipped:     0"));

	// Verify files were actually updated
	assert_eq!(fs::read_to_string(dest.path().join("file1.txt")).unwrap(), "updated content v1");
	assert_eq!(fs::read_to_string(dest.path().join("file2.txt")).unwrap(), "updated content v2");
}

#[test]
#[ignore] // Slow test - requires 2GB file creation and sync
fn test_large_file_update_with_delta_sync() {
	let (source, dest) = setup_test_dir("delta_sync");

	// Create a large file (2GB) to trigger local delta sync
	// Using sparse file for speed - only allocates actual written blocks
	let large_file = source.path().join("large.bin");
	let file = fs::File::create(&large_file).unwrap();
	file.set_len(2 * 1024 * 1024 * 1024).unwrap(); // 2GB
	drop(file);

	// Write some actual data at the beginning
	let mut file = fs::OpenOptions::new().write(true).open(&large_file).unwrap();
	file.write_all(b"START OF FILE").unwrap();
	file.seek(SeekFrom::End(-13)).unwrap();
	file.write_all(b"END OF FILE!!").unwrap();
	drop(file);

	// Initial sync
	let output = Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert!(output.status.success());
	assert!(dest.path().join("large.bin").exists());

	// Wait for mtime to change
	std::thread::sleep(std::time::Duration::from_secs(2));

	// Modify the file slightly (change just the beginning)
	let mut file = fs::OpenOptions::new().write(true).open(&large_file).unwrap();
	file.write_all(b"MODIFIED FILE").unwrap();
	drop(file);

	// Sync again - should use delta sync
	let output = Command::new(sy_bin())
		.args(["-v", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());
	let stdout = String::from_utf8_lossy(&output.stdout);

	// Delta sync should be used for large files
	// The output should mention delta sync
	assert!(stdout.contains("Files updated:     1"));

	// If delta sync was used, it should appear in summary
	// Note: Delta sync only triggers for files >1GB on local, and this is >2GB
	if stdout.contains("Delta sync:") {
		// Verify delta sync stats are shown
		assert!(stdout.contains("1 files"));
	}

	// Verify the file was updated correctly
	let dest_file = dest.path().join("large.bin");
	let mut file = fs::File::open(&dest_file).unwrap();
	let mut buf = [0u8; 13];
	file.read_exact(&mut buf).unwrap();
	assert_eq!(&buf, b"MODIFIED FILE");
}

#[test]
fn test_directory_cache_created() {
	let (source, dest) = setup_test_dir("cache_created");

	// Create test structure
	fs::create_dir_all(source.path().join("dir1/dir2")).unwrap();
	fs::write(source.path().join("file1.txt"), "content1").unwrap();
	fs::write(source.path().join("dir1/file2.txt"), "content2").unwrap();
	fs::write(source.path().join("dir1/dir2/file3.txt"), "content3").unwrap();

	// Run sync with --use-cache
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--use-cache=true"])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Verify cache file was created
	let cache_file = dest.path().join(".sy-dir-cache.json");
	assert!(cache_file.exists(), "Directory cache file should be created at {}", cache_file.display());

	// Verify cache contains valid JSON
	let cache_content = fs::read_to_string(&cache_file).unwrap();
	let cache_json: serde_json::Value = serde_json::from_str(&cache_content).unwrap();

	// Verify cache has expected structure
	assert!(cache_json.get("directories").is_some());
	assert!(cache_json.get("version").is_some());
	assert!(cache_json.get("last_updated").is_some());
}

#[test]
fn test_directory_cache_not_created_by_default() {
	let (source, dest) = setup_test_dir("cache_default");

	fs::write(source.path().join("file.txt"), "content").unwrap();

	// Run sync WITHOUT --use-cache
	let output = Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert!(output.status.success());

	// Verify cache file was NOT created
	let cache_file = dest.path().join(".sy-dir-cache.json");
	assert!(!cache_file.exists(), "Directory cache should not be created without --use-cache flag");
}

#[test]
fn test_directory_cache_persists() {
	let (source, dest) = setup_test_dir("cache_persist");

	// Create nested directory structure
	fs::create_dir_all(source.path().join("a/b/c")).unwrap();
	fs::write(source.path().join("a/file1.txt"), "content1").unwrap();
	fs::write(source.path().join("a/b/file2.txt"), "content2").unwrap();
	fs::write(source.path().join("a/b/c/file3.txt"), "content3").unwrap();

	// First sync with cache
	Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--use-cache=true"])
		.output()
		.unwrap();

	let cache_file = dest.path().join(".sy-dir-cache.json");
	assert!(cache_file.exists());

	// Read initial cache
	let initial_cache = fs::read_to_string(&cache_file).unwrap();
	let initial_json: serde_json::Value = serde_json::from_str(&initial_cache).unwrap();
	let initial_dirs = initial_json["directories"].as_object().unwrap();

	// Should have cached root + 3 subdirectories (a, a/b, a/b/c)
	assert!(initial_dirs.len() >= 3, "Cache should contain at least 3 directories, found {}", initial_dirs.len());

	// Wait to ensure mtime would change if directories were rescanned
	std::thread::sleep(std::time::Duration::from_millis(100));

	// Second sync with cache (no changes)
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--use-cache=true"])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Cache should still exist and be valid
	assert!(cache_file.exists());
	let second_cache = fs::read_to_string(&cache_file).unwrap();
	let second_json: serde_json::Value = serde_json::from_str(&second_cache).unwrap();

	// Verify cache still has expected structure
	assert!(second_json.get("directories").is_some());
	assert!(second_json.get("version").is_some());
}

#[test]
fn test_directory_cache_clear() {
	let (source, dest) = setup_test_dir("cache_clear");

	fs::write(source.path().join("file.txt"), "content").unwrap();

	// First sync with cache
	Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--use-cache=true"])
		.output()
		.unwrap();

	let cache_file = dest.path().join(".sy-dir-cache.json");
	assert!(cache_file.exists(), "Cache should be created");

	// Sync with --clear-cache
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--clear-cache"])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Cache should be deleted
	assert!(!cache_file.exists(), "Cache should be deleted after --clear-cache");
}

#[test]
fn test_directory_cache_dry_run() {
	let (source, dest) = setup_test_dir("cache_dry_run");

	fs::write(source.path().join("file.txt"), "content").unwrap();

	// Dry run with --use-cache should not save cache
	let output = Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--use-cache=true", "--dry-run"])
		.output()
		.unwrap();

	assert!(output.status.success());

	let cache_file = dest.path().join(".sy-dir-cache.json");
	assert!(!cache_file.exists(), "Cache should not be saved during dry-run");
}

#[test]
fn test_directory_cache_updates_on_new_directories() {
	let (source, dest) = setup_test_dir("cache_updates");

	// Initial sync with one directory
	fs::create_dir_all(source.path().join("dir1")).unwrap();
	fs::write(source.path().join("dir1/file1.txt"), "content1").unwrap();

	Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--use-cache=true"])
		.output()
		.unwrap();

	let cache_file = dest.path().join(".sy-dir-cache.json");
	let initial_cache = fs::read_to_string(&cache_file).unwrap();
	let initial_json: serde_json::Value = serde_json::from_str(&initial_cache).unwrap();
	let initial_count = initial_json["directories"].as_object().unwrap().len();

	// Wait to ensure mtime changes
	std::thread::sleep(std::time::Duration::from_secs(2));

	// Add new directories
	fs::create_dir_all(source.path().join("dir2")).unwrap();
	fs::create_dir_all(source.path().join("dir3/subdir")).unwrap();
	fs::write(source.path().join("dir2/file2.txt"), "content2").unwrap();
	fs::write(source.path().join("dir3/subdir/file3.txt"), "content3").unwrap();

	// Sync again
	Command::new(sy_bin())
		.args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap(), "--use-cache=true"])
		.output()
		.unwrap();

	// Cache should be updated with new directories
	let updated_cache = fs::read_to_string(&cache_file).unwrap();
	let updated_json: serde_json::Value = serde_json::from_str(&updated_cache).unwrap();
	let updated_count = updated_json["directories"].as_object().unwrap().len();

	// Should have more directories now (added dir2, dir3, dir3/subdir)
	assert!(
		updated_count > initial_count,
		"Cache should be updated with new directories. Initial: {}, Updated: {}",
		initial_count,
		updated_count
	);
}