简体   繁体   中英

How will CFile deal with bad sectors?

I write a Visual C++ program which will read a file via CFile. However, one user of the program told me that when he uses the program to read a file on a hard disk with bad sectors, the program will be deadlocked.

Since it is very hard to simulate a bad sector on my own environment, I just wonder what will CFile do when dealing with bad sectors?

Thanks

This is a filesystem/disk driver level issue. CFile itself likely has absolutely no logic at all for dealing with bad sectors. It will (presumably) make a system call to fill a fixed buffer with data read from a file handle.

If the bad data is causing your program to deadlock perhaps you should add an integrity check if possible. Eg checksum or hash? If you detect the corrupted data then you can notify the user and abort.

Otherwise the only reasonable solution is for the user to get a new disk drive.

When CFile performs a read, it boils down to the following code:

if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL))
    CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);

Basically, it's just a Win32 ReadFile() call, and if it doesn't succeed it throws an exception.

So I would expect an exception to be thrown in this case - perhaps something in your code is catching, swallowing and ignoring the exception? you could try to catch the exception when you're performing the read(s) and print a message then terminate the program. At least your program won't be 'deadlocked'.

On the other hand, the 'deadlock' could be Windows or some other system component (antivirus?) getting hung or taking a long, long time to determine that there's an error before it gets around to returning from the ReadFile() call. If your user is trying to read a disk with bad sectors on it, I'm not sure how much effort would be appropriate in making your program "work". That's kind of like trying to drive across a bridge that's been washed out and complaining that your car ends up in the river.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM