none
How will CFile deal with bad sectors? RRS feed

  • Question

  • Hi,

     

    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

    Sunday, February 28, 2016 2:33 AM

Answers

  • It depends if the problem with the bad sector causes an detectable error.  At the very least, you should use the CFile API correctly, and handle error conditions.  If an error occurs, then CFile::Open, for example, may return false, and it will set the details in the CFileException.

    Each other CFile operation is documented independently.  The general pattern is that the methods will return true if they succeed without error, and will return false when something goes wrong.

    File systems are cleverly designed to detect errors when they occur, but they are not perfectly robust.  Some failures can cause corruption.  There's no way to characterize what may happen in that case, but certainly, even the operating system itself often relies to some degree on the correct operation of the primary disk, so one could rightly expect a system to misbehave in nearly any manor including a deadlock.

    PS: Depending on how critical the operation of your software is, you're very likely wasting your time if you've already diagnosed the problem to be bad sectors.  Unless you're controlling fuel valves or robotic surgery software, I wouldn't give it much more thought.  If you're writing systems that are required to be robust in the face of disk failure, then you should leverage redundancy to reduce the probability of corruption.

    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Sunday, February 28, 2016 5:31 AM
  • "I just wonder what will CFile do when dealing with bad sectors?"

    CFile has no saying in this, it's up to the operating system/disk driver and the disk itself. The application or sometimes the entire OS may freeze if bad sectors are read.

    That said, this is a pointless question. If you have bad sectors on the hard disk then you replace the disk, you don't complain that a program freezes. Unless that program happen to be a disk data recovery program but I would guess that such programs do not use CFile so probably that's not the case here.

    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Sunday, February 28, 2016 4:57 AM
    Moderator
  • Additionally, you may test behavior of your program (handling of exceptions etc). The simplest - open a file on a network share and unplug the cable. IIRC,  a simulator of disk errors can be found in the WDK or hardware test kit.

    -- pa

    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Sunday, February 28, 2016 1:40 PM
  • At all I guess that this efford to handle bad sectors is a kind of too high and does not solve the real problem. I would not waste time in this...

    If a HDD has bad sectors than something really went wrong with this HDD. In general if you think there are bad sectors chkdsk should be used to check the entire HDD for bad sectors. In my experience I found out if a HDD starts to have bad sectors than it is time to replace this particular HDD as fast as possible. In most of the cases the HDD died a few weeks after I figured out there are bad sectors.

    At all all API functions using file I/O expect a correct non corrupt file system on the disk. If a sector is bad, than the file system is on this position corrupt. Therefore no file i/o function will guarantee the correct work. To be sure everything works correct a RAW disk access would be necessary.


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Tuesday, March 1, 2016 10:14 AM
    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Tuesday, March 1, 2016 10:11 AM

All replies

  • "I just wonder what will CFile do when dealing with bad sectors?"

    CFile has no saying in this, it's up to the operating system/disk driver and the disk itself. The application or sometimes the entire OS may freeze if bad sectors are read.

    That said, this is a pointless question. If you have bad sectors on the hard disk then you replace the disk, you don't complain that a program freezes. Unless that program happen to be a disk data recovery program but I would guess that such programs do not use CFile so probably that's not the case here.

    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Sunday, February 28, 2016 4:57 AM
    Moderator
  • It depends if the problem with the bad sector causes an detectable error.  At the very least, you should use the CFile API correctly, and handle error conditions.  If an error occurs, then CFile::Open, for example, may return false, and it will set the details in the CFileException.

    Each other CFile operation is documented independently.  The general pattern is that the methods will return true if they succeed without error, and will return false when something goes wrong.

    File systems are cleverly designed to detect errors when they occur, but they are not perfectly robust.  Some failures can cause corruption.  There's no way to characterize what may happen in that case, but certainly, even the operating system itself often relies to some degree on the correct operation of the primary disk, so one could rightly expect a system to misbehave in nearly any manor including a deadlock.

    PS: Depending on how critical the operation of your software is, you're very likely wasting your time if you've already diagnosed the problem to be bad sectors.  Unless you're controlling fuel valves or robotic surgery software, I wouldn't give it much more thought.  If you're writing systems that are required to be robust in the face of disk failure, then you should leverage redundancy to reduce the probability of corruption.

    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Sunday, February 28, 2016 5:31 AM
  • Agree with guys, there is absolutely nothing to do with that sectors from your bussiness. I've remember very old times for runing BIOS or some utilities that check entire disk and mark possible bad sectors on disk, if I want continue work normaly. But functionality time of my disk was counted... Simply you can make some first tests with opening that file and check success return value of that, then give to user relevant info.

    Regards...

    Duris_I

    Sunday, February 28, 2016 8:27 AM
  • Additionally, you may test behavior of your program (handling of exceptions etc). The simplest - open a file on a network share and unplug the cable. IIRC,  a simulator of disk errors can be found in the WDK or hardware test kit.

    -- pa

    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Sunday, February 28, 2016 1:40 PM
  • If I can also recommend, pls look at ReadFile or ReadFileEx APIs. There are also some maybe other benefits that you can implement and better diagnose some events.
    • Edited by Duris_I Sunday, February 28, 2016 4:03 PM
    Sunday, February 28, 2016 4:03 PM
  • Additionally, you may test behavior of your program (handling of exceptions etc). The simplest - open a file on a network share and unplug the cable. IIRC,  a simulator of disk errors can be found in the WDK or hardware test kit.

    -- pa

    Hi,

    Are you sure there is a simulator that can simulate the disk errors in WDK? I try to use "WDK simulate disk error" or "hardware test kit simulate disk error" on Google.com but cannot find a result. The only one I can find is this http://stackoverflow.com/questions/19793242/can-i-simulate-disk-errors-using-software-or-programming-so-chkdsk-will-return-e and no one give a valuable answer.

    Thanks

    Tuesday, March 1, 2016 9:51 AM
  • At all I guess that this efford to handle bad sectors is a kind of too high and does not solve the real problem. I would not waste time in this...

    If a HDD has bad sectors than something really went wrong with this HDD. In general if you think there are bad sectors chkdsk should be used to check the entire HDD for bad sectors. In my experience I found out if a HDD starts to have bad sectors than it is time to replace this particular HDD as fast as possible. In most of the cases the HDD died a few weeks after I figured out there are bad sectors.

    At all all API functions using file I/O expect a correct non corrupt file system on the disk. If a sector is bad, than the file system is on this position corrupt. Therefore no file i/o function will guarantee the correct work. To be sure everything works correct a RAW disk access would be necessary.


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Tuesday, March 1, 2016 10:14 AM
    • Marked as answer by tempc Saturday, January 26, 2019 3:18 AM
    Tuesday, March 1, 2016 10:11 AM
  • Are you sure there is a simulator that can simulate the disk errors in WDK?

    It used to be once, maybe it was removed from the current WDK or renamed. Try asking on specialized filesystem or storage forums.

    -- pa

    Tuesday, March 1, 2016 10:44 AM