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
|