fork/gdu

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

Daniel Mildefeat: allow scanning multiple directories (#653)34f3996

master
8.6 KiB377 linesraw
1package report
2
3import (
4	"bytes"
5	"errors"
6	"fmt"
7	"io"
8	"math"
9	"os"
10	"strconv"
11	"sync"
12	"time"
13
14	"github.com/dundee/gdu/v5/build"
15	"github.com/dundee/gdu/v5/internal/common"
16	"github.com/dundee/gdu/v5/pkg/analyze"
17	"github.com/dundee/gdu/v5/pkg/device"
18	"github.com/dundee/gdu/v5/pkg/fs"
19	"github.com/fatih/color"
20)
21
22const clearTerminalLine = "\r\x1b[2K"
23
24// UI struct
25type UI struct {
26	*common.UI
27	output           io.Writer
28	exportOutput     io.Writer
29	red              *color.Color
30	orange           *color.Color
31	writtenChan      chan struct{}
32	outputAttributes fs.JSONAttributes
33	top              int
34	depth            int
35	summarize        bool
36}
37
38// CreateExportUI creates UI for stdout
39func CreateExportUI(
40	output io.Writer,
41	exportOutput io.Writer,
42	useColors bool,
43	showProgress bool,
44	useSIPrefix bool,
45	top int,
46	depth int,
47	summarize bool,
48	outputAttributes fs.JSONAttributes,
49) *UI {
50	ui := &UI{
51		UI: &common.UI{
52			ShowProgress: showProgress,
53			Analyzer:     analyze.CreateAnalyzer(),
54			UseSIPrefix:  useSIPrefix,
55		},
56		output:           output,
57		exportOutput:     exportOutput,
58		writtenChan:      make(chan struct{}),
59		outputAttributes: outputAttributes,
60		top:              top,
61		depth:            depth,
62		summarize:        summarize,
63	}
64	if !useSIPrefix {
65		ui.SetBlockSizeFromEnvironment()
66	}
67	ui.red = color.New(color.FgRed).Add(color.Bold)
68	ui.orange = color.New(color.FgYellow).Add(color.Bold)
69
70	if !useColors {
71		color.NoColor = true
72	}
73
74	return ui
75}
76
77// StartUILoop stub
78func (ui *UI) StartUILoop() error {
79	return nil
80}
81
82// SetCollapsePath sets the flag to collapse paths
83func (ui *UI) SetCollapsePath(value bool) {
84}
85
86// SetShowSymlinkTarget is a no-op for the export UI (no rendered file list)
87func (ui *UI) SetShowSymlinkTarget(value bool) {
88}
89
90// ListDevices lists mounted devices and shows their disk usage
91func (ui *UI) ListDevices(getter device.DevicesInfoGetter) error {
92	return errors.New("exporting devices list is not supported")
93}
94
95// ReadAnalysis reads analysis report from JSON file
96func (ui *UI) ReadAnalysis(input io.Reader) error {
97	return errors.New("reading analysis is not possible while exporting")
98}
99
100// ReadFromStorage reads analysis data from persistent key-value storage
101func (ui *UI) ReadFromStorage(storagePath, path string) error {
102	storage := analyze.NewStorage(storagePath, path)
103	closeFn := storage.Open()
104	defer closeFn()
105
106	dir, err := storage.GetDirForPath(path)
107	if err != nil {
108		return err
109	}
110
111	var waitWritten sync.WaitGroup
112	if ui.ShowProgress {
113		waitWritten.Add(1)
114		go func() {
115			defer waitWritten.Done()
116			ui.updateProgress()
117		}()
118	}
119
120	return ui.exportDir(dir, &waitWritten)
121}
122
123// AnalyzePath analyzes recursively disk usage in given path
124func (ui *UI) AnalyzePath(path string, _ fs.Item) error {
125	var (
126		dir         fs.Item
127		wait        sync.WaitGroup
128		waitWritten sync.WaitGroup
129	)
130
131	if ui.ShowProgress {
132		waitWritten.Add(1)
133		go func() {
134			defer waitWritten.Done()
135			ui.updateProgress()
136		}()
137	}
138
139	wait.Add(1)
140	go func() {
141		defer wait.Done()
142		dir = ui.Analyzer.AnalyzeDir(path, ui.CreateIgnoreFunc(), ui.CreateFileTypeFilter())
143		if ui.IsFilteringFiles() {
144			dir.UpdateStatsWithFileFiltering(make(fs.HardLinkedItems, 10))
145		} else {
146			dir.UpdateStats(make(fs.HardLinkedItems, 10))
147		}
148	}()
149
150	wait.Wait()
151
152	return ui.exportDir(dir, &waitWritten)
153}
154
155// AnalyzePaths is not supported by the export UI: the ncdu-compatible export
156// format holds exactly one root directory, so there is nowhere to record a
157// virtual dir grouping several of them.
158func (ui *UI) AnalyzePaths(paths []string) error {
159	if len(paths) == 1 {
160		return ui.AnalyzePath(paths[0], nil)
161	}
162	return errors.New("exporting more than one directory is not supported")
163}
164
165func (ui *UI) topDir(dir fs.Item) fs.Item {
166	files := analyze.CollectTopFiles(dir, ui.top)
167
168	topDir := &analyze.Dir{
169		File: &analyze.File{
170			Name:  dir.GetName(),
171			Mtime: dir.GetMtime(),
172		},
173	}
174	if d, ok := dir.(*analyze.Dir); ok {
175		topDir.BasePath = d.BasePath
176	}
177	for _, f := range files {
178		file := *f.(*analyze.File)
179		file.Parent = topDir
180		topDir.AddFile(&file)
181	}
182	topDir.UpdateStats(make(fs.HardLinkedItems, 10))
183	return topDir
184}
185
186func (ui *UI) limitDirByDepth(dir fs.Item, currentDepth int) fs.Item {
187	if d, ok := dir.(*analyze.Dir); ok {
188		limited := &analyze.Dir{
189			File: &analyze.File{
190				Name:   d.GetName(),
191				Mtime:  d.GetMtime(),
192				Parent: d.GetParent(),
193				Size:   d.GetSize(),
194				Usage:  d.GetUsage(),
195			},
196			BasePath:  d.BasePath,
197			ItemCount: d.ItemCount,
198		}
199		if currentDepth == ui.depth {
200			return limited
201		}
202		for f := range d.GetFiles(fs.SortBySize, fs.SortDesc) {
203			if f.IsDir() {
204				child := ui.limitDirByDepth(f, currentDepth+1)
205				if child != nil {
206					child.SetParent(limited)
207					limited.AddFile(child)
208				}
209			} else if currentDepth+1 <= ui.depth {
210				file := *f.(*analyze.File)
211				file.Parent = limited
212				limited.AddFile(&file)
213			}
214		}
215		return limited
216	}
217
218	return dir
219}
220
221func (ui *UI) summarizeDir(dir fs.Item) fs.Item {
222	summary := &analyze.Dir{
223		File: &analyze.File{
224			Name:  dir.GetName(),
225			Mtime: dir.GetMtime(),
226		},
227	}
228	if d, ok := dir.(*analyze.Dir); ok {
229		summary.BasePath = d.BasePath
230		summary.ItemCount = d.ItemCount
231		summary.Size = d.GetSize()
232		summary.Usage = d.GetUsage()
233	}
234	return summary
235}
236
237func (ui *UI) exportDir(dir fs.Item, waitWritten *sync.WaitGroup) error {
238	// Sorting is now handled by GetFiles with sort parameters
239
240	var (
241		buff bytes.Buffer
242		err  error
243	)
244
245	buff.Write([]byte(`[1,2,{"progname":"gdu","progver":"`))
246	buff.Write([]byte(build.Version))
247	buff.Write([]byte(`","timestamp":`))
248	buff.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10)))
249	buff.Write([]byte("},\n"))
250
251	switch {
252	case ui.summarize:
253		dir = ui.summarizeDir(dir)
254	case ui.top > 0:
255		dir = ui.topDir(dir)
256	case ui.depth > 0:
257		dir = ui.limitDirByDepth(dir, 0)
258	}
259
260	if err := dir.EncodeJSON(&buff, true, ui.outputAttributes); err != nil {
261		return err
262	}
263	if _, err = buff.Write([]byte("]\n")); err != nil {
264		return err
265	}
266	if _, err = buff.WriteTo(ui.exportOutput); err != nil {
267		return err
268	}
269
270	if f, ok := ui.exportOutput.(*os.File); ok {
271		err = f.Close()
272		if err != nil {
273			return err
274		}
275	}
276
277	if ui.ShowProgress {
278		ui.writtenChan <- struct{}{}
279		waitWritten.Wait()
280	}
281
282	return nil
283}
284
285func (ui *UI) updateProgress() {
286	waitingForWrite := false
287
288	progressRunes := []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
289
290	doneChan := ui.Analyzer.GetDone()
291
292	i := 0
293	for {
294		fmt.Fprint(ui.output, clearTerminalLine)
295
296		progress := ui.Analyzer.GetProgress()
297
298		select {
299		case <-doneChan:
300			fmt.Fprint(ui.output, clearTerminalLine)
301			waitingForWrite = true
302		case <-ui.writtenChan:
303			fmt.Fprint(ui.output, clearTerminalLine)
304			return
305		default:
306		}
307
308		fmt.Fprintf(ui.output, "\r %s ", string(progressRunes[i]))
309
310		if waitingForWrite {
311			fmt.Fprint(ui.output, "Writing output file...")
312		} else {
313			fmt.Fprint(ui.output, "Scanning... Total items: "+
314				ui.red.Sprint(common.FormatNumber(int64(progress.ItemCount)))+
315				" size: "+
316				ui.formatSize(progress.TotalUsage))
317		}
318
319		time.Sleep(100 * time.Millisecond)
320		i++
321		i %= 10
322	}
323}
324
325func (ui *UI) formatSize(size int64) string {
326	if formatted, ok := ui.FormatBlockSize(size); ok {
327		return ui.orange.Sprint(formatted)
328	}
329	if ui.UseSIPrefix {
330		return ui.formatWithDecPrefix(size)
331	}
332	return ui.formatWithBinPrefix(size)
333}
334
335func (ui *UI) formatWithBinPrefix(size int64) string {
336	fsize := float64(size)
337	asize := math.Abs(fsize)
338
339	switch {
340	case asize >= common.Ei:
341		return ui.orange.Sprintf("%.1f", fsize/common.Ei) + " EiB"
342	case asize >= common.Pi:
343		return ui.orange.Sprintf("%.1f", fsize/common.Pi) + " PiB"
344	case asize >= common.Ti:
345		return ui.orange.Sprintf("%.1f", fsize/common.Ti) + " TiB"
346	case asize >= common.Gi:
347		return ui.orange.Sprintf("%.1f", fsize/common.Gi) + " GiB"
348	case asize >= common.Mi:
349		return ui.orange.Sprintf("%.1f", fsize/common.Mi) + " MiB"
350	case asize >= common.Ki:
351		return ui.orange.Sprintf("%.1f", fsize/common.Ki) + " KiB"
352	default:
353		return ui.orange.Sprintf("%d", size) + " B"
354	}
355}
356
357func (ui *UI) formatWithDecPrefix(size int64) string {
358	fsize := float64(size)
359	asize := math.Abs(fsize)
360
361	switch {
362	case asize >= common.E:
363		return ui.orange.Sprintf("%.1f", fsize/common.E) + " EB"
364	case asize >= common.P:
365		return ui.orange.Sprintf("%.1f", fsize/common.P) + " PB"
366	case asize >= common.T:
367		return ui.orange.Sprintf("%.1f", fsize/common.T) + " TB"
368	case asize >= common.G:
369		return ui.orange.Sprintf("%.1f", fsize/common.G) + " GB"
370	case asize >= common.M:
371		return ui.orange.Sprintf("%.1f", fsize/common.M) + " MB"
372	case asize >= common.K:
373		return ui.orange.Sprintf("%.1f", fsize/common.K) + " kB"
374	default:
375		return ui.orange.Sprintf("%d", size) + " B"
376	}
377}