Skip to content

Commit

Permalink
xattrs: use unix flocks when writing multiple xattrs
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Feb 10, 2022
1 parent 9c927f9 commit e2c0a09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/flock-xattr-set-multiple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: use an exclcusive write lock when writing multiple attributes

The xattr package can use an exclusive write lock when writing multiple extended attributes

https://github.com/cs3org/reva/pull/2528
20 changes: 17 additions & 3 deletions pkg/storage/utils/decomposedfs/xattrs/xattrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
package xattrs

import (
"os"
"strings"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/pkg/errors"
"github.com/pkg/xattr"
"golang.org/x/sys/unix"
)

// Declare a list of xattr keys
Expand Down Expand Up @@ -139,12 +141,24 @@ func Set(filePath string, key string, val string) error {
}

// SetMultiple allows setting multiple key value pairs at once
// TODO the changes are protected with an flock
// the changes are protected with an flock
func SetMultiple(filePath string, attribs map[string]string) error {

// FIXME: Lock here
// h, err := lockedfile.OpenFile(filePath, os.O_WRONLY, 0) // 0? Open File only workn for files ... but we want to lock dirs ... or symlinks
// or we append .lock to the file and use https://github.com/gofrs/flock
h, err := os.Open(filePath)
if err != nil {
return err
}
defer h.Close()
err = unix.Flock(int(h.Fd()), unix.LOCK_EX)
if err != nil {
return err
}
defer unix.Flock(int(h.Fd()), unix.LOCK_UN)

for key, val := range attribs {
if err := Set(filePath, key, val); err != nil {
if err := xattr.FSet(h, key, []byte(val)); err != nil {
return err
}
}
Expand Down

0 comments on commit e2c0a09

Please sign in to comment.