diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2015-12-01 22:41:06 +0000 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2015-12-01 22:41:06 +0000 |
commit | 668b9871e64cb82ac30c8defb29d56d774f3c140 (patch) | |
tree | 9b4bba7cbcbd2660485cf803402c4d4889e62b10 /lib/ffi-xattr.rb | |
parent | 182a03798d49a3c0450b0f137977037cf9376e99 (diff) |
Completely re-vamped restore command. Fixes #12403
The byteback-restore command now uses the rsync xattrs to display
information about the files due to be restored. It can handle filenames
with spaces (!) and other characters.
Diffstat (limited to 'lib/ffi-xattr.rb')
-rw-r--r-- | lib/ffi-xattr.rb | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/ffi-xattr.rb b/lib/ffi-xattr.rb new file mode 100644 index 0000000..2dd1e23 --- /dev/null +++ b/lib/ffi-xattr.rb @@ -0,0 +1,78 @@ +require 'ffi' +require 'ffi-xattr/version' +require 'ffi-xattr/error' + +case RUBY_PLATFORM +when /linux/ + require 'ffi-xattr/linux_lib' +when /darwin|bsd/ + require 'ffi-xattr/darwin_lib' +when /mingw/ + require 'ffi-xattr/windows_lib' +else + raise NotImplementedError, "ffi-xattr not supported on #{RUBY_PLATFORM}" +end + +class Xattr + include Enumerable + + # Create a new Xattr instance with path. + # Use <tt>:no_follow => true</tt> in options to work on symlink itself instead of following it. + def initialize(path, options = {}) + @path = + if path.respond_to?(:to_path) + path.to_path + elsif path.respond_to?(:to_str) + path.to_str + else + path + end + raise Errno::ENOENT, @path unless File.exist?(@path) + + @no_follow = !!options[:no_follow] + end + + # List extended attribute names + def list + Lib.list @path, @no_follow + end + + # Get an extended attribute value + def get(key) + Lib.get @path, @no_follow, key.to_s + end + alias_method :[], :get + + # Set an extended attribute value + def set(key, value) + Lib.set @path, @no_follow, key.to_s, value.to_s + end + alias_method :[]=, :set + + # Remove an extended attribute value + def remove(key) + Lib.remove @path, @no_follow, key.to_s + end + + # Iterates over pairs of extended attribute names and values + def each(&blk) + list.each do |key| + yield key, get(key) + end + end + + # Returns hash of extended attributes + + def to_hash + res = {} + each { |k,v| res[k] = v } + + res + end + + alias_method :to_h, :to_hash + + def as_json(*args) + to_hash + end +end |