summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/file
diff options
context:
space:
mode:
authorDavid Schmitt <david@dasz.at>2010-05-11 18:30:49 +0200
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commita90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d (patch)
tree46348d616d5661ea363d18b8d06666aa449f1957 /lib/puppet/provider/file
parentb51be2809a81e36fee603693f9d70155c49e94fd (diff)
downloadpuppet-a90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d.tar.gz
puppet-a90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d.tar.xz
puppet-a90bcb08c3dc0a3c777f8a6c7d75cb6533a8804d.zip
Start extracting the owner managment for files into providers
Diffstat (limited to 'lib/puppet/provider/file')
-rw-r--r--lib/puppet/provider/file/posix.rb109
-rw-r--r--lib/puppet/provider/file/win32.rb78
2 files changed, 187 insertions, 0 deletions
diff --git a/lib/puppet/provider/file/posix.rb b/lib/puppet/provider/file/posix.rb
new file mode 100644
index 000000000..131fd547e
--- /dev/null
+++ b/lib/puppet/provider/file/posix.rb
@@ -0,0 +1,109 @@
+Puppet::Type.type(:file).provide :posix do
+ desc "Uses POSIX functionality to manage file's users and rights."
+
+ confine :feature => :posix
+
+ include Puppet::Util::POSIX
+ include Puppet::Util::Warnings
+
+ require 'etc'
+
+ def id2name(id)
+ return id.to_s if id.is_a?(Symbol)
+ return nil if id > Puppet[:maximum_uid].to_i
+
+ begin
+ user = Etc.getpwuid(id)
+ rescue TypeError
+ return nil
+ rescue ArgumentError
+ return nil
+ end
+
+ if user.uid == ""
+ return nil
+ else
+ return user.name
+ end
+ end
+
+ def insync?(current, should)
+ return true unless should
+
+ should.each do |value|
+ if value =~ /^\d+$/
+ uid = Integer(value)
+ elsif value.is_a?(String)
+ fail "Could not find user %s" % value unless uid = uid(value)
+ else
+ uid = value
+ end
+
+ return true if uid == current
+ end
+
+ unless Puppet::Util::SUIDManager.uid == 0
+ warnonce "Cannot manage ownership unless running as root"
+ return true
+ end
+
+ return false
+ end
+
+ # Determine if the user is valid, and if so, return the UID
+ def validuser?(value)
+ begin
+ number = Integer(value)
+ return number
+ rescue ArgumentError
+ number = nil
+ end
+ if number = uid(value)
+ return number
+ else
+ return false
+ end
+ end
+
+ def retrieve(resource)
+ unless stat = resource.stat(false)
+ return :absent
+ end
+
+ currentvalue = stat.uid
+
+ # On OS X, files that are owned by -2 get returned as really
+ # large UIDs instead of negative ones. This isn't a Ruby bug,
+ # it's an OS X bug, since it shows up in perl, too.
+ if currentvalue > Puppet[:maximum_uid].to_i
+ self.warning "Apparently using negative UID (%s) on a platform that does not consistently handle them" % currentvalue
+ currentvalue = :silly
+ end
+
+ return currentvalue
+ end
+
+ def sync(path, links, should)
+ # Set our method appropriately, depending on links.
+ if links == :manage
+ method = :lchown
+ else
+ method = :chown
+ end
+
+ uid = nil
+ should.each do |user|
+ break if uid = validuser?(user)
+ end
+
+ raise Puppet::Error, "Could not find user(s) %s" % @should.join(",") unless uid
+
+ begin
+ File.send(method, uid, nil, path)
+ rescue => detail
+ raise Puppet::Error, "Failed to set owner to '%s': %s" % [uid, detail]
+ end
+
+ return :file_changed
+ end
+end
diff --git a/lib/puppet/provider/file/win32.rb b/lib/puppet/provider/file/win32.rb
new file mode 100644
index 000000000..45a36f213
--- /dev/null
+++ b/lib/puppet/provider/file/win32.rb
@@ -0,0 +1,78 @@
+Puppet::Type.type(:file).provide :win32 do
+ desc "Uses Win32 functionality to manage file's users and rights."
+
+ confine :feature => :win32
+
+ include Puppet::Util::Warnings
+
+ require 'sys/admin'
+
+ def id2name(id)
+ return id.to_s if id.is_a?(Symbol)
+ return nil if id > Puppet[:maximum_uid].to_i
+ # should translate ID numbers to usernames
+ return id
+ end
+
+ def insync?(current, should)
+ return true unless should
+
+ should.each do |value|
+ if value =~ /^\d+$/
+ uid = Integer(value)
+ elsif value.is_a?(String)
+ fail "Could not find user %s" % value unless uid = uid(value)
+ else
+ uid = value
+ end
+
+ return true if uid == current
+ end
+
+ unless Puppet::Util::SUIDManager.uid == 0
+ warnonce "Cannot manage ownership unless running as root"
+ return true
+ end
+
+ return false
+ end
+
+ # Determine if the user is valid, and if so, return the UID
+ def validuser?(value)
+ info "Is '%s' a valid user?" % value
+ return 0
+ begin
+ number = Integer(value)
+ return number
+ rescue ArgumentError
+ number = nil
+ end
+ if number = uid(value)
+ return number
+ else
+ return false
+ end
+ end
+
+ def retrieve(resource)
+ unless stat = resource.stat(false)
+ return :absent
+ end
+
+ currentvalue = stat.uid
+
+ # On OS X, files that are owned by -2 get returned as really
+ # large UIDs instead of negative ones. This isn't a Ruby bug,
+ # it's an OS X bug, since it shows up in perl, too.
+ if currentvalue > Puppet[:maximum_uid].to_i
+ self.warning "Apparently using negative UID (%s) on a platform that does not consistently handle them" % currentvalue
+ currentvalue = :silly
+ end
+
+ return currentvalue
+ end
+
+ def sync(path, links, should)
+ info("should set '%s'%%owner to '%s'" % [path, should])
+ end
+end