diff options
author | Luke Kanies <luke@madstop.com> | 2005-07-12 05:51:43 +0000 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2005-07-12 05:51:43 +0000 |
commit | 99a9f4b0c16b42ea16e1df9e38714697215fc308 (patch) | |
tree | 1c18cf5996eb0e412aba4a4752db363a0d79fabb | |
parent | d6e2102643ea380c5e7b63a722dae31ed499571d (diff) | |
download | puppet-99a9f4b0c16b42ea16e1df9e38714697215fc308.tar.gz puppet-99a9f4b0c16b42ea16e1df9e38714697215fc308.tar.xz puppet-99a9f4b0c16b42ea16e1df9e38714697215fc308.zip |
adding link functionality
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@376 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/type/pfile.rb | 76 | ||||
-rw-r--r-- | test/types/tc_file.rb | 33 |
2 files changed, 106 insertions, 3 deletions
diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb index 5ff0432cd..e8f50e74b 100644 --- a/lib/puppet/type/pfile.rb +++ b/lib/puppet/type/pfile.rb @@ -13,7 +13,6 @@ module Puppet class State class PFileCreate < Puppet::State require 'etc' - attr_accessor :file @name = :create @event = :file_created @@ -160,9 +159,79 @@ module Puppet end end + class PFileLink < Puppet::State + require 'etc' + attr_reader :link + + @name = :link + + # create the link + def self.create(file,link) + begin + debug("Creating symlink '%s' to '%s'" % [file, link]) + unless File.symlink(file,link) + raise TypeError.new("Could not create symlink '%s'" % link) + end + rescue => detail + raise TypeError.new("Cannot create symlink '%s': %s" % + [file,detail]) + end + end + + # remove an existing link + def self.remove(link) + if FileTest.symlink?(link) + debug("Removing symlink '%s'" % link) + begin + File.unlink(link) + rescue + raise TypeError.new("Failed to remove symlink '%s'" % link) + end + elsif FileTest.exists?(link) + raise TypeError.new("Cannot remove normal file '%s'" % link) + else + debug("Symlink '%s' does not exist" % link) + end + end + + def retrieve + if FileTest.symlink?(@link) + self.is = File.readlink(@link) + return + else + self.is = nil + return + end + end + + # we know the link should exist, but it should also point back + # to us + def should=(link) + @link = link + @should = self.parent[:path] + + # unless we're fully qualified or we've specifically allowed + # relative links. Relative links are currently disabled, until + # someone actually asks for them + unless @should =~ /^\// or self.parent[:relativelinks] + @should = File.expand_path @should + end + end + + # this is somewhat complicated, because it could exist and be + # a file + def sync + if @is + self.class.remove(@is) + end + self.class.create(@should,@link) + + return :link_created + end + end + class PFileUID < Puppet::State require 'etc' - attr_accessor :file @name = :owner @event = :inode_changed @@ -371,7 +440,8 @@ module Puppet Puppet::State::PFileGroup, Puppet::State::PFileMode, Puppet::State::PFileChecksum, - Puppet::State::PFileSetUID + Puppet::State::PFileSetUID, + Puppet::State::PFileLink, ] @parameters = [ diff --git a/test/types/tc_file.rb b/test/types/tc_file.rb index 6fd502fef..32a67cf41 100644 --- a/test/types/tc_file.rb +++ b/test/types/tc_file.rb @@ -137,6 +137,39 @@ class TestFile < Test::Unit::TestCase } end + # just test normal links + def test_normal_links + link = "/tmp/puppetlink" + assert_nothing_raised() { + @file[:link] = link + } + # assert we got a fully qualified link + assert(@file.state(:link).should =~ /^\//) + + # assert we aren't linking to ourselves + assert(File.expand_path(@file.state(:link).link) != + File.expand_path(@file[:path])) + + # assert the should value does point to us + assert_equal(File.expand_path(@file.state(:link).should), + File.expand_path(@file[:path])) + + assert_nothing_raised() { + @file.evaluate + } + assert_nothing_raised() { + @file.sync + } + assert_nothing_raised() { + @file.evaluate + } + assert(@file.insync?()) + assert_nothing_raised() { + @file.delete(:link) + } + system("rm -f %s" % link) + end + def test_checksums types = %w{md5 md5lite timestamp ctime} files = %w{/tmp/sumtest} |