Module: Xtendr

Included in:
XtendrProxy
Defined in:
lib/xtendr.rb

Defined Under Namespace

Modules: FFI

Instance Method Summary collapse

Instance Method Details

#getx(attribute, *opts) { ... } ⇒ String?

Get the value of an extended attribute for the current object. Calls #to_s on self to get the path to the file the attribute is set on.

Parameters:

  • attribute (String)

    The extended attribute to retrieve.

Yields:

  • Block is run when the attribute is not found.

Returns:

  • (String, nil)

    The value of the attribute, or nil if it is not set and no block is passed.



22
23
24
25
26
# File 'lib/xtendr.rb', line 22

def getx(attribute, *opts)
  getx! attribute, *opts
rescue Errno::ENOATTR
  block_given? ? yield : nil
end

#getx!(attribute, *opts) ⇒ String

Get the value of an extended attribute, raising an error if the attribute is not found.

The bang version of getx works the the same way as getx, exept that when the attribute is not found. In that case, rather than returning nil, an ENOATTR error is raised.

Parameters:

  • attribute (String)

    The extended attribute to retrieve.

Returns:

  • (String)

    The value of the requested attribute.

Raises:

  • (Errno::ENOATTR)

    Describes the attribute that couldn't be located.



38
39
40
41
42
43
44
45
# File 'lib/xtendr.rb', line 38

def getx!(attribute, *opts)
  options = opts.include?(:no_follow) ? 1 : 0
  len = FFI.getxattr(self.to_s, attribute, nil, 0, 0, options)
  raise_error(attribute, ::FFI::LastError::error) if len < 0
  buffer = ::FFI::Buffer.alloc_out(len)
  FFI.getxattr(self.to_s, attribute, buffer, buffer.size, 0, options)
  buffer.get_string(0, buffer.size)
end

#listxArray<String>

List all of the extended attribute for the object.

Returns:

  • (Array<String>)

    the list of all keys which have extended attributes set.



78
79
80
81
82
83
# File 'lib/xtendr.rb', line 78

def listx
  len = FFI.listxattr(self.to_s, nil, 0, 0)
  buffer = ::FFI::Buffer.alloc_out(len)
  FFI.listxattr(self.to_s, buffer, buffer.size, 0)
  buffer.get_bytes(0, buffer.size).split("\0")
end

#remove_allxObject

Remove all extended file attributes from the object



94
95
96
97
98
# File 'lib/xtendr.rb', line 94

def remove_allx
  listx.each do |attribute|
    removex(attribute)
  end
end

#removex(attribute) ⇒ nil

Removes the given extended attribute from the filesystem object.

Parameters:

  • attribute (String)

    The attribute to remove the extended attribute for.

Returns:

  • (nil)


89
90
91
# File 'lib/xtendr.rb', line 89

def removex(attribute)
  FFI.removexattr(self.to_s, attribute, 0)
end

#setx(attribute, value, *opts) ⇒ nil

Set an extended attribute on a filesystem object. :create:: Raises an error if the attribute already exists.

Parameters:

  • attribute (String)

    The extended attribute to retrieve.

  • value (String)

    The value to set the extended attribute to.

  • opts (Array<:create, :replace, :no_follow>)

    Optional flags which influence how setting is done.

Returns:

  • (nil)


56
57
58
59
# File 'lib/xtendr.rb', line 56

def setx(attribute, value, *opts)
  retval = FFI.setxattr(self.to_s, attribute, value, value.size, 0, setx_options(*opts))
  raise_error(nil, ::FFI::LastError::error) if retval < 0
end