1+ // +build linux darwin
2+
13package continuity
24
35import (
46 "fmt"
57 "sort"
8+
9+ "github.com/stevvooe/continuity/sysx"
610)
711
812// Getxattr returns all of the extended attributes for the file at path p.
913func (d * driver ) Getxattr (p string ) (map [string ][]byte , error ) {
10- xattrs , err := Listxattr (p )
14+ xattrs , err := sysx . Listxattr (p )
1115 if err != nil {
1216 return nil , fmt .Errorf ("listing %s xattrs: %v" , p , err )
1317 }
@@ -16,9 +20,9 @@ func (d *driver) Getxattr(p string) (map[string][]byte, error) {
1620 m := make (map [string ][]byte , len (xattrs ))
1721
1822 for _ , attr := range xattrs {
19- value , err := Getxattr (p , attr )
23+ value , err := sysx . Getxattr (p , attr )
2024 if err != nil {
21- return nil , fmt .Errorf ("getting %q xattr on %s: %v" , p , attr , err )
25+ return nil , fmt .Errorf ("getting %q xattr on %s: %v" , attr , p , err )
2226 }
2327
2428 // NOTE(stevvooe): This append/copy tricky relies on unique
@@ -34,6 +38,52 @@ func (d *driver) Getxattr(p string) (map[string][]byte, error) {
3438// any symbolic links, if necessary. All attributes on the target are
3539// replaced by the values from attr. If the operation fails to set any
3640// attribute, those already applied will not be rolled back.
37- func (d * driver ) Setxattr (path string , attr map [string ][]byte ) error {
38- panic ("not implemented" )
41+ func (d * driver ) Setxattr (path string , attrMap map [string ][]byte ) error {
42+ for attr , value := range attrMap {
43+ if err := sysx .Setxattr (path , attr , value , 0 ); err != nil {
44+ return fmt .Errorf ("error setting xattr %q on %s: %v" , attr , path , err )
45+ }
46+ }
47+
48+ return nil
49+ }
50+
51+ // LGetxattr returns all of the extended attributes for the file at path p
52+ // not following symbolic links.
53+ func (d * driver ) LGetxattr (p string ) (map [string ][]byte , error ) {
54+ xattrs , err := sysx .LListxattr (p )
55+ if err != nil {
56+ return nil , fmt .Errorf ("listing %s xattrs: %v" , p , err )
57+ }
58+
59+ sort .Strings (xattrs )
60+ m := make (map [string ][]byte , len (xattrs ))
61+
62+ for _ , attr := range xattrs {
63+ value , err := sysx .LGetxattr (p , attr )
64+ if err != nil {
65+ return nil , fmt .Errorf ("getting %q xattr on %s: %v" , attr , p , err )
66+ }
67+
68+ // NOTE(stevvooe): This append/copy tricky relies on unique
69+ // xattrs. Break this out into an alloc/copy if xattrs are no
70+ // longer unique.
71+ m [attr ] = append (m [attr ], value ... )
72+ }
73+
74+ return m , nil
75+ }
76+
77+ // LSetxattr sets all of the extended attributes on file at path, not
78+ // following any symbolic links. All attributes on the target are
79+ // replaced by the values from attr. If the operation fails to set any
80+ // attribute, those already applied will not be rolled back.
81+ func (d * driver ) LSetxattr (path string , attrMap map [string ][]byte ) error {
82+ for attr , value := range attrMap {
83+ if err := sysx .LSetxattr (path , attr , value , 0 ); err != nil {
84+ return fmt .Errorf ("error setting xattr %q on %s: %v" , attr , path , err )
85+ }
86+ }
87+
88+ return nil
3989}
0 commit comments