msy 0.4.2

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
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::PathBuf;
use msy::compress::{decompress, Compression};
use msy::delta::{apply_delta, compute_checksums, Delta};
use msy::sparse::DataRegion;
use msy::sync::scanner::Scanner;

#[derive(Parser)]
#[command(name = "sy-remote")]
#[command(about = "Remote helper for sy - executes on remote hosts via SSH")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Scan a directory and output file list as JSON
    Scan {
        /// Directory to scan
        path: PathBuf,
        /// Disable .gitignore respect
        #[arg(long)]
        no_git_ignore: bool,
        /// Include .git directory
        #[arg(long, default_value_t = false)]
        include_git: bool,
    },
    /// Compute block checksums for a file
    Checksums {
        /// File to compute checksums for
        path: PathBuf,
        /// Block size in bytes
        #[arg(long)]
        block_size: usize,
    },
    /// Compute file checksum (for verification)
    FileChecksum {
        /// File to compute checksum for
        path: PathBuf,
        /// Checksum type: "fast" (xxHash3) or "cryptographic" (BLAKE3)
        #[arg(long, default_value = "fast")]
        checksum_type: String,
    },
    /// Apply delta operations to a file (reads delta JSON from stdin)
    ApplyDelta {
        /// Existing file to apply delta to
        base_file: PathBuf,
        /// Output file path
        output_file: PathBuf,
    },
    /// Receive a file (potentially compressed) from stdin and write to disk
    ReceiveFile {
        /// Output file path
        output_path: PathBuf,
        /// Optional modification time (seconds since epoch)
        #[arg(long)]
        mtime: Option<u64>,
    },
    /// Receive a sparse file with specified data regions
    ReceiveSparseFile {
        /// Output file path
        output_path: PathBuf,
        /// Total file size in bytes
        #[arg(long)]
        total_size: u64,
        /// Data regions as JSON (array of {offset, length} objects)
        #[arg(long)]
        regions: String,
        /// Optional modification time (seconds since epoch)
        #[arg(long)]
        mtime: Option<u64>,
    },
}

#[derive(Debug, Serialize, Deserialize)]
struct ScanOutput {
    entries: Vec<FileEntryJson>,
}

#[derive(Debug, Serialize, Deserialize)]
struct FileEntryJson {
    path: String,
    size: u64,
    mtime: i64,
    is_dir: bool,
    // Extended metadata for full preservation
    is_symlink: bool,
    symlink_target: Option<String>,
    is_sparse: bool,
    allocated_size: u64,
    #[serde(default)]
    xattrs: Option<Vec<(String, String)>>, // (key, base64-encoded value)
    inode: Option<u64>,
    nlink: u64,
    #[serde(default)]
    acls: Option<String>, // ACL text format (one per line)
}

fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Scan {
            path,
            no_git_ignore,
            include_git,
        } => {
            let scanner = Scanner::new(&path)
                .respect_gitignore(!no_git_ignore)
                .include_git_dir(include_git);
            let entries = scanner.scan()?;

            let json_entries: Vec<FileEntryJson> = entries
                .into_iter()
                .map(|e| {
                    let mtime = e
                        .modified
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_secs() as i64;

                    // Encode xattrs to base64 for transport
                    let xattrs = e.xattrs.map(|xattrs_map| {
                        use base64::{engine::general_purpose, Engine as _};
                        xattrs_map
                            .into_iter()
                            .map(|(key, value)| {
                                let encoded = general_purpose::STANDARD.encode(&value);
                                (key, encoded)
                            })
                            .collect()
                    });

                    // Convert ACLs from bytes to string
                    let acls = e
                        .acls
                        .and_then(|acl_bytes| String::from_utf8(acl_bytes).ok());

                    FileEntryJson {
                        path: e.path.to_string_lossy().to_string(),
                        size: e.size,
                        mtime,
                        is_dir: e.is_dir,
                        is_symlink: e.is_symlink,
                        symlink_target: e.symlink_target.map(|p| p.to_string_lossy().to_string()),
                        is_sparse: e.is_sparse,
                        allocated_size: e.allocated_size,
                        xattrs,
                        inode: e.inode,
                        nlink: e.nlink,
                        acls,
                    }
                })
                .collect();

            let output = ScanOutput {
                entries: json_entries,
            };

            println!("{}", serde_json::to_string(&output)?);
        }
        Commands::Checksums { path, block_size } => {
            let checksums = compute_checksums(&path, block_size)?;
            println!("{}", serde_json::to_string(&checksums)?);
        }
        Commands::FileChecksum {
            path,
            checksum_type,
        } => {
            use msy::integrity::{ChecksumType, IntegrityVerifier};

            let csum_type = match checksum_type.as_str() {
                "fast" => ChecksumType::Fast,
                "cryptographic" => ChecksumType::Cryptographic,
                _ => anyhow::bail!(
                    "Invalid checksum type: {}. Use 'fast' or 'cryptographic'",
                    checksum_type
                ),
            };

            let verifier = IntegrityVerifier::new(csum_type, false);
            let checksum = verifier.compute_file_checksum(&path)?;

            // Output checksum as hex string
            println!("{}", checksum.to_hex());
        }
        Commands::ApplyDelta {
            base_file,
            output_file,
        } => {
            // Read delta data from stdin (may be compressed)
            let mut stdin_data = Vec::new();
            std::io::stdin().read_to_end(&mut stdin_data)?;

            // Check if data is compressed (Zstd magic: 0x28, 0xB5, 0x2F, 0xFD)
            let delta_json = if stdin_data.len() >= 4
                && stdin_data[0] == 0x28
                && stdin_data[1] == 0xB5
                && stdin_data[2] == 0x2F
                && stdin_data[3] == 0xFD
            {
                // Decompress zstd data
                let decompressed = decompress(&stdin_data, Compression::Zstd)?;
                String::from_utf8(decompressed)
                    .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?
            } else {
                // Uncompressed JSON
                String::from_utf8(stdin_data)
                    .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?
            };

            let delta: Delta = serde_json::from_str(&delta_json)?;
            let stats = apply_delta(&base_file, &delta, &output_file)?;
            println!(
                "{{\"operations_count\": {}, \"literal_bytes\": {}}}",
                stats.operations_count, stats.literal_bytes
            );
        }
        Commands::ReceiveFile { output_path, mtime } => {
            // Read file data from stdin (may be compressed)
            let mut stdin_data = Vec::new();
            std::io::stdin().read_to_end(&mut stdin_data)?;

            // Check if data is compressed (Zstd magic: 0x28, 0xB5, 0x2F, 0xFD)
            let file_data = if stdin_data.len() >= 4
                && stdin_data[0] == 0x28
                && stdin_data[1] == 0xB5
                && stdin_data[2] == 0x2F
                && stdin_data[3] == 0xFD
            {
                // Decompress zstd data
                decompress(&stdin_data, Compression::Zstd)?
            } else {
                // Uncompressed data
                stdin_data
            };

            // Ensure parent directory exists
            if let Some(parent) = output_path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            // Write file
            let mut output_file = std::fs::File::create(&output_path)?;
            output_file.write_all(&file_data)?;
            output_file.flush()?;

            // Set mtime if provided
            if let Some(mtime_secs) = mtime {
                use std::time::{Duration, UNIX_EPOCH};
                let mtime = UNIX_EPOCH + Duration::from_secs(mtime_secs);
                let _ = filetime::set_file_mtime(
                    &output_path,
                    filetime::FileTime::from_system_time(mtime),
                );
            }

            // Report success with bytes written
            println!("{{\"bytes_written\": {}}}", file_data.len());
        }
        Commands::ReceiveSparseFile {
            output_path,
            total_size,
            regions,
            mtime,
        } => {
            // Parse data regions from JSON
            let data_regions: Vec<DataRegion> = serde_json::from_str(&regions)?;

            // Ensure parent directory exists
            if let Some(parent) = output_path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            // Create file and set its size (creates sparse file with holes)
            let mut output_file = std::fs::File::create(&output_path)?;
            output_file.set_len(total_size)?;

            // Read and write each data region from stdin
            let mut stdin = std::io::stdin();
            let mut total_bytes_written = 0u64;

            for region in &data_regions {
                // Seek to the region's offset
                output_file.seek(SeekFrom::Start(region.offset))?;

                // Read exactly `region.length` bytes from stdin
                let mut buffer = vec![0u8; region.length as usize];
                stdin.read_exact(&mut buffer)?;

                // Write to file
                output_file.write_all(&buffer)?;
                total_bytes_written += region.length;
            }

            output_file.flush()?;
            output_file.sync_all()?;

            // Set mtime if provided
            if let Some(mtime_secs) = mtime {
                use std::time::{Duration, UNIX_EPOCH};
                let mtime = UNIX_EPOCH + Duration::from_secs(mtime_secs);
                let _ = filetime::set_file_mtime(
                    &output_path,
                    filetime::FileTime::from_system_time(mtime),
                );
            }

            // Report success with total data bytes written (not file size)
            println!(
                "{{\"bytes_written\": {}, \"file_size\": {}, \"regions\": {}}}",
                total_bytes_written,
                total_size,
                data_regions.len()
            );
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_receive_sparse_file_basic() {
        let temp = TempDir::new().unwrap();
        let output_path = temp.path().join("sparse_output.dat");

        // Create data regions: 2 regions
        // Region 1: offset 0, length 1024 (data: "A" repeated)
        // Region 2: offset 2048, length 512 (data: "B" repeated)
        let regions = vec![
            DataRegion {
                offset: 0,
                length: 1024,
            },
            DataRegion {
                offset: 2048,
                length: 512,
            },
        ];

        let total_size = 4096; // 4KB total file
        let regions_json = serde_json::to_string(&regions).unwrap();

        // Prepare input data (simulating stdin)
        let mut input_data = Vec::new();
        input_data.extend(vec![b'A'; 1024]); // Region 1 data
        input_data.extend(vec![b'B'; 512]); // Region 2 data

        // Simulate the command (we'll manually execute the logic)
        let mut output_file = std::fs::File::create(&output_path).unwrap();
        output_file.set_len(total_size).unwrap();

        // Parse regions
        let data_regions: Vec<DataRegion> = serde_json::from_str(&regions_json).unwrap();

        // Write regions
        let mut offset_in_buffer = 0;
        for region in &data_regions {
            use std::io::Seek;
            output_file
                .seek(std::io::SeekFrom::Start(region.offset))
                .unwrap();
            output_file
                .write_all(&input_data[offset_in_buffer..offset_in_buffer + region.length as usize])
                .unwrap();
            offset_in_buffer += region.length as usize;
        }

        output_file.flush().unwrap();
        drop(output_file);

        // Verify the file
        let result = std::fs::read(&output_path).unwrap();
        assert_eq!(result.len(), 4096);

        // Check region 1 (offset 0, length 1024) has 'A's
        assert!(result[0..1024].iter().all(|&b| b == b'A'));

        // Check hole (offset 1024..2048) has zeros
        assert!(result[1024..2048].iter().all(|&b| b == 0));

        // Check region 2 (offset 2048, length 512) has 'B's
        assert!(result[2048..2560].iter().all(|&b| b == b'B'));

        // Check remaining (offset 2560..4096) has zeros
        assert!(result[2560..4096].iter().all(|&b| b == 0));

        // Verify file is sparse (on Unix)
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt;
            let metadata = std::fs::metadata(&output_path).unwrap();
            let allocated = metadata.blocks() * 512;
            // File should use less space than total size (due to holes)
            // On some filesystems this might not work, so we just check it doesn't panic
            let _ = allocated < total_size;
        }
    }

    #[test]
    fn test_receive_sparse_file_single_region() {
        let temp = TempDir::new().unwrap();
        let output_path = temp.path().join("single_region.dat");

        // Single region at offset 1MB
        let regions = vec![DataRegion {
            offset: 1024 * 1024,
            length: 100,
        }];

        let total_size = 1024 * 1024 + 200; // Slightly larger
        let regions_json = serde_json::to_string(&regions).unwrap();

        let input_data = vec![b'X'; 100];

        // Execute logic
        let mut output_file = std::fs::File::create(&output_path).unwrap();
        output_file.set_len(total_size).unwrap();

        let data_regions: Vec<DataRegion> = serde_json::from_str(&regions_json).unwrap();

        use std::io::Seek;
        for region in &data_regions {
            output_file
                .seek(std::io::SeekFrom::Start(region.offset))
                .unwrap();
            output_file.write_all(&input_data).unwrap();
        }

        output_file.flush().unwrap();
        drop(output_file);

        // Verify
        let metadata = std::fs::metadata(&output_path).unwrap();
        assert_eq!(metadata.len(), total_size);

        // Read the specific region
        let mut file = std::fs::File::open(&output_path).unwrap();
        file.seek(std::io::SeekFrom::Start(1024 * 1024)).unwrap();
        let mut buffer = vec![0u8; 100];
        file.read_exact(&mut buffer).unwrap();
        assert!(buffer.iter().all(|&b| b == b'X'));
    }

    #[test]
    fn test_data_region_json_serialization() {
        let regions = vec![
            DataRegion {
                offset: 0,
                length: 1024,
            },
            DataRegion {
                offset: 4096,
                length: 2048,
            },
        ];

        let json = serde_json::to_string(&regions).unwrap();
        let deserialized: Vec<DataRegion> = serde_json::from_str(&json).unwrap();

        assert_eq!(regions.len(), deserialized.len());
        assert_eq!(regions[0].offset, deserialized[0].offset);
        assert_eq!(regions[0].length, deserialized[0].length);
        assert_eq!(regions[1].offset, deserialized[1].offset);
        assert_eq!(regions[1].length, deserialized[1].length);
    }
}