Changing group permission using java in unix

Discussion in 'Java' started by Sidhartha, Jul 7, 2008.

  1. Sidhartha

    Sidhartha Guest

    Hi,
    I have a java application which will create files in a particular
    directory in the unix server.The application is started using
    root.When the file is created the permission it has is -rw-r-r.I need
    to change this and give group also rw.Anyone knows how to do this
    after file is created.I dont wanna run "chmod" using
    Process.getRuntime().Is there any other java api to do this.Please
    help me

    Thanks,
    Sidhartha
    Sidhartha, Jul 7, 2008
    #1
    1. Advertising

  2. Sidhartha

    shakah Guest

    On Jul 7, 2:02 pm, Sidhartha <> wrote:
    > Hi,
    > I have a java application which will create files in a particular
    > directory in the unix server.The application is started using
    > root.When the file is created the permission it has is -rw-r-r.I need
    > to change this and give group also rw.Anyone knows how to do this
    > after file is created.I dont wanna run "chmod" using
    > Process.getRuntime().Is there any other java api to do this.Please
    > help me
    >
    > Thanks,
    > Sidhartha


    Can you set the umask before launching your Java app, e.g.:

    jc@jc-ubuntu:~/tmp$ cat filetest.java
    public class filetest {
    public static void main(String [] args)
    throws Exception {
    java.io.File f = java.io.File.createTempFile("test-",".tmp") ;
    System.out.println("created '" + f.getAbsoluteFile() + "'") ;
    }
    }

    jc@jc-ubuntu:~/tmp$ java filetest
    created '/tmp/test-50302.tmp'

    jc@jc-ubuntu:~/tmp$ (umask 011; java filetest)
    created '/tmp/test-52674.tmp'

    jc@jc-ubuntu:~/tmp$ java filetest
    created '/tmp/test-23429.tmp'

    jc@jc-ubuntu:~/tmp$ (umask 012; java filetest)
    created '/tmp/test-31612.tmp'

    jc@jc-ubuntu:~/tmp$ ls -ltr /tmp/test-*.tmp
    -rw-r--r-- 1 jc jc 0 2008-07-07 15:17 /tmp/test-50302.tmp
    -rw-rw-rw- 1 jc jc 0 2008-07-07 15:17 /tmp/test-52674.tmp
    -rw-r--r-- 1 jc jc 0 2008-07-07 15:17 /tmp/test-23429.tmp
    -rw-rw-r-- 1 jc jc 0 2008-07-07 15:19 /tmp/test-31612.tmp

    ?
    shakah, Jul 7, 2008
    #2
    1. Advertising

  3. Sidhartha

    Tom Anderson Guest

    On Mon, 7 Jul 2008, Steve W. Jackson wrote:

    > In article
    > <>,
    > Sidhartha <> wrote:
    >
    >> I have a java application which will create files in a particular
    >> directory in the unix server.The application is started using root.When
    >> the file is created the permission it has is -rw-r-r.I need to change
    >> this and give group also rw.Anyone knows how to do this after file is
    >> created.I dont wanna run "chmod" using Process.getRuntime().Is there
    >> any other java api to do this.Please help me

    >
    > Managing file and directory permissions is platform-specific, so there
    > should probably be no expectation of a Java API for this.


    I'm pretty sure Steve is right about this.

    > However, you might take a look at the java.io.FilePermission class to
    > see if it offers insights into solving your situation. Since the
    > Javadocs don't really address the various classes of permissions, I'm
    > guessing it won't help much.


    I don't think it does.

    > Still, if your app is running as root (dangerous enough), why *not*
    > simply use "chmod g+w file" on it?


    Isn't the fact that a program is running as root usually taken as a reason
    *not* to do things like that?! Okay, we're in java, so not really a worry
    as it would be in C. But still!

    I'd be tempted to write a JNI wrapper around the chmod libc call, rather
    than running the chmod command, but that's probably just because i'm a bit
    JNI-crazy at the moment. It's not hard, though:

    public class Chmod {
    private Chmod() {}
    public static void setGroupWriteable(File f, boolean gw) throws IOException {
    int mode = figureOutMode(f, 0000020, gw) ;
    int ret = chmod(f.getCanonicalPath(), mode) ;
    if (ret != 0) throw new IOException("chmod failed: errno = " + ret) ;
    }
    private static native int chmod(String path, int mode) ;
    }

    JNIEXPORT jint JNICALL Java_Chmod_chmod(JNIEnv *env, jclass class, jstring path, jint mode) {
    char *pathStr = (*env)->GetStringChars(env, path, NULL) ;
    int ok = chmod(pathStr, mode) ;
    (*env)->ReleaseStringChars(env, path, pathStr) ;
    if (ok != 0) return errno ;
    else return 0 ;
    }

    Writing figureOutMode is actually slightly involved, because you have to
    do a stat() to get the current mode before masking out the right bits.
    Wrapping stat isn't hard, and the logic is also not hard.

    tom

    --
    There are lousy reviews, and then there's empirical shitness. -- pikelet
    Tom Anderson, Jul 7, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Johnson

    Changing UNIX primary group

    Justin Johnson, Aug 12, 2003, in forum: Python
    Replies:
    3
    Views:
    11,660
    Justin Johnson
    Aug 13, 2003
  2. Aienthiwan
    Replies:
    2
    Views:
    566
    Aienthiwan
    Jan 19, 2004
  3. Akaketwa
    Replies:
    1
    Views:
    4,833
    impaler
    Sep 22, 2006
  4. Jonay Aloat
    Replies:
    5
    Views:
    276
    Alex Buell
    Mar 13, 2006
  5. MS
    Replies:
    17
    Views:
    1,152
    Seebs
    Mar 16, 2011
Loading...

Share This Page