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

Various bugfixes and improvements around buffer md5 hash calculation and fastdirty #3430

Merged
merged 7 commits into from
Aug 18, 2024

Commits on Aug 18, 2024

  1. calcHash: remove unneeded h.Write() error checks

    According to the Go hash package documentation [1]:
    
    type Hash interface {
    	// Write (via the embedded io.Writer interface) adds more data to the running hash.
    	// It never returns an error.
    	io.Writer
    
    [1] https://pkg.go.dev/hash#Hash
    dmaluka committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    e0f5361 View commit details
    Browse the repository at this point in the history
  2. calcHash: use correct line endings

    Make calcHash() respect the buffer's file endings (unix vs dos), to make
    its calculation of the file size consistent with how we calculate it in
    other cases (i.e. when opening or saving the file) and with the
    `fastdirty` option documentation, i.e. make calcHash() return
    ErrFileTooLarge if and only if the exact file size exceeds 50KB.
    dmaluka committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    c0f6b65 View commit details
    Browse the repository at this point in the history
  3. buffer/settings: On fastdirty off set to on in case of "large file"

    This behavior is then aligned to the actual documentation of `fastdirty`.
    Additionally set the origHash to zero in case the buffer was already modified.
    JoeKar authored and dmaluka committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    352fd2b View commit details
    Browse the repository at this point in the history
  4. buffer/settings: Don't use Modified() before we updated origHash

    When we have already enabled `fastdirty` but have not updated origHash
    yet, we shouldn't use Modified() since it depends on origHash which is
    still outdated, and thus returns wrong values.
    
    This fixes the following issue: enable `fastdirty`, modify the buffer,
    save the buffer and disable `fastdirty` -> micro wrongly reports the
    buffer as modified (whereas it has just been saved).
    
    Note that this fix, though, also causes a regression: e.g. if we run
    `set fastdirty false` while fastdirty is already disabled, micro may
    unexpectedly report a non-modified buffer as modified (in the case if
    isModified is true but the buffer it actually not modified, since its
    md5 sum matches and fastdirty is disabled), since this fix assumes that
    since we are disabling fastdirty, it has been enabled. This shall be
    fixed by PR zyedidia#3343 which makes `set` do nothing if the option value
    doesn't change.
    dmaluka committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    93efc9e View commit details
    Browse the repository at this point in the history
  5. 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`.
    dmaluka committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    7fe98cc View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    d31095f View commit details
    Browse the repository at this point in the history
  7. buffer: Set fastdirty=true for large file when reopening

    Similarly to how we force `fastdirty` to true when opening a large file
    (when creating the buffer), force it also when reopening a file, in case
    the file on disk became large since we opened it.
    dmaluka committed Aug 18, 2024
    Configuration menu
    Copy the full SHA
    0b15b57 View commit details
    Browse the repository at this point in the history