
Help on file write() on Unix using C ...
| The problem is, given the file descriptor, how will i know that it is
| valid at the time of write() ?
|
| Problem sequence ...
| 1. I open a file using open()
| 2. I start writing on the file descriptor using the write().
| 3. Some user from the unix prompt deletes the file I have opened.
| 4. When I write again to the same file descriptor, I get "no error".
| Whatever I write just vanishes with no clue where it is going.
|
| access() & stat() operates on file name, so if someone just deletes my
| file and create another file with the same file name, will fool access()
| & stat(). I want something which will work on the file descripter I
| have.
|
| No point in restricting the user from deleting the file.
|
| I can use fopen() & fwrite() or fstream() if needed. But the problem
| should be solved.
|
| I am working on HP-UX 9.x.
Your problem is platform specific and not really a C language problem.
C is making the appropriate requests to the operating system and it is
probably doing it entirely correctly.
I'll go ahead and explain what is happening.
UNIX (such as HP/UX) uses a filesystem where all files are just numerically
identified block collections. Files get names by the fact that a directory
entry (a name) points to the file by its number (called an inode for the
data structure used to store the status of the file).
Each file can have one or more names pointing to it and it keeps a count
of how many names. When you do "rm" you remove a name pointer and decrease
the count. When the count reaches zero, the file goes away.
When you open a file, the file descriptor (or FILE pointer) is associated
with the actual file inode, not the name. The name can go away and the
file is still open.
If there were two names, A and B, and you open it as A, remove A, then
write the file, you can read it as B and see the changes.
The fact that the file is open adds one to the count. Closing it subtracts
one, so it won't go away while it is open, even if it has no names. Your
writes do go into the file, but because it has no names, there is no other
way to get that data back out except by using that still open file descriptor
in that program.
It is a feature. It is actually used to create very temporary files that
go away when closed.
It is a platform dependent behaviour and is not specified, nor contradicted,
by the C standard.
If you want to find out if the file has been removed to zero names, the
fstat() function might be available on your system, which gives the status
on the basis of the open file descriptor. You get back a "struct stat" in
which is member "st_nlink" which should have a zero for the count, unless
the OS includes open counts, in which case you should have a one for the
count.
As for your original question which asked how to tell when the write()
is valid, the answer is that the write() is valid even though you think
you have deleted the file. This is because it is not really deleted.
If you have further questions, e-mail me directly, or post to the
newsgroup comp.unix.programmer.
--
-- *-----------------------------* Phil Howard KA9WGN * --
-- | Inturnet, Inc. | Senior Systems Administrator | --
-- | Business Internet Solutions | Senior Network Administrator | --
-- *-----------------------------* philh at intur.net * --