forked from snowyu/xattr.go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ IsXattrExists and - remove 'user.' prefix for linux.
- Loading branch information
Showing
1 changed file
with
52 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,83 @@ | ||
package xattr | ||
|
||
/* | ||
import ( | ||
"strings" | ||
"strings" | ||
) | ||
const ( | ||
userPrefix = "user." | ||
userPrefix = "user." | ||
) | ||
// Linux xattrs have a manditory prefix of "user.". This is prepended | ||
// transparently for Get/Set/Remove and hidden in List | ||
*/ | ||
|
||
|
||
func IsXattrExists(path, name string) bool { | ||
_, err := getxattr(path, name, nil, 0) | ||
return err == nil | ||
} | ||
|
||
// Retrieve extended attribute data associated with path. | ||
func Getxattr(path, name string) ([]byte, error) { | ||
name = userPrefix + name | ||
// find size. | ||
size, err := getxattr(path, name, nil, 0) | ||
if err != nil { | ||
return nil, &XAttrError{"getxattr", path, name, err} | ||
} | ||
buf := make([]byte, size) | ||
// Read into buffer of that size. | ||
read, err := getxattr(path, name, &buf[0], size) | ||
if err != nil { | ||
return nil, &XAttrError{"getxattr", path, name, err} | ||
} | ||
return buf[:read], nil | ||
//name = userPrefix + name | ||
// find size. | ||
size, err := getxattr(path, name, nil, 0) | ||
if err != nil { | ||
return nil, &XAttrError{"getxattr", path, name, err} | ||
} | ||
buf := make([]byte, size) | ||
// Read into buffer of that size. | ||
read, err := getxattr(path, name, &buf[0], size) | ||
if err != nil { | ||
return nil, &XAttrError{"getxattr", path, name, err} | ||
} | ||
return buf[:read], nil | ||
} | ||
|
||
// Retrieves a list of names of extended attributes associated with the | ||
// given path in the file system. | ||
func Listxattr(path string) ([]string, error) { | ||
// find size. | ||
size, err := listxattr(path, nil, 0) | ||
if err != nil { | ||
return nil, &XAttrError{"listxattr", path, "", err} | ||
} | ||
buf := make([]byte, size) | ||
// Read into buffer of that size. | ||
read, err := listxattr(path, &buf[0], size) | ||
if err != nil { | ||
return nil, &XAttrError{"listxattr", path, "", err} | ||
} | ||
return stripUserPrefix(nullTermToStrings(buf[:read])), nil | ||
// find size. | ||
size, err := listxattr(path, nil, 0) | ||
if err != nil { | ||
return nil, &XAttrError{"listxattr", path, "", err} | ||
} | ||
buf := make([]byte, size) | ||
// Read into buffer of that size. | ||
read, err := listxattr(path, &buf[0], size) | ||
if err != nil { | ||
return nil, &XAttrError{"listxattr", path, "", err} | ||
} | ||
return nullTermToStrings(buf[:read]), nil | ||
//return stripUserPrefix(nullTermToStrings(buf[:read])), nil | ||
} | ||
|
||
// Associates name and data together as an attribute of path. | ||
func Setxattr(path, name string, data []byte) error { | ||
name = userPrefix + name | ||
if err := setxattr(path, name, &data[0], len(data)); err != nil { | ||
return &XAttrError{"setxattr", path, name, err} | ||
} | ||
return nil | ||
//name = userPrefix + name | ||
if err := setxattr(path, name, &data[0], len(data)); err != nil { | ||
return &XAttrError{"setxattr", path, name, err} | ||
} | ||
return nil | ||
} | ||
|
||
// Remove the attribute. | ||
func Removexattr(path, name string) error { | ||
name = userPrefix + name | ||
if err := removexattr(path, name); err != nil { | ||
return &XAttrError{"removexattr", path, name, err} | ||
} | ||
return nil | ||
//name = userPrefix + name | ||
if err := removexattr(path, name); err != nil { | ||
return &XAttrError{"removexattr", path, name, err} | ||
} | ||
return nil | ||
} | ||
|
||
// Strip off "user." prefixes from attribute names. | ||
func stripUserPrefix(s []string) []string { | ||
for i, a := range s { | ||
if strings.HasPrefix(a, userPrefix) { | ||
s[i] = a[5:] | ||
} | ||
} | ||
return s | ||
for i, a := range s { | ||
if strings.HasPrefix(a, userPrefix) { | ||
s[i] = a[5:] | ||
} | ||
} | ||
return s | ||
} |