Operating System - HP-UX
1752843 Members
3710 Online
108789 Solutions
New Discussion юеВ

incorrect file permissions after Java createNewFile

 
Joyce Esker_1
Occasional Visitor

incorrect file permissions after Java createNewFile

We're creating a file in Java using a very simple program:
myFile = new File("/tmp/testFile.txt");
myFile.createNewFile();

For some reason on HP-UX this always creates a file with permissions:
-rwxrwxr-x

However if I perform "touch /tmp/testFile.txt" on HP-UX the file is created with permissions:
-rw-rw-r--

Also, the same java code above will create a file on Solaris, Linux, etc. with permissions:
-rw-rw-r--

My umask is set to:
2

java version 1.5.0.09
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0.09-_18_aug_2007_07_52)
Java HotSpot(TM) Server VM (build 1.5.0.09 jinteg:08.18.07-13:19 IA64, mixed mode)
8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: incorrect file permissions after Java createNewFile

Apparently Java ignores the usual HP-UX conventions when creating files.

When HP-UX creates a file via HP-UX commands (touch, vi, cat, etc.) it is created with permissions of 666 (rw-rw-rw-) and then the umask is applied to get the permissions you expect. In your case the umask of 002 yields 664 (rw-rw-r--).

It appears that Java is starting off with permissions of 777 (rwxrwxrwx) for the file and then applying the umask. So a umask of 002 yields 775 (rwxrwxr-x).

2 things I can think of to try are:

1) Change the permissions of the file to what you want in your java program.

2) Try a newer version of Java and see if that fixes the issue.
Steven E. Protter
Exalted Contributor

Re: incorrect file permissions after Java createNewFile

Shalom,

That is the default umask setting on the system of the user that starts java hot spot. You can alter it by altering the start up script.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: incorrect file permissions after Java createNewFile

>myFile = new File("/tmp/testFile.txt");

Is there another parm to File where you can provide the default permissions?
The system call creat(2) has one.
Michael J Mole
New Member

Re: incorrect file permissions after Java createNewFile

There is no way to specify permissions in Java 5. Java 6 allows you to set read/write/execute for user and all, but not at the granularity of user/group/other.
Michael J Mole
New Member

Re: incorrect file permissions after Java createNewFile

I also tried this (on behalf of Joyce on behalf of me) with Java 6 and see the same behavior. Permissions are still set as -rwxrwxr-x
James R. Ferguson
Acclaimed Contributor

Re: incorrect file permissions after Java createNewFile

Hi Joyce & Michael:

> I also tried this (on behalf of Joyce on behalf of me) with Java 6 and see the same behavior. Permissions are still set as -rwxrwxr-x

This suggests that the process using 'creat()' to create a file has supplied a 'mode' argument of '0777' when you use a umask of '0002'.

Two things come into play when a file is created. First, when a 'creat()' system call is performed, the code has the option of specifying an octal permissions value. The shell uses 666 for files. Whatever value is supplied, though, is subject to the application of the 'umaask' of the process. Hence if the 'umask' is 022 and the default permissions for the 'create()' are 666, the "subtraction" of the two means that the created file will have permissions of 644. With a creation mask of 666 and a 'umask' of 000, the file has permissions of 666.

If the code performing the file creation uses a creation mask of 644 (instead of 666), then even with a 'umask' of 000, a resulting file will have permissions of 644.

Hence, the creating code has the "upper limit" of the least restrictive permissions that will apply.

You can evaluate this behavior with this small Perl script:

# cat ./mymodes
#!/usr/bin/perl
use strict;
use Fcntl;
my $file = '/tmp/myfile';
unlink $file;
sysopen( FH, $file, O_CREAT, 0777 ) or die "Error: $!\n";
print "$file created\n";

Regards!

...JRF...
Joyce Esker_1
Occasional Visitor

Re: incorrect file permissions after Java createNewFile

Also note that we posted here because the behavior seems to be unique to HP environment; same code sequence yields different/expected results on other platforms.
Dennis Handly
Acclaimed Contributor

Re: incorrect file permissions after Java createNewFile

>we posted here because the behavior seems to be unique to HP environment; same code sequence yields different/expected results on other platforms.

Then you should be talking to the Response Center and see what they say.