Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fixing named pipes and socket files on unix #62

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions pkg/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"io/fs"
"os"

"github.com/thushan/smash/internal/algorithms"
)
Expand Down Expand Up @@ -63,10 +64,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.
Expand All @@ -79,25 +98,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
Expand Down Expand Up @@ -139,16 +139,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
}

Expand Down Expand Up @@ -235,7 +232,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
}
Expand All @@ -247,3 +244,9 @@ 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 ||
fio.Mode()&os.ModeDevice != 0 ||
fio.Mode()&os.ModeSymlink != 0
}
Loading