|
| 1 | +require 'etc' |
| 2 | + |
| 3 | +# class to use in openssl providers to handle file permission (mode, group and owner) |
| 4 | +class Puppet::Provider::Openssl < Puppet::Provider |
| 5 | + include Puppet::Util::POSIX |
| 6 | + |
| 7 | + def owner |
| 8 | + if File.exist?(@resource[:path]) |
| 9 | + Etc.getpwuid(File.stat(@resource[:path]).uid).name |
| 10 | + else |
| 11 | + :absent |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + def owner=(should) |
| 16 | + File.send(:chown, uid(should), nil, resource[:path]) |
| 17 | + rescue => detail |
| 18 | + raise Puppet::Error, _("Failed to set owner to '%{should}': %{detail}") % { should: should, detail: detail }, detail.backtrace |
| 19 | + end |
| 20 | + |
| 21 | + def group |
| 22 | + if File.exist?(@resource[:path]) |
| 23 | + Etc.getgrgid(File.stat(@resource[:path]).gid).name |
| 24 | + else |
| 25 | + :absent |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def group=(should) |
| 30 | + File.send(:chown, nil, gid(should), resource[:path]) |
| 31 | + rescue => detail |
| 32 | + raise Puppet::Error, _("Failed to set group to '%{should}': %{detail}") % { should: should, detail: detail }, detail.backtrace |
| 33 | + end |
| 34 | + |
| 35 | + # Return the mode as an octal string, not as an integer. |
| 36 | + def mode |
| 37 | + if File.exist?(@resource[:path]) |
| 38 | + '0%o' % (File.stat(@resource[:path]).mode & 0o07777) |
| 39 | + else |
| 40 | + :absent |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + # Set the file mode, converting from a string to an integer. |
| 45 | + def mode=(should) |
| 46 | + File.chmod(Integer('0' + should), @resource[:path]) |
| 47 | + end |
| 48 | + |
| 49 | + def set_file_perm(filename, owner = nil, group = nil, mode = nil) |
| 50 | + File.send(:chown, uid(owner), nil, filename) if owner |
| 51 | + File.send(:chown, nil, gid(group), filename) if group |
| 52 | + File.chmod(Integer('0' + mode), filename) if mode |
| 53 | + end |
| 54 | +end |
0 commit comments