Merge pull request #3430 from dmaluka/calchash-fixes

Various bugfixes and improvements around buffer md5 hash calculation and `fastdirty`
This commit is contained in:
Jöran Karl 2024-08-18 19:56:06 +02:00 committed by GitHub
commit f88ac6d4fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 30 deletions

View File

@ -64,10 +64,6 @@ var (
// BTStdout is a buffer that only writes to stdout
// when closed
BTStdout = BufType{6, false, true, true}
// ErrFileTooLarge is returned when the file is too large to hash
// (fastdirty is automatically enabled)
ErrFileTooLarge = errors.New("File is too large to hash")
)
// SharedBuffer is a struct containing info that is shared among buffers
@ -554,7 +550,11 @@ func (b *Buffer) ReOpen() error {
err = b.UpdateModTime()
if !b.Settings["fastdirty"].(bool) {
calcHash(b, &b.origHash)
if len(data) > LargeFileThreshold {
b.Settings["fastdirty"] = true
} else {
calcHash(b, &b.origHash)
}
}
b.isModified = false
b.RelocateCursors()
@ -649,37 +649,23 @@ func (b *Buffer) Size() int {
}
// calcHash calculates md5 hash of all lines in the buffer
func calcHash(b *Buffer, out *[md5.Size]byte) error {
func calcHash(b *Buffer, out *[md5.Size]byte) {
h := md5.New()
size := 0
if len(b.lines) > 0 {
n, e := h.Write(b.lines[0].data)
if e != nil {
return e
}
size += n
h.Write(b.lines[0].data)
for _, l := range b.lines[1:] {
n, e = h.Write([]byte{'\n'})
if e != nil {
return e
if b.Endings == FFDos {
h.Write([]byte{'\r', '\n'})
} else {
h.Write([]byte{'\n'})
}
size += n
n, e = h.Write(l.data)
if e != nil {
return e
}
size += n
h.Write(l.data)
}
}
if size > LargeFileThreshold {
return ErrFileTooLarge
}
h.Sum((*out)[:0])
return nil
}
func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight.Def {

View File

@ -1,6 +1,8 @@
package buffer
import (
"crypto/md5"
"github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/screen"
)
@ -10,10 +12,14 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
if option == "fastdirty" {
if !nativeValue.(bool) {
if !b.Modified() {
e := calcHash(b, &b.origHash)
if e == ErrFileTooLarge {
b.Settings["fastdirty"] = false
if b.Size() > LargeFileThreshold {
b.Settings["fastdirty"] = true
} else {
if !b.isModified {
calcHash(b, &b.origHash)
} else {
// prevent using an old stale origHash value
b.origHash = [md5.Size]byte{}
}
}
}