Help on file write() on Unix using C ... 
Author Message
 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.

All help appreciated.

Thanks

Bosco



Mon, 08 May 2000 03:00:00 GMT  
 Help on file write() on Unix using C ...


Quote:
> 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.

Either use the standard solution (FILE streams and the functions that work
with them) or repost this in comp.unix.programmer, where they are better
equipped to help with Unix-specific problems.

--
[-                               firewind                                   -]

[-          "You're just jealous because the voices talk to -me-."          -]
[-                   Have a good day, and enjoy your C.                     -]
[-          (on a crusade of grumpiness where grumpiness is due)            -]



Tue, 09 May 2000 03:00:00 GMT  
 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       * --



Tue, 09 May 2000 03:00:00 GMT  
 Help on file write() on Unix using C ...

Quote:

>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.

In unix this is also called unlink() and only removes the _directory_
entry for the file.  The file is still open and fully functional.
Sometimes unix programs use this 'feature' to hide a file.  You open(),
and immediately unlink() the file.  Until the process either close()s the
file or exits (closing it also), the file can be used normally.

Quote:
>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.

See above, it's going to the file.

Quote:

>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.

They're not 'fooled' - see above.

Quote:

>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 gather you want to keep others from messing with your file.
I suggest a chmod(S_IRUSR | S_IWUSR) (check the values of the S_? constants)
after you open it, making it read/write only for the owner.  Unless other users
have root permissions, they can't touch it.

--




Tue, 09 May 2000 03:00:00 GMT  
 Help on file write() on Unix using C ...

Quote:

> 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().

Hi Don Bosco Durai,

The "open()" and "write()" functions are not standard C functions.
You can simply and portably use the standard C functions "fopen()"
and "fwrite()" instead.

Quote:
> 3. Some user from the unix prompt deletes the file I have opened.

This si system specific. On my OS you can not delete a file while it
is being created (ie. before the "fclose()"). Please ask the Unix
programming experts about this. You'll find them in:

Quote:
> access() & stat() operates on file name, so if someone just deletes my

These are not standard C functions either. The behaviour of all non-
standard functions is very compiler specific.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 09 May 2000 03:00:00 GMT  
 Help on file write() on Unix using C ...


Quote:
> 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.

What are you trying to achieve?  Isn't the problem exactly the same
if the user deletes the file immediately after the program closes it,
or terminates?

If the program wants to read the file back and you want to be sure
it doesn't go away in the meantime, the answer is not to close
the file before you've done with it.  Duplicate your file descriptor
if necessary: the file will exist until all descriptors are closed.
Things will work even if the file name goes away.

--



Tue, 09 May 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. File rading writing help in UNIX

3. Two CS files (using namespaces)

4. using write() to write a struct to a file

5. Writing CGI using UNIX-C

6. Check the file size and number symbols in a file using C in Unix

7. file read/write errors under unix/Solaris

8. How to write rtf file of a word file using VC++

9. Binary file I/O using ofstream::write() blows away remaining data to end of file

10. resx files needed for cs - files ??

11. Help with writing and using DLL using Borland 3.1

12. CS files display in VS6 like CPP files ?

 

 
Powered by phpBB® Forum Software