gofrs / flock

Thread-safe file locking library in Go
https://pkg.go.dev/github.com/gofrs/flock
BSD 3-Clause "New" or "Revised" License
578 stars 66 forks source link

Add New(*os.File) #16

Closed theory closed 6 years ago

theory commented 6 years ago

I'm not wed to the name, but do want to have more control over the creation of the file handle.

theory commented 6 years ago

Wrong branch.

theckman commented 6 years ago

@theory FWIW, I wouldn't be against adding a New(*os.File) function.

theory commented 6 years ago

Hrm. I've been working on a project where I need to know if a file exists before I try to lock it. And if it doesn't exist, I don't want it created, which is what Flock does (os.O_CREATE|os.O_WRONLY).

I can think of three other ways to do this:

  1. Check for the existence of the file by trying to open it before I ask Flock to open it. Feels silly to have two file handles, but I can appreciate the idea to have a private one just for the lock.
  2. Add an interface to tell Flock what permissions are used, in case I want the lock to fail if the file doesn't exist. This would make it so I don't have to open the file until after I have the lock.
  3. As an extension of option 2, a way to get at the file handle might be useful, (GetFile()), but I can appreciate that one might not want to provide a potential footgun.

LOCKING IS HARD!