Skip to content

Commit

Permalink
Make FlockFile function private to not expose it for misuse. (#3204)
Browse files Browse the repository at this point in the history
* Make FlockFile function private to not expose it for misuse.

* Update pr number in changelog

* fix changelog

Co-authored-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
dragotin and butonic authored Sep 8, 2022
1 parent 48f76f9 commit 7bea458
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 8 additions & 0 deletions changelog/unreleased/makeflockfileprivate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Enhancement: Make the function flockFile private

Having that function exported is tempting people to use the func
to get the name for calling the lock functions. That is wrong, as
this function is just a helper to generate the lock file name from
a given file to lock.

https://github.com/cs3org/reva/pull/3204
10 changes: 7 additions & 3 deletions pkg/storage/utils/filelocks/filelocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func acquireLock(file string, write bool) (*flock.Flock, error) {
var err error

// Create a file to carry the log
n := FlockFile(file)
n := flockFile(file)
if len(n) == 0 {
return nil, errors.New("lock path is empty")
}
Expand Down Expand Up @@ -107,9 +107,9 @@ func acquireLock(file string, write bool) (*flock.Flock, error) {
return flock, nil
}

// FlockFile returns the flock filename for a given file name
// flockFile returns the flock filename for a given file name
// it returns an empty string if the input is empty
func FlockFile(file string) string {
func flockFile(file string) string {
var n string
if len(file) > 0 {
n = file + ".flock"
Expand All @@ -119,12 +119,16 @@ func FlockFile(file string) string {

// AcquireReadLock tries to acquire a shared lock to read from the
// file and returns a lock object or an error accordingly.
// Call with the file to lock. This function creates .lock file next
// to it.
func AcquireReadLock(file string) (*flock.Flock, error) {
return acquireLock(file, false)
}

// AcquireWriteLock tries to acquire a shared lock to write from the
// file and returns a lock object or an error accordingly.
// Call with the file to lock. This function creates an extra .lock
// file next to it.
func AcquireWriteLock(file string) (*flock.Flock, error) {
return acquireLock(file, true)
}
Expand Down

0 comments on commit 7bea458

Please sign in to comment.