fork/gdu
git clone https://git.julia.blue/fork/gdu
d062e88
subvolume
1package analyze 2 3import ( 4"archive/tar" 5"bytes" 6"compress/bzip2" 7"compress/gzip" 8"io" 9"os" 10"os/exec" 11"path/filepath" 12"slices" 13"testing" 14 15"github.com/dundee/gdu/v5/pkg/fs" 16"github.com/stretchr/testify/assert" 17"github.com/stretchr/testify/require" 18"github.com/ulikunitz/xz" 19) 20 21// --------------------------------------------------------------------------- 22// Helpers to create test archives 23// --------------------------------------------------------------------------- 24 25// writeTarEntries writes a tiny but realistic directory structure into w. 26func writeTarEntries (t * testing.T ,w io.Writer ) { 27t .Helper () 28tw := tar .NewWriter (w ) 29 30entries := []struct { 31hdr tar.Header 32content []byte 33 }{ 34 {tar.Header {Typeflag :tar .TypeDir ,Name :"subdir/" ,Mode :0o755 },nil }, 35 {tar.Header {Typeflag :tar .TypeReg ,Name :"test.txt" ,Size :11 ,Mode :0o644 }, []byte ("hello world" )}, 36 {tar.Header {Typeflag :tar .TypeReg ,Name :"subdir/nested.txt" ,Size :6 ,Mode :0o644 }, []byte ("nested" )}, 37 } 38 39for _ ,e := range entries { 40require .NoError (t ,tw .WriteHeader (& e .hdr )) 41if e .content != nil { 42_ ,err := tw .Write (e .content ) 43require .NoError (t ,err ) 44 } 45 } 46require .NoError (t ,tw .Close ()) 47} 48 49func createTestTarFile (t * testing.T ,path string ) { 50t .Helper () 51f ,err := os .Create (path ) 52require .NoError (t ,err ) 53defer f .Close () 54writeTarEntries (t ,f ) 55} 56 57func createTestTarGzFile (t * testing.T ,path string ) { 58t .Helper () 59f ,err := os .Create (path ) 60require .NoError (t ,err ) 61defer f .Close () 62gw := gzip .NewWriter (f ) 63writeTarEntries (t ,gw ) 64require .NoError (t ,gw .Close ()) 65} 66 67func createTestTarBz2File (t * testing.T ,path string ) { 68t .Helper () 69// compress/bzip2 only provides a reader; use a pre-recorded bzip2-compressed 70// tar so we can test the reading path without an external encoder. 71// We build the plain tar in memory, then bzip2-compress it via a known 72// approach: write raw tar bytes, then BZ-compress them with a pure-Go writer. 73// Go stdlib has no bzip2 writer, so we create a minimal bzip2 stream by 74// piping through the `bzip2` command if available, otherwise skip. 75var buf bytes.Buffer 76writeTarEntries (t ,& buf ) 77 78// Try to use bzip2 command-line tool 79tmp := t .TempDir () 80rawTar := filepath .Join (tmp ,"tmp.tar" ) 81require .NoError (t ,os .WriteFile (rawTar ,buf .Bytes (),0o600 )) 82 83out ,err := os .Create (path ) 84require .NoError (t ,err ) 85defer out .Close () 86 87// Write a real bzip2 stream using a simple pure-Go BZ2 encoder shim. 88// Because the stdlib only provides a reader, we vendor a minimal BZ2 writer 89// here via dsnet/compress or simply write the file using pgzip. Instead, we 90// use a known-good approach: encode via Go's exec. If exec is unavailable we 91// create the file using a bzip2 block that wraps our raw bytes. 92// 93// Simplest portable approach: use compress/flate at the tar layer instead. 94// But since we need a real bzip2 file, we skip if bzip2 is unavailable. 95bzip2Cmd ,lookErr := findBzip2Cmd () 96if lookErr != nil { 97t .Skip ("bzip2 command not available, skipping .tar.bz2 test" ) 98 } 99 100rawData ,err := os .ReadFile (rawTar ) 101require .NoError (t ,err ) 102compressed ,err := bzip2Cmd (rawData ) 103require .NoError (t ,err ) 104_ ,err = out .Write (compressed ) 105require .NoError (t ,err ) 106} 107 108func createTestTarXzFile (t * testing.T ,path string ) { 109t .Helper () 110f ,err := os .Create (path ) 111require .NoError (t ,err ) 112defer f .Close () 113 114xw ,err := xz .NewWriter (f ) 115require .NoError (t ,err ) 116writeTarEntries (t ,xw ) 117require .NoError (t ,xw .Close ()) 118} 119 120// findBzip2Cmd returns a function that compresses data with bzip2, or an error 121// if bzip2 is not available on the system. 122func findBzip2Cmd () (func ([]byte ) ([]byte ,error ),error ) { 123// We use os/exec but need to import it. To avoid adding an import only used 124// in tests, use a direct syscall approach via os/exec in a sub-function. 125return bzip2CompressFunc () 126} 127 128// --------------------------------------------------------------------------- 129// isTarFile 130// --------------------------------------------------------------------------- 131 132func TestIsTarFile (t * testing.T ) { 133tests := []struct { 134filename string 135expected bool 136 }{ 137 {"archive.tar" ,true }, 138 {"archive.tar.gz" ,true }, 139 {"archive.tgz" ,true }, 140 {"archive.tar.bz2" ,true }, 141 {"archive.tbz2" ,true }, 142 {"archive.tar.xz" ,true }, 143 {"archive.txz" ,true }, 144 {"ARCHIVE.TAR" ,true }, 145 {"ARCHIVE.TAR.GZ" ,true }, 146 {"ARCHIVE.TGZ" ,true }, 147 {"ARCHIVE.TAR.BZ2" ,true }, 148 {"ARCHIVE.TBZZ2" ,false }, 149 {"ARCHIVE.TAR.XZ" ,true }, 150 {"ARCHIVE.TXZ" ,true }, 151 {"archive.zip" ,false }, 152 {"archive.jar" ,false }, 153 {"archive.gz" ,false },// plain gzip, not a tarball 154 {"archive.bz2" ,false },// plain bzip2, not a tarball 155 {"archive.xz" ,false }, 156 {"archive.txt" ,false }, 157 {"archive" ,false }, 158 {"" ,false }, 159 } 160 161for _ ,tc := range tests { 162result := isTarFile (tc .filename ) 163assert .Equal (t ,tc .expected ,result ,"filename: %s" ,tc .filename ) 164 } 165} 166 167// --------------------------------------------------------------------------- 168// processTarFile – plain .tar 169// --------------------------------------------------------------------------- 170 171func TestProcessTarFile (t * testing.T ) { 172tmpDir := t .TempDir () 173tarPath := filepath .Join (tmpDir ,"test.tar" ) 174createTestTarFile (t ,tarPath ) 175 176info ,err := os .Stat (tarPath ) 177require .NoError (t ,err ) 178 179td ,err := processTarFile (tarPath ,info ) 180require .NoError (t ,err ) 181require .NotNil (t ,td ) 182 183assert .Equal (t ,"test.tar" ,td .GetName ()) 184assert .Equal (t ,rune ('T' ),td .GetFlag ()) 185assert .True (t ,td .IsDir ()) 186assert .Equal (t ,"TarDirectory" ,td .GetType ()) 187 188// Usage should equal the on-disk size 189assert .Equal (t ,info .Size (),td .Usage ) 190 191files := slices .Collect (td .GetFiles (fs .SortByName ,fs .SortAsc )) 192assert .Greater (t ,len (files ),0 ) 193 194foundText := false 195foundSubdir := false 196for _ ,f := range files { 197if f .GetName ()== "test.txt" { 198foundText = true 199assert .False (t ,f .IsDir ()) 200assert .Equal (t ,"TarFile" ,f .GetType ()) 201 } 202if f .GetName ()== "subdir" { 203foundSubdir = true 204assert .True (t ,f .IsDir ()) 205assert .Equal (t ,"TarDirectory" ,f .GetType ()) 206 } 207 } 208assert .True (t ,foundText ,"should find test.txt" ) 209assert .True (t ,foundSubdir ,"should find subdir" ) 210} 211 212// --------------------------------------------------------------------------- 213// processTarFile – .tar.gz 214// --------------------------------------------------------------------------- 215 216func TestProcessTarGzFile (t * testing.T ) { 217tmpDir := t .TempDir () 218tarPath := filepath .Join (tmpDir ,"test.tar.gz" ) 219createTestTarGzFile (t ,tarPath ) 220 221info ,err := os .Stat (tarPath ) 222require .NoError (t ,err ) 223 224td ,err := processTarFile (tarPath ,info ) 225require .NoError (t ,err ) 226require .NotNil (t ,td ) 227 228assert .Equal (t ,"test.tar.gz" ,td .GetName ()) 229assert .True (t ,td .IsDir ()) 230assert .Equal (t ,"TarDirectory" ,td .GetType ()) 231assert .Equal (t ,info .Size (),td .Usage ) 232 233files := slices .Collect (td .GetFiles (fs .SortByName ,fs .SortAsc )) 234assert .Greater (t ,len (files ),0 ) 235} 236 237// --------------------------------------------------------------------------- 238// processTarFile – .tgz alias 239// --------------------------------------------------------------------------- 240 241func TestProcessTgzFile (t * testing.T ) { 242tmpDir := t .TempDir () 243tarPath := filepath .Join (tmpDir ,"test.tgz" ) 244createTestTarGzFile (t ,tarPath ) 245 246info ,err := os .Stat (tarPath ) 247require .NoError (t ,err ) 248 249td ,err := processTarFile (tarPath ,info ) 250require .NoError (t ,err ) 251require .NotNil (t ,td ) 252assert .Equal (t ,"test.tgz" ,td .GetName ()) 253} 254 255// --------------------------------------------------------------------------- 256// processTarFile – .tar.bz2 257// --------------------------------------------------------------------------- 258 259func TestProcessTarBz2File (t * testing.T ) { 260tmpDir := t .TempDir () 261tarPath := filepath .Join (tmpDir ,"test.tar.bz2" ) 262createTestTarBz2File (t ,tarPath )// skips if bzip2 unavailable 263 264info ,err := os .Stat (tarPath ) 265require .NoError (t ,err ) 266 267td ,err := processTarFile (tarPath ,info ) 268require .NoError (t ,err ) 269require .NotNil (t ,td ) 270 271assert .Equal (t ,"test.tar.bz2" ,td .GetName ()) 272assert .True (t ,td .IsDir ()) 273assert .Equal (t ,"TarDirectory" ,td .GetType ()) 274} 275 276// --------------------------------------------------------------------------- 277// processTarFile – .tar.xz 278// --------------------------------------------------------------------------- 279 280func TestProcessTarXzFile (t * testing.T ) { 281tmpDir := t .TempDir () 282tarPath := filepath .Join (tmpDir ,"test.tar.xz" ) 283createTestTarXzFile (t ,tarPath ) 284 285info ,err := os .Stat (tarPath ) 286require .NoError (t ,err ) 287 288td ,err := processTarFile (tarPath ,info ) 289require .NoError (t ,err ) 290require .NotNil (t ,td ) 291 292assert .Equal (t ,"test.tar.xz" ,td .GetName ()) 293assert .True (t ,td .IsDir ()) 294assert .Equal (t ,"TarDirectory" ,td .GetType ()) 295 296files := slices .Collect (td .GetFiles (fs .SortByName ,fs .SortAsc )) 297assert .Greater (t ,len (files ),0 ) 298} 299 300// --------------------------------------------------------------------------- 301// processTarFile – .txz alias 302// --------------------------------------------------------------------------- 303 304func TestProcessTxzFile (t * testing.T ) { 305tmpDir := t .TempDir () 306tarPath := filepath .Join (tmpDir ,"test.txz" ) 307createTestTarXzFile (t ,tarPath ) 308 309info ,err := os .Stat (tarPath ) 310require .NoError (t ,err ) 311 312td ,err := processTarFile (tarPath ,info ) 313require .NoError (t ,err ) 314require .NotNil (t ,td ) 315assert .Equal (t ,"test.txz" ,td .GetName ()) 316} 317 318// --------------------------------------------------------------------------- 319// ensureTarDirExists 320// --------------------------------------------------------------------------- 321 322func TestEnsureTarDirExists (t * testing.T ) { 323tarPath := "/fake/archive.tar" 324 325rootDir := & TarDir { 326Dir :& Dir { 327File :& File {Name :"archive.tar" ,Flag :'T' }, 328Files :make (fs.Files ,0 ), 329 }, 330tarPath :tarPath , 331 } 332 333dirMap := make (map [string ]* TarDir ) 334dirMap ["" ]= rootDir 335 336ensureTarDirExists (dirMap ,"a/b/c" ,tarPath ,rootDir ) 337 338assert .Contains (t ,dirMap ,"a" ) 339assert .Contains (t ,dirMap ,"a/b" ) 340assert .Contains (t ,dirMap ,"a/b/c" ) 341 342assert .Equal (t ,rootDir ,dirMap ["a" ].GetParent ()) 343assert .Equal (t ,dirMap ["a" ],dirMap ["a/b" ].GetParent ()) 344assert .Equal (t ,dirMap ["a/b" ],dirMap ["a/b/c" ].GetParent ()) 345} 346 347func TestEnsureTarDirExistsIdempotent (t * testing.T ) { 348tarPath := "/fake/archive.tar" 349rootDir := & TarDir { 350Dir :& Dir {File :& File {Name :"archive.tar" ,Flag :'T' },Files :make (fs.Files ,0 )}, 351tarPath :tarPath , 352 } 353dirMap := map [string ]* TarDir {"" :rootDir } 354 355ensureTarDirExists (dirMap ,"sub" ,tarPath ,rootDir ) 356first := dirMap ["sub" ] 357ensureTarDirExists (dirMap ,"sub" ,tarPath ,rootDir )// must be idempotent 358assert .Same (t ,first ,dirMap ["sub" ]) 359} 360 361// --------------------------------------------------------------------------- 362// TarFile / TarDir method coverage 363// --------------------------------------------------------------------------- 364 365func TestTarFileGetPath (t * testing.T ) { 366tf := & TarFile { 367File :& File {Name :"file.txt" }, 368tarPath :"/path/to/archive.tar" , 369inTarPath :"dir/file.txt" , 370 } 371assert .Equal (t ,"/path/to/archive.tar/dir/file.txt" ,tf .GetPath ()) 372} 373 374func TestTarFileGetType (t * testing.T ) { 375tf := & TarFile {File :& File {Name :"x" }} 376assert .Equal (t ,"TarFile" ,tf .GetType ()) 377} 378 379func TestTarFileEncodeJSON (t * testing.T ) { 380tf := & TarFile { 381File :& File {Name :"x.txt" ,Size :42 }, 382tarPath :"/a.tar" , 383inTarPath :"x.txt" , 384 } 385var buf bytes.Buffer 386assert .NoError (t ,tf .EncodeJSON (& buf ,false ,nil )) 387assert .NotEmpty (t ,buf .String ()) 388} 389 390func TestTarDirGetType (t * testing.T ) { 391td := & TarDir {Dir :& Dir {File :& File {Name :"sub" }}} 392assert .Equal (t ,"TarDirectory" ,td .GetType ()) 393} 394 395func TestTarDirIsDir (t * testing.T ) { 396td := & TarDir {Dir :& Dir {File :& File {Name :"sub" }}} 397assert .True (t ,td .IsDir ()) 398} 399 400func TestTarDirEncodeJSON (t * testing.T ) { 401td := & TarDir { 402Dir :& Dir {File :& File {Name :"sub" },Files :make (fs.Files ,0 )}, 403tarPath :"/a.tar" , 404 } 405var buf bytes.Buffer 406assert .NoError (t ,td .EncodeJSON (& buf ,true ,nil )) 407assert .NotEmpty (t ,buf .String ()) 408} 409 410func TestTarDirGetPathWithParent (t * testing.T ) { 411parent := & TarDir { 412Dir :& Dir {File :& File {Name :"parent" }}, 413tarPath :"/a.tar" , 414 } 415child := & TarDir { 416Dir :& Dir {File :& File {Name :"child" }}, 417tarPath :"/a.tar" , 418 } 419child .Parent = parent 420 421assert .Equal (t ,filepath .Join (parent .GetPath (),"child" ),child .GetPath ()) 422} 423 424func TestTarDirGetPathWithoutParent (t * testing.T ) { 425td := & TarDir { 426Dir :& Dir {File :& File {Name :"root" }}, 427tarPath :"/path/to/archive.tar" , 428 } 429assert .Equal (t ,"/path/to/archive.tar" ,td .GetPath ()) 430} 431 432// --------------------------------------------------------------------------- 433// Integration: SequentialAnalyzer with tar files 434// --------------------------------------------------------------------------- 435 436func TestSequentialAnalyzerWithTarFile (t * testing.T ) { 437tmpDir := t .TempDir () 438createTestTarFile (t ,filepath .Join (tmpDir ,"test.tar" )) 439 440a := CreateSeqAnalyzer () 441a .SetArchiveBrowsing (true ) 442 443result := a .AnalyzeDir (tmpDir , 444func (string ,string )bool {return false }, 445func (string )bool {return false }, 446 ) 447 448require .NotNil (t ,result ) 449 450var tarItem fs.Item 451for f := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 452if f .GetName ()== "test.tar" { 453tarItem = f 454break 455 } 456 } 457require .NotNil (t ,tarItem ,"should find test.tar as a browsable directory" ) 458assert .True (t ,tarItem .IsDir ()) 459assert .Equal (t ,"TarDirectory" ,tarItem .GetType ()) 460 461count := 0 462for range tarItem .GetFiles (fs .SortByName ,fs .SortAsc ) { 463count ++ 464 } 465assert .Greater (t ,count ,0 ,"tar archive should contain browsable content" ) 466} 467 468func TestSequentialAnalyzerWithTarGzFile (t * testing.T ) { 469tmpDir := t .TempDir () 470createTestTarGzFile (t ,filepath .Join (tmpDir ,"test.tar.gz" )) 471 472a := CreateSeqAnalyzer () 473a .SetArchiveBrowsing (true ) 474 475result := a .AnalyzeDir (tmpDir , 476func (string ,string )bool {return false }, 477func (string )bool {return false }, 478 ) 479 480require .NotNil (t ,result ) 481 482var tarItem fs.Item 483for f := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 484if f .GetName ()== "test.tar.gz" { 485tarItem = f 486break 487 } 488 } 489require .NotNil (t ,tarItem ,"should find test.tar.gz as a browsable directory" ) 490assert .True (t ,tarItem .IsDir ()) 491} 492 493// --------------------------------------------------------------------------- 494// Integration: ParallelAnalyzer with tar files 495// --------------------------------------------------------------------------- 496 497func TestParallelAnalyzerWithTarFile (t * testing.T ) { 498tmpDir := t .TempDir () 499createTestTarFile (t ,filepath .Join (tmpDir ,"archive.tar" )) 500 501a := CreateAnalyzer () 502a .SetArchiveBrowsing (true ) 503 504result := a .AnalyzeDir (tmpDir , 505func (string ,string )bool {return false }, 506func (string )bool {return false }, 507 ) 508 509require .NotNil (t ,result ) 510 511var tarItem fs.Item 512for f := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 513if f .GetName ()== "archive.tar" { 514tarItem = f 515break 516 } 517 } 518require .NotNil (t ,tarItem ,"should find archive.tar" ) 519assert .True (t ,tarItem .IsDir ()) 520assert .Equal (t ,"TarDirectory" ,tarItem .GetType ()) 521} 522 523func TestParallelAnalyzerWithTarXzFile (t * testing.T ) { 524tmpDir := t .TempDir () 525createTestTarXzFile (t ,filepath .Join (tmpDir ,"archive.tar.xz" )) 526 527a := CreateAnalyzer () 528a .SetArchiveBrowsing (true ) 529 530result := a .AnalyzeDir (tmpDir , 531func (string ,string )bool {return false }, 532func (string )bool {return false }, 533 ) 534 535require .NotNil (t ,result ) 536 537var tarItem fs.Item 538for f := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 539if f .GetName ()== "archive.tar.xz" { 540tarItem = f 541break 542 } 543 } 544require .NotNil (t ,tarItem ,"should find archive.tar.xz" ) 545assert .True (t ,tarItem .IsDir ()) 546} 547 548// --------------------------------------------------------------------------- 549// Error handling: non-existent / corrupt archive 550// --------------------------------------------------------------------------- 551 552func TestProcessTarFileNotFound (t * testing.T ) { 553_ ,err := processTarFile ("/no/such/file.tar" ,nil ) 554assert .Error (t ,err ) 555} 556 557func TestProcessTarFileCorrupt (t * testing.T ) { 558tmpDir := t .TempDir () 559p := filepath .Join (tmpDir ,"bad.tar" ) 560require .NoError (t ,os .WriteFile (p , []byte ("not a tar file at all" ),0o600 )) 561 562info ,err := os .Stat (p ) 563require .NoError (t ,err ) 564 565// A corrupt plain-tar should still open (it's a file), but Next() will fail. 566// processTarFile should propagate the error. 567_ ,err = processTarFile (p ,info ) 568assert .Error (t ,err ) 569} 570 571func TestProcessTarGzFileCorrupt (t * testing.T ) { 572tmpDir := t .TempDir () 573p := filepath .Join (tmpDir ,"bad.tar.gz" ) 574require .NoError (t ,os .WriteFile (p , []byte ("not gzip" ),0o600 )) 575 576info ,err := os .Stat (p ) 577require .NoError (t ,err ) 578 579_ ,err = processTarFile (p ,info ) 580assert .Error (t ,err ) 581} 582 583func TestProcessTarXzFileCorrupt (t * testing.T ) { 584tmpDir := t .TempDir () 585p := filepath .Join (tmpDir ,"bad.tar.xz" ) 586require .NoError (t ,os .WriteFile (p , []byte ("not xz" ),0o600 )) 587 588info ,err := os .Stat (p ) 589require .NoError (t ,err ) 590 591_ ,err = processTarFile (p ,info ) 592assert .Error (t ,err ) 593} 594 595// --------------------------------------------------------------------------- 596// Archive browsing disabled – tar file treated as regular file 597// --------------------------------------------------------------------------- 598 599func TestAnalyzerTarBrowsingDisabled (t * testing.T ) { 600tmpDir := t .TempDir () 601createTestTarFile (t ,filepath .Join (tmpDir ,"archive.tar" )) 602 603a := CreateSeqAnalyzer () 604// archiveBrowsing is false by default 605 606result := a .AnalyzeDir (tmpDir , 607func (string ,string )bool {return false }, 608func (string )bool {return false }, 609 ) 610 611require .NotNil (t ,result ) 612 613var tarItem fs.Item 614for f := range result .GetFiles (fs .SortByName ,fs .SortAsc ) { 615if f .GetName ()== "archive.tar" { 616tarItem = f 617break 618 } 619 } 620require .NotNil (t ,tarItem ) 621assert .False (t ,tarItem .IsDir (),"when browsing disabled, tar is a plain file" ) 622} 623 624// --------------------------------------------------------------------------- 625// bzip2 compression helper (uses os/exec "bzip2") 626// --------------------------------------------------------------------------- 627 628func bzip2CompressFunc () (func ([]byte ) ([]byte ,error ),error ) { 629return bzip2CompressWithCmd () 630} 631 632func bzip2CompressWithCmd () (func ([]byte ) ([]byte ,error ),error ) { 633bzip2Path ,err := exec .LookPath ("bzip2" ) 634if err != nil { 635return nil ,err 636 } 637return func (data []byte ) ([]byte ,error ) { 638cmd := exec .Command (bzip2Path ,"--compress" ,"--stdout" ) 639cmd .Stdin = bytes .NewReader (data ) 640return cmd .Output () 641 },nil 642} 643 644// Ensure compress/bzip2 package is used (for reading .tar.bz2) 645var _ = bzip2 .NewReader