static void detach_disk_image_and_free(disk_image_t *image, vdrive_t *floppy, unsigned int unit) { disk_image_t *oldimg; if (floppy == NULL || floppy->image == NULL) return; oldimg = floppy->image; detach_disk_image(image, floppy, unit); if ((image != NULL) && (image == oldimg)) disk_image_destroy(image); }
static int attach_disk_image(disk_image_t **imgptr, vdrive_t *floppy, const char *filename, unsigned int unit, int devicetype) { disk_image_t *image; disk_image_t new_image; int err = -1; if (filename == NULL) { log_error(attach_log, "No name, cannot attach floppy image."); return -1; } new_image.gcr = NULL; new_image.p64 = lib_calloc(1, sizeof(TP64Image)); new_image.read_only = (unsigned int)attach_device_readonly_enabled[unit - 8]; switch (devicetype) { case ATTACH_DEVICE_NONE: case ATTACH_DEVICE_VIRT: case ATTACH_DEVICE_FS: new_image.device = DISK_IMAGE_DEVICE_FS; break; case ATTACH_DEVICE_RAW: new_image.device = DISK_IMAGE_DEVICE_RAW; break; } disk_image_media_create(&new_image); switch (devicetype) { case ATTACH_DEVICE_NONE: case ATTACH_DEVICE_VIRT: case ATTACH_DEVICE_FS: disk_image_fsimage_name_set(&new_image, filename); break; case ATTACH_DEVICE_RAW: disk_image_rawimage_driver_name_set(&new_image); break; } if (disk_image_open(&new_image) < 0) { P64ImageDestroy((PP64Image) new_image.p64); lib_free(new_image.p64); disk_image_media_destroy(&new_image); return -1; } detach_disk_image_and_free(*imgptr, floppy, unit); *imgptr = disk_image_create(); image = *imgptr; memcpy(image, &new_image, sizeof(disk_image_t)); switch (unit) { case 8: case 9: case 10: case 11: err = drive_image_attach(image, unit); err &= vdrive_attach_image(image, unit, floppy); err &= machine_drive_image_attach(image, unit); break; } if (err) { disk_image_close(image); disk_image_media_destroy(image); disk_image_destroy(image); *imgptr = NULL; } return err; }