On a linux system there's no need for special handling of devices. Just use the device file interface.
#Python 3 (byte strings) - 141 bytes
Python 3 (byte strings) - 141 bytes
d=input()
f=open(d,'r+b')
z=f.seek
z(0,2)
s=f.tell()
i=0
while i<2:
z(0);f.write([b'\0',b'\xff'][i]*s);f.flush();z(0)
if f.read()==x:i+=1
It's fairly straightforward, and not really optimised heavily, but it works. Here's a basic rundown.
- Get input (device file path)
- Open device file
- Seek to the end, get filesize (block devices are always their real size)
- enter write-and-check loop
- construct 0-bit and 1-bit strings (x)
- write bitstring
- flush output (I could have set buffering=0 but this is shorter)
- test file against x, and increment step of loop if it passes
exit loop when the increment is high enough
As a bonus, you could modify this for any set and number of byte-modification patterns, like 0x55/0xaa for stronger overwriting effects.
I did actually test this on a device file, using loopback. However, I'm not 100% sure the checking actually works. It might be necessary to close and reopen the file each pass, due to buffering behaviors. I would hope flush prevents this.
*edited to incorporate some suggestions in the comments