Skip to content

Commit 7fe98cc

Browse files
committed
calcHash: Remove checking file size
Let calcHash() unconditionally hash whatever buffer it is asked to hash, and let its callers explicitly check if the buffer is too large before calling calcHash(). This makes things simpler and less error-prone (no extra source of truth about whether the file is too large, we don't need to remember to check if calcHash() fails, we can be sure calcHash() will actually update the provided hash), and actually faster (since just calculating the buffer size, i.e. adding line lengths, is faster than md5 calculation). In particular, this fixes the following bugs: 1. Since ReOpen() doesn't check calcHash() return value, if the reloaded file is too large while the old version of the file is not, calcHash() returns ErrFileTooLarge and doesn't update origHash, so so Modified() returns true since the reloaded file's md5 sum doesn't match the old origHash, so micro wrongly reports the newly reloaded file as modified. 2. Since Modified() doesn't check calcHash() return value, Modified() may return false positives or false negatives if the buffer has *just* become too large so calcHash() returns ErrFileTooLarge and doesn't update `buff`.
1 parent 93efc9e commit 7fe98cc

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

internal/buffer/buffer.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ var (
6464
// BTStdout is a buffer that only writes to stdout
6565
// when closed
6666
BTStdout = BufType{6, false, true, true}
67-
68-
// ErrFileTooLarge is returned when the file is too large to hash
69-
// (fastdirty is automatically enabled)
70-
ErrFileTooLarge = errors.New("File is too large to hash")
7167
)
7268

7369
// SharedBuffer is a struct containing info that is shared among buffers
@@ -649,32 +645,23 @@ func (b *Buffer) Size() int {
649645
}
650646

651647
// calcHash calculates md5 hash of all lines in the buffer
652-
func calcHash(b *Buffer, out *[md5.Size]byte) error {
648+
func calcHash(b *Buffer, out *[md5.Size]byte) {
653649
h := md5.New()
654650

655-
size := 0
656651
if len(b.lines) > 0 {
657-
n, _ := h.Write(b.lines[0].data)
658-
size += n
652+
h.Write(b.lines[0].data)
659653

660654
for _, l := range b.lines[1:] {
661655
if b.Endings == FFDos {
662-
n, _ = h.Write([]byte{'\r', '\n'})
656+
h.Write([]byte{'\r', '\n'})
663657
} else {
664-
n, _ = h.Write([]byte{'\n'})
658+
h.Write([]byte{'\n'})
665659
}
666-
size += n
667-
n, _ = h.Write(l.data)
668-
size += n
660+
h.Write(l.data)
669661
}
670662
}
671663

672-
if size > LargeFileThreshold {
673-
return ErrFileTooLarge
674-
}
675-
676664
h.Sum((*out)[:0])
677-
return nil
678665
}
679666

680667
func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight.Def {

internal/buffer/settings.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
1212

1313
if option == "fastdirty" {
1414
if !nativeValue.(bool) {
15-
if !b.isModified {
16-
e := calcHash(b, &b.origHash)
17-
if e == ErrFileTooLarge {
18-
b.Settings["fastdirty"] = true
19-
}
15+
if b.Size() > LargeFileThreshold {
16+
b.Settings["fastdirty"] = true
2017
} else {
21-
b.origHash = [md5.Size]byte{}
22-
if b.Size() > LargeFileThreshold {
23-
b.Settings["fastdirty"] = true
18+
if !b.isModified {
19+
calcHash(b, &b.origHash)
20+
} else {
21+
b.origHash = [md5.Size]byte{}
2422
}
2523
}
2624
}

0 commit comments

Comments
 (0)