From aeedf88b296fe974788503721f6ab54470d41fd3 Mon Sep 17 00:00:00 2001 From: Thushan Fernando Date: Wed, 7 Feb 2024 17:36:04 +1100 Subject: [PATCH 1/2] Slicer tweaks. --- pkg/slicer/slicer.go | 64 ++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/pkg/slicer/slicer.go b/pkg/slicer/slicer.go index a327427..c468d1b 100644 --- a/pkg/slicer/slicer.go +++ b/pkg/slicer/slicer.go @@ -3,10 +3,10 @@ package slicer import ( "encoding/gob" "errors" + "github.com/thushan/smash/internal/algorithms" "io" "io/fs" - - "github.com/thushan/smash/internal/algorithms" + "os" ) type Slicer struct { @@ -63,10 +63,28 @@ func NewConfigured(algorithm algorithms.Algorithm, slices int, size, threshold u defaultBytes: []byte{}, } } -func (slicer *Slicer) SliceFS(fs fs.FS, name string, options *Options) (SlicerStats, error) { +func (slicer *Slicer) SliceFS(fileSystem fs.FS, name string, options *Options) (SlicerStats, error) { stats := SlicerStats{Hash: slicer.defaultBytes, Filename: name} - f, err := fs.Open(name) + fio, ferr := fs.Stat(fileSystem, name) + + if ferr != nil { + return stats, ferr + } + + size := uint64(fio.Size()) + isEmptyFile := size == 0 + + if !shouldAnalyseBasedOnSize(size, options.MinSize, options.MaxSize) || + shouldIgnoreFileMode(fio) || + isEmptyFile { + stats.IgnoredFile = true + stats.EmptyFile = isEmptyFile + stats.Hash = nil + return stats, nil + } + + f, err := fileSystem.Open(name) defer func(fs io.Closer) { if fs == nil { // Ignore ReadOnly issues. @@ -79,25 +97,6 @@ func (slicer *Slicer) SliceFS(fs fs.FS, name string, options *Options) (SlicerSt return stats, err } - fi, err := f.Stat() - - if err != nil { - return stats, err - } - - size := uint64(fi.Size()) - - if size == 0 { - stats.EmptyFile = true - stats.Hash = nil - return stats, nil - } - - if !shouldAnalyse(size, options.MinSize, options.MaxSize) { - stats.IgnoredFile = true - return stats, nil - } - stats.FileSize = size stats.Slices = slicer.slices stats.SliceSize = slicer.sliceSize @@ -139,16 +138,13 @@ func (slicer *Slicer) Slice(sr *io.SectionReader, options *Options, stats *Slice */ size := uint64(sr.Size()) + isEmptyFile := size == 0 - if size == 0 { - // Zero byte file, nothing we can do - stats.EmptyFile = true - stats.Hash = nil - return nil - } - - if !shouldAnalyse(size, options.MinSize, options.MaxSize) { + if !shouldAnalyseBasedOnSize(size, options.MinSize, options.MaxSize) || + isEmptyFile { stats.IgnoredFile = true + stats.EmptyFile = isEmptyFile + stats.Hash = nil return nil } @@ -235,7 +231,7 @@ func (slicer *Slicer) Slice(sr *io.SectionReader, options *Options, stats *Slice stats.Hash = algo.Sum(nil) return nil } -func shouldAnalyse(fileSize, minSize, maxSize uint64) bool { +func shouldAnalyseBasedOnSize(fileSize, minSize, maxSize uint64) bool { if minSize == DefaultMinSize && maxSize == DefaultMaxSize { return true } @@ -247,3 +243,7 @@ func shouldAnalyse(fileSize, minSize, maxSize uint64) bool { } return true } +func shouldIgnoreFileMode(fio os.FileInfo) bool { + return fio.Mode()&os.ModeNamedPipe != 0 || + fio.Mode()&os.ModeSocket != 0 +} From 9cf4ce3142e212a5d126ecfc60f5e352172be829 Mon Sep 17 00:00:00 2001 From: Thushan Fernando Date: Wed, 7 Feb 2024 17:54:07 +1100 Subject: [PATCH 2/2] Lint --- pkg/slicer/slicer.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/slicer/slicer.go b/pkg/slicer/slicer.go index c468d1b..2fbe2d4 100644 --- a/pkg/slicer/slicer.go +++ b/pkg/slicer/slicer.go @@ -3,10 +3,11 @@ package slicer import ( "encoding/gob" "errors" - "github.com/thushan/smash/internal/algorithms" "io" "io/fs" "os" + + "github.com/thushan/smash/internal/algorithms" ) type Slicer struct { @@ -245,5 +246,7 @@ func shouldAnalyseBasedOnSize(fileSize, minSize, maxSize uint64) bool { } func shouldIgnoreFileMode(fio os.FileInfo) bool { return fio.Mode()&os.ModeNamedPipe != 0 || - fio.Mode()&os.ModeSocket != 0 + fio.Mode()&os.ModeSocket != 0 || + fio.Mode()&os.ModeDevice != 0 || + fio.Mode()&os.ModeSymlink != 0 }