Ruby 1.8.7 世界可写检查? File.world_writable?只有 1.9.x

标签 ruby macos

我需要检查一个特定的文件,看看它是否是全局可写的。在 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

RubyFiddle .

关于Ruby 1.8.7 世界可写检查? File.world_writable?只有 1.9.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12653065/

相关文章:

macos - 在Catalina MacOS 10.15下,Apache下的PHP限制了通过文件链接对某些文件的访问,可能是由于隐私更改

python :如何打开火狐(OS X)

css - 单击单选按钮时标签颜色更改

php - 人工输入的智能日期范围解析?

ruby - 在 Ruby 中什么可以强制转换为整数?

asp.net - Mac OS X Docker基准ASP.NET错误:找不到libdl

macos - NSWindow beginSheet finishHandler 未调用

ruby - 无法在 RedHat 7 上安装 Compass

ruby-on-rails - 为什么在引用对象属性时应该使用字符串而不是符号?

macos - 如何拦截Cmd+Q