diff options
-rw-r--r-- | analyze/file.go | 52 | ||||
-rw-r--r-- | analyze/file_test.go | 19 | ||||
-rw-r--r-- | stdout/stdout_test.go | 15 |
3 files changed, 60 insertions, 26 deletions
diff --git a/analyze/file.go b/analyze/file.go index 72977f5..834cbc7 100644 --- a/analyze/file.go +++ b/analyze/file.go @@ -14,11 +14,11 @@ type Item interface { GetName() string GetFlag() rune IsDir() bool - GetMutliLinkInode() uint64 GetSize() int64 GetUsage() int64 GetItemCount() int GetParent() *Dir + getItemStats(links AlreadyCountedHardlinks) (int, int64, int64) } // File struct @@ -66,9 +66,25 @@ func (f *File) GetItemCount() int { return 1 } -// GetMutliLinkInode returns inode number of file with multiple hard links -func (f *File) GetMutliLinkInode() uint64 { - return f.Mli +func (f *File) alreadyCounted(links AlreadyCountedHardlinks) bool { + mli := f.Mli + if mli > 0 { + if !links[mli] { + links[mli] = true + return false + } else { + f.Flag = 'H' + return true + } + } + return false +} + +func (f *File) getItemStats(links AlreadyCountedHardlinks) (int, int64, int64) { + if f.alreadyCounted(links) { + return 1, 0, 0 + } + return 1, f.GetSize(), f.GetUsage() } // Dir struct @@ -102,13 +118,18 @@ func (f *Dir) GetPath() string { return filepath.Join(f.Parent.GetPath(), f.Name) } +func (f *Dir) getItemStats(links AlreadyCountedHardlinks) (int, int64, int64) { + f.UpdateStats(links) + return f.ItemCount, f.GetSize(), f.GetUsage() +} + // UpdateStats recursively updates size and item count func (f *Dir) UpdateStats(links AlreadyCountedHardlinks) { totalSize := int64(4096) totalUsage := int64(4096) var itemCount int for _, entry := range f.Files { - count, size, usage := getItemStats(entry, links) + count, size, usage := entry.getItemStats(links) totalSize += size totalUsage += usage itemCount += count @@ -125,27 +146,6 @@ func (f *Dir) UpdateStats(links AlreadyCountedHardlinks) { f.Usage = totalUsage } -func getItemStats(entry Item, links AlreadyCountedHardlinks) (int, int64, int64) { - itemCount := 1 - - if entry.IsDir() { - entry.(*Dir).UpdateStats(links) - itemCount = entry.(*Dir).ItemCount - } - - mli := entry.GetMutliLinkInode() - if mli > 0 { - if !links[mli] { - links[mli] = true - - } else { - entry.(*File).Flag = 'H' - return 1, 0, 0 - } - } - return itemCount, entry.GetSize(), entry.GetUsage() -} - // Files - slice of pointers to File type Files []Item diff --git a/analyze/file_test.go b/analyze/file_test.go index 4a81e1e..f38d5ad 100644 --- a/analyze/file_test.go +++ b/analyze/file_test.go @@ -8,6 +8,25 @@ import ( "github.com/stretchr/testify/assert" ) +func TestIsDir(t *testing.T) { + dir := Dir{ + File: &File{ + Name: "xxx", + Size: 5, + }, + ItemCount: 2, + } + file := &File{ + Name: "yyy", + Size: 2, + Parent: &dir, + } + dir.Files = Files{file} + + assert.True(t, dir.IsDir()) + assert.False(t, file.IsDir()) +} + func TestFind(t *testing.T) { dir := Dir{ File: &File{ diff --git a/stdout/stdout_test.go b/stdout/stdout_test.go index c4808ad..48f9c08 100644 --- a/stdout/stdout_test.go +++ b/stdout/stdout_test.go @@ -26,6 +26,21 @@ func TestAnalyzePath(t *testing.T) { assert.Contains(t, output.String(), "nested") } +func TestAnalyzeSubdir(t *testing.T) { + fin := testdir.CreateTestDir() + defer fin() + + buff := make([]byte, 10) + output := bytes.NewBuffer(buff) + + ui := CreateStdoutUI(output, false, false, false) + ui.SetIgnoreDirPaths([]string{"/xxx"}) + ui.AnalyzePath("test_dir/nested", nil) + ui.StartUILoop() + + assert.Contains(t, output.String(), "file2") +} + func TestAnalyzePathWithErr(t *testing.T) { buff := make([]byte, 10) output := bytes.NewBuffer(buff) |