Skip to content

Commit 0abc33e

Browse files
author
Christopher Brown
committed
CHEF-174 and cleanup around treating the mode as string or int, and converting to octal when necessary
also added feature tests
1 parent 1ba15b3 commit 0abc33e

6 files changed

Lines changed: 103 additions & 13 deletions

File tree

chef-server/config/dependencies.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# dependencies are generated using a strict version, don't forget to edit the dependency versions when upgrading.
2-
merb_gems_version = "1.0.10"
2+
merb_gems_version = "1.0.11"
33

44
# For more information about each component, please read http://wiki.merbivore.com/faqs/merb_components
55
dependency "merb-core", merb_gems_version

chef/lib/chef/provider/file.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ def negative_complement(big)
3737
end
3838
big
3939
end
40+
41+
def octal_mode(mode)
42+
((mode.respond_to?(:oct) ? mode.oct : mode.to_i) & 007777)
43+
end
4044

41-
private :negative_complement
45+
private :negative_complement, :octal_mode
4246

4347
def load_current_resource
4448
@current_resource = Chef::Resource::File.new(@new_resource.name)
@@ -47,7 +51,7 @@ def load_current_resource
4751
cstats = ::File.stat(@current_resource.path)
4852
@current_resource.owner(cstats.uid)
4953
@current_resource.group(cstats.gid)
50-
@current_resource.mode("%o" % (cstats.mode & 007777))
54+
@current_resource.mode(octal_mode(cstats.mode))
5155
@current_resource.checksum(checksum(@current_resource.path))
5256
end
5357
@current_resource
@@ -104,17 +108,17 @@ def set_group
104108
def compare_mode
105109
case @new_resource.mode
106110
when /^\d+$/, Integer
107-
real_mode = sprintf("%o" % (@new_resource.mode & 007777))
108-
real_mode.to_i == @current_resource.mode.to_i
111+
octal_mode(@new_resource.mode) == octal_mode(@current_resource.mode)
109112
else
110113
false
111114
end
112115
end
113116

114117
def set_mode
115118
unless compare_mode && @new_resource.mode != nil
116-
Chef::Log.info("Setting mode to #{sprintf("%o" % (@new_resource.mode & 007777))} for #{@new_resource}")
117-
::File.chmod(@new_resource.mode.to_i, @new_resource.path)
119+
Chef::Log.info("Setting mode to #{sprintf("%o" % octal_mode(@new_resource.mode))} for #{@new_resource}")
120+
# CHEF-174, bad mojo around treating integers as octal. If a string is passed, we try to do the "right" thing
121+
::File.chmod(octal_mode(@new_resource.mode), @new_resource.path)
118122
@new_resource.updated = true
119123
end
120124
end
@@ -125,9 +129,9 @@ def action_create
125129
::File.open(@new_resource.path, "w+") { |f| }
126130
@new_resource.updated = true
127131
end
128-
set_owner if @new_resource.owner != nil
129-
set_group if @new_resource.group != nil
130-
set_mode if @new_resource.mode != nil
132+
set_owner unless @new_resource.owner.nil?
133+
set_group unless @new_resource.group.nil?
134+
set_mode unless @new_resource.mode.nil?
131135
end
132136

133137
def action_create_if_missing

chef/lib/chef/resource/file.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def mode(arg=nil)
5959
set_or_return(
6060
:mode,
6161
arg,
62-
:regex => /^\d{3,4}$/
62+
:regex => /^0?\d{3,4}$/
6363
)
6464
end
6565

@@ -81,4 +81,4 @@ def path(arg=nil)
8181

8282
end
8383
end
84-
end
84+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# Cookbook Name:: files
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Opscode
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
file "#{node[:tmpdir]}/octal0644.txt" do
21+
owner 'nobody'
22+
mode 0644
23+
action :create
24+
end
25+
26+
file "#{node[:tmpdir]}/octal2644.txt" do
27+
owner 'nobody'
28+
mode 02644
29+
action :create
30+
end
31+
32+
file "#{node[:tmpdir]}/decimal644.txt" do
33+
owner 'nobody'
34+
mode 644
35+
action :create
36+
end
37+
38+
file "#{node[:tmpdir]}/decimal2644.txt" do
39+
owner 'nobody'
40+
mode 2644
41+
action :create
42+
end
43+
44+
file "#{node[:tmpdir]}/string644.txt" do
45+
owner 'nobody'
46+
mode "644"
47+
action :create
48+
end
49+
50+
file "#{node[:tmpdir]}/string0644.txt" do
51+
owner 'nobody'
52+
mode "0644"
53+
action :create
54+
end
55+
56+
file "#{node[:tmpdir]}/string2644.txt" do
57+
owner 'nobody'
58+
mode "2644"
59+
action :create
60+
end
61+

features/manage_files.feature

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ Feature: Manage Files
4040
Then the run should exit '0'
4141
And the atime of 'touch_test.txt' should be different
4242
And the mtime of 'touch_test.txt' should be different
43-
43+
44+
Scenario: Set the accessibility of a created file
45+
Given a validated node
46+
And it includes the recipe 'manage_files::set_the_accessibility_of_a_created_file'
47+
When I run the chef-client
48+
Then the run should exit '0'
49+
And the file named 'octal0644.txt' should have octal mode '0644'
50+
And the file named 'octal2644.txt' should have octal mode '2644'
51+
And the file named 'decimal644.txt' should have decimal mode '644'
52+
And the file named 'decimal2644.txt' should have decimal mode '2644'
53+
And the file named 'string644.txt' should have octal mode '644'
54+
And the file named 'string0644.txt' should have octal mode '0644'
55+
And the file named 'string2644.txt' should have octal mode '2644'
56+
57+

features/steps/files.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@
8080
cstats = File.stat(File.join(tmpdir, filename))
8181
cstats.uid.should == uid
8282
end
83+
84+
Then /^the file named '(.+)' should have octal mode '(.+)'$/ do |filename, expected_mode|
85+
cstats = File.stat(File.join(tmpdir, filename))
86+
(cstats.mode & 007777).should == expected_mode.oct
87+
end
88+
89+
Then /^the file named '(.+)' should have decimal mode '(.+)'$/ do |filename, expected_mode|
90+
cstats = File.stat(File.join(tmpdir, filename))
91+
(cstats.mode & 007777).should == expected_mode.to_i
92+
end
93+

0 commit comments

Comments
 (0)