有没有比解析 ls 输出更好的方法来读取 Ruby 中的 ACL?
在 Mac OS X 上,ls
和 chmod
有一些额外的功能来处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是你所追求的吗?
Is this what you're after?
lstat
怎么样?上面的意思是名为“file”的文件的权限为644。
how about
lstat
?The above means the file called "file" has permission of 644.
据我所知,确实没有更好的选择。 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.
我正在查看 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?我创建了一个 gem 来执行 ACL 读取和修改:
I created a gem to do ACL reading and modifications:
这听起来像是IRB 的工作。 (在命令行中输入
irb
)。如果您还没有这样做,那么如果您正在编写 Ruby,则应该考虑使用 IRB;如果您正在编写 Ruby on Rails 应用程序,则应该考虑使用rails console
。当您在 Rails 应用程序的根目录启动后者时,您将加载 IRB 以及与 Rails 应用程序关联的所有(或大部分)代码。我制作了两个文件“file_1.txt”和“file_2.txt”。作为用户“charlie”,我从这些文件所在的目录打开一个 IRB shell,并在这两个文件上使用 Ruby 的 File 类:
然后,我执行与 root 相同的操作:
这就是我的操作方式,以了解什么我需要这样做,希望这有助于回答您的问题。
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/orrails 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:
Then, I did the same as root:
That's my modus operandi, to learn what I need to do, and I hope that helps answer your question.