...

Source file src/go.etcd.io/etcd/client/pkg/v3/fileutil/lock_flock.go

Documentation: go.etcd.io/etcd/client/pkg/v3/fileutil

     1  // Copyright 2016 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build !windows && !plan9 && !solaris
    16  // +build !windows,!plan9,!solaris
    17  
    18  package fileutil
    19  
    20  import (
    21  	"os"
    22  	"syscall"
    23  )
    24  
    25  func flockTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
    26  	f, err := os.OpenFile(path, flag, perm)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	if err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
    31  		f.Close()
    32  		if err == syscall.EWOULDBLOCK {
    33  			err = ErrLocked
    34  		}
    35  		return nil, err
    36  	}
    37  	return &LockedFile{f}, nil
    38  }
    39  
    40  func flockLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
    41  	f, err := os.OpenFile(path, flag, perm)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	if err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
    46  		f.Close()
    47  		return nil, err
    48  	}
    49  	return &LockedFile{f}, err
    50  }
    51  

View as plain text