简体   繁体   English

如何检查 java 中目录的写权限?

[英]How to check write permissions of a directory in java?

I would like a code snippet that checks whether a directory has read/write permissions and do something if it does, and does something else if it doesnt.我想要一个代码片段来检查一个目录是否具有读/写权限,如果有,则执行某些操作,如果没有,则执行其他操作。 I tried an example shown here:我尝试了此处显示的示例:

try {
    AccessController.checkPermission(new FilePermission("/tmp/*", "read,write"));
System.out.println("Good");
    // Has permission
} catch (SecurityException e) {
    // Does not have permission
System.out.println("Bad");
}

The problem is the exception is always triggered, so it always ends up printing "Bad" regardless of whether the directory has write permissions or not.问题是异常总是被触发,所以不管目录是否有写权限,它总是最终打印“坏”。 (I chmod the directories to 777 or 000 to test). (我将目录更改为 777 或 000 进行测试)。

Is there an alternative or some way to accomplish what I need?有没有替代方法或某种方法来完成我需要的?

if you just want to check if you can write:如果您只想检查是否可以编写:

File f = new File("path");
if(f.canWrite()) {
  // write access
} else {
  // no write access
}

for checking read access, there is a function canRead()检查读访问,有一个 function canRead()

In Java 7 i do it like this:在 Java 7我这样做:

if(Files.isWritable(path)){
  //ok, write
}

Docs 文档

You should use the path of the directory alone ( "/tmp" ) to query the permissions of a directory:您应该单独使用目录的路径( "/tmp" )来查询目录的权限:

AccessController.checkPermission(new FilePermission("/tmp", "read,write"));

With "/tmp/*" you query the permissions of all files inside the /tmp directory.使用"/tmp/*"可以查询/tmp目录中所有文件的权限。

Java has its own permission model revolving around the use of an AccessController and Permission classes. Java 有自己的权限 model 围绕使用AccessControllerPermission类。 The permissions are granted to a code source (the location from where the classes are loaded), and in some/most cases these permissions are different from any underlying permissions required to access the desired resource.权限被授予代码源(加载类的位置),并且在某些/大多数情况下,这些权限不同于访问所需资源所需的任何底层权限。

For instance, although you may have granted all users to read and write to the /tmp directory, this isn't sufficient for the AccessController to grant your code the necessary permission.例如,尽管您可能已授予所有用户对/tmp目录的读写权限,但这还不足以让 AccessController 授予您的代码必要的权限。 You'll also need to add a rule in the policy file used (by the AccessController) to read and write files from the /tmp directory.您还需要在(由 AccessController 使用的)策略文件中添加一条规则,以从 /tmp 目录读取和写入文件。 The rule to be created will be equivalent to the following:要创建的规则将等同于以下内容:

grant codeBase "<location of the codebase>" {
    permission java.io.FilePermission "/tmp/-", "read, write";
};

This seems to work fine:这似乎工作正常:

assertFalse(Files.isWritable(new File("/etc/").toPath()));
assertTrue(Files.isWritable(new File("/tmp/").toPath()));

Do you want to check permissions for folder or for files in folder ?您要检查文件夹文件夹中文件的权限吗?

"/*" in path name means a directory and all the files contained in that directory.路径名中的“/*”表示一个目录以及该目录中包含的所有文件。

see javadoc 见 javadoc

java.io.File has two methods canRead and canWrite that should suffice. java.io.File有两个方法canReadcanWrite就足够了。

if(DocumentFile.fromFile(file).canWrite()){
   //allowed
   ...
}else{
   ...
}

On Windows, File.canWrite() does not always provide an accurate result.在 Windows 上,File.canWrite() 并不总是提供准确的结果。 I would recommand using the following:我建议使用以下内容:

import java.nio.file.Files;
import java.nio.file.Paths;
...

if(Files.isWritable(Paths.get("path"))){
  //ok, write
}

Oops, one more thing.哎呀,还有一件事。 The directory also has to be executable, at least on linux:该目录也必须是可执行的,至少在 linux 上:

  /** Make sure output dir exists and is writeable. */
  public boolean validateOutputDir(Path publishDirectory, Formatter error) {
    if (!Files.exists(publishDirectory)) {
      error.format(" Output directory '%s' does not exist%n", publishDirectory);
      return false;
    }
    if (!Files.isDirectory(publishDirectory)) {
      error.format(" Output directory '%s' is not a directory%n", publishDirectory);
      return false;
    }
    if (!Files.isWritable(publishDirectory)) {
      error.format(" Output directory '%s' is not writeable%n", publishDirectory);
      return false;
    }
    if (!Files.isExecutable(publishDirectory)) {
      error.format(" Output directory '%s' is not executable%n", publishDirectory);
      return false;
    }
    return true;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM