2

I am running java 7 applications on unix machines. Is there a way to get the current umask value in pure java ?

In C I would use a combination of umask system calls, but I don't think I can call that in Java without resorting to JNI. Is there another approach ?

Edit: Here is a C example (from GUN libc docs):

      mode_t
      read_umask (void)
      {
        mode_t mask = umask (0);
        umask (mask);
        return mask;
      }
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
  • 5
    Java is a platform-independent language, so what is umask on windows? – StarPinkER Sep 01 '13 at 13:44
  • Have a look at the [java.nio.file.attribute](http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/package-summary.html) package. – McDowell Sep 01 '13 at 13:51
  • @StarPinkER In java 7 nio, you will find a lots of optional operations which are implemented only in certain platforms (for instance `PosixFileAttribute`). – paradigmatic Sep 01 '13 at 14:18
  • @McDowell I found nothing there... – paradigmatic Sep 01 '13 at 14:20
  • @paradigmatic - You want the mask of the current Java process? Does [JNA](http://en.wikipedia.org/wiki/Java_Native_Access) support all your target platforms? A __C__ example of exactly what you want would clarify. – McDowell Sep 01 '13 at 18:43

2 Answers2

2

A simple solution, if there is no Class/Method to get the umask, why don't you get it before call java and pass as a property?

user207421
  • 305,947
  • 44
  • 307
  • 483
James Liu
  • 269
  • 1
  • 11
  • That's an interesting solution. If I call java through a script, I could pass it as an env variable. The solution provided by @ortang would then be a nice fallback. – paradigmatic Sep 02 '13 at 06:24
1

Can you clarify? Do you want to read the umask of the application(the current java process)? Or do you want to read the umask value of some files on the file system?

You can use NIO (the used code is from the javadocs) to get some file attributes, or you can execute a shell command, since the process created with Runtime.execute inherits the umask of it's creator process.

So you should be able to solve your problem without the use of JNI.

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;

public class Test {

    private static final String COMMAND = "/bin/bash -c umask -S";


    public static String getUmask() {
        final Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec(COMMAND);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String umask = reader.readLine();
            if (process.waitFor() == 0)
                return umask;
        } catch (final IOException e) {
            e.printStackTrace();
        } catch (final InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
        return "";
    }

    public static void main(String[] args) throws IOException {
        /*
         * NIO
         */
        PosixFileAttributes attrs = Files.getFileAttributeView(Paths.get("testFile"), PosixFileAttributeView.class)
                .readAttributes();
        System.out.format("%s %s%n", attrs.owner().getName(), PosixFilePermissions.toString(attrs.permissions()));

        /*
         * execute shell command to get umask of current process
         */
        System.out.println(getUmask());

    }

}
Ortwin Angermeier
  • 5,957
  • 2
  • 34
  • 34
  • 2
    Files don't have umasks. They have permission sets which *result from* the umask in effect when they were created, and may have been modified since. They aren't umasks, and they aren't evidence of what the umask was when created. – user207421 Sep 01 '13 at 23:34
  • Yes I am interested in the process umask not in the posix permissions of a file. – paradigmatic Sep 02 '13 at 06:27