It is my understanding that the kernel provides buffering for the `write'
system call. That is, `write' only writes to the kernel's buffers and only
later is the data actually written to disk. I am wondering about the case
when the disk is full. Is it possible for the `write' to succeed in this
situation, i.e., write to the kernel buffer succeeds but the failure only
happens later when the buffer is written to disk? It seems like this can
happen with NFS mounted disks.
What about `close'? I know that one should always check the status of
`fclose' because it also flushes any buffered data. Does something similar
happen with `close'?
The reason that I am wondering about this is that the following code
fragment ``failed'':
while ((n = read (fdfrom, buf, sizeof(buf))) > 0)
{
if (n != write (fdto, buf, n))
{
fprintf (stderr, "write failed.\n");
exit (-1);
}
}
close (fdto);
close (fdfrom);
(void) unlink (from);
This code fragment is used to copy a file from one called 'from' with file
descriptor 'fdfrom' to another with descriptor 'fdto'. If the copy is
successful, the original file ('from') is deleted. If a write error occurs,
the program exits. By failed, I mean that the file 'from' was deleted even
though the disk was full, that is, it appears that the 'write' succeeded.
Note:
1. The file 'from' contained about 200 bytes of information
2. It was opened via:
if ((fdfrom = open (from, O_RDONLY, 0666)) < 0)
exit_error ("Unable to open input file.", 1);
Comments?
Thanks,
--JOhn