github.com/etecs-ru/ristretto@v0.9.1/z/file_default.go (about)

     1  //go:build !linux
     2  // +build !linux
     3  
     4  /*
     5   * Copyright 2020 Dgraph Labs, Inc. and Contributors
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package z
    21  
    22  import "fmt"
    23  
    24  // Truncate would truncate the mmapped file to the given size. On Linux, we truncate
    25  // the underlying file and then call mremap, but on other systems, we unmap first,
    26  // then truncate, then re-map.
    27  func (m *MmapFile) Truncate(maxSz int64) error {
    28  	if err := m.Sync(); err != nil {
    29  		return fmt.Errorf("while sync file: %s, error: %v\n", m.Fd.Name(), err)
    30  	}
    31  	if err := Munmap(m.Data); err != nil {
    32  		return fmt.Errorf("while munmap file: %s, error: %v\n", m.Fd.Name(), err)
    33  	}
    34  	if err := m.Fd.Truncate(maxSz); err != nil {
    35  		return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err)
    36  	}
    37  	var err error
    38  	m.Data, err = Mmap(m.Fd, true, maxSz) // Mmap up to max size.
    39  	return err
    40  }