file_linux.go 726 B

1234567891011121314151617181920212223242526
  1. /*
  2. * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. package z
  6. import (
  7. "fmt"
  8. )
  9. // Truncate would truncate the mmapped file to the given size. On Linux, we truncate
  10. // the underlying file and then call mremap, but on other systems, we unmap first,
  11. // then truncate, then re-map.
  12. func (m *MmapFile) Truncate(maxSz int64) error {
  13. if err := m.Sync(); err != nil {
  14. return fmt.Errorf("while sync file: %s, error: %v\n", m.Fd.Name(), err)
  15. }
  16. if err := m.Fd.Truncate(maxSz); err != nil {
  17. return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err)
  18. }
  19. var err error
  20. m.Data, err = mremap(m.Data, int(maxSz)) // Mmap up to max size.
  21. return err
  22. }