My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# $Id$
# symbolic mode support for ruby's chmod. blame it to edpratomo.

class Regexp
def global_match(str, &proc)
retval = nil
loop do
res = str.sub(self) do |m|
proc.call($~) # pass MatchData obj
''
end
break retval if res == str
str = res
retval ||= true
end
end
end

class File
def self.symbolic_mode_chmod(sym_mode, *filenames)
re = Regexp.new(<<'PATT', Regexp::EXTENDED)
^([+-=])
(
[rwxXst]+ # at least one of protection bits
|
[ugo] # or exactly one of u/g/o
)
(?![^+-=]) # not followed by anything except +/-/=
PATT
user_handlers = {
'u' => lambda {|bit| bit},
'g' => lambda {|bit| bit >> 3},
'o' => lambda {|bit| bit >> 6},
}
sym_to_oct = {
't' => lambda {|user| 01000},
'r' => lambda {|user| user_handlers[user][0400]},
'w' => lambda {|user| user_handlers[user][0200]},
'x' => lambda {|user| user_handlers[user][0100]},
's' => lambda {|user| user == 'u' and return 04000; user == 'g' and return 02000},
}
masks = {
'u' => 0700,
'g' => 0070,
'o' => 0007,
}
ops = {
'+' => lambda {|my_mode,lmode| lmode | my_mode },
'-' => lambda {|my_mode,lmode| lmode & ~my_mode },
}

filenames.inject([]) do |oct_modes,filename|
fixup = nil
# get current file mode
prev_mode = File.stat(filename).mode & ~0100000

sym_mode.split(',').each do |str|
users = 'ugo'
s = str.sub(/^([ugoa]+)?/) do |m|
unless $1
# define o's w removal
fixup = lambda {|lmode| ops['-'][sym_to_oct['w']['o'], lmode]}
else
users = $1 unless $1 == 'a'
end
''
end
users.split('').each do |user|
# this op needs knowledge about user, so define it here
ops['='] = lambda {|my_mode,lmode| lmode & ~masks[user] | my_mode}

ret = re.global_match(s) do |m|
op = m[1]
oct_mode = m[2].split('').inject(0) do |memo,sym|
if sym_to_oct.has_key?(sym)
memo |= sym_to_oct[sym][user]
else
# handle u/g/o sym
memo = user_handlers[user][prev_mode & masks[sym]]
end
end
prev_mode = ops[op][oct_mode,prev_mode]
end
ret or raise "Invalid symbolic mode: #{sym_mode}"
end # end of each user
end
oct_modes.push [fixup ? fixup[prev_mode] : prev_mode, filename.clone]
oct_modes
end # end of filenames.inject
end

class << self
alias old_chmod chmod
private :symbolic_mode_chmod
end
end

# supress redefine warning
begin
old_v = $VERBOSE
$VERBOSE = false

class File
def self.chmod(mode, *filenames)
if mode.is_a?(String)
return old_chmod(mode.oct, *filenames) if mode =~ /\d/
symbolic_mode_chmod(mode, *filenames).inject(0) do |memo,e|
memo += old_chmod(e[0], e[1])
end
else
old_chmod(mode, *filenames)
end
end
end

# monkey patch!
module FileUtils
def chmod(mode, list, options = {})
begin
fu_check_options options, :noop, :verbose
rescue ArgumentError
fu_check_options options, OPT_TABLE['chmod']
end
list = fu_list(list)
fu_output_message sprintf('chmod %s %s', mode.to_s, list.join(' ')) if options[:verbose]
return if options[:noop]
list.each do |path|
Entry_.new(path).chmod mode
end
end
end
ensure
$VERBOSE = old_v
end

if $0 == __FILE__
require 'fileutils'
require 'test/unit'

class File
def self.sym2oct(sym_mode, *filenames)
symbolic_mode_chmod(sym_mode, *filenames)
end
end

class TestExtensions < Test::Unit::TestCase
def setup
FileUtils.mkdir_p "testdata"
@test_file = File.join("testdata", "foo.txt")
File.open(@test_file, "w") {}
# initially -rw-rw-r--
end

def teardown
FileUtils.rm_r "testdata"
end

def try(sym_mode)
oct_mode = File.sym2oct(sym_mode, @test_file)[0][0]
system "chmod #{sym_mode} #{@test_file}"
chmod_res = sprintf("%04o", File.stat(@test_file).mode & ~0100000)
our_res = sprintf("%04o", oct_mode)
assert_equal(chmod_res, our_res, %(chmod "#{sym_mode}"))
end

%w(
a+rwx o+u ug+rx-w o-x g+x+u go=u
g=rw,a+x,o-w
a+w,g+r,o+x
ugo+w,a-w
a=rwx
o=rwx
-rwx
=rwx
+rwx
).each_with_index do |sym_mode,i|
class_eval <<"CODE"
def test_#{sprintf("%02d", i)}
try("#{sym_mode}")
end
CODE
end

def test_exc_01
assert_raise(RuntimeError) { File.sym2oct('-xu', @test_file) }
end
def test_exc_02
assert_raise(RuntimeError) { File.sym2oct('-ux', @test_file) }
end
def test_actual
sym_mode = 'a+x'
oct_mode = File.sym2oct(sym_mode, @test_file)[0][0]
res = sprintf("%04o", oct_mode)
num = File.chmod("a+x", @test_file)
actual_res = sprintf("%04o", File.stat(@test_file).mode & ~0100000)
assert_equal(res, actual_res, %(chmod "#{sym_mode}"))
assert_equal(1, num, "number of successful chmod")
end
end
end
Show details Hide details

Change log

r5 by ed.pratomo on Aug 08, 2008   Diff
- replaced proc with lambda for forward
compatibility
- tested with ruby 1.9.0 -r18448 and 1.8.6
p111
Go to: 
Project members, sign in to write a code review

Older revisions

r4 by ed.pratomo on Apr 09, 2008   Diff
added monkey patch for FileUtils
r3 by ed.pratomo on Mar 31, 2008   Diff
changed test class name
r2 by ed.pratomo on Mar 31, 2008   Diff
added symbolic mode support for ruby's
chmod
All revisions of this file

File info

Size: 5270 bytes, 200 lines

File properties

svn:keywords
Id
Hosted by Google Code