Class: Xattr

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

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)
  options = ()
  result = _allocate_result(Raw.getxattr(@path, attribute, nil, 0, 0, options))
  _error(Raw.getxattr(@path, attribute, result, result.size, 0, options))
  result.to_s
end

#listObject

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
  options = ()
  result = _allocate_result(Raw.listxattr(@path, nil, 0, options))
  _error(Raw.listxattr(@path, result, result.size, options))
  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, ()))
  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, options={})
  opts = ()
  opts |= Raw::CREATE if options[:create]
  opts |= Raw::REPLACE if options[:replace]
  value = value.to_s
  _error(Raw.setxattr(@path, attribute, value, value.size, 0, opts))
  value
end