Skip to content

Commit 6a11abb

Browse files
committed
Merge pull request #2193 from domcleal/tickets/2.7.x/pup-1255-default-file-mode
(PUP-1255) Fix assumed default file mode to 0644
2 parents 233e800 + 4e10a0a commit 6a11abb

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

lib/puppet/type/file.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,8 @@ def to_trans(retrieve = true)
736736
def write(property)
737737
remove_existing(:file)
738738

739-
assumed_default_mode = 0644
740-
741739
mode = self.should(:mode) # might be nil
742-
mode_int = mode ? symbolic_mode_to_int(mode, assumed_default_mode) : nil
740+
mode_int = mode ? symbolic_mode_to_int(mode, Puppet::Util::DEFAULT_POSIX_MODE) : nil
743741

744742
if write_temporary_file?
745743
Puppet::Util.replace_file(self[:path], mode_int) do |file|

lib/puppet/util.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,21 @@ def binread(file)
538538
# The default_mode is the mode to use when the target file doesn't already
539539
# exist; if the file is present we copy the existing mode/owner/group values
540540
# across.
541+
542+
DEFAULT_POSIX_MODE = 0644
543+
DEFAULT_WINDOWS_MODE = nil
544+
541545
def replace_file(file, default_mode, &block)
542546
raise Puppet::DevError, "replace_file requires a block" unless block_given?
543547

548+
unless default_mode
549+
if Puppet.features.microsoft_windows?
550+
default_mode = DEFAULT_WINDOWS_MODE
551+
else
552+
default_mode = DEFAULT_POSIX_MODE
553+
end
554+
end
555+
544556
file = Pathname(file)
545557
tempfile = Tempfile.new(file.basename.to_s, file.dirname.to_s)
546558

spec/unit/type/file_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,73 @@
11481148
expect { file.write :NOTUSED }.to_not raise_error(Puppet::Error)
11491149
end
11501150
end
1151+
1152+
describe "when resource mode is supplied" do
1153+
before { file.stubs(:property_fix) }
1154+
1155+
context "and writing temporary files" do
1156+
before { file.stubs(:write_temporary_file?).returns(true) }
1157+
1158+
it "should convert symbolic mode to int" do
1159+
file[:mode] = 'oga=r'
1160+
Puppet::Util.expects(:replace_file).with(file[:path], 0444)
1161+
file.write :NOTUSED
1162+
end
1163+
1164+
it "should support int modes" do
1165+
file[:mode] = '0444'
1166+
Puppet::Util.expects(:replace_file).with(file[:path], 0444)
1167+
file.write :NOTUSED
1168+
end
1169+
end
1170+
1171+
context "and not writing temporary files" do
1172+
before { file.stubs(:write_temporary_file?).returns(false) }
1173+
1174+
it "should set a umask of 0" do
1175+
file[:mode] = 'oga=r'
1176+
Puppet::Util.expects(:withumask).with(0)
1177+
file.write :NOTUSED
1178+
end
1179+
1180+
it "should convert symbolic mode to int" do
1181+
file[:mode] = 'oga=r'
1182+
File.expects(:open).with(file[:path], anything, 0444)
1183+
file.write :NOTUSED
1184+
end
1185+
1186+
it "should support int modes" do
1187+
file[:mode] = '0444'
1188+
File.expects(:open).with(file[:path], anything, 0444)
1189+
file.write :NOTUSED
1190+
end
1191+
end
1192+
end
1193+
1194+
describe "when resource mode is not supplied" do
1195+
context "and content is supplied" do
1196+
it "should default to 0644 mode" do
1197+
file = described_class.new(:path => path, :content => "file content")
1198+
1199+
file.write :NOTUSED
1200+
1201+
expect(File.stat(file[:path]).mode & 0777).to eq(0644)
1202+
end
1203+
end
1204+
1205+
context "and no content is supplied" do
1206+
it "should use puppet's default umask of 022" do
1207+
file = described_class.new(:path => path)
1208+
1209+
umask_from_the_user = 0777
1210+
Puppet::Util.withumask(umask_from_the_user) do
1211+
file.write :NOTUSED
1212+
end
1213+
1214+
expect(File.stat(file[:path]).mode & 0777).to eq(0644)
1215+
end
1216+
end
1217+
end
11511218
end
11521219

11531220
describe "#fail_if_checksum_is_wrong" do

0 commit comments

Comments
 (0)