eeee
eeee

Reputation: 139

How can I set the umask from within java?

I'm new to Java. Where is umask exposed in the api?

Upvotes: 13

Views: 19950

Answers (4)

Ich
Ich

Reputation: 1378

import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermission;
import java.util.EnumSet;

File file = new File("/some/path");
Files.setPosixFilePermissions(file.toPath(), EnumSet.of(
                PosixFilePermission.OWNER_READ,
                PosixFilePermission.OWNER_WRITE
            ));

Upvotes: 0

Stephen C
Stephen C

Reputation: 718678

Another approach is to use a 3rd-party Java library that exposes POSIX system calls; e.g.

The problem with this approach is that it is intrinsically non-portable (won't work on a non-POSIX compliant platform), and requires a platform-specific native library ... and all that that entails.

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

java.nio.file.attribute.PosixFileAttributes in Java SE 7.

Upvotes: 0

Yuval Adam
Yuval Adam

Reputation: 165182

You can't fiddle with the umask directly, since Java is an abstraction and the umask is POSIX-implementation specific. But you have the following API:

File f;
f.setExecutable(true);
f.setReadable(false);
f.setWritable(true);

There are some more APIs available, check the docs.

If you must have direct access to the umask, either do it via JNI and the chmod() syscall, or spawn a new process with exec("chmod").

Upvotes: 13

Related Questions