Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Changing group permission using java in unix

Reply
Thread Tools

Changing group permission using java in unix

 
 
Sidhartha
Guest
Posts: n/a
 
      07-07-2008
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
 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 2:02*pm, Sidhartha <sidhartha.namb...@gmail.com> 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

?
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      07-07-2008
On Mon, 7 Jul 2008, Steve W. Jackson wrote:

> In article
> <81ed0638-090b-4e7e-8a11->,
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Establish read permission of a file in C (UNIX / Linux) MS C Programming 17 03-16-2011 03:47 AM
Fixed: The machine-default permission settings do not grant Local Activation permission for the COM Server application with CLSID {555F3418-D99E-4E51-800A-6E89CFD8B1D7} to the user NT AUTHORITY\LOCAL SERVICE SID (S-1-5-19). Skybuck Flying Windows 64bit 1 06-29-2009 06:17 PM
Unix command that can post question to a group of expert like using the newsgroup. Jonay Aloat C++ 5 03-13-2006 03:28 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
Changing UNIX primary group Justin Johnson Python 3 08-13-2003 07:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57