How to detect a complete disk error and resume the program after receiving free disk space

I am writing a LINUX application that writes to disk with fprintf and fwrite. I would like you to have error traps on the disk, ask the user to make more space, and then resume, as if nothing had happened. Is there an elegant solution for this?

+4
source share
3 answers

You can reserve space in large chunks (say, 64 KB or 1 MB) and use custom wrappers for fwrite and fprintf to make sure that data is written only in an already reserved area. These shells will also allocate new disk space for your files as needed. Then you will only have a few points in your code where β€œexit from disk space” may occur, and this error is relatively easy to repair if you know what you just highlighted.

+1
source

Check the return value of each call on fprintf() and fwrite() . If any of the calls returns a negative value, check errno to see if errno is equal to EDQUOT or ENOSPC , see the manpage for writing (or in the case of fprintf() may even be ENOMEM , as indicated in some manpages for fprintf , but not all ) If so, you are probably out of disk space.

As for the resumption of the operation, as if nothing had happened; which is a little more complicated; you will need to keep track of what data was successfully written to disk so that after you notify the user and indicate that it is time to try again, you can resume recording this data from the place where the error occurred. This means saving the state of the record in some structure (i.e., not only on the stack) so that you can return from your recording function and then resume it later. (Either this, or record in a separate thread, and ask the thread to inform the main thread, and then block until the main thread notifies you that it is safe to continue ... it can be a bit complicated, though)

+3
source

If you can use boost , then it's pretty simple.

boost :: filesystem :: space returns disk storage information. The Input to space method is the path to the file, and the result is a space_info structure that contains the capacity, free space, and available space. Read more about space_info here .

+2
source

Source: https://habr.com/ru/post/1486433/


All Articles