github.com/bcskill/bcschain/v3@v3.4.9-beta2/flock/flock.go (about)

     1  package flock
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  )
     7  
     8  type Releaser interface {
     9  	Release() error
    10  }
    11  
    12  // New locks the file with the provided name. If the file does not exist, it is
    13  // created. The returned Releaser is used to release the lock. existed is true
    14  // if the file to lock already existed. A non-nil error is returned if the
    15  // locking has failed. Neither this function nor the returned Releaser is
    16  // goroutine-safe.
    17  func New(fileName string) (r Releaser, existed bool, err error) {
    18  	if err = os.MkdirAll(filepath.Dir(fileName), 0755); err != nil {
    19  		return
    20  	}
    21  
    22  	_, err = os.Stat(fileName)
    23  	existed = err == nil
    24  
    25  	r, err = newLock(fileName)
    26  	return
    27  }