-1

Here I am trying to create a function to (eventually) support the wiping of drives using multiple specifications. The problem I am running in to, is when I try to write ZeroBlock to the disk, it gets written, but the code behaves as thou it has failed. the reason i say it worked is that it cleared the boot sector from the drive in my testing system

def WipeDisk(Drive, WipeSpec, Passes):
    DiskSize = int(System.HDD[Drive].Size)
    DiskSect = int(System.HDD[Drive].Sectors())
    SectSize = int(System.HDD[Drive].SectSz)
    System.HDD[Drive].Start = time.time()
    if (WipeSpec == "Zero"):
            with open("/dev/zero", "rb") as Zero:
                    ZeroBlock = Zero.read(SectSize)
            Zero.close()
            Pass = 0
            with open(System.HDD[Drive].Device, "wb") as Disk:
                    while (Pass < Passes):
                            Current = 1
                            while (Current < DiskSect):
                                    if (Disk.write(ZeroBlock)):
                                            if (Current %((DiskSect*Passes)/100) == 0):
                                                    (variable updates)
                                            if (Current == DiskSect):
                                                    Pass = (Pass+1)
                                    else:
                                            System.HDD[Drive].Error = 1
                                            Pass = Passes
                                            break
                                    Current = (Current+1)
                    if (Pass == Passes):
                            System.HDD[Drive].Current = Current
                            System.HDD[Drive].Percent = "100"
                            System.HDD[Drive].Complete = 1
                    Disk.close()
    else:
            print("Unknown Wipe Specification: "+WipeSpec)
AntonTakk
  • 3
  • 2
  • 2
    Can you better define "behaves as though it failed" – C.B. Mar 16 '15 at 19:46
  • the statements after if (Disk.write(ZeroBlock)): never executes. it jumps directly down to the associated else: where System.HDD[Drive].Error = 1 is set – AntonTakk Mar 16 '15 at 19:51
  • There's a bunch of code there that doesn't seem necessary, while other stuff is missing. That said, your code is very un-Pythonic to start with, in particular you haven't grokked the idea of `with` yet. I'd clean all that up before even trying to find any errors in it, chances are you find and fix the errors on the way. – Ulrich Eckhardt Mar 16 '15 at 20:35

1 Answers1

1

The documentation for file.write says:

Write a string to the file. There is no return value.

You appear to be incorrectly assuming that write returns some value indicating whether the write succeeded. It doesn't.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • not sure how ive managed to miss that line every time ive looked at the manual for .write() That being the case, how would I go about determining the success or failure of a write()? for data desctuction, I can't simply hope it works, I need to verify – AntonTakk Mar 16 '15 at 20:06
  • @AntonTakk: It should raise an exception if there is some kind of I/O error. However, if you want to be extra sure for security purposes, you should read back the values from the disk and verify that they are zero. – BrenBarn Mar 16 '15 at 20:11