01 - checking your access to a file.rb
来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 42 行
RB
42 行
File.readable?('/bin/ls') # => trueFile.readable?('/etc/passwd-') # => falsefilename = 'test_file'File.open(filename, 'w') {}File.writable?(filename) # => trueFile.writable?('/bin/ls') # => falseFile.executable?('/bin/ls') # => trueFile.executable?(filename) # => false#---File.owned? 'test_file' # => trueFile.grpowned? 'test_file' # => trueFile.owned? '/bin/ls' # => false#---File.lstat('test_file').mode & 0777 # Keep only the permission bits.# => 420 # That is, 0644 octal.#---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?Process.uid = 1000what_can_i_do?# UID=0, GID=0# Effective UID=1000, Effective GID=0# To you, /bin/ls is: readable?, executable?#---
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?