github.com/rigado/snapd@v2.42.5-go-mod+incompatible/osutil/flock.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package osutil
    21  
    22  import (
    23  	"errors"
    24  	"os"
    25  	"syscall"
    26  )
    27  
    28  // FileLock describes a file system lock
    29  type FileLock struct {
    30  	file *os.File
    31  }
    32  
    33  var ErrAlreadyLocked = errors.New("cannot acquire lock, already locked")
    34  
    35  // NewFileLock creates and opens the lock file given by "path"
    36  func NewFileLock(path string) (*FileLock, error) {
    37  	mode := syscall.O_RDWR | syscall.O_CREAT | syscall.O_NOFOLLOW | syscall.O_CLOEXEC
    38  	file, err := os.OpenFile(path, mode, os.FileMode(0600))
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	l := &FileLock{file: file}
    43  	return l, nil
    44  }
    45  
    46  // Path returns the path of the lock file.
    47  func (l *FileLock) Path() string {
    48  	return l.file.Name()
    49  }
    50  
    51  // Close closes the lock, unlocking it automatically if needed.
    52  func (l *FileLock) Close() error {
    53  	return l.file.Close()
    54  }
    55  
    56  // Lock acquires an exclusive lock and blocks until the lock is free.
    57  func (l *FileLock) Lock() error {
    58  	return syscall.Flock(int(l.file.Fd()), syscall.LOCK_EX)
    59  }
    60  
    61  // TryLock acquires an exclusive lock and errors if the lock cannot be acquired.
    62  func (l *FileLock) TryLock() error {
    63  	err := syscall.Flock(int(l.file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
    64  	if err == syscall.EWOULDBLOCK {
    65  		return ErrAlreadyLocked
    66  	}
    67  	return err
    68  }
    69  
    70  // Unlock releases an acquired lock.
    71  func (l *FileLock) Unlock() error {
    72  	return syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN)
    73  }