Skip to content

Commit

Permalink
Merge pull request #1336 from kolyshkin/lockfile-nits
Browse files Browse the repository at this point in the history
pkg/lockfile: nits
  • Loading branch information
giuseppe committed Sep 12, 2022
2 parents 4e33c65 + 7d52f17 commit b630454
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions pkg/lockfile/lockfile_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,17 @@ func newLastWriterID() []byte {
}

// openLock opens the file at path and returns the corresponding file
// descriptor. Note that the path is opened read-only when ro is set. If ro
// is unset, openLock will open the path read-write and create the file if
// necessary.
// descriptor. The path is opened either read-only or read-write,
// depending on the value of ro argument.
//
// openLock will create the file and its parent directories,
// if necessary.
func openLock(path string, ro bool) (fd int, err error) {
if ro {
fd, err = unix.Open(path, os.O_RDONLY|unix.O_CLOEXEC|os.O_CREATE, 0)
} else {
fd, err = unix.Open(path,
os.O_RDWR|unix.O_CLOEXEC|os.O_CREATE,
unix.S_IRUSR|unix.S_IWUSR|unix.S_IRGRP|unix.S_IROTH,
)
flags := os.O_RDONLY | unix.O_CLOEXEC | os.O_CREATE
if !ro {
flags |= os.O_RDWR
}

fd, err = unix.Open(path, flags, 0o644)
if err == nil {
return
}
Expand All @@ -91,7 +89,7 @@ func openLock(path string, ro bool) (fd int, err error) {
return openLock(path, ro)
}

return
return fd, &os.PathError{Op: "open", Path: path, Err: err}
}

// createLockerForPath returns a Locker object, possibly (depending on the platform)
Expand Down Expand Up @@ -158,7 +156,7 @@ func (l *lockfile) lock(lType int16, recursive bool) {
// If we're the first reference on the lock, we need to open the file again.
fd, err := openLock(l.file, l.ro)
if err != nil {
panic(fmt.Sprintf("error opening %q: %v", l.file, err))
panic(err)
}
l.fd = uintptr(fd)

Expand Down

0 comments on commit b630454

Please sign in to comment.