logana 0.5.1

Turn any log source — files, compressed archives, Docker, or OTel streams — into structured data. Filter by pattern, field, or date range; annotate lines; bookmark findings; and export to Markdown, Jira, or AI assistants via the built-in MCP server.
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
use std::fs::File;
use std::io::{self, Read, Write};
use std::path::Path;
use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
};

use tempfile::NamedTempFile;

#[derive(Debug, Clone, Copy)]
pub struct ArchiveExtractionProgress {
    pub file_index: usize,
    pub fraction: f64,
}

pub(crate) struct ProgressReader<R: Read> {
    inner: R,
    bytes_read: u64,
    total_bytes: u64,
    file_index: Arc<AtomicUsize>,
    progress_tx: tokio::sync::watch::Sender<ArchiveExtractionProgress>,
}

impl<R: Read> ProgressReader<R> {
    pub fn new(
        inner: R,
        total_bytes: u64,
        file_index: Arc<AtomicUsize>,
        progress_tx: tokio::sync::watch::Sender<ArchiveExtractionProgress>,
    ) -> Self {
        Self {
            inner,
            bytes_read: 0,
            total_bytes,
            file_index,
            progress_tx,
        }
    }
}

impl<R: Read> Read for ProgressReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.inner.read(buf)?;
        self.bytes_read = self.bytes_read.saturating_add(n as u64);
        let fraction = if self.total_bytes > 0 {
            (self.bytes_read as f64 / self.total_bytes as f64).min(1.0)
        } else {
            0.0
        };
        let _ = self.progress_tx.send(ArchiveExtractionProgress {
            file_index: self.file_index.load(Ordering::Relaxed),
            fraction,
        });
        Ok(n)
    }
}

impl<R: Read + io::Seek> io::Seek for ProgressReader<R> {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        let new_pos = self.inner.seek(pos)?;
        self.bytes_read = new_pos;
        Ok(new_pos)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ArchiveType {
    Gz,
    Bz2,
    Xz,
    Zip,
    Tar,
    TarGz,
    TarBz2,
    TarXz,
}

pub struct ExtractedFile {
    pub name: String,
    pub temp_file: NamedTempFile,
}

pub fn detect_archive_type(path: &str) -> Option<ArchiveType> {
    let lower = path.to_ascii_lowercase();
    if lower.ends_with(".tar.gz") || lower.ends_with(".tgz") {
        Some(ArchiveType::TarGz)
    } else if lower.ends_with(".tar.bz2") || lower.ends_with(".tbz2") {
        Some(ArchiveType::TarBz2)
    } else if lower.ends_with(".tar.xz") || lower.ends_with(".txz") {
        Some(ArchiveType::TarXz)
    } else if lower.ends_with(".tar") {
        Some(ArchiveType::Tar)
    } else if lower.ends_with(".gz") {
        Some(ArchiveType::Gz)
    } else if lower.ends_with(".bz2") {
        Some(ArchiveType::Bz2)
    } else if lower.ends_with(".xz") {
        Some(ArchiveType::Xz)
    } else if lower.ends_with(".zip") {
        Some(ArchiveType::Zip)
    } else {
        None
    }
}

pub fn extract(path: &str) -> Result<Vec<ExtractedFile>, String> {
    let (tx, _rx) = tokio::sync::watch::channel(ArchiveExtractionProgress {
        file_index: 0,
        fraction: 0.0,
    });
    extract_with_progress(path, tx, None)
}

pub fn extract_with_progress(
    path: &str,
    progress_tx: tokio::sync::watch::Sender<ArchiveExtractionProgress>,
    name_tx: Option<tokio::sync::mpsc::UnboundedSender<String>>,
) -> Result<Vec<ExtractedFile>, String> {
    let archive_type = detect_archive_type(path)
        .ok_or_else(|| format!("'{}' is not a recognised archive format", path))?;
    let file_size = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0);
    let file_index = Arc::new(AtomicUsize::new(0));

    match archive_type {
        ArchiveType::Gz => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader = ProgressReader::new(file, file_size, file_index, progress_tx);
            let mut decoder = flate2::read::GzDecoder::new(reader);
            Ok(vec![decompress_to_temp(&mut decoder, stem(&stem(path)))?])
        }
        ArchiveType::Bz2 => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader = ProgressReader::new(file, file_size, file_index, progress_tx);
            let mut decoder = bzip2::read::BzDecoder::new(reader);
            Ok(vec![decompress_to_temp(&mut decoder, stem(&stem(path)))?])
        }
        ArchiveType::Xz => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader = ProgressReader::new(file, file_size, file_index, progress_tx);
            let mut decoder = xz2::read::XzDecoder::new(reader);
            Ok(vec![decompress_to_temp(&mut decoder, stem(&stem(path)))?])
        }
        ArchiveType::Zip => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader =
                ProgressReader::new(file, file_size, file_index.clone(), progress_tx.clone());
            let mut archive = zip::ZipArchive::new(reader).map_err(|e| e.to_string())?;
            let mut results = Vec::new();
            let mut logical_idx = 0usize;
            for i in 0..archive.len() {
                let mut entry = archive.by_index(i).map_err(|e| e.to_string())?;
                if entry.is_dir() {
                    continue;
                }
                let name = entry
                    .enclosed_name()
                    .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
                    .unwrap_or_else(|| format!("file_{i}"));
                file_index.store(logical_idx, Ordering::Relaxed);
                let _ = progress_tx.send(ArchiveExtractionProgress {
                    file_index: logical_idx,
                    fraction: 0.0,
                });
                results.push(decompress_to_temp(&mut entry, name)?);
                let _ = progress_tx.send(ArchiveExtractionProgress {
                    file_index: logical_idx,
                    fraction: 1.0,
                });
                logical_idx += 1;
            }
            Ok(results)
        }
        ArchiveType::Tar => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader = ProgressReader::new(file, file_size, file_index.clone(), progress_tx);
            let mut archive = tar::Archive::new(reader);
            extract_tar_entries_with_progress(&mut archive, &file_index, &name_tx)
        }
        ArchiveType::TarGz => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader = ProgressReader::new(file, file_size, file_index.clone(), progress_tx);
            let decoder = flate2::read::GzDecoder::new(reader);
            let mut archive = tar::Archive::new(decoder);
            extract_tar_entries_with_progress(&mut archive, &file_index, &name_tx)
        }
        ArchiveType::TarBz2 => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader = ProgressReader::new(file, file_size, file_index.clone(), progress_tx);
            let decoder = bzip2::read::BzDecoder::new(reader);
            let mut archive = tar::Archive::new(decoder);
            extract_tar_entries_with_progress(&mut archive, &file_index, &name_tx)
        }
        ArchiveType::TarXz => {
            let file = File::open(path).map_err(|e| e.to_string())?;
            let reader = ProgressReader::new(file, file_size, file_index.clone(), progress_tx);
            let decoder = xz2::read::XzDecoder::new(reader);
            let mut archive = tar::Archive::new(decoder);
            extract_tar_entries_with_progress(&mut archive, &file_index, &name_tx)
        }
    }
}

fn extract_tar_entries_with_progress<R: Read>(
    archive: &mut tar::Archive<R>,
    file_index: &Arc<AtomicUsize>,
    name_tx: &Option<tokio::sync::mpsc::UnboundedSender<String>>,
) -> Result<Vec<ExtractedFile>, String> {
    let mut results = Vec::new();
    let mut idx = 0usize;
    for entry in archive.entries().map_err(|e| e.to_string())? {
        let mut entry = entry.map_err(|e| e.to_string())?;
        if entry.header().entry_type() != tar::EntryType::Regular {
            continue;
        }
        let name = entry
            .path()
            .map_err(|e| e.to_string())?
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| "file".to_string());
        if let Some(tx) = name_tx {
            let _ = tx.send(name.clone());
        }
        file_index.store(idx, Ordering::Relaxed);
        results.push(decompress_to_temp(&mut entry, name)?);
        idx += 1;
    }
    Ok(results)
}

/// Returns `true` for archive types where listing filenames requires decompressing
/// (compressed TAR variants). These use the streaming single-pass extraction path.
pub fn uses_streaming_path(archive_type: &ArchiveType) -> bool {
    matches!(
        archive_type,
        ArchiveType::TarGz | ArchiveType::TarBz2 | ArchiveType::TarXz
    )
}

/// List file names contained in an archive without full decompression.
/// Only valid for two-pass formats (GZ, BZ2, XZ single-file; ZIP; uncompressed TAR).
/// Returns an error for streaming TAR variants — use `uses_streaming_path` to check first.
pub fn list_archive_files(path: &str) -> Result<Vec<String>, String> {
    let archive_type = detect_archive_type(path)
        .ok_or_else(|| format!("'{}' is not a recognised archive format", path))?;
    match archive_type {
        ArchiveType::Gz | ArchiveType::Bz2 | ArchiveType::Xz => Ok(vec![stem(&stem(path))]),
        ArchiveType::Zip => list_zip_names(path),
        ArchiveType::Tar => list_tar_names(path),
        ArchiveType::TarGz | ArchiveType::TarBz2 | ArchiveType::TarXz => {
            Err("use streaming path for compressed TAR archives".to_string())
        }
    }
}

fn list_zip_names(path: &str) -> Result<Vec<String>, String> {
    let file = File::open(path).map_err(|e| e.to_string())?;
    let mut archive = zip::ZipArchive::new(file).map_err(|e| e.to_string())?;
    let mut names = Vec::new();
    for i in 0..archive.len() {
        let entry = archive.by_index_raw(i).map_err(|e| e.to_string())?;
        if entry.is_dir() {
            continue;
        }
        let name = entry
            .enclosed_name()
            .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
            .unwrap_or_else(|| format!("file_{i}"));
        names.push(name);
    }
    Ok(names)
}

fn list_tar_names(path: &str) -> Result<Vec<String>, String> {
    let file = File::open(path).map_err(|e| e.to_string())?;
    let mut archive = tar::Archive::new(file);
    let mut names = Vec::new();
    for entry in archive.entries().map_err(|e| e.to_string())? {
        let entry = entry.map_err(|e| e.to_string())?;
        if entry.header().entry_type() != tar::EntryType::Regular {
            continue;
        }
        let name = entry
            .path()
            .map_err(|e| e.to_string())?
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| "file".to_string());
        names.push(name);
    }
    Ok(names)
}

fn stem(path: &str) -> String {
    Path::new(path)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("file")
        .to_string()
}

fn decompress_to_temp(reader: &mut dyn Read, name: String) -> Result<ExtractedFile, String> {
    let mut tmp = NamedTempFile::new().map_err(|e| e.to_string())?;
    io::copy(reader, &mut tmp).map_err(|e| e.to_string())?;
    tmp.flush().map_err(|e| e.to_string())?;
    Ok(ExtractedFile {
        name,
        temp_file: tmp,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{Seek, SeekFrom, Write};

    fn read_extracted(file: &mut ExtractedFile) -> String {
        let mut content = String::new();
        file.temp_file.seek(SeekFrom::Start(0)).unwrap();
        file.temp_file.read_to_string(&mut content).unwrap();
        content
    }

    // detect_archive_type

    #[test]
    fn test_detect_gz() {
        assert_eq!(detect_archive_type("app.log.gz"), Some(ArchiveType::Gz));
    }

    #[test]
    fn test_detect_bz2() {
        assert_eq!(detect_archive_type("app.log.bz2"), Some(ArchiveType::Bz2));
    }

    #[test]
    fn test_detect_xz() {
        assert_eq!(detect_archive_type("app.log.xz"), Some(ArchiveType::Xz));
    }

    #[test]
    fn test_detect_zip() {
        assert_eq!(detect_archive_type("logs.zip"), Some(ArchiveType::Zip));
    }

    #[test]
    fn test_detect_tar() {
        assert_eq!(detect_archive_type("logs.tar"), Some(ArchiveType::Tar));
    }

    #[test]
    fn test_detect_tar_gz_long() {
        assert_eq!(detect_archive_type("logs.tar.gz"), Some(ArchiveType::TarGz));
    }

    #[test]
    fn test_detect_tgz_short() {
        assert_eq!(detect_archive_type("logs.tgz"), Some(ArchiveType::TarGz));
    }

    #[test]
    fn test_detect_tar_bz2_long() {
        assert_eq!(
            detect_archive_type("logs.tar.bz2"),
            Some(ArchiveType::TarBz2)
        );
    }

    #[test]
    fn test_detect_tbz2_short() {
        assert_eq!(detect_archive_type("logs.tbz2"), Some(ArchiveType::TarBz2));
    }

    #[test]
    fn test_detect_tar_xz_long() {
        assert_eq!(detect_archive_type("logs.tar.xz"), Some(ArchiveType::TarXz));
    }

    #[test]
    fn test_detect_txz_short() {
        assert_eq!(detect_archive_type("logs.txz"), Some(ArchiveType::TarXz));
    }

    #[test]
    fn test_detect_plain_log_returns_none() {
        assert_eq!(detect_archive_type("app.log"), None);
        assert_eq!(detect_archive_type("app.json"), None);
    }

    #[test]
    fn test_detect_case_insensitive() {
        assert_eq!(detect_archive_type("APP.LOG.GZ"), Some(ArchiveType::Gz));
        assert_eq!(detect_archive_type("LOGS.TAR.GZ"), Some(ArchiveType::TarGz));
        assert_eq!(detect_archive_type("LOGS.ZIP"), Some(ArchiveType::Zip));
    }

    // extract roundtrips

    fn make_gz(content: &[u8]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        let mut enc = flate2::write::GzEncoder::new(&mut tmp, flate2::Compression::default());
        enc.write_all(content).unwrap();
        enc.finish().unwrap();
        tmp
    }

    fn make_bz2(content: &[u8]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        let mut enc = bzip2::write::BzEncoder::new(&mut tmp, bzip2::Compression::default());
        enc.write_all(content).unwrap();
        enc.finish().unwrap();
        tmp
    }

    fn make_xz(content: &[u8]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        let mut enc = xz2::write::XzEncoder::new(&mut tmp, 1);
        enc.write_all(content).unwrap();
        enc.finish().unwrap();
        tmp
    }

    fn make_zip(entries: &[(&str, &[u8])]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        let mut zip = zip::ZipWriter::new(&mut tmp);
        for (name, content) in entries {
            zip.start_file(*name, zip::write::SimpleFileOptions::default())
                .unwrap();
            zip.write_all(content).unwrap();
        }
        zip.finish().unwrap();
        tmp
    }

    fn make_tar(entries: &[(&str, &[u8])]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        {
            let mut builder = tar::Builder::new(&mut tmp);
            for (name, content) in entries {
                let mut header = tar::Header::new_gnu();
                header.set_size(content.len() as u64);
                header.set_mode(0o644);
                header.set_cksum();
                builder.append_data(&mut header, name, *content).unwrap();
            }
            builder.finish().unwrap();
        }
        tmp
    }

    fn make_tar_gz(entries: &[(&str, &[u8])]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        let enc = flate2::write::GzEncoder::new(&mut tmp, flate2::Compression::default());
        let mut builder = tar::Builder::new(enc);
        for (name, content) in entries {
            let mut header = tar::Header::new_gnu();
            header.set_size(content.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();
            builder.append_data(&mut header, name, *content).unwrap();
        }
        builder.into_inner().unwrap().finish().unwrap();
        tmp
    }

    fn make_tar_bz2(entries: &[(&str, &[u8])]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        let enc = bzip2::write::BzEncoder::new(&mut tmp, bzip2::Compression::default());
        let mut builder = tar::Builder::new(enc);
        for (name, content) in entries {
            let mut header = tar::Header::new_gnu();
            header.set_size(content.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();
            builder.append_data(&mut header, name, *content).unwrap();
        }
        builder.into_inner().unwrap().finish().unwrap();
        tmp
    }

    fn make_tar_xz(entries: &[(&str, &[u8])]) -> NamedTempFile {
        let mut tmp = NamedTempFile::new().unwrap();
        let enc = xz2::write::XzEncoder::new(&mut tmp, 1);
        let mut builder = tar::Builder::new(enc);
        for (name, content) in entries {
            let mut header = tar::Header::new_gnu();
            header.set_size(content.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();
            builder.append_data(&mut header, name, *content).unwrap();
        }
        builder.into_inner().unwrap().finish().unwrap();
        tmp
    }

    #[test]
    fn test_extract_gz_roundtrip() {
        let content = b"hello from gz\n";
        let tmp = make_gz(content);
        let path = tmp.path().to_str().unwrap().to_string() + ".gz";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_bz2_roundtrip() {
        let content = b"hello from bz2\n";
        let tmp = make_bz2(content);
        let path = tmp.path().to_str().unwrap().to_string() + ".bz2";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_xz_roundtrip() {
        let content = b"hello from xz\n";
        let tmp = make_xz(content);
        let path = tmp.path().to_str().unwrap().to_string() + ".xz";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_zip_single_file() {
        let content = b"hello from zip\n";
        let tmp = make_zip(&[("app.log", content)]);
        let path = tmp.path().to_str().unwrap().to_string() + ".zip";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].name, "app.log");
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_zip_multiple_files() {
        let tmp = make_zip(&[("a.log", b"aaa\n"), ("b.log", b"bbb\n")]);
        let path = tmp.path().to_str().unwrap().to_string() + ".zip";
        std::fs::copy(tmp.path(), &path).unwrap();
        let files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 2);
        let names: Vec<&str> = files.iter().map(|f| f.name.as_str()).collect();
        assert!(names.contains(&"a.log"));
        assert!(names.contains(&"b.log"));
    }

    #[test]
    fn test_extract_zip_skips_directories() {
        let mut tmp = NamedTempFile::new().unwrap();
        let mut zip = zip::ZipWriter::new(&mut tmp);
        zip.add_directory("logs/", zip::write::SimpleFileOptions::default())
            .unwrap();
        zip.start_file("logs/app.log", zip::write::SimpleFileOptions::default())
            .unwrap();
        zip.write_all(b"content\n").unwrap();
        zip.finish().unwrap();
        let path = tmp.path().to_str().unwrap().to_string() + ".zip";
        std::fs::copy(tmp.path(), &path).unwrap();
        let files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].name, "app.log");
    }

    #[test]
    fn test_extract_tar_single_file() {
        let content = b"hello from tar\n";
        let tmp = make_tar(&[("app.log", content)]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].name, "app.log");
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_tar_multiple_files() {
        let tmp = make_tar(&[("a.log", b"aaa\n"), ("b.log", b"bbb\n")]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar";
        std::fs::copy(tmp.path(), &path).unwrap();
        let files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 2);
    }

    #[test]
    fn test_extract_tar_gz_roundtrip() {
        let content = b"hello from tar.gz\n";
        let tmp = make_tar_gz(&[("app.log", content)]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar.gz";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_tar_bz2_roundtrip() {
        let content = b"hello from tar.bz2\n";
        let tmp = make_tar_bz2(&[("app.log", content)]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar.bz2";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_tar_xz_roundtrip() {
        let content = b"hello from tar.xz\n";
        let tmp = make_tar_xz(&[("app.log", content)]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar.xz";
        std::fs::copy(tmp.path(), &path).unwrap();
        let mut files = extract(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
    }

    #[test]
    fn test_extract_nonexistent_file_returns_err() {
        let result = extract("/nonexistent/path/file.gz");
        assert!(result.is_err());
    }

    #[test]
    fn test_progress_reader_counts_bytes() {
        use std::sync::Arc;
        use tokio::sync::watch;
        let data = b"hello world";
        let (tx, rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let file_index = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut reader = ProgressReader::new(
            std::io::Cursor::new(data),
            data.len() as u64,
            file_index,
            tx,
        );
        let mut buf = Vec::new();
        reader.read_to_end(&mut buf).unwrap();
        assert_eq!(buf, data);
        let p = rx.borrow();
        assert_eq!(p.file_index, 0);
        assert!((p.fraction - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_progress_reader_partial_read() {
        use std::sync::Arc;
        use tokio::sync::watch;
        let data = b"abcdefghij"; // 10 bytes
        let (tx, rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let file_index = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut reader = ProgressReader::new(std::io::Cursor::new(data), 10u64, file_index, tx);
        let mut buf = [0u8; 5];
        reader.read_exact(&mut buf).unwrap();
        let p = rx.borrow();
        assert!((p.fraction - 0.5).abs() < 1e-9);
    }

    #[test]
    fn test_progress_reader_zero_total_bytes_does_not_panic() {
        use std::sync::Arc;
        use tokio::sync::watch;
        let (tx, rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let file_index = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut reader = ProgressReader::new(std::io::Cursor::new(b"hi"), 0u64, file_index, tx);
        let mut buf = Vec::new();
        reader.read_to_end(&mut buf).unwrap();
        assert_eq!(rx.borrow().fraction, 0.0);
    }

    #[test]
    fn test_list_gz_returns_single_stem() {
        use std::io::Write;
        let tmp = tempfile::Builder::new()
            .prefix("logana_list_gz_")
            .suffix(".log.gz")
            .tempfile()
            .unwrap();
        let path_str = tmp.path().to_str().unwrap().to_string();
        {
            let f = std::fs::OpenOptions::new()
                .write(true)
                .open(tmp.path())
                .unwrap();
            let mut enc = flate2::write::GzEncoder::new(f, flate2::Compression::default());
            enc.write_all(b"content").unwrap();
            enc.finish().unwrap();
        }
        let names = list_archive_files(&path_str).unwrap();
        let expected = std::path::Path::new(&path_str)
            .file_stem()
            .and_then(|s| std::path::Path::new(s).file_stem())
            .and_then(|s| s.to_str())
            .unwrap_or("file")
            .to_string();
        assert_eq!(names.len(), 1);
        assert_eq!(names, vec![expected]);
    }

    #[test]
    fn test_list_archive_files_errors_for_streaming_formats() {
        assert!(
            list_archive_files("file.tar.gz")
                .unwrap_err()
                .contains("streaming")
        );
        assert!(
            list_archive_files("file.tar.bz2")
                .unwrap_err()
                .contains("streaming")
        );
        assert!(
            list_archive_files("file.tar.xz")
                .unwrap_err()
                .contains("streaming")
        );
    }

    #[test]
    fn test_list_zip_files_without_decompression() {
        let tmp = make_zip(&[("a.log", b"aaa"), ("b.log", b"bbb")]);
        let path = tmp.path().to_str().unwrap().to_string() + ".zip";
        std::fs::copy(tmp.path(), &path).unwrap();
        let names = list_archive_files(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        let mut names = names;
        names.sort();
        assert_eq!(names, vec!["a.log", "b.log"]);
    }

    #[test]
    fn test_list_tar_files_without_decompression() {
        let tmp = make_tar(&[("x.log", b"xxx"), ("y.log", b"yyy")]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar";
        std::fs::copy(tmp.path(), &path).unwrap();
        let names = list_archive_files(&path).unwrap();
        std::fs::remove_file(&path).unwrap();
        let mut names = names;
        names.sort();
        assert_eq!(names, vec!["x.log", "y.log"]);
    }

    #[test]
    fn test_uses_streaming_path_for_compressed_tar() {
        assert!(uses_streaming_path(&ArchiveType::TarGz));
        assert!(uses_streaming_path(&ArchiveType::TarBz2));
        assert!(uses_streaming_path(&ArchiveType::TarXz));
        assert!(!uses_streaming_path(&ArchiveType::Gz));
        assert!(!uses_streaming_path(&ArchiveType::Zip));
        assert!(!uses_streaming_path(&ArchiveType::Tar));
    }

    #[test]
    fn test_extract_with_progress_gz_sends_progress() {
        use tokio::sync::watch;
        let content = b"hello from gz progress\n";
        let tmp = make_gz(content);
        let path = tmp.path().to_str().unwrap().to_string() + ".log.gz";
        std::fs::copy(tmp.path(), &path).unwrap();
        let (tx, rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let mut files = extract_with_progress(&path, tx, None).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
        let p = rx.borrow();
        assert_eq!(p.file_index, 0);
        assert!(
            (p.fraction - 1.0).abs() < 1e-9,
            "expected fraction 1.0, got {}",
            p.fraction
        );
    }

    #[test]
    fn test_extract_with_progress_zip_multiple_files() {
        use tokio::sync::watch;
        let tmp = make_zip(&[("a.log", b"aaa\n"), ("b.log", b"bbb\n")]);
        let path = tmp.path().to_str().unwrap().to_string() + ".zip";
        std::fs::copy(tmp.path(), &path).unwrap();
        let (tx, rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let files = extract_with_progress(&path, tx, None).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 2);
        let names: Vec<&str> = files.iter().map(|f| f.name.as_str()).collect();
        assert!(names.contains(&"a.log"));
        assert!(names.contains(&"b.log"));
        let p = rx.borrow();
        assert_eq!(p.file_index, 1, "last file_index should be 1 (second file)");
        assert!(
            (p.fraction - 1.0).abs() < 1e-9,
            "last fraction should be 1.0"
        );
    }

    #[test]
    fn test_extract_with_progress_bz2_sends_progress() {
        use tokio::sync::watch;
        let content = b"hello from bz2 progress\n";
        let tmp = make_bz2(content);
        let path = tmp.path().to_str().unwrap().to_string() + ".log.bz2";
        std::fs::copy(tmp.path(), &path).unwrap();
        let (tx, rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let mut files = extract_with_progress(&path, tx, None).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
        let p = rx.borrow();
        assert!((p.fraction - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_extract_with_progress_tar_increments_file_index() {
        use tokio::sync::watch;
        let tmp = make_tar(&[("a.log", b"aaa\n"), ("b.log", b"bbb\n")]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar";
        std::fs::copy(tmp.path(), &path).unwrap();
        let (tx, rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let files = extract_with_progress(&path, tx, None).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 2);
        let p = rx.borrow();
        assert_eq!(p.file_index, 1);
    }

    #[test]
    fn test_extract_with_progress_tar_gz_streaming_sends_names() {
        use tokio::sync::{mpsc, watch};
        let content = b"log data\n";
        let tmp = make_tar_gz(&[("app.log", content)]);
        let path = tmp.path().to_str().unwrap().to_string() + ".tar.gz";
        std::fs::copy(tmp.path(), &path).unwrap();
        let (tx, _rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let (name_tx, mut name_rx) = mpsc::unbounded_channel();
        let mut files = extract_with_progress(&path, tx, Some(name_tx)).unwrap();
        std::fs::remove_file(&path).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(read_extracted(&mut files[0]).as_bytes(), content);
        assert_eq!(name_rx.try_recv().unwrap(), "app.log");
    }

    #[test]
    fn test_progress_reader_seek_updates_bytes_read() {
        use std::io::{Seek, SeekFrom};
        use std::sync::Arc;
        use tokio::sync::watch;
        let data = b"abcdefghij"; // 10 bytes
        let (tx, _rx) = watch::channel(ArchiveExtractionProgress {
            file_index: 0,
            fraction: 0.0,
        });
        let file_index = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut reader =
            ProgressReader::new(std::io::Cursor::new(data as &[u8]), 10u64, file_index, tx);
        let mut buf = [0u8; 5];
        reader.read_exact(&mut buf).unwrap();
        assert_eq!(reader.bytes_read, 5);
        let pos = reader.seek(SeekFrom::Start(0)).unwrap();
        assert_eq!(pos, 0);
        assert_eq!(reader.bytes_read, 0);
        let pos = reader.seek(SeekFrom::Start(7)).unwrap();
        assert_eq!(pos, 7);
        assert_eq!(reader.bytes_read, 7);
    }
}