vitess.io/vitess@v0.16.2/go/test/endtoend/filelock/filelock_unix.go (about)

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