github.com/opentofu/opentofu@v1.7.1/internal/states/statemgr/filesystem_lock_unix.go (about)

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