does bitcoind download bad blocks? i haven't read the source code for bitcoind, but i want to know if it downloads bad blocks which will later be rejected by the network (and presumably later removed from bitcoind's block files)?

i have a script which parses block files into a database and if a block is in the files at one point in time, then gets removed later then my script (as it currently stands) will end up importing the incorrect block.

if bitcoind does download bad blocks, how should i avoid importing these into my database? should i just wait and never import the latest, say, 5 blocks?
Bitcoin will store a block on disk as soon as it has verified its proof-of-work.

Blocks are stored append-only in the block files, and never deleted or overwritten. You cannot judge from the block files which are considered valid, or which are considered the currently best chain - that information is kept in the block index database.