github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/states/statemgr/filesystem_lock_unix.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  //go:build !windows
     5  // +build !windows
     6  
     7  package statemgr
     8  
     9  import (
    10  	"io"
    11  	"log"
    12  	"syscall"
    13  )
    14  
    15  // use fcntl POSIX locks for the most consistent behavior across platforms, and
    16  // hopefully some campatibility over NFS and CIFS.
    17  func (s *Filesystem) lock() error {
    18  	log.Printf("[TRACE] statemgr.Filesystem: locking %s using fcntl flock", s.path)
    19  	flock := &syscall.Flock_t{
    20  		Type:   syscall.F_RDLCK | syscall.F_WRLCK,
    21  		Whence: int16(io.SeekStart),
    22  		Start:  0,
    23  		Len:    0,
    24  	}
    25  
    26  	fd := s.stateFileOut.Fd()
    27  	return syscall.FcntlFlock(fd, syscall.F_SETLK, flock)
    28  }
    29  
    30  func (s *Filesystem) unlock() error {
    31  	log.Printf("[TRACE] statemgr.Filesystem: unlocking %s using fcntl flock", s.path)
    32  	flock := &syscall.Flock_t{
    33  		Type:   syscall.F_UNLCK,
    34  		Whence: int16(io.SeekStart),
    35  		Start:  0,
    36  		Len:    0,
    37  	}
    38  
    39  	fd := s.stateFileOut.Fd()
    40  	return syscall.FcntlFlock(fd, syscall.F_SETLK, flock)
    41  }