fix: do not read whole file on listings

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
pull/607/head
Henrique Dias 2019-01-06 09:24:09 +00:00
parent c830b80269
commit 28b326ab1d
1 changed files with 12 additions and 9 deletions

View File

@ -76,7 +76,7 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) {
return file, file.readListing(opts.Checker)
}
err = file.detectType(opts.Modify)
err = file.detectType(opts.Modify, true)
if err != nil {
return nil, err
}
@ -126,7 +126,7 @@ func (i *FileInfo) Checksum(algo string) error {
return nil
}
func (i *FileInfo) detectType(modify bool) error {
func (i *FileInfo) detectType(modify, saveContent bool) error {
reader, err := i.Fs.Open(i.Path)
if err != nil {
return err
@ -160,18 +160,21 @@ func (i *FileInfo) detectType(modify bool) error {
return nil
default:
i.Type = "text"
if !modify {
i.Type = "textImmutable"
}
if saveContent {
afs := &afero.Afero{Fs: i.Fs}
content, err := afs.ReadFile(i.Path)
if err != nil {
return err
}
if !modify {
i.Type = "textImmutable"
}
i.Content = string(content)
}
}
return nil
}
@ -238,7 +241,7 @@ func (i *FileInfo) readListing(checker rules.Checker) error {
} else {
listing.NumFiles++
err := file.detectType(true)
err := file.detectType(true, false)
if err != nil {
return err
}