summaryrefslogtreecommitdiff
path: root/lib/chef/provider/file
diff options
context:
space:
mode:
authorLamont Granquist <lamont@opscode.com>2013-04-08 15:15:53 -0700
committerLamont Granquist <lamont@opscode.com>2013-04-08 15:15:53 -0700
commitb5890f29bd80e61374acb1c7969bcb961560b09c (patch)
treee834f43b8568121dfea17ca108c5aeae30517dec /lib/chef/provider/file
parent4e367cb684d2f52f3ab67345e40b08f9197cee27 (diff)
downloadchef-b5890f29bd80e61374acb1c7969bcb961560b09c.tar.gz
tempfiles: moar composition and binmode for windows
Diffstat (limited to 'lib/chef/provider/file')
-rw-r--r--lib/chef/provider/file/content.rb3
-rw-r--r--lib/chef/provider/file/tempfile.rb63
2 files changed, 65 insertions, 1 deletions
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 (<lamont@opscode.com>)
+# 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