...

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

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

     1  // Copyright 2015 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 solaris
    16  // +build solaris
    17  
    18  package fileutil
    19  
    20  import (
    21  	"os"
    22  	"syscall"
    23  )
    24  
    25  func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
    26  	var lock syscall.Flock_t
    27  	lock.Start = 0
    28  	lock.Len = 0
    29  	lock.Pid = 0
    30  	lock.Type = syscall.F_WRLCK
    31  	lock.Whence = 0
    32  	lock.Pid = 0
    33  	f, err := os.OpenFile(path, flag, perm)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	if err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &lock); err != nil {
    38  		f.Close()
    39  		if err == syscall.EAGAIN {
    40  			err = ErrLocked
    41  		}
    42  		return nil, err
    43  	}
    44  	return &LockedFile{f}, nil
    45  }
    46  
    47  func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
    48  	var lock syscall.Flock_t
    49  	lock.Start = 0
    50  	lock.Len = 0
    51  	lock.Pid = 0
    52  	lock.Type = syscall.F_WRLCK
    53  	lock.Whence = 0
    54  	f, err := os.OpenFile(path, flag, perm)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	if err = syscall.FcntlFlock(f.Fd(), syscall.F_SETLKW, &lock); err != nil {
    59  		f.Close()
    60  		return nil, err
    61  	}
    62  	return &LockedFile{f}, nil
    63  }
    64  

View as plain text