有没有比解析 ls 输出更好的方法来读取 Ruby 中的 ACL?

发布于 2024-10-25 00:27:16 字数 1894 浏览 1 评论 0原文

在 Mac OS X 上,lschmod 有一些额外的功能来处理 ACL 操作系统在默认 posix 权限之上的权限。我有一些权限问题需要修复,并且我编写了一个脚本来帮助修复这些问题,直到 Apple 修复该错误。这是解析 ls 获取 ACL

result = `#{Escape.shell_command(["ls", "-led", file])}`
if result.empty?
    # ls error...
else
    @acl = result.split("\n")[1..-1].collect do |ace|
        ace = ace.split(": ", 2)
        ace[0] = ace[0].to_i
        ace
    end
    # acl processing code...
end

我添加了 < a href="http://rubygems.org/gems/escape" rel="nofollow noreferrer">escape gem,但它实际上仍然是相同的代码。

但我知道一般来说解析 ls 在脚本中,那么有没有更好的方法来阅读 ACL 权限?

我需要 ACE 及其索引与 chmod 稍后在脚本中:

system("chmod -a# #{index} \"#{file}\"")

On Mac OS X, ls and chmod have some extra features for handling the ACL permissions that the OS layers on top of the default posix permissions. I have some permission problems that need fixing, and I wrote a script to help patch up these problems until Apple fix that bug. Here's the code that parses ls to get the the ACL:

result = `#{Escape.shell_command(["ls", "-led", file])}`
if result.empty?
    # ls error...
else
    @acl = result.split("\n")[1..-1].collect do |ace|
        ace = ace.split(": ", 2)
        ace[0] = ace[0].to_i
        ace
    end
    # acl processing code...
end

I added the escape gem, but it's still virtually the same code.

But I know it's a bad idea in general to parse ls in a script, so is there a better way to read the ACL permissions from a file?

I need the ACEs and their indices to use with chmod later on in the script:

system("chmod -a# #{index} \"#{file}\"")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深府石板幽径 2024-11-01 00:27:16

这就是你所追求的吗?

File.chmod(0644, path)              # Sets the file to 0644
printf("%o", File.stat(path).mode)  # Returns the mode as an 
                                    # integer and is converted to octal
100644 => nil 

Is this what you're after?

File.chmod(0644, path)              # Sets the file to 0644
printf("%o", File.stat(path).mode)  # Returns the mode as an 
                                    # integer and is converted to octal
100644 => nil 
╭⌒浅淡时光〆 2024-11-01 00:27:16

lstat 怎么样?

>> File.lstat('file')
=> #<File::Stat dev=0x803, ino=3365, mode=0100644, nlink=1, uid=0, gid=0, rdev=0x0, size=1328, blksize=4096, blocks=8, atime=2011-03-30 08:39:30 +0800, mtime=2011-03-30 08:36:34 +0800, ctime=2011-03-30 08:36:34 +0800>
>> print "%o" % ( File.lstat('file').mode & 0777  )
644

上面的意思是名为“file”的文件的权限为644。

how about lstat?

>> File.lstat('file')
=> #<File::Stat dev=0x803, ino=3365, mode=0100644, nlink=1, uid=0, gid=0, rdev=0x0, size=1328, blksize=4096, blocks=8, atime=2011-03-30 08:39:30 +0800, mtime=2011-03-30 08:36:34 +0800, ctime=2011-03-30 08:36:34 +0800>
>> print "%o" % ( File.lstat('file').mode & 0777  )
644

The above means the file called "file" has permission of 644.

蔚蓝源自深海 2024-11-01 00:27:16

据我所知,确实没有更好的选择。 ACL 似乎不会通过 Finder 或系统事件暴露给 OSA,因此 appscript 没有帮助。您可以使用 FFI 和 POSIX ACL 函数,但该级别的 API 非常烦人。

As far as I can tell, there isn't really a better option. ACLs don't seem to be exposed to OSA by the Finder or System Events, so appscript won't help. You could use FFI and the POSIX ACL functions, but the API at that level is exceedingly annoying.

做个少女永远怀春 2024-11-01 00:27:16

我正在查看 Ruby 1.8.7 文件 类。如果我一分钟的阅读是正确的,我收集到的是,进程的权限将控制您从文件中看到的内容,这正是 ls 所做的。

因此,如果 Ruby 脚本以 root 身份运行,它可以获取有关文件的各种信息,但如果它以用户身份运行,并且该用户不在该文件的组中,并且该文件不是世界可读的例如,那么脚本无法看到该文件。

似乎如果您以某个用户身份运行脚本,并且想要更改文件的权限,您可以收集它们并使用上面链接中的 Ruby 1.8.7 的 chmod 进行更改,没有?

I'm looking through the Ruby 1.8.7 File class. If my one-minute reading is correct, what I gather is that the permissions of the process will govern what you can see from the file, which is exactly what ls will do.

So, if the Ruby script is running as root, it can get all kinds of info about a file, but if it is running as a user, and that user is not in the file's group, and the file is not world-readable for example, then the script cannot see that file.

It seems like if you are running a script as a certain user, and you want to change a file's permissions, you can gather them up and change them with Ruby 1.8.7's chmod in the above link, no?

眼眸 2024-11-01 00:27:16

我创建了一个 gem 来执行 ACL 读取和修改:

>> require 'acl'
=> true
>> acl = OSX::ACL.of("tmp")
=> #<OSX::ACL:0x007f92eaabc578 @path="tmp">
>> acl.entries
=> [#<OSX::ACL::Entry:0x007f92eaaf7510 @components=["user", "FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046", "_www", "70", "allow", "read"]>]
>> ace = acl.entries.first
=> #<OSX::ACL::Entry:0x007f92eaaf7510 @components=["user", "FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046", "_www", "70", "allow", "read"]>
>> ace.assignment
=> #<OSX::ACL::Assignment:0x007f92ea2a0060 @type="user", @uuid="FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046", @name="_www", @id="70">
>> ace.assignment.type
=> "user"
>> ace.assignment.name
=> "_www"
>> ace.rules
=> ["allow"]
>> ace.permissions
=> ["read"]
>> acl.remove_entry_at_index(0)
chmod -a# 0 tmp # user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046:_www:70:allow:read
=> true

I created a gem to do ACL reading and modifications:

>> require 'acl'
=> true
>> acl = OSX::ACL.of("tmp")
=> #<OSX::ACL:0x007f92eaabc578 @path="tmp">
>> acl.entries
=> [#<OSX::ACL::Entry:0x007f92eaaf7510 @components=["user", "FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046", "_www", "70", "allow", "read"]>]
>> ace = acl.entries.first
=> #<OSX::ACL::Entry:0x007f92eaaf7510 @components=["user", "FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046", "_www", "70", "allow", "read"]>
>> ace.assignment
=> #<OSX::ACL::Assignment:0x007f92ea2a0060 @type="user", @uuid="FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046", @name="_www", @id="70">
>> ace.assignment.type
=> "user"
>> ace.assignment.name
=> "_www"
>> ace.rules
=> ["allow"]
>> ace.permissions
=> ["read"]
>> acl.remove_entry_at_index(0)
chmod -a# 0 tmp # user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000046:_www:70:allow:read
=> true
云归处 2024-11-01 00:27:16

这听起来像是IRB 的工作。 (在命令行中输入irb)。如果您还没有这样做,那么如果您正在编写 Ruby,则应该考虑使用 IRB;如果您正在编写 Ruby on Rails 应用程序,则应该考虑使用 rails console。当您在 Rails 应用程序的根目录启动后者时,您将加载 IRB 以及与 Rails 应用程序关联的所有(或大部分)代码。

我制作了两个文件“file_1.txt”和“file_2.txt”。作为用户“charlie”,我从这些文件所在的目录打开一个 IRB shell,并在这两个文件上使用 Ruby 的 File 类:

-rw-r--r--  1 root     staff  30 Mar 22 09:06 file_1.txt
-rwxrwxrwx  1 charlie  staff  16 Mar 22 09:06 file_2.txt

charlie:stackoverflow charlie$ man ls
charlie:stackoverflow charlie$ irb
ruby-1.8.7-p330 :001 > File.writable?("file_1.txt")
=> false 

然后,我执行与 root 相同的操作:

irb(main):002:0> File.writable?("file_1.txt")
=> true
irb(main):003:0> File.writable?("file_2.txt")
=> true
irb(main):004:0> 

这就是我的操作方式,以了解什么我需要这样做,希望这有助于回答您的问题。

This sounds like a job for IRB. (Type irb at the command line). If you don't already, you should consider using IRB if you're writing Ruby, and/or rails console if you're writing a Ruby on Rails application. When you launch the latter at the root of a Rails application, you will load IRB with all (or most) of the code associated with your Rails app.

I made two files, "file_1.txt" and "file_2.txt". As user "charlie", I open up an IRB shell from the directory where these files are located, and played around with Ruby's File class on those two files:

-rw-r--r--  1 root     staff  30 Mar 22 09:06 file_1.txt
-rwxrwxrwx  1 charlie  staff  16 Mar 22 09:06 file_2.txt

charlie:stackoverflow charlie$ man ls
charlie:stackoverflow charlie$ irb
ruby-1.8.7-p330 :001 > File.writable?("file_1.txt")
=> false 

Then, I did the same as root:

irb(main):002:0> File.writable?("file_1.txt")
=> true
irb(main):003:0> File.writable?("file_2.txt")
=> true
irb(main):004:0> 

That's my modus operandi, to learn what I need to do, and I hope that helps answer your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文