github.com/blend/go-sdk@v1.20220411.3/filelock/filelock_unix.go (about)

     1  //go:build darwin || dragonfly || freebsd || illumos || linux || netbsd || openbsd
     2  // +build darwin dragonfly freebsd illumos linux netbsd openbsd
     3  
     4  /*
     5  
     6  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     7  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     8  
     9  */
    10  
    11  package filelock
    12  
    13  import (
    14  	"io/fs"
    15  	"syscall"
    16  )
    17  
    18  type lockType int16
    19  
    20  const (
    21  	readLock  lockType = syscall.LOCK_SH
    22  	writeLock lockType = syscall.LOCK_EX
    23  )
    24  
    25  func lock(f File, lt lockType) (err error) {
    26  	for {
    27  		err = syscall.Flock(int(f.Fd()), int(lt))
    28  		if err != syscall.EINTR {
    29  			break
    30  		}
    31  	}
    32  	if err != nil {
    33  		return &fs.PathError{
    34  			Op:   lt.String(),
    35  			Path: f.Name(),
    36  			Err:  err,
    37  		}
    38  	}
    39  	return nil
    40  }
    41  
    42  func unlock(f File) error {
    43  	return lock(f, syscall.LOCK_UN)
    44  }
    45  
    46  func isNotSupported(err error) bool {
    47  	return err == syscall.ENOSYS || err == syscall.ENOTSUP || err == syscall.EOPNOTSUPP || err == ErrNotSupported
    48  }