Skip to content

Commit 7f2e140

Browse files
authored
Merge pull request #8810 from joshcooper/chmod_11345
(PUP-11345) Assert path in Puppet::FileSystem.chmod
2 parents 72a6751 + a7000d5 commit 7f2e140

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

lib/puppet/file_system.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def self.exclusive_create(path, mode, &block)
396396
# @api public
397397
#
398398
def self.chmod(mode, path)
399-
@impl.chmod(mode, path)
399+
@impl.chmod(mode, assert_path(path))
400400
end
401401

402402
# Replace the contents of a file atomically, creating the file if necessary.

lib/puppet/file_system/file_impl.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,23 @@ def symlink(path, dest, options = {})
130130
end
131131

132132
def symlink?(path)
133-
File.symlink?(path)
133+
::File.symlink?(path)
134134
end
135135

136136
def readlink(path)
137-
File.readlink(path)
137+
::File.readlink(path)
138138
end
139139

140140
def unlink(*paths)
141-
File.unlink(*paths)
141+
::File.unlink(*paths)
142142
end
143143

144144
def stat(path)
145-
File.stat(path)
145+
::File.stat(path)
146146
end
147147

148148
def lstat(path)
149-
File.lstat(path)
149+
::File.lstat(path)
150150
end
151151

152152
def compare_stream(path, stream)
@@ -159,7 +159,7 @@ def chmod(mode, path)
159159

160160
def replace_file(path, mode = nil)
161161
begin
162-
stat = Puppet::FileSystem.lstat(path)
162+
stat = lstat(path)
163163
gid = stat.gid
164164
uid = stat.uid
165165
mode ||= stat.mode & 07777
@@ -180,7 +180,7 @@ def replace_file(path, mode = nil)
180180
tempfile_path = tempfile.path
181181
FileUtils.chown(uid, gid, tempfile_path) if uid && gid
182182
chmod(mode, tempfile_path)
183-
File.rename(tempfile_path, Puppet::FileSystem.path_string(path))
183+
::File.rename(tempfile_path, path_string(path))
184184
ensure
185185
tempfile.close!
186186
end

lib/puppet/file_system/jruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def unlink(*paths)
1414
def replace_file(path, mode = nil, &block)
1515
# MRI Ruby rename checks if destination is a directory and raises, while
1616
# JRuby removes the directory and replaces the file.
17-
if Puppet::FileSystem.directory?(path)
17+
if directory?(path)
1818
raise Errno::EISDIR, _("Is a directory: %{directory}") % { directory: path }
1919
end
2020

lib/puppet/file_system/windows.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def read_preserve_line_endings(path)
123123
LOCK_VIOLATION = 33
124124

125125
def replace_file(path, mode = nil)
126-
if Puppet::FileSystem.directory?(path)
126+
if directory?(path)
127127
raise Errno::EISDIR, _("Is a directory: %{directory}") % { directory: path }
128128
end
129129

@@ -159,14 +159,14 @@ def replace_file(path, mode = nil)
159159
end
160160

161161
set_dacl(tempfile.path, dacl) if dacl
162-
File.rename(tempfile.path, Puppet::FileSystem.path_string(path))
162+
::File.rename(tempfile.path, path_string(path))
163163
ensure
164164
tempfile.close!
165165
end
166166
rescue Puppet::Util::Windows::Error => e
167167
case e.code
168168
when ACCESS_DENIED, SHARING_VIOLATION, LOCK_VIOLATION
169-
raise Errno::EACCES.new(Puppet::FileSystem.path_string(path), e)
169+
raise Errno::EACCES.new(path_string(path), e)
170170
else
171171
raise SystemCallError.new(e.message)
172172
end
@@ -193,7 +193,7 @@ def secure_dacl(current_sid)
193193
end
194194

195195
def get_dacl_from_file(path)
196-
sd = Puppet::Util::Windows::Security.get_security_descriptor(Puppet::FileSystem.path_string(path))
196+
sd = Puppet::Util::Windows::Security.get_security_descriptor(path_string(path))
197197
sd.dacl
198198
rescue Puppet::Util::Windows::Error => e
199199
raise e unless e.code == FILE_NOT_FOUND

spec/unit/file_system_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,12 @@ def increment_counter_in_multiple_processes(file, num_procs, options)
984984
end
985985

986986
it 'preserves file ownership' do
987-
allow(Puppet::FileSystem).to receive(:lstat)
988-
.with(Puppet::FileSystem.pathname(dest))
989-
.and_return(double(uid: 1, gid: 2))
987+
FileUtils.touch(dest)
988+
allow(File).to receive(:lstat).and_call_original
989+
allow(File).to receive(:lstat).with(Pathname.new(dest)).and_return(double(uid: 1, gid: 2, 'directory?': false))
990990

991-
expect(FileUtils).to receive(:chown).with(1, 2, /#{dest}/)
991+
allow(File).to receive(:chown).and_call_original
992+
expect(FileUtils).to receive(:chown).with(1, 2, any_args)
992993

993994
Puppet::FileSystem.replace_file(dest, 0644) { |f| f.write(content) }
994995
end
@@ -1163,4 +1164,33 @@ def increment_counter_in_multiple_processes(file, num_procs, options)
11631164
expect(File.mtime(dest)).to be_within(1).of(tomorrow)
11641165
end
11651166
end
1167+
1168+
context '#chmod' do
1169+
let(:dest) { tmpfile('abs_file') }
1170+
1171+
it "changes the mode given an absolute string" do
1172+
Puppet::FileSystem.touch(dest)
1173+
Puppet::FileSystem.chmod(0644, dest)
1174+
expect(File.stat(dest).mode & 0777).to eq(0644)
1175+
end
1176+
1177+
it "returns true if given an absolute pathname" do
1178+
Puppet::FileSystem.touch(dest)
1179+
Puppet::FileSystem.chmod(0644, Pathname.new(dest))
1180+
expect(File.stat(dest).mode & 0777).to eq(0644)
1181+
end
1182+
1183+
it "raises if the file doesn't exist" do
1184+
klass = Puppet::Util::Platform.windows? ? Puppet::Error : Errno::ENOENT
1185+
expect {
1186+
Puppet::FileSystem.chmod(0644, dest)
1187+
}.to raise_error(klass)
1188+
end
1189+
1190+
it "raises ArgumentError if dest is invalid" do
1191+
expect {
1192+
Puppet::FileSystem.chmod(0644, nil)
1193+
}.to raise_error(ArgumentError, /expected Pathname, got: 'NilClass'/)
1194+
end
1195+
end
11661196
end

0 commit comments

Comments
 (0)