Class: OSX::ACL

Inherits:
Object
  • Object
show all
Defined in:
lib/acl.rb,
lib/acl/entry.rb,
lib/acl/version.rb,
lib/acl/assignment.rb

Defined Under Namespace

Classes: Assignment, Entries, Entry

Constant Summary collapse

VERSION =
"1.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



21
22
23
# File 'lib/acl.rb', line 21

def entries
  @entries
end

#pathObject

Returns the value of attribute path.



21
22
23
# File 'lib/acl.rb', line 21

def path
  @path
end

Class Method Details

.of(path) ⇒ Object



23
24
25
# File 'lib/acl.rb', line 23

def self.of(path)
  new.tap {|acl| acl.path = path }
end

Instance Method Details

#apiObject



131
132
133
# File 'lib/acl.rb', line 131

def api
  API
end

#entry_linesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/acl.rb', line 63

def entry_lines
  file_descriptor, acl_text_ptr, acl_ptr = nil
  begin
    file_descriptor = File.open(path, "r")
  rescue Errno::ELOOP
    return []
  rescue Errno::EOPNOTSUPP
    return []
  rescue Errno::ENOENT
    return []
  end
  acl_ptr = api.acl_get_fd(file_descriptor.fileno)
  acl_text_ptr = api.acl_to_text(acl_ptr, nil)
  return [] if acl_text_ptr.null?
  ace_lines = acl_text_ptr.read_string.split("\n")[1..-1]
  ace_lines
ensure
  api.acl_free(acl_text_ptr)
  api.acl_free(acl_ptr)
  file_descriptor.close if file_descriptor
end

#file_flagsObject



93
94
95
96
97
98
99
# File 'lib/acl.rb', line 93

def file_flags
  flags = ""
  Open3.popen3("stat", "-f", "%f", path) do |stdin,stdout,stderr,thread|
    flags = stdout.read
  end
  flags.to_i.to_s(8)
end

#make_entriesObject



31
32
33
# File 'lib/acl.rb', line 31

def make_entries
  Entries.new(self, entry_lines.map {|line| ACL::Entry.from_text(line) })
end

#orphansObject



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

def orphans
  entries.select {|e| e.orphaned? }
end

#preserving_flagsObject

Wraps a file action

first removes file flags that would cause the action to fail
then yields to the block to perform the action
then restores the flags


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/acl.rb', line 105

def preserving_flags
  original_file_flags = file_flags
  if original_file_flags == "0"
    yield
  else
    begin
      system("chflags", "0", path)
      yield
    ensure
      system("chflags", original_file_flags, path)
    end
  end
end

#remove_entry_at_index(index) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/acl.rb', line 119

def remove_entry_at_index(index)
  args = ["chmod", "-a#", index.to_s, path]
  puts "#{args[0]} #{args[1]} #{args[2]} #{Shellwords.escape(args[3])} # #{entries[index].to_s}"
  if ENV['OSX_ACL_NOOP'] == "yes"
    true
  else
    preserving_flags do
      system(*args)
    end
  end
end

#remove_orphans!Object



85
86
87
# File 'lib/acl.rb', line 85

def remove_orphans!
  entries.remove_if {|entry| entry.orphaned? }
end