How do I detect a file write error in C?

Posted by rich on Stack Overflow See other posts from Stack Overflow or by rich
Published on 2012-07-04T04:03:42Z Indexed on 2012/07/04 15:16 UTC
Read the original article Hit count: 178

Filed under:
|

I have an embedded environment where a user might insert or remove a USB flash drive. I would like to know if the drive has been removed, or if there is some other problem when I try to write to the drive. However, Linux just saves the information in its buffers and returns with no indicated error.
The computer I'm using comes with a 2.4.26 kernel and libc 2.3.2.
I'm mounting the drive this way:
i = mount(MEMORY_DEV_PATH, MEMORY_MNT_PATH, "vfat", MS_SYNCHRONOUS, NULL);
That works:
50:/root # mount
/dev/scsi/host0/bus0/target0/lun0/part1 on /mem type vfat (rw,sync)
50:/root #

Later, I try to copy a file to it:

int ifile, ofile;
ifile = open("/tmp/tmpmidi.mid", O_RDONLY);
if (ifile < 0)
{
    perror("open in");
    break;
}
ofile = open(current_file_name.c_str(), O_WRONLY | O_SYNC);
if (ofile < 0)
{
    perror("open out");
    break;
}
#define BUFSZ 256
char buffer[BUFSZ];
while (1)
{
    i = read(ifile, buffer, BUFSZ);
    if (i < 0)
    {
        perror("read");
        break;
    }
    j = write(ofile, buffer, i);
    if (j < 0)
    {
        perror("write");
        break;
    }
    if (i != j)
    {
        perror("Sizes wrong");
        break;
    }
    if (i < BUFSZ)
    {
        printf("Copy is finished, I hope\n");
        close(ifile);
        close(ofile);
        break;
    }
}

If this snippet of code is executed with a write-protected USB memory, the result is

Copy is finished, I hope

amid a flurry of error messages from the kernel on the console.
I believe the same thing would happen if I simply removed the USB drive (without unmounting it).
I have also fiddled with devfs. I figured out how to get it to automatically mount the drive, (with the REGISTER event) but it never seems to trigger the UNREGISTER when I pull out the memory.
How can I determine in my program whether I have successfully created a file?

Update 4 July: It was a silly oversight of me not to check the result from close(). Unfortunately, the file can be closed without error. So that didn't help. What about fsync()? That sounds like a good idea, but that didn't catch the error either.
There might be some interesting information in /sys if I had such a thing. I believe that didn't get added until 2.6.?.
The comment(s) about the quality of my flash drive are probably justified. It's one of the earlier ones. In fact, write protect switches seem to be extremely rare these days.
I think I have to use the overkill option: Create a file, unmount & remount the drive, and check to see if the file is there. If that doesn't solve my problem, then something is really messed up! Note to myself: Make sure the file you try to create isn't already there!
By the way, this does happen to be a C++ program. You can tell by the .c_str() which I had intended to edit out for simplicity.

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux