我需要检查一个特定的文件,看看它是否是全局可写的。在 1.9.x 中,有 a handy check for this , 但此检查在 1.8.7 中不存在。出于兼容性原因,我需要在 1.8.7 中编写此脚本。
在 1.8.7 中是否有我缺少的执行此检查的好方法,或者我是否需要使用 stat 以自己的方式进行检查?
编辑 到目前为止,这是我想出的。有点古怪和糟糕,但它有效:
def world_writable?(file)
write_bit = Integer(sprintf("%o", File.stat(file).mode)[-1,1])
if [2, 3, 6, 7].include?(write_bit)
return true
else
return false
end
end
if world_writable?('/Users/nate/Desktop/scriptrunnertest/test1.sh')
puts "World writable"
else
puts "Not World Writable"
end
我对 ruby 很陌生,所以要温柔。
edit2 没关系,那根本行不通。
edit3 修复了它
最佳答案
您可以获得文件的 stat
对象,并检查其 mode
成员...
world_writable = File.stat("testfile").mode == 0x100777
您应该能够将其添加到 File
...
class File
def self.world_writable? path
permissions = File.stat(path).mode
permissions == 0x100777 && permissions || nil
end
end
关于Ruby 1.8.7 世界可写检查? File.world_writable?只有 1.9.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12653065/