首页 > 解决方案 > H2 Embedded db需要通过java更改数据库文件h2db.mv.db的权限

问题描述

我们可以更改h2db.mv.db文件的权限吗?现在设置为664需要通过java代码更改为770。

标签: javah2db

解决方案


要更改现有文件的权限,您可以使用

Path p = Paths.get("/path/to/h2db.mv.db");
Files.setPosixFilePermissions(p, PosixFilePermissions.fromString("rwxrwx---"));

where/path/to/h2db.mv.db是文件的绝对或相对路径。

但是,770 不应该用于数据库,数据库文件既不是目录也不是可执行文件。也许你的意思是660,使用PosixFilePermissions.fromString("rw-rw----")它。


如果您只想使用 Java 代码指定初始权限,则需要在创建数据库之前创建一个具有这些权限的新空文件:

Path p = Paths.get("/path/to/h2db.mv.db");
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-rw----");
if (Files.notExists(p)) {
    Files.createFile(p, PosixFilePermissions.asFileAttribute(perms));
}
// Some permissions may be removed by umask during file creation, so
// they need to be set again
Files.setPosixFilePermissions(p, perms);
Connection c = DriverManager.getConnection("jdbc:h2:/path/to/h2db");

umask进程0007umask 0007_ 有了这样umask的新文件就会有权限660,新的可执行文件和目录也会有权限770


推荐阅读