File diff 03b0b1263122 → 8f2355969e7e
floppyemu/__init__.py
Show inline comments
 
import subprocess
 
import os
 

	
 

	
 
SINGLE_DISK_OFFSET = 1572864
 
MAX_IMAGES_ON_DRIVE = 1000
 
LABEL_START_OFFSET = 0x2B
 
LABEL_LENGTH = 11
 

	
 

	
 
def get_or_create_empty_image(index, temp_path, size=1440):
 
    filename = 'empty{}.ima'.format(index)
 
    filename = os.path.join(temp_path, filename)
 

	
 
    if os.path.isfile(filename):
 
        os.remove(filename)
 
    proc = subprocess.Popen(['mkfs.msdos', '-C', filename, str(size)])
 
    rval = proc.wait()
 
    if rval != 0:
 
        print(proc.stdout.read())
 
        print(proc.stderr.read())
 
        raise Exception('Not created')
 

	
 
    return filename
 

	
 

	
 
def format_images(device, indexes, temp_path, size=1440, callback=None):
 
@@ -95,13 +99,24 @@ def list_images(device):
 
                disksize = sectorsize * sectorcount
 
                if bootsector[0x26] == 0x29:
 
                    # label probably exists.
 
                    # As an additional precaution check fs type.
 
                    # If it's valid, then get label, otherwise consider disk unlabeled
 
                    if bootsector[0x36:0x3E] in (b'FAT12   ', b'FAT16   ', b'FAT     '):
 
                        label = bootsector[0x2B:0x36].decode(encoding='cp1251')
 
                        label = bootsector[LABEL_START_OFFSET:LABEL_START_OFFSET + LABEL_LENGTH].decode(
 
                            encoding='cp1251'
 
                        )
 
                dfile.seek(SINGLE_DISK_OFFSET - 512, 1)
 
            else:
 
                disksize = 0
 
                dfile.seek(SINGLE_DISK_OFFSET - 512, 1)
 
            rval.append((label, disksize))
 
    return rval
 

	
 

	
 
def update_label(device, index, label):
 
    offset = SINGLE_DISK_OFFSET * index
 
    label = label[0:11]
 
    label = label + ' ' * (LABEL_LENGTH - len(label))
 
    with open(device, 'wb') as dfile:
 
        dfile.seek(offset + LABEL_START_OFFSET, 0)
 
        dfile.write(label.encode(encoding='cp1251'))