file_linux.go raw

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