bale 0.1.0

A mmap-first, fixed-stride zip-like pack format
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Archive compaction to reclaim space from orphaned data.

use crate::{
    ArchivePath, ArchiveRead, ArchiveReader, ArchiveWrite, ArchiveWriter, BaleError, EntryKind,
};
use nix::sys::stat::SFlag;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

/// Default permission bits for directories (rwxr-xr-x).
const DEFAULT_DIR_PERM: u32 = 0o755;

/// Guard that deletes a temp file on drop unless marked to persist.
struct TempFileGuard {
    /// Path to the temp file.
    path: PathBuf,
    /// If true, the file is kept (renamed to final destination).
    persist: bool,
}

impl TempFileGuard {
    /// Creates a guard for the given path.
    fn new(path: PathBuf) -> Self {
        Self {
            path,
            persist: false,
        }
    }

    /// Marks the file to be persisted (not deleted on drop).
    fn persist(&mut self) {
        self.persist = true;
    }
}

impl Drop for TempFileGuard {
    fn drop(&mut self) {
        if !self.persist {
            let _ = fs::remove_file(&self.path);
        }
    }
}

/// Statistics from a compact operation.
#[derive(Debug, Clone, Copy, Default)]
pub struct CompactStats {
    /// Original archive size in bytes.
    pub original_size: u64,
    /// Compacted archive size in bytes.
    pub compacted_size: u64,
    /// Number of duplicate/shadowed entries removed.
    pub entries_removed: usize,
    /// Bytes reclaimed by compaction.
    pub bytes_reclaimed: u64,
}

/// Compacts an archive, removing orphaned data and duplicate entries.
///
/// This operation:
/// 1. Opens the archive for reading
/// 2. Creates a temp file with a new writer
/// 3. Copies non-duplicate entries (keeping only the last occurrence of each path)
/// 4. Sorts entries by path for efficient binary search
/// 5. Atomically replaces the original file
///
/// # Errors
///
/// Returns an error if:
/// - The archive cannot be opened
/// - The temp file cannot be created
/// - Writing fails
/// - The rename operation fails
pub fn compact(path: impl AsRef<Path>) -> Result<CompactStats, BaleError> {
    let path = path.as_ref();

    // Get original size.
    let original_size = fs::metadata(path)?.len();

    // Open existing archive for reading.
    let reader = ArchiveReader::open(path)?;
    let alignment = reader.alignment();
    let path_size = reader.path_size() as u16;

    // Collect entries, keeping only the last occurrence of each path (shadowing).
    // We iterate in order and track seen paths to identify duplicates.
    let mut seen_paths: HashSet<Vec<u8>> = HashSet::new();
    let mut entries_to_copy: Vec<_> = Vec::new();
    let mut total_entries = 0usize;

    for (header, path_bytes) in reader.iter_entries() {
        total_entries += 1;
        // Normalize path by trimming null padding for comparison.
        let trimmed: Vec<u8> = path_bytes.iter().copied().take_while(|&b| b != 0).collect();

        // Track all entries, we'll deduplicate later by keeping last occurrence.
        entries_to_copy.push((header, path_bytes.to_vec(), trimmed));
    }

    // Deduplicate: reverse, keep first of each path, reverse back.
    // This keeps the last occurrence of each path (shadowing behavior).
    entries_to_copy.reverse();
    let mut final_entries: Vec<_> = Vec::new();
    for (header, path_bytes, trimmed) in entries_to_copy {
        if seen_paths.insert(trimmed) {
            final_entries.push((header, path_bytes));
        }
    }
    final_entries.reverse();

    // Collect explicit directory paths (without trailing slashes).
    let explicit_dirs: HashSet<Vec<u8>> = final_entries
        .iter()
        .filter(|(header, _)| header.kind() == EntryKind::Directory)
        .map(|(_, path_bytes)| {
            let trimmed: Vec<u8> = path_bytes.iter().copied().take_while(|&b| b != 0).collect();
            // Remove trailing slash if present.
            if trimmed.ends_with(b"/") {
                trimmed[..trimmed.len() - 1].to_vec()
            } else {
                trimmed
            }
        })
        .collect();

    // Collect all implicit directories from file paths.
    let mut missing_dirs: HashSet<Vec<u8>> = HashSet::new();
    for (_, path_bytes) in &final_entries {
        let trimmed: Vec<u8> = path_bytes.iter().copied().take_while(|&b| b != 0).collect();

        // Extract all parent directories from this path.
        let mut parent = trimmed.as_slice();
        while let Some(pos) = parent.iter().rposition(|&b| b == b'/') {
            parent = &parent[..pos];
            if parent.is_empty() {
                break;
            }
            let parent_vec = parent.to_vec();
            if !explicit_dirs.contains(&parent_vec) {
                missing_dirs.insert(parent_vec);
            }
        }
    }

    // Sort by path for binary search.
    final_entries.sort_by(|a, b| a.1.cmp(&b.1));

    let entries_removed = total_entries - final_entries.len();

    // Create temp file in same directory (for atomic rename).
    // TempFileGuard ensures cleanup on error.
    let parent = path.parent().unwrap_or(Path::new("."));
    let temp_path = parent.join(format!(
        ".{}.compact.tmp",
        path.file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("archive")
    ));
    let mut guard = TempFileGuard::new(temp_path.clone());

    // Write compacted archive.
    {
        let mut writer = ArchiveWriter::create_with_options(&temp_path, alignment, path_size)?;

        // First, add any missing directory entries.
        // Sort them to ensure parent directories come before children.
        let mut missing_dirs_sorted: Vec<_> = missing_dirs.into_iter().collect();
        missing_dirs_sorted.sort();

        for dir_bytes in &missing_dirs_sorted {
            let dir_str = std::str::from_utf8(dir_bytes)?;
            // Use default directory mode (rwxr-xr-x).
            writer.add_folder(dir_str, SFlag::S_IFDIR.bits() | DEFAULT_DIR_PERM)?;
        }

        for (header, path_bytes) in &final_entries {
            // Read the data from the original archive.
            let data = reader.read_data(header)?;

            // Get the path as a validated UTF-8 string.
            let archive_path = ArchivePath::from_null_padded_bytes(path_bytes);
            let path_str = archive_path.to_str_checked()?;

            // Get mode from external attributes.
            let mode = header.external_attrs.get() >> 16;

            writer.add_entry(path_str, data, mode)?;
        }

        writer.sync()?;
    }

    // Get compacted size.
    let compacted_size = fs::metadata(&temp_path)?.len();

    // Atomic rename.
    fs::rename(&temp_path, path)?;
    guard.persist();

    Ok(CompactStats {
        original_size,
        compacted_size,
        entries_removed,
        bytes_reclaimed: original_size.saturating_sub(compacted_size),
    })
}

/// Statistics from a rename duplicates operation.
#[derive(Debug, Clone, Default)]
pub struct RenameStats {
    /// Number of entries renamed.
    pub entries_renamed: usize,
    /// Mapping of old paths to new paths.
    pub renames: Vec<(String, String)>,
}

/// Renames duplicate paths in an archive.
///
/// When multiple entries share the same path, earlier occurrences are renamed
/// with numeric suffixes while the last occurrence keeps the original name.
/// For example: `file.txt` x 3 → `file(1).txt`, `file(2).txt`, `file.txt`
///
/// This preserves shadowing semantics where the last entry "wins".
///
/// # Errors
///
/// Returns an error if:
/// - The archive cannot be opened
/// - The temp file cannot be created
/// - Writing fails
/// - The rename operation fails
pub fn rename_duplicates(path: impl AsRef<Path>) -> Result<RenameStats, BaleError> {
    let path = path.as_ref();

    // Open existing archive for reading.
    let reader = ArchiveReader::open(path)?;
    let alignment = reader.alignment();
    let path_size = reader.path_size() as u16;

    // First pass: count occurrences of each path.
    let mut path_counts: HashMap<String, usize> = HashMap::new();
    for (_header, path_bytes) in reader.iter_entries() {
        let archive_path = ArchivePath::from_null_padded_bytes(path_bytes);
        let path_str = archive_path.to_str_checked()?;
        *path_counts.entry(path_str.to_owned()).or_insert(0) += 1;
    }

    // Check if there are any duplicates.
    let has_duplicates = path_counts.values().any(|&count| count > 1);
    if !has_duplicates {
        return Ok(RenameStats::default());
    }

    // Second pass: collect entries with renamed paths.
    // Track current occurrence number for each path.
    let mut path_occurrences: HashMap<String, usize> = HashMap::new();
    let mut entries: Vec<_> = Vec::new();
    let mut renames: Vec<(String, String)> = Vec::new();

    for (header, path_bytes) in reader.iter_entries() {
        let archive_path = ArchivePath::from_null_padded_bytes(path_bytes);
        let original_path = archive_path.to_str_checked()?.to_owned();
        let total_count = path_counts[&original_path];
        let occurrence = {
            let entry = path_occurrences.entry(original_path.clone()).or_insert(0);
            *entry += 1;
            *entry
        };

        // Determine the new path name.
        let new_path = if total_count > 1 && occurrence < total_count {
            // This is a duplicate that's not the last occurrence - rename it.
            let renamed = archive_path.with_suffix(occurrence)?;

            // Verify renamed path fits within path_size.
            if renamed.len() > path_size as usize {
                return Err(BaleError::PathTooLong {
                    path: renamed.to_string(),
                    max: path_size as usize,
                });
            }

            let renamed_str = renamed.to_string();
            renames.push((original_path, renamed_str.clone()));
            renamed_str
        } else {
            // Either not a duplicate, or it's the last occurrence - keep original.
            original_path
        };

        entries.push((header, new_path));
    }

    // Sort entries by the new path for binary search.
    entries.sort_by(|a, b| a.1.cmp(&b.1));

    // Create temp file in same directory (for atomic rename).
    // TempFileGuard ensures cleanup on error.
    let parent = path.parent().unwrap_or(Path::new("."));
    let temp_path = parent.join(format!(
        ".{}.rename.tmp",
        path.file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("archive")
    ));
    let mut guard = TempFileGuard::new(temp_path.clone());

    // Write archive with renamed entries.
    {
        let mut writer = ArchiveWriter::create_with_options(&temp_path, alignment, path_size)?;

        for (header, new_path) in &entries {
            let data = reader.read_data(header)?;
            let mode = header.external_attrs.get() >> 16;
            writer.add_entry(new_path, data, mode)?;
        }

        writer.sync()?;
    }

    // Atomic rename.
    fs::rename(&temp_path, path)?;
    guard.persist();

    Ok(RenameStats {
        entries_renamed: renames.len(),
        renames,
    })
}

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

    /// Compacting an empty archive works.
    #[test]
    fn compact_empty_archive() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.bale");

        // Create empty archive.
        {
            let mut writer = ArchiveWriter::create(&path).unwrap();
            writer.sync().unwrap();
        }

        let stats = compact(&path).unwrap();
        assert_eq!(stats.entries_removed, 0);

        // Verify archive is still valid.
        let reader = ArchiveReader::open(&path).unwrap();
        assert_eq!(reader.entry_count(), 0);
    }

    /// Compacting removes shadowed duplicates.
    #[test]
    fn compact_removes_duplicates() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.bale");

        // Create archive with duplicate entries.
        {
            let mut writer = ArchiveWriter::create(&path).unwrap();
            writer.add_entry("file.txt", b"original", 0o644).unwrap();
            writer.add_entry("file.txt", b"updated", 0o644).unwrap();
            writer.add_entry("other.txt", b"other", 0o644).unwrap();
            writer.sync().unwrap();
        }

        let original_size = fs::metadata(&path).unwrap().len();

        let stats = compact(&path).unwrap();
        assert_eq!(stats.entries_removed, 1); // One duplicate removed.
        assert!(stats.compacted_size < original_size);

        // Verify archive has 2 entries with correct data.
        let reader = ArchiveReader::open(&path).unwrap();
        assert_eq!(reader.entry_count(), 2);

        let entry = reader.find_entry("file.txt").unwrap();
        let data = reader.read_data(entry).unwrap();
        assert_eq!(data, b"updated"); // Latest version kept.
    }

    /// Compacting sorts entries by path.
    #[test]
    fn compact_sorts_entries() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.bale");

        // Create archive with unsorted entries.
        {
            let mut writer = ArchiveWriter::create(&path).unwrap();
            writer.add_entry("c.txt", b"c", 0o644).unwrap();
            writer.add_entry("a.txt", b"a", 0o644).unwrap();
            writer.add_entry("b.txt", b"b", 0o644).unwrap();
            writer.sync().unwrap();
        }

        compact(&path).unwrap();

        // Verify entries are sorted.
        let reader = ArchiveReader::open(&path).unwrap();
        let paths: Vec<String> = reader
            .iter_entries()
            .map(|(_, p)| {
                ArchivePath::from_null_padded_bytes(p)
                    .to_str_checked()
                    .unwrap()
                    .to_owned()
            })
            .collect();

        assert_eq!(paths, vec!["a.txt", "b.txt", "c.txt"]);
    }

    /// Compacting preserves file permissions.
    #[test]
    fn compact_preserves_mode() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.bale");

        {
            let mut writer = ArchiveWriter::create(&path).unwrap();
            writer.add_entry("exec.sh", b"#!/bin/bash", 0o755).unwrap();
            writer.add_entry("data.txt", b"data", 0o644).unwrap();
            writer.sync().unwrap();
        }

        compact(&path).unwrap();

        let reader = ArchiveReader::open(&path).unwrap();
        let exec_entry = reader.find_entry("exec.sh").unwrap();
        let data_entry = reader.find_entry("data.txt").unwrap();

        assert_eq!(exec_entry.external_attrs.get() >> 16, 0o755);
        assert_eq!(data_entry.external_attrs.get() >> 16, 0o644);
    }

    /// Insert suffix before file extension.
    #[test]
    fn insert_suffix_with_extension() {
        let path = ArchivePath::from_bytes(b"file.txt");
        assert_eq!(path.with_suffix(1).unwrap().as_str(), Some("file(1).txt"));

        let path = ArchivePath::from_bytes(b"image.png");
        assert_eq!(path.with_suffix(2).unwrap().as_str(), Some("image(2).png"));

        let path = ArchivePath::from_bytes(b"archive.tar.gz");
        assert_eq!(
            path.with_suffix(3).unwrap().as_str(),
            Some("archive.tar(3).gz")
        );
    }

    /// Insert suffix for files without extension.
    #[test]
    fn insert_suffix_no_extension() {
        let path = ArchivePath::from_bytes(b"README");
        assert_eq!(path.with_suffix(1).unwrap().as_str(), Some("README(1)"));

        let path = ArchivePath::from_bytes(b"Makefile");
        assert_eq!(path.with_suffix(5).unwrap().as_str(), Some("Makefile(5)"));
    }

    /// Insert suffix handles directories with dots correctly.
    #[test]
    fn insert_suffix_directory_with_dot() {
        // Directory has a dot, but file has no extension.
        let path = ArchivePath::from_bytes(b"foo.d/bar");
        assert_eq!(path.with_suffix(1).unwrap().as_str(), Some("foo.d/bar(1)"));

        // Directory has a dot, file has extension.
        let path = ArchivePath::from_bytes(b"foo.d/bar.txt");
        assert_eq!(
            path.with_suffix(2).unwrap().as_str(),
            Some("foo.d/bar(2).txt")
        );

        // Nested directories with dots.
        let path = ArchivePath::from_bytes(b"a.b/c.d/file.ext");
        assert_eq!(
            path.with_suffix(3).unwrap().as_str(),
            Some("a.b/c.d/file(3).ext")
        );
    }

    /// Rename duplicates on archive with no duplicates does nothing.
    #[test]
    fn rename_duplicates_no_duplicates() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.bale");

        {
            let mut writer = ArchiveWriter::create(&path).unwrap();
            writer.add_entry("a.txt", b"a", 0o644).unwrap();
            writer.add_entry("b.txt", b"b", 0o644).unwrap();
            writer.sync().unwrap();
        }

        let stats = rename_duplicates(&path).unwrap();
        assert_eq!(stats.entries_renamed, 0);
        assert!(stats.renames.is_empty());

        // Verify archive unchanged.
        let reader = ArchiveReader::open(&path).unwrap();
        assert_eq!(reader.entry_count(), 2);
    }

    /// Rename duplicates renames earlier occurrences.
    #[test]
    fn rename_duplicates_renames_earlier() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.bale");

        // Create archive with 3 duplicate entries.
        {
            let mut writer = ArchiveWriter::create(&path).unwrap();
            writer.add_entry("file.txt", b"version 1", 0o644).unwrap();
            writer.add_entry("file.txt", b"version 2", 0o644).unwrap();
            writer.add_entry("file.txt", b"version 3", 0o644).unwrap();
            writer.sync().unwrap();
        }

        let stats = rename_duplicates(&path).unwrap();
        assert_eq!(stats.entries_renamed, 2);

        // Verify renames.
        assert!(
            stats
                .renames
                .contains(&("file.txt".to_string(), "file(1).txt".to_string()))
        );
        assert!(
            stats
                .renames
                .contains(&("file.txt".to_string(), "file(2).txt".to_string()))
        );

        // Verify archive has 3 unique entries.
        let reader = ArchiveReader::open(&path).unwrap();
        assert_eq!(reader.entry_count(), 3);

        // Last occurrence keeps original name.
        let entry = reader.find_entry("file.txt").unwrap();
        let data = reader.read_data(entry).unwrap();
        assert_eq!(data, b"version 3");

        // Earlier occurrences are renamed.
        let entry1 = reader.find_entry("file(1).txt").unwrap();
        let data1 = reader.read_data(entry1).unwrap();
        assert_eq!(data1, b"version 1");

        let entry2 = reader.find_entry("file(2).txt").unwrap();
        let data2 = reader.read_data(entry2).unwrap();
        assert_eq!(data2, b"version 2");
    }

    /// Rename duplicates sorts entries after renaming.
    #[test]
    fn rename_duplicates_sorts_entries() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("test.bale");

        {
            let mut writer = ArchiveWriter::create(&path).unwrap();
            writer.add_entry("z.txt", b"z1", 0o644).unwrap();
            writer.add_entry("z.txt", b"z2", 0o644).unwrap();
            writer.add_entry("a.txt", b"a", 0o644).unwrap();
            writer.sync().unwrap();
        }

        rename_duplicates(&path).unwrap();

        // Verify entries are sorted.
        let reader = ArchiveReader::open(&path).unwrap();
        let paths: Vec<String> = reader
            .iter_entries()
            .map(|(_, p)| {
                ArchivePath::from_null_padded_bytes(p)
                    .to_str_checked()
                    .unwrap()
                    .to_owned()
            })
            .collect();

        assert_eq!(paths, vec!["a.txt", "z(1).txt", "z.txt"]);
    }
}