I wrote a C++ application on linux using cstdio. It contains one function which should overwrite a whole device with random data. It is given a file name, creates blocks of random data in memory and writes them one after the other using fwrite() to the file. If the device is full, the function should return. If there is a different write error, it should throw an exception. So when an error occurs I ask ferror() and want to recognize if this is a "disk is full" - a different error. I would expect to get ENOSPC when the disk is full. But the function writes to the disk and when it's full, the value returned by ferror() is 1 (which is EPERM). perror on the other hand prints the correct "No space left on device". Is this a bug in my C++ libs? Or is EPERM the correct error code? If it is the correct error code, is EPERM only returned when the disk is full and therefore suitable for recognizing this specific error?
ferror
only tells you whether there was an error on the stream. To see what the error actually was, you have to examine errno
.
perror
examines errno
which is why it prints the correct error message.
ferror
is not supposed to return an errno
value; it just returns a non-zero value if a stream error occurred. You should inspect errno
directly.
ferror just indicates that an error occured, something you should already know if you tested the result of the function you used to write (or the result of fclose). And POSIX mandates that those functions (fwrite, fputc, fprintf, fclose) set errno at a value describing the problem.
Is this a bug in my C++ libs
More likely to be a bug in the kernel or the device driver. But neither is likely.
May be because on ext
filesystems part of them by default is reserved for root
. So, there may be space left but the current user can't use it.
man mke2fs
-m reserved-blocks-percentage
Specify the percentage of the filesystem blocks reserved for the
super-user. This avoids fragmentation, and allows root-owned
daemons, such as syslogd(8), to continue to function correctly
after non-privileged processes are prevented from writing to the
filesystem. The default percentage is 5%.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.