new to Ruby and I've been stuck on this issue for days. I have an array of directories in which I would like to get only the 3-4 digit file permissions bit for all files/directories underneath it (0744).
The problem appears to be the File::Stat class is throwing errors converting the files into an integer.
Any insight or documentation into this issue would be of great help. Here is initial code to break down and convert the files:
%w(/etc /bin /usr/lbin /sbin).each do |dir|
Dir.glob("#{dir}/**/*").each do |c|
m = File.stat("#{c}").world_readable?
sprintf("%o", m)
end
end
And here is my error:
jtest.rb:4:in `sprintf': can't convert File::Stat into Integer (TypeError)
from jtest.rb:4
from jtest.rb:2:in `each'
from jtest.rb:2
from jtest.rb:1:in `each'
from jtest.rb:1
Does this not give you what you want?
File.stat("#{c}").mode.to_s(8)
Note that .mode is giving you the file permissions as in integer, I think you are just getting confused because the integer representation is base 10, whereas the permissions as you would see them in a console are displayed as base 8.
This should be close to what you want:
%w(/etc /bin /usr/bin /usr/lbin /usr/usb /sbin /usr/sbin).each do |dir|
Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).each do |c| # include hidden files
unless File.symlink?(c)
puts c + " - " + File.stat(c).mode.to_s(8)
end
end
end
%w(/etc /bin /usr/bin /usr/lbin /usr/usb /sbin /usr/sbin).each do |dir|
Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).each do |file|
begin
m = File.stat(file).mode
puts "File #{file} has a permission #{File.stat(file).mode.to_s(8)}" \
if (m%512 - m%64) / 64 < m%8
rescue => e
puts "[ERR] Unable to handle #{file}. Message: #{e.message}"
end
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.