From 8a9d6039ff0de0c34a34b6b7a6206650d17e5c72 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Sun, 3 Jan 2021 22:41:25 +0100 Subject: [PATCH] do not store path in every file item --- analyze/dir.go | 7 +----- analyze/file.go | 51 ++++++++++++++++++++++++++------------------ analyze/file_test.go | 1 + cli/cli.go | 2 +- cli/cli_test.go | 1 + 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/analyze/dir.go b/analyze/dir.go index 8211844c1..947e7ba18 100644 --- a/analyze/dir.go +++ b/analyze/dir.go @@ -25,6 +25,7 @@ func ProcessDir(path string, progress *CurrentProgress, ignore ShouldDirBeIgnore concurrencyLimitChannel := make(chan bool, 2*runtime.NumCPU()) var wait sync.WaitGroup dir := processDir(path, progress, concurrencyLimitChannel, &wait, ignore) + dir.BasePath = filepath.Dir(path) wait.Wait() dir.UpdateStats() return dir @@ -33,10 +34,6 @@ func ProcessDir(path string, progress *CurrentProgress, ignore ShouldDirBeIgnore func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel chan bool, wait *sync.WaitGroup, ignoreDir ShouldDirBeIgnored) *File { var file *File var err error - path, err = filepath.Abs(path) - if err != nil { - log.Print(err.Error()) - } files, err := ioutil.ReadDir(path) if err != nil { @@ -45,7 +42,6 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel dir := File{ Name: filepath.Base(path), - Path: path, IsDir: true, ItemCount: 1, Files: make([]*File, 0, len(files)), @@ -76,7 +72,6 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel } else { file = &File{ Name: f.Name(), - Path: entryPath, Size: f.Size(), ItemCount: 1, Parent: &dir, diff --git a/analyze/file.go b/analyze/file.go index c7d175d65..e7a752595 100644 --- a/analyze/file.go +++ b/analyze/file.go @@ -2,12 +2,13 @@ package analyze import ( "os" + "path/filepath" ) // File struct type File struct { Name string - Path string + BasePath string Size int64 ItemCount int IsDir bool @@ -15,31 +16,17 @@ type File struct { Parent *File } -// Files - slice of pointers to File -type Files []*File - -// Find searches File in Files and returns its index, or -1 -func (s Files) Find(file *File) int { - for i, item := range s { - if item == file { - return i - } +// Path retruns absolute path of the file +func (f *File) Path() string { + if f.BasePath != "" { + return filepath.Join(f.BasePath, f.Name) } - return -1 -} - -// Remove removes File from Files -func (s Files) Remove(file *File) Files { - index := s.Find(file) - if index == -1 { - return s - } - return append(s[:index], s[index+1:]...) + return filepath.Join(f.Parent.Path(), f.Name) } // RemoveFile removes file from dir func (f *File) RemoveFile(file *File) { - error := os.RemoveAll(file.Path) + error := os.RemoveAll(file.Path()) if error != nil { panic(error) } @@ -73,3 +60,25 @@ func (f *File) UpdateStats() { f.ItemCount = itemCount + 1 f.Size = totalSize } + +// Files - slice of pointers to File +type Files []*File + +// Find searches File in Files and returns its index, or -1 +func (s Files) Find(file *File) int { + for i, item := range s { + if item == file { + return i + } + } + return -1 +} + +// Remove removes File from Files +func (s Files) Remove(file *File) Files { + index := s.Find(file) + if index == -1 { + return s + } + return append(s[:index], s[index+1:]...) +} diff --git a/analyze/file_test.go b/analyze/file_test.go index 364ab67b9..7d77dc4ca 100644 --- a/analyze/file_test.go +++ b/analyze/file_test.go @@ -61,6 +61,7 @@ func TestRemove(t *testing.T) { func TestRemoveFile(t *testing.T) { dir := &File{ Name: "xxx", + BasePath: ".", Size: 5, ItemCount: 3, } diff --git a/cli/cli.go b/cli/cli.go index 78d570060..e2f1269ce 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -167,7 +167,7 @@ func (ui *UI) ShouldDirBeIgnored(path string) bool { } func (ui *UI) showDir() { - ui.currentDirPath = ui.currentDir.Path + ui.currentDirPath = ui.currentDir.Path() ui.currentDirLabel.SetText("--- " + ui.currentDirPath + " ---") ui.table.Clear() diff --git a/cli/cli_test.go b/cli/cli_test.go index 7bd3b821c..e7e056ce9 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -22,6 +22,7 @@ func TestFooter(t *testing.T) { dir := analyze.File{ Name: "xxx", + BasePath: ".", Size: 5, ItemCount: 2, }