Can we change the permission of h2db.mv.db file. Right now it is set to 664 need to change to 770 through java code.
Asked
Active
Viewed 527 times
1 Answers
0
To change permissions of existing file you can use
Path p = Paths.get("/path/to/h2db.mv.db");
Files.setPosixFilePermissions(p, PosixFilePermissions.fromString("rwxrwx---"));
where /path/to/h2db.mv.db
is an absolute or relative path to your file.
770, however, should not be used for database, database file is neither directory nor executable. Perhaps you meant 660, use PosixFilePermissions.fromString("rw-rw----")
for it.
If you want to specify initial permissions using the Java code only, you need to create a new empty file with these permissions before creation of database:
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");
It could be more reasonable to set the umask
of the process to 0007
instead (from the shell code, for example, with umask 0007
). With such umask
new files will have permissions 660
, new executables and directories will have permissions 770
.

Evgenij Ryazanov
- 6,960
- 2
- 10
- 18
-
This I have done, my main point of asking the question is when we are creating this file at that time we can change the permission. The solution you provided is when the file was already created... – Shubham kapoor Apr 20 '20 at 18:54