fork/gdu

git clone https://git.julia.blue/fork/gdu

ShivamB25feat: customize JSON export attributes (#605)d062e88

master
6.0 KiB254 linesraw
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
20	tarPath string // path to the original tar file
21}
22
23// TarFile represents a file inside a tar archive
24type TarFile struct {
25	*File
26	tarPath   string
27	inTarPath string // path inside the tar archive
28}
29
30// GetPath returns the virtual path for tar file
31func (tf *TarFile) GetPath() string {
32	return tf.tarPath + "/" + tf.inTarPath
33}
34
35// GetType returns type of tar file
36func (tf *TarFile) GetType() string {
37	return "TarFile"
38}
39
40// EncodeJSON encodes tar file to JSON
41func (tf *TarFile) EncodeJSON(writer io.Writer, topLevel bool, attributes fs.JSONAttributes) error {
42	return tf.File.EncodeJSON(writer, topLevel, attributes)
43}
44
45// GetType returns type of tar directory
46func (td *TarDir) GetType() string {
47	return "TarDirectory"
48}
49
50// IsDir returns true for TarDir
51func (td *TarDir) IsDir() bool {
52	return true
53}
54
55// EncodeJSON encodes tar directory to JSON
56func (td *TarDir) EncodeJSON(writer io.Writer, topLevel bool, attributes fs.JSONAttributes) error {
57	return td.Dir.EncodeJSON(writer, topLevel, attributes)
58}
59
60// GetPath returns the virtual path for tar directory
61func (td *TarDir) GetPath() string {
62	if td.Parent != nil {
63		return filepath.Join(td.Parent.GetPath(), td.Name)
64	}
65	return 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 {
70	lower := strings.ToLower(filename)
71	return strings.HasSuffix(lower, ".tar") ||
72		strings.HasSuffix(lower, ".tar.gz") ||
73		strings.HasSuffix(lower, ".tgz") ||
74		strings.HasSuffix(lower, ".tar.bz2") ||
75		strings.HasSuffix(lower, ".tbz2") ||
76		strings.HasSuffix(lower, ".tar.xz") ||
77		strings.HasSuffix(lower, ".txz")
78}
79
80// multiCloser closes multiple io.Closer instances in sequence
81type multiCloser struct {
82	closers []io.Closer
83}
84
85func (mc *multiCloser) Close() error {
86	var firstErr error
87	for _, c := range mc.closers {
88		if err := c.Close(); err != nil && firstErr == nil {
89			firstErr = err
90		}
91	}
92	return 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) {
98	f, err := os.Open(tarPath)
99	if err != nil {
100		return nil, nil, err
101	}
102
103	lower := strings.ToLower(tarPath)
104	switch {
105	case strings.HasSuffix(lower, ".tar.gz") || strings.HasSuffix(lower, ".tgz"):
106		gr, err := gzip.NewReader(f)
107		if err != nil {
108			f.Close()
109			return nil, nil, err
110		}
111		return tar.NewReader(gr), &multiCloser{closers: []io.Closer{gr, f}}, nil
112
113	case strings.HasSuffix(lower, ".tar.bz2") || strings.HasSuffix(lower, ".tbz2"):
114		br := bzip2.NewReader(f)
115		return tar.NewReader(br), f, nil
116
117	case strings.HasSuffix(lower, ".tar.xz") || strings.HasSuffix(lower, ".txz"):
118		xr, err := xz.NewReader(f)
119		if err != nil {
120			f.Close()
121			return nil, nil, err
122		}
123		return tar.NewReader(xr), f, nil
124
125	default: // plain .tar
126		return 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) {
134	tr, closer, err := openTarReader(tarPath)
135	if err != nil {
136		return nil, err
137	}
138	defer closer.Close()
139
140	tarDir := &TarDir{
141		Dir: &Dir{
142			File: &File{
143				Name:  filepath.Base(tarPath),
144				Flag:  'T',
145				Size:  info.Size(),
146				Usage: info.Size(),
147				Mtime: info.ModTime(),
148			},
149			ItemCount: 1,
150			Files:     make(fs.Files, 0),
151		},
152		tarPath: tarPath,
153	}
154
155	dirMap := make(map[string]*TarDir)
156	dirMap[""] = tarDir
157
158	var totalUncompressed int64
159
160	for {
161		header, err := tr.Next()
162		if err == io.EOF {
163			break
164		}
165		if err != nil {
166			return nil, err
167		}
168
169		// Normalize path: strip leading ./ and trailing /
170		name := strings.TrimPrefix(header.Name, "./")
171		name = strings.TrimSuffix(name, "/")
172
173		switch header.Typeflag {
174		case tar.TypeDir:
175			if name != "" {
176				ensureTarDirExists(dirMap, name, tarPath, tarDir)
177			}
178
179		case tar.TypeReg, tar.TypeLink, tar.TypeSymlink:
180			dirPath := filepath.Dir(name)
181			if dirPath == "." {
182				dirPath = ""
183			}
184			ensureTarDirExists(dirMap, dirPath, tarPath, tarDir)
185
186			parentDir := dirMap[dirPath]
187			totalUncompressed += header.Size
188
189			tarFile := &TarFile{
190				File: &File{
191					Name:   filepath.Base(name),
192					Flag:   ' ',
193					Size:   header.Size,
194					Usage:  header.Size,
195					Mtime:  header.ModTime,
196					Parent: parentDir,
197				},
198				tarPath:   tarPath,
199				inTarPath: name,
200			}
201			parentDir.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
207	tarDir.Size = totalUncompressed
208	tarDir.Usage = info.Size()
209
210	return 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) {
215	if path == "" || path == "." {
216		return
217	}
218
219	if _, exists := dirMap[path]; exists {
220		return
221	}
222
223	// Ensure parent directory exists first
224	parentPath := filepath.Dir(path)
225	if parentPath != "." && parentPath != "" && parentPath != path {
226		ensureTarDirExists(dirMap, parentPath, tarPath, rootDir)
227	}
228
229	var parent *TarDir
230	if parentPath == "" || parentPath == "." || parentPath == path {
231		parent = rootDir
232	} else {
233		parent = dirMap[parentPath]
234	}
235
236	newDir := &TarDir{
237		Dir: &Dir{
238			File: &File{
239				Name:   filepath.Base(path),
240				Flag:   'T',
241				Size:   4096,
242				Usage:  4096,
243				Mtime:  time.Now(),
244				Parent: parent,
245			},
246			ItemCount: 1,
247			Files:     make(fs.Files, 0),
248		},
249		tarPath: tarPath,
250	}
251
252	dirMap[path] = newDir
253	parent.AddFile(newDir)
254}