participate


Java Programming - How to use FilePermission class to change the file permission in Linux
<<   Back to Forum  |   Give us Feedback
10 Duke Stars available
This topic has 8 replies on 1 page.
Sankar_Gandhi
Posts:4
Registered: 2/19/07
How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 2:25 AM

 
Hi all,
I'm running in a issue related to changing the file permission under Linux environment. I'm using Suse 10.0 Linux and run jdk 1.5 java runtime.

I want to create a file during execution time and change the permission of the same. I use File.createNewFile() API to create file. By default it does not have all the 3 permission (read,write,execute) for all the user ( user,owner,group). Through program I have to change the file permission ( equal to execute ,chmod 777 filename).I donot want to use runtime.exec() with chmod as the argument , since it creates a process to change the permission which may impact the performance. I want to try FilePermission class , by giving filename and permission as the argument. But it is not changing the permission . Could any one faced this problem ?

If any body guides me, how to change the permission of the file under Linux using FilePermission class , it would be helpful.

Thanks,
Sankar
 
ekupcik
Posts:163
Registered: 8/7/97
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 3:08 AM (reply 1 of 8)  (In reply to original post )

 
FilePermission has nothing to do with the permissions in the filesystem. It used by the java SecurityManager only.

But JDK 1.6 should provide what you are looking for. In 1.6 java.io.File has methods like setExecuteable(boolean), setWriteable(boolean),....
 
glevner
Posts:124
Registered: 5/23/05
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 4:30 AM (reply 2 of 8)  (In reply to original post )

 
In Java 1.5 I know of only two solutions. One, as you say, is to exec chmod. That will indeed fork a process. The alternative is to use JNI and invoke the chmod() system call yourself. JNI also has some overhead, but presumably less than a fork. Try out both, and let us know which is faster...
 
BIJ001
Posts:6,897
Registered: 2003.06.06.
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 4:47 AM (reply 3 of 8)  (In reply to original post )

 
As a general solution, you could set your umask in an appropriate way prior to starting the java program, if it is okay that all files will be created with that one permission set.
 
Sankar_Gandhi
Posts:4
Registered: 2/19/07
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 9:53 PM (reply 4 of 8)  (In reply to #1 )

 
Thanks for reply ekupcik ,

Can I confirm , there is no alternative method in JDK 1.5 to change the file permission other than Runtime.exec() method.In that case I will try to optimize the ovrehead in causing in Runtime.exec().
 
Sankar_Gandhi
Posts:4
Registered: 2/19/07
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 9:55 PM (reply 5 of 8)  (In reply to #2 )

 
Thanks glevner,

I will let you peoples know the performance comparision of both method
 
Sankar_Gandhi
Posts:4
Registered: 2/19/07
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 10:59 PM (reply 6 of 8)  (In reply to #3 )

 
Thanks BIJ001 ,

How do I set the Umask prior to starting the program ? Could pls explain in steps.
 
glevner
Posts:124
Registered: 5/23/05
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 11:30 PM (reply 7 of 8)  (In reply to #6 )

 
How do I set the Umask prior to starting the program
? Could pls explain in steps.

I don't think umask can help you. The mode mask prevents permission bits from being set when a file is created, but does not force any bits to be set. So if execute permission is not set explicitly when a file is created, umask will do nothing about it.

But if you want more information about umask, see the man page for your shell script (sh, csh, etc.) or "man 2 umask".
 
ejp
Posts:26,294
Registered: 11/5/97
Re: How to use FilePermission class to change the file permission in Linux   
Feb 19, 2007 11:35 PM (reply 8 of 8)  (In reply to #7 )

 
So tell us, you are writing a program which creates scripts or copies executable binaries. Otherwise you wouldn't be setting the 'x' permission at all. So how many of these is your program going to create per second and how much impact is the cost of Runtime.exec() going to have? Given that there is no alternative?
 
This topic has 8 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 29
  • Guests : 132

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums