fork/gdu
git clone https://git.julia.blue/fork/gdu
d062e88
master
1package analyze 2 3import ( 4"archive/tar" 5"compress/bzip2" 6"compress/gzip" 7"io" 8"os" 9"path/filepath" 10"strings" 11"time" 12 13"github.com/dundee/gdu/v5/pkg/fs" 14"github.com/ulikunitz/xz" 15) 16 17// TarDir represents a directory structure inside a tar archive 18type TarDir struct { 19* Dir 20tarPath string // path to the original tar file 21} 22 23// TarFile represents a file inside a tar archive 24type TarFile struct { 25* File 26tarPath string 27inTarPath string // path inside the tar archive 28} 29 30// GetPath returns the virtual path for tar file 31func (tf * TarFile )GetPath ()string { 32return tf .tarPath + "/" + tf .inTarPath 33} 34 35// GetType returns type of tar file 36func (tf * TarFile )GetType ()string { 37return "TarFile" 38} 39 40// EncodeJSON encodes tar file to JSON 41func (tf * TarFile )EncodeJSON (writer io.Writer ,topLevel bool ,attributes fs.JSONAttributes )error { 42return tf .File .EncodeJSON (writer ,topLevel ,attributes ) 43} 44 45// GetType returns type of tar directory 46func (td * TarDir )GetType ()string { 47return "TarDirectory" 48} 49 50// IsDir returns true for TarDir 51func (td * TarDir )IsDir ()bool { 52return true 53} 54 55// EncodeJSON encodes tar directory to JSON 56func (td * TarDir )EncodeJSON (writer io.Writer ,topLevel bool ,attributes fs.JSONAttributes )error { 57return td .Dir .EncodeJSON (writer ,topLevel ,attributes ) 58} 59 60// GetPath returns the virtual path for tar directory 61func (td * TarDir )GetPath ()string { 62if td .Parent != nil { 63return filepath .Join (td .Parent .GetPath (),td .Name ) 64 } 65return td .tarPath 66} 67 68// isTarFile checks if a file is a tar archive (tar, tar.gz, tgz, tar.bz2, tbz2, tar.xz, txz) 69func isTarFile (filename string )bool { 70lower := strings .ToLower (filename ) 71return strings .HasSuffix (lower ,".tar" )|| 72strings .HasSuffix (lower ,".tar.gz" )|| 73strings .HasSuffix (lower ,".tgz" )|| 74strings .HasSuffix (lower ,".tar.bz2" )|| 75strings .HasSuffix (lower ,".tbz2" )|| 76strings .HasSuffix (lower ,".tar.xz" )|| 77strings .HasSuffix (lower ,".txz" ) 78} 79 80// multiCloser closes multiple io.Closer instances in sequence 81type multiCloser struct { 82closers []io.Closer 83} 84 85func (mc * multiCloser )Close ()error { 86var firstErr error 87for _ ,c := range mc .closers { 88if err := c .Close ();err != nil && firstErr == nil { 89firstErr = err 90 } 91 } 92return firstErr 93} 94 95// openTarReader opens a tar archive and returns a tar.Reader and a Closer for cleanup. 96// It automatically wraps the reader with the appropriate decompressor based on file extension. 97func openTarReader (tarPath string ) (* tar.Reader , io.Closer ,error ) { 98f ,err := os .Open (tarPath ) 99if err != nil { 100return nil ,nil ,err 101 } 102 103lower := strings .ToLower (tarPath ) 104switch { 105case strings .HasSuffix (lower ,".tar.gz" )|| strings .HasSuffix (lower ,".tgz" ): 106gr ,err := gzip .NewReader (f ) 107if err != nil { 108f .Close () 109return nil ,nil ,err 110 } 111return tar .NewReader (gr ),& multiCloser {closers : []io.Closer {gr ,f }},nil 112 113case strings .HasSuffix (lower ,".tar.bz2" )|| strings .HasSuffix (lower ,".tbz2" ): 114br := bzip2 .NewReader (f ) 115return tar .NewReader (br ),f ,nil 116 117case strings .HasSuffix (lower ,".tar.xz" )|| strings .HasSuffix (lower ,".txz" ): 118xr ,err := xz .NewReader (f ) 119if err != nil { 120f .Close () 121return nil ,nil ,err 122 } 123return tar .NewReader (xr ),f ,nil 124 125default :// plain .tar 126return tar .NewReader (f ),f ,nil 127 } 128} 129 130// processTarFile reads a tar archive and returns a TarDir representing its contents. 131// TarDir.Size is set to the total uncompressed content size; TarDir.Usage is the 132// size of the archive file on disk. 133func processTarFile (tarPath string ,info os.FileInfo ) (* TarDir ,error ) { 134tr ,closer ,err := openTarReader (tarPath ) 135if err != nil { 136return nil ,err 137 } 138defer closer .Close () 139 140tarDir := & TarDir { 141Dir :& Dir { 142File :& File { 143Name :filepath .Base (tarPath ), 144Flag :'T' , 145Size :info .Size (), 146Usage :info .Size (), 147Mtime :info .ModTime (), 148 }, 149ItemCount :1 , 150Files :make (fs.Files ,0 ), 151 }, 152tarPath :tarPath , 153 } 154 155dirMap := make (map [string ]* TarDir ) 156dirMap ["" ]= tarDir 157 158var totalUncompressed int64 159 160for { 161header ,err := tr .Next () 162if err == io .EOF { 163break 164 } 165if err != nil { 166return nil ,err 167 } 168 169// Normalize path: strip leading ./ and trailing / 170name := strings .TrimPrefix (header .Name ,"./" ) 171name = strings .TrimSuffix (name ,"/" ) 172 173switch header .Typeflag { 174case tar .TypeDir : 175if name != "" { 176ensureTarDirExists (dirMap ,name ,tarPath ,tarDir ) 177 } 178 179case tar .TypeReg ,tar .TypeLink ,tar .TypeSymlink : 180dirPath := filepath .Dir (name ) 181if dirPath == "." { 182dirPath = "" 183 } 184ensureTarDirExists (dirMap ,dirPath ,tarPath ,tarDir ) 185 186parentDir := dirMap [dirPath ] 187totalUncompressed += header .Size 188 189tarFile := & TarFile { 190File :& File { 191Name :filepath .Base (name ), 192Flag :' ' , 193Size :header .Size , 194Usage :header .Size , 195Mtime :header .ModTime , 196Parent :parentDir , 197 }, 198tarPath :tarPath , 199inTarPath :name , 200 } 201parentDir .AddFile (tarFile ) 202 } 203// Other types (device files, fifos, etc.) are silently skipped 204 } 205 206// Size = total uncompressed content; Usage = compressed archive on disk 207tarDir .Size = totalUncompressed 208tarDir .Usage = info .Size () 209 210return tarDir ,nil 211} 212 213// ensureTarDirExists ensures all directories in the specified path exist within dirMap 214func ensureTarDirExists (dirMap map [string ]* TarDir ,path ,tarPath string ,rootDir * TarDir ) { 215if path == "" || path == "." { 216return 217 } 218 219if _ ,exists := dirMap [path ];exists { 220return 221 } 222 223// Ensure parent directory exists first 224parentPath := filepath .Dir (path ) 225if parentPath != "." && parentPath != "" && parentPath != path { 226ensureTarDirExists (dirMap ,parentPath ,tarPath ,rootDir ) 227 } 228 229var parent * TarDir 230if parentPath == "" || parentPath == "." || parentPath == path { 231parent = rootDir 232 }else { 233parent = dirMap [parentPath ] 234 } 235 236newDir := & TarDir { 237Dir :& Dir { 238File :& File { 239Name :filepath .Base (path ), 240Flag :'T' , 241Size :4096 , 242Usage :4096 , 243Mtime :time .Now (), 244Parent :parent , 245 }, 246ItemCount :1 , 247Files :make (fs.Files ,0 ), 248 }, 249tarPath :tarPath , 250 } 251 252dirMap [path ]= newDir 253parent .AddFile (newDir ) 254}