Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the disk full error and let program resume after getting free disk space

I am writing an application to run on LINUX, which writes to disk with fprintf & fwrite. I would like to be able to trap "disk full" errors, prompt the user to make more space and then resume operation as if nothing had happened. Is there any graceful solution for it?

like image 886
tianya Avatar asked Jan 13 '23 03:01

tianya


1 Answers

Check the return value of each call to fprintf() and fwrite(). If either call returns a negative value, check errno to see if errno is equal to EDQUOT or ENOSPC, see manpage for write (or in case of fprintf() maybe even ENOMEM, as mentioned in some manpages for fprintf but not in all). If so, you're probably out of disk space.

As for resuming the operation as if nothing ever happened; that's a little bit harder; you'll need to keep track of what data you successfully wrote to the disk, so that after you've notified the user and they've indicated that it's time to try again, you can resume writing that data from the point at which the error occurred. That means keeping the state of the write in a structure of some sort (i.e. not just on the stack) so that you can return from your writing-function and then resume it later on. (Either that, or do the writing in a separate thread, and have the thread notify the main thread and then block until the main thread notifies back that it's safe to continue... that might get a little tricky though)

like image 50
Jeremy Friesner Avatar answered Jan 16 '23 21:01

Jeremy Friesner