From b5890f29bd80e61374acb1c7969bcb961560b09c Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 8 Apr 2013 15:15:53 -0700 Subject: tempfiles: moar composition and binmode for windows --- lib/chef/provider/file/content.rb | 3 +- lib/chef/provider/file/tempfile.rb | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 lib/chef/provider/file/tempfile.rb (limited to 'lib/chef/provider/file') diff --git a/lib/chef/provider/file/content.rb b/lib/chef/provider/file/content.rb index 2f7987e6e1..3b83555d7f 100644 --- a/lib/chef/provider/file/content.rb +++ b/lib/chef/provider/file/content.rb @@ -17,6 +17,7 @@ # require 'chef/provider/file_content_base' +require 'chef/provider/file/tempfile' class Chef class Provider @@ -24,7 +25,7 @@ class Chef class Content < Chef::Provider::FileContentBase def file_for_provider if @new_resource.content - tempfile = Tempfile.open(tempfile_basename, tempfile_dirname) + tempfile = Chef::Provider::File::Tempfile.new(@new_resource).tempfile tempfile.write(@new_resource.content) tempfile.close tempfile diff --git a/lib/chef/provider/file/tempfile.rb b/lib/chef/provider/file/tempfile.rb new file mode 100644 index 0000000000..42c4862159 --- /dev/null +++ b/lib/chef/provider/file/tempfile.rb @@ -0,0 +1,63 @@ +# +# Author:: Lamont Granquist () +# Copyright:: Copyright (c) 2013 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "tempfile" + +class Chef + class Provider + class File + class Tempfile + + attr_reader :new_resource + + def initialize(new_resource) + @new_resource = new_resource + end + + def tempfile + @tempfile ||= ::Tempfile.open(tempfile_basename, tempfile_dirname, tempfile_flags) + end + + private + + # + # These are important for windows to get permissions right, and may + # be useful for SELinux and other ACL approaches. Please use them + # as the arguments to Tempfile.new() consistently. + # + def tempfile_basename + basename = ::File.basename(@new_resource.name) + basename.insert 0, "." unless Chef::Platform.windows? # dotfile if we're not on windows + basename + end + + def tempfile_dirname + Chef::Config[:file_deployment_uses_destdir] ? ::File.dirname(@new_resource.path) : Dir::tmpdir + end + + def tempfile_flags + if new_resource.binmode + { :binmode => true } + else + {} + end + end + end + end + end +end -- cgit v1.2.1