Changing group permission using java in unix

S

Sidhartha

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
 
S

shakah

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

?
 
T

Tom Anderson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top