github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/vfs/default_unix.go (about)

     1  // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  //go:build darwin || dragonfly || freebsd || netbsd || openbsd || solaris
     6  // +build darwin dragonfly freebsd netbsd openbsd solaris
     7  
     8  package vfs
     9  
    10  import (
    11  	"os"
    12  	"syscall"
    13  
    14  	"github.com/cockroachdb/errors"
    15  )
    16  
    17  func wrapOSFileImpl(osFile *os.File) File {
    18  	return &unixFile{File: osFile, fd: osFile.Fd()}
    19  }
    20  
    21  func (defaultFS) OpenDir(name string) (File, error) {
    22  	f, err := os.OpenFile(name, syscall.O_CLOEXEC, 0)
    23  	if err != nil {
    24  		return nil, errors.WithStack(err)
    25  	}
    26  	return &unixFile{f, InvalidFd}, nil
    27  }
    28  
    29  // Assert that unixFile implements vfs.File.
    30  var _ File = (*unixFile)(nil)
    31  
    32  type unixFile struct {
    33  	*os.File
    34  	fd uintptr
    35  }
    36  
    37  func (*unixFile) Prefetch(offset int64, length int64) error { return nil }
    38  func (*unixFile) Preallocate(offset, length int64) error    { return nil }
    39  
    40  func (f *unixFile) SyncData() error {
    41  	return f.Sync()
    42  }
    43  
    44  func (f *unixFile) SyncTo(int64) (fullSync bool, err error) {
    45  	if err = f.Sync(); err != nil {
    46  		return false, err
    47  	}
    48  	return true, nil
    49  }