From c380bfe234769165ba5c2e2d197dd1a1bdfa3e2b Mon Sep 17 00:00:00 2001 From: luke Date: Tue, 13 Jun 2006 17:26:32 +0000 Subject: Fixing the main bug reported on the list today relating to file sourcing truncating linked-to files. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1258 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/type/pfile.rb | 14 ++++++--- lib/puppet/type/pfile/source.rb | 4 +++ test/types/file.rb | 63 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb index 388e05388..7ff7576ab 100644 --- a/lib/puppet/type/pfile.rb +++ b/lib/puppet/type/pfile.rb @@ -103,12 +103,12 @@ module Puppet newvalues(:true, :false, :inf, /^[0-9]+$/) munge do |value| - value = super - case value + newval = super(value) + case newval when :true, :inf: true when :false: false else - value + newval end end end @@ -863,9 +863,15 @@ module Puppet def write(usetmp = true) mode = self.should(:mode) - if FileTest.exists?(self[:path]) + #if FileTest.exists?(self[:path]) + if s = stat(false) # this makes sure we have a copy for posterity @backed = self.handlebackup + + if s.ftype == "link" and self[:links] != :follow + # Remove existing links, since we're writing out a file + File.unlink(self[:path]) + end end # The temporary file diff --git a/lib/puppet/type/pfile/source.rb b/lib/puppet/type/pfile/source.rb index 0e63485a9..53e5d18b3 100755 --- a/lib/puppet/type/pfile/source.rb +++ b/lib/puppet/type/pfile/source.rb @@ -183,6 +183,10 @@ module Puppet # here doesn't really matter, because the @should values will be # overridden when we 'retrieve'. munge do |source| + if source.is_a? Symbol + return source + end + # Remove any trailing slashes source.sub!(/\/$/, '') unless @parent.uri2obj(source) diff --git a/test/types/file.rb b/test/types/file.rb index 69ffad85e..96a4aa617 100644 --- a/test/types/file.rb +++ b/test/types/file.rb @@ -1204,6 +1204,69 @@ class TestFile < Test::Unit::TestCase assert_equal(path, File.readlink(link), "Link was created incorrectly") end + + def test_replace_links_with_files + base = tempfile() + + Dir.mkdir(base) + + file = File.join(base, "file") + link = File.join(base, "link") + File.open(file, "w") { |f| f.puts "yayness" } + File.symlink(file, link) + + obj = Puppet::Type.type(:file).create( + :path => link, + :ensure => "file" + ) + + assert_apply(obj) + + assert_equal("yayness\n", File.read(file), + "Original file got changed") + assert_equal("file", File.lstat(link).ftype, "File is still a link") + end + + def test_no_erase_linkedto_files + base = tempfile() + + Dir.mkdir(base) + + dirs = {} + %w{other source target}.each do |d| + dirs[d] = File.join(base, d) + Dir.mkdir(dirs[d]) + end + + file = File.join(dirs["other"], "file") + sourcefile = File.join(dirs["source"], "sourcefile") + link = File.join(dirs["target"], "link") + + File.open(file, "w") { |f| f.puts "other" } + File.open(sourcefile, "w") { |f| f.puts "source" } + File.symlink(file, link) + + obj = Puppet::Type.type(:file).create( + :path => dirs["target"], + :ensure => "file", + :source => dirs["source"], + :recurse => true + ) + + + trans = assert_events([:file_created, :file_created], obj) + + newfile = File.join(dirs["target"], "sourcefile") + + assert(File.exists?(newfile), "File did not get copied") + + assert_equal(File.read(sourcefile), File.read(newfile), + "File did not get copied correctly.") + + assert_equal("other\n", File.read(file), + "Original file got changed") + assert_equal("file", File.lstat(link).ftype, "File is still a link") + end end # $Id$ -- cgit