This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
Checking Your Access to a File | ||
---|---|---|
Code | Expected | Actual |
File.readable?('/bin/ls') |
true | true |
File.readable?('/etc/passwd-') |
false | false |
filename = 'test_file' File.open(filename, 'w') {} File.writable?(filename) |
true | true |
File.writable?('/bin/ls') |
false | false |
File.executable?('/bin/ls') |
true | true |
File.executable?(filename) |
false | false |
File.owned? 'test_file' |
true | true |
File.grpowned? 'test_file' |
true | true |
File.owned? '/bin/ls' |
false | false |
File.lstat('test_file').mode & 0777 # Keep only the permission bits. |
420 | 420 |
def what_can_i_do? sys = Process::Sys puts "UID=#{sys.getuid}, GID=#{sys.getgid}" puts "Effective UID=#{sys.geteuid}, Effective GID=#{sys.getegid}" file = '/bin/ls' can_do = [:readable?, :writable?, :executable?].inject([]) do |arr, method| arr << method if File.send(method, file); arr end puts "To you, #{file} is: #{can_do.join(', ')}" end what_can_i_do? |
UID=0, GID=0 Effective UID=0, Effective GID=0 To you, /bin/ls is: readable?, writable?, executable? |
UID=1000, GID=1000 Effective UID=1000, Effective GID=1000 To you, /bin/ls is: readable?, executable? |
Process.uid = 1000 what_can_i_do? |
UID=0, GID=0 Effective UID=1000, Effective GID=0 To you, /bin/ls is: readable?, executable? |
UID=1000, GID=1000 Effective UID=1000, Effective GID=1000 To you, /bin/ls is: readable?, executable? |