summaryrefslogtreecommitdiff
path: root/lib/ffi-xattr/windows_lib.rb
blob: d4199ccb58aad1accaad80da23aba5c344d81add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# encoding: utf-8
require 'ffi-xattr'

class Xattr # :nodoc: all
  module Lib
    class << self

      def list(path, no_follow)
        lines = `dir /r "#{path}"`.split("\n")

        xattrs = []
        lines.each { |line|
          if line =~ /\:\$DATA$/
            size = line.split(' ')[0].gsub(/[^0-9]/,'').to_i

            if size > 0
              xattrs << line.split(':')[1]
            end
          end
        }
        xattrs
      end

      def get(path, no_follow, key)
        fp = "#{path}:#{key}"
        if File.exists?(fp)
          File.binread(fp)
        else
          raise "No such key. #{key.inspect} #{path.inspect}"
        end
      end

      def set(path, no_follow, key, value)
        File.open("#{path}:#{key}",'wb') { |io| io << value }
      end

      def remove(path, no_follow, key)
        # done this way because Windows have no function to remove Alternate Data Stream
        # quickest way is to set the value to 0 byte length instead of trying to create another file then apply the attributes, especially when dealing with a big file
        self.set(path, false, key, '')
      end
    end

  end
end