From b06ac02617573109f851730486367c223e167169 Mon Sep 17 00:00:00 2001 From: sk91 Date: Mon, 7 Nov 2022 12:40:59 +0200 Subject: [PATCH 1/3] feat: add an option to configure lock file permissions --- filemutex_flock.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/filemutex_flock.go b/filemutex_flock.go index a71fe8d..31a5037 100644 --- a/filemutex_flock.go +++ b/filemutex_flock.go @@ -2,11 +2,17 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris // +build darwin dragonfly freebsd linux netbsd openbsd solaris package filemutex -import "golang.org/x/sys/unix" +import ( + "os" + "path/filepath" + + "golang.org/x/sys/unix" +) const ( mkdirPerm = 0750 @@ -19,7 +25,19 @@ type FileMutex struct { } func New(filename string) (*FileMutex, error) { - fd, err := unix.Open(filename, unix.O_CREAT|unix.O_RDONLY, mkdirPerm) + return new(filename, mkdirPerm) +} + +func NewWithUnixFilePerm(filename string, perm uint32) (*FileMutex, error) { + return new(filename, perm) +} + +func new(filename string, perm uint32) (*FileMutex, error) { + dir := filepath.Dir(filename) + if err := os.MkdirAll(dir, os.FileMode(perm)); err != nil { + return nil, err + } + fd, err := unix.Open(filename, unix.O_CREAT|unix.O_RDONLY, perm) if err != nil { return nil, err } From bab025f75c73cd1ffdcbbc1add8bd1512dde429c Mon Sep 17 00:00:00 2001 From: sk91 Date: Wed, 9 Nov 2022 12:48:40 +0200 Subject: [PATCH 2/3] fix: constructor naming and perm type --- filemutex_flock.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/filemutex_flock.go b/filemutex_flock.go index 31a5037..3024c2c 100644 --- a/filemutex_flock.go +++ b/filemutex_flock.go @@ -15,7 +15,7 @@ import ( ) const ( - mkdirPerm = 0750 + mkdirPerm os.FileMode = 0750 ) // FileMutex is similar to sync.RWMutex, but also synchronizes across processes. @@ -28,16 +28,16 @@ func New(filename string) (*FileMutex, error) { return new(filename, mkdirPerm) } -func NewWithUnixFilePerm(filename string, perm uint32) (*FileMutex, error) { +func NewWithMode(filename string, perm os.FileMode) (*FileMutex, error) { return new(filename, perm) } -func new(filename string, perm uint32) (*FileMutex, error) { +func new(filename string, perm os.FileMode) (*FileMutex, error) { dir := filepath.Dir(filename) if err := os.MkdirAll(dir, os.FileMode(perm)); err != nil { return nil, err } - fd, err := unix.Open(filename, unix.O_CREAT|unix.O_RDONLY, perm) + fd, err := unix.Open(filename, unix.O_CREAT|unix.O_RDONLY, uint32(perm)) if err != nil { return nil, err } From 55ffc3a026e24648a9b777eaee5903605fa54c09 Mon Sep 17 00:00:00 2001 From: sk91 Date: Wed, 9 Nov 2022 16:15:48 +0200 Subject: [PATCH 3/3] remove mkdir all --- filemutex_flock.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/filemutex_flock.go b/filemutex_flock.go index 3024c2c..36e5d6c 100644 --- a/filemutex_flock.go +++ b/filemutex_flock.go @@ -9,7 +9,6 @@ package filemutex import ( "os" - "path/filepath" "golang.org/x/sys/unix" ) @@ -33,10 +32,6 @@ func NewWithMode(filename string, perm os.FileMode) (*FileMutex, error) { } func new(filename string, perm os.FileMode) (*FileMutex, error) { - dir := filepath.Dir(filename) - if err := os.MkdirAll(dir, os.FileMode(perm)); err != nil { - return nil, err - } fd, err := unix.Open(filename, unix.O_CREAT|unix.O_RDONLY, uint32(perm)) if err != nil { return nil, err