package fsutil import ( "os" "path/filepath" ) // Lock takes an exclusive lock on path, creating it if needed, and blocks // until it gets it. The lock is shared with other processes. The returned // function releases it. func Lock(path string) (func(), error) { if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return nil, err } f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600) if err != nil { return nil, err } if err := lockFile(f); err != nil { _ = f.Close() return nil, err } return func() { _ = unlockFile(f) _ = f.Close() }, nil }