Operating System - HP-UX
1820172 Members
4114 Online
109620 Solutions
New Discussion юеВ

Writting to a read only file throws NO exception!

 
Chamitha Wijesekera
Frequent Advisor

Writting to a read only file throws NO exception!

Hi, I have application that uses a code segment to make a file read only. However on my HP-UX machine the file is displayed as read only but when it is opened by the application and an attempt made to write to it, no exception is generated. I use the following test program to make a file read only and then attempt to write data to it afterwards. This operation completes successfully without the expected exception that should be generated not permitting me to write to a read only file. Any ideas on this?

By the way this same code works on another HP-UX machine I tested it on. Are there any patches associated with such an issue?

The test program I use is,

import java.io.*;

public class TestFile
{
public static void main(String[] args)
{
System.out.println("Hello World!");
TestFile ff= new TestFile();
ff.writeFile();
System.out.println("done");
}

public void writeFile()
{
try
{
File f = new File("./testfile.txt");
FileOutputStream fos = new FileOutputStream( f );
fos.write("aaaaaa".getBytes());
f.setReadOnly();

fos.close();


FileOutputStream fos1 = new FileOutputStream( f );
fos1.write("dddddddd".getBytes());

fos1.close();

}
catch(Exception e)
{
e.printStackTrace();
}

}
}
6 REPLIES 6

Re: Writting to a read only file throws NO exception!

CHeck the umask setting on the two different HPUX servers for the user that runs this code fragment... are they the same?

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Sanjay_6
Honored Contributor

Re: Writting to a read only file throws NO exception!

Hi,

If the application is run as superuser, it will overwrite/modify the file in question irrespective of who the owner is and the file permission.

If you want an exception thrown, modify your application to check the file permission before writing to the file. If it read only it should throw and exception else write to the file.

Hope this helps.

Regds
Chamitha Wijesekera
Frequent Advisor

Re: Writting to a read only file throws NO exception!

I did run the program as super user. However I also created the file and assigned it as read only as super user too. Therefore shouldnt it thrown an exception anyways?
Chamitha Wijesekera
Frequent Advisor

Re: Writting to a read only file throws NO exception!

The umask of both the machines are identicle
A. Clay Stephenson
Acclaimed Contributor

Re: Writting to a read only file throws NO exception!

I thought you might be running as super-user but I waited until more data was available for responding. As super-user, if you assigned the file mode 000, it would still not throw an exception. Uid (or euid) 0 checks NO permissions.
If it ain't broke, I can fix that.
Chamitha Wijesekera
Frequent Advisor

Re: Writting to a read only file throws NO exception!

I see. Well the wiered part is that when I run the program as super user on another HP-UX machine it throws an exception!