How can I create a corrupt file with specified file size?
By corrupt I mean make a empty file or take an actual f开发者_运维百科ile and corrupting it so it becomes unreadable.
dd if=/dev/urandom of=somefile bs=somesize count=1
See the man page for details on the size.
Burn the file onto a cd/dvd then lightly scratch the disk
Files can contain any arbitrary pattern of bytes, so there is no (digital) way to create a file that is "corrupted". You can certainly modify a file in an existing format (for example, an XML file) so that it no longer is valid in whatever format it's supposed to be, but it's still just a file on disk and is perfectly readable.
You generally need to physically alter the storage medium on which the file is stored, in order to make the file actually unreadable.
My guess is that your best way is to mount a FUSE filesystem which is modified to return errors for specific files. As FUSE really is a filesystem driver (albeit in userspace), it can throw back whatever error code you want.
精彩评论