Class: Xattr
- Inherits:
-
Object
- Object
- Xattr
- Defined in:
- lib/xattr.rb
Overview
Extended attributes extend the basic attributes of files and directories in the file system. They are stored as name:data pairs associated with file system objects (files, directories, symlinks, etc).
Defined Under Namespace
Modules: Raw
Constant Summary collapse
- VERSION =
"0.1"
Instance Attribute Summary collapse
-
#follow_symlinks ⇒ Object
Should we follow symlinks? #set, #get, #list, and #remove normally operate on the target of the path if it is a symbolic link.
Instance Method Summary collapse
-
#get(attribute) ⇒ Object
Get an attribute See
man 2 getxattr
for a synopsis of errors that may be raised. -
#initialize(path) ⇒ Xattr
constructor
A new instance of Xattr.
-
#list ⇒ Object
Return an Array of all attributes.
-
#remove(attribute) ⇒ Object
Remove an attribute See
man 2 removexattr
for a synopsis of errors that may be raised. -
#set(attribute, value, options = {}) ⇒ Object
Set an attribute (with options) Valid key => value pairs for
options:Hash
: *:create
=>true
||false
: fail if the named attribute already exists.
Constructor Details
#initialize(path) ⇒ Xattr
Returns a new instance of Xattr.
33 34 35 36 |
# File 'lib/xattr.rb', line 33 def initialize(path) @path = path @follow_symlinks = true end |
Instance Attribute Details
#follow_symlinks ⇒ Object
Should we follow symlinks? #set, #get, #list, and #remove normally operate on the target of the path if it is a symbolic link. If #follow_symlinks is false they will act on the link itself.
41 42 43 |
# File 'lib/xattr.rb', line 41 def follow_symlinks @follow_symlinks end |
Instance Method Details
#get(attribute) ⇒ Object
Get an attribute
See man 2 getxattr
for a synopsis of errors that may be raised.
56 57 58 59 60 61 |
# File 'lib/xattr.rb', line 56 def get(attribute) = _follow_symlinks_option() result = _allocate_result(Raw.getxattr(@path, attribute, nil, 0, 0, )) _error(Raw.getxattr(@path, attribute, result, result.size, 0, )) result.to_s end |
#list ⇒ Object
Return an Array of all attributes
See man 2 listxattr
for a synopsis of errors that may be raised.
46 47 48 49 50 51 |
# File 'lib/xattr.rb', line 46 def list = _follow_symlinks_option() result = _allocate_result(Raw.listxattr(@path, nil, 0, )) _error(Raw.listxattr(@path, result, result.size, )) result.to_str.split("\000") end |
#remove(attribute) ⇒ Object
Remove an attribute
See man 2 removexattr
for a synopsis of errors that may be raised.
89 90 91 92 93 |
# File 'lib/xattr.rb', line 89 def remove(attribute) value = get(attribute) _error(Raw.removexattr(@path, attribute, _follow_symlinks_option())) value end |
#set(attribute, value, options = {}) ⇒ Object
Set an attribute (with options)
Valid key => value pairs for options:Hash
:
-
:create
=>true
||false
: fail if the named attribute already exists. Default=false
-
:replace
=>true
||false
: fail if the named attribute does not exist. Default=false
Failure to specify :create
or :replace
allows creation and replacement.
See man 2 setxattr
for a synopsis of errors that may be raised.
76 77 78 79 80 81 82 83 |
# File 'lib/xattr.rb', line 76 def set(attribute, value, ={}) opts = _follow_symlinks_option() opts |= Raw::CREATE if [:create] opts |= Raw::REPLACE if [:replace] value = value.to_s _error(Raw.setxattr(@path, attribute, value, value.size, 0, opts)) value end |