无法在 macOS 上打开文件 – FileNotFoundException,操作不允许

huangapple go评论81阅读模式
英文:

Can't open file on macOS - FileNotFoundException, Operation not permitted

问题

这是一个简单的程序:

    new FileInputStream("/Users/me/Desktop/foo.png")

我正在尝试从IntelliJ运行它,但我得到了以下错误:

> 异常线程 "main" java.io.FileNotFoundException: /Users/me/Desktop/foo.png (操作不允许)

Unix权限通常为 `rw-r--r--`。这与macOS的额外安全性有关。

如何在macOS上的Java程序中打开文件?

这是macOS 11.0 beta(Big Sur)。也许在11.0之前也会发生这种情况,我不清楚。
英文:

It's a simple program:

new FileInputStream("/Users/me/Desktop/foo.png")

I'm trying to run it from IntelliJ and I get:

> Exception in thread "main" java.io.FileNotFoundException: /Users/me/Desktop/foo.png (Operation not permitted)

The unix perms are the usual rw-r--r--. It's something to do with macOS's extra security.

How can I open files in Java programs on macOS now?

This is macOS 11.0 beta (Big Sur). Maybe it was happening before 11.0 too; I don't know.

答案1

得分: 2

你遇到了一个特定于 MacOS 的问题。在 MacOS 中,仅检查经典的 POSIX 权限是不够的,因为操作系统引入了许多机制来提供细粒度的控制以及安全性。

访问控制列表(ACL)

POSIX 权限提供了三个分组,可以对所有者、组和其他所有人的访问进行控制。另一方面,ACL 允许创建访问控制实体,这些实体可以以多种方式定义。这不是一个关于 *NIX 的入门指南,所以让我们直接进入重点。要检查 ACL,请打开终端会话,导航到包含目标文件的目录,然后键入:

ls -le foo.png

您应该会看到 POSIX 权限,后面是一个编号的 ACL 规则列表。如果您的账户受到其中一个规则的影响,您可以按如下方式删除该规则:

sudo chmod -a# num foo.png

其中 num 是有问题规则的数值标识符。

文件标志

文件标志是另一个并行的权限系统。再次使用终端,尝试:

ls -lO foo.png

注意:标志是小写字母 L,后面是大写字母 O。不要将其中一个与数字 1 或 0 混淆。

您将再次看到 POSIX 信息,后面是描述与所讨论文件相关的任何标志的字符串。您关心的标志是 uchgschg(分别表示用户不可变标志和系统不可变标志)。

您可以按如下方式去除这些标志:

sudo chflags nouchg foo.png
sudo chflags noschg foo.png

扩展属性

这样检查文件的扩展属性:

xattr -l $foo.png

这样删除所有扩展属性:

sudo xattr -c $foo.png

系统完整性保护

系统完整性保护可能已将您的文件指定为 "rootless"。通过终端检查状态:

ls -l@ foo.png

清除 rootless 标志有点麻烦。通过恢复磁盘启动,打开终端,导航到文件所在位置,然后键入:

sudo xattr -d com.apple.rootless foo.png

虽然我不是系统管理员,但至少其中一个方法应该能解决您的问题,前提是您已经验证过您的账户具有 POSIX 权限,并且文件不仅仅是通过 Finder 锁定的(按下 Command-I,查找角落中的小复选框)。

英文:

You're running into a MacOS specific issue. Checking classic POSIX permissions isn't quite enough with MacOS, since the operating system introduced a number of mechanisms to provide fine-grained control as well as security.

Access Control Lists

POSIX permissions provide for three groupings to which access may be controlled: owner, group and everyone else. ACLs, on the other hand, allow for the creation of Access Control Entities, which maybe defined in a number of ways. This isn't a *NIX primer, so let's just get to the meaty bits. To check ACL, pull up a terminal session, navigate to the directory containing your target file and type:

ls -le foo.png

You should see POSIX permissions, followed by a numbered list of ACL rules. If your account is affected by one of those rules, you can remove the rule as follows:

sudo chmod -a# num foo.png

where num is the numerical identifier for the offending rule.

File Flags

File flags are another parallel permissions system. Using terminal again, try:

ls -lO foo.png

NOTE: the flag is lowercase letter L followed up uppercase letter O. Con't confuse one or the other for numerals 1 or 0.

You'll again see POSIX info, followed by a string describing any flags pertaining to the file in question. The flags you care about are uchg and schg (denoting user immutability flag and system immutability flag, respectively).

You can get rid of these flags as follows:

sudo chflags nouchg foo.png
sudo chflags noschg foo.png

Extended Attributes

Check your file for extended attributes like so:

xattr -l $foo.png

Remove all extended attributes like so:

sudo xattr -c $foo.png

System Integrity Protection

System Integrity Protection may have designated your file as "rootless." Check the status at the terminal with:

ls -l@ foo.png

Clearing the rootless flag is a bit more painful. Boot via recovery disk, pull up a terminal, navigate to your file and type:

sudo xattr -d com.apple.rootless foo.png

I'm no sysadmin, but at least one of these things should cover your issue, assuming you've verified that your account has POSIX permissions and the file isn't just simply locked via Finder (Command-I, look for the little checkbox in the corner).

huangapple
  • 本文由 发表于 2020年10月3日 07:46:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64179397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定