github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/vfs/file_lock_unix.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
    16  
    17  package vfs
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  	"sync"
    25  	"syscall"
    26  )
    27  
    28  var lockedFiles struct {
    29  	mu struct {
    30  		sync.Mutex
    31  		files map[string]bool
    32  	}
    33  }
    34  
    35  // lockCloser hides all of an os.File's methods, except for Close.
    36  type lockCloser struct {
    37  	name string
    38  	f    *os.File
    39  }
    40  
    41  func (l lockCloser) Close() error {
    42  	lockedFiles.mu.Lock()
    43  	defer lockedFiles.mu.Unlock()
    44  	if !lockedFiles.mu.files[l.name] {
    45  		fmt.Printf("panic: lock file %q is not locked\n", l.name)
    46  	}
    47  	delete(lockedFiles.mu.files, l.name)
    48  
    49  	return l.f.Close()
    50  }
    51  
    52  func (defaultFS) Lock(name string) (io.Closer, error) {
    53  	lockedFiles.mu.Lock()
    54  	defer lockedFiles.mu.Unlock()
    55  	if lockedFiles.mu.files == nil {
    56  		lockedFiles.mu.files = map[string]bool{}
    57  	}
    58  	if lockedFiles.mu.files[name] {
    59  		return nil, errors.New("lock held by current process")
    60  	}
    61  
    62  	f, err := os.Create(name)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	spec := syscall.Flock_t{
    67  		Type:   syscall.F_WRLCK,
    68  		Whence: io.SeekStart,
    69  		Start:  0,
    70  		Len:    0, // 0 means to lock the entire file.
    71  		Pid:    int32(os.Getpid()),
    72  	}
    73  	if err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &spec); err != nil {
    74  		f.Close()
    75  		return nil, err
    76  	}
    77  	lockedFiles.mu.files[name] = true
    78  	return lockCloser{name, f}, nil
    79  }