Avoid file_object, it's not necessary to keep it. --- src/usb-device-cd.c | 116 +++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/src/usb-device-cd.c b/src/usb-device-cd.c index 03b8f5ee..c33bb290 100644 --- a/src/usb-device-cd.c +++ b/src/usb-device-cd.c @@ -48,7 +48,6 @@ typedef struct SpiceCdLU { char *filename; - GFile *file_object; GFileInputStream *stream; uint64_t size; uint32_t blockSize; @@ -95,7 +94,6 @@ typedef struct SpiceUsbEmulatedDevice UsbCd; static int cd_device_open_stream(SpiceCdLU *unit, const char *filename) { - int error = 0; unit->device = 0; if (!unit->filename && !filename) { @@ -111,33 +109,32 @@ static int cd_device_open_stream(SpiceCdLU *unit, const char *filename) } int fd = open(unit->filename, O_RDONLY | O_NONBLOCK); - if (fd > 0) { - struct stat file_stat = { 0 }; - if (fstat(fd, &file_stat) || file_stat.st_size == 0) { - file_stat.st_size = 0; - unit->device = 1; - if (!ioctl(fd, BLKGETSIZE64, &file_stat.st_size) && - !ioctl(fd, BLKSSZGET, &unit->blockSize)) { - } - } - unit->size = file_stat.st_size; - close(fd); - if (unit->size) { - unit->file_object = g_file_new_for_path(unit->filename); - unit->stream = g_file_read(unit->file_object, NULL, NULL); - } - if (!unit->stream) { - SPICE_DEBUG("%s: can't open stream on %s", __FUNCTION__, unit->filename); - g_clear_object(&unit->file_object); - error = -1; + if (fd < 0) { + SPICE_DEBUG("%s: can't open file %s", __FUNCTION__, unit->filename); + return -1; + } + + struct stat file_stat = { 0 }; + if (fstat(fd, &file_stat) || file_stat.st_size == 0) { + file_stat.st_size = 0; + unit->device = 1; + if (!ioctl(fd, BLKGETSIZE64, &file_stat.st_size) && + !ioctl(fd, BLKSSZGET, &unit->blockSize)) { } } - else { - SPICE_DEBUG("%s: can't open file %s", __FUNCTION__, unit->filename); - error = -1; + unit->size = file_stat.st_size; + close(fd); + if (unit->size) { + GFile *file_object = g_file_new_for_path(unit->filename); + unit->stream = g_file_read(file_object, NULL, NULL); + g_clear_object(&file_object); + } + if (!unit->stream) { + SPICE_DEBUG("%s: can't open stream on %s", __FUNCTION__, unit->filename); + return -1; } - return error; + return 0; } static int cd_device_load(SpiceCdLU *unit, gboolean load) @@ -235,7 +232,6 @@ static gboolean check_device(HANDLE h) static int cd_device_open_stream(SpiceCdLU *unit, const char *filename) { - int error = 0; HANDLE h; unit->device = 0; if (!unit->filename && !filename) { @@ -254,42 +250,41 @@ static int cd_device_open_stream(SpiceCdLU *unit, const char *filename) unit->filename = g_strdup(filename); } h = open_file(unit->filename); - if (h) { - LARGE_INTEGER size = { 0 }; - if (!GetFileSizeEx(h, &size)) { - uint64_t buffer[256]; - uint32_t res; - unit->device = check_device(h); - SPICE_DEBUG("%s: CD device %srecognized on %s", - __FUNCTION__, unit->device ? "" : "NOT ", unit->filename); - res = ioctl_out(h, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, - buffer, sizeof(buffer)); - if (!res) - { - DISK_GEOMETRY_EX *pg = (DISK_GEOMETRY_EX *)buffer; - unit->blockSize = pg->Geometry.BytesPerSector; - size = pg->DiskSize; - } else { - SPICE_DEBUG("%s: can't obtain size of %s (error %u)", - __FUNCTION__, unit->filename, res); - } - } - unit->size = size.QuadPart; - CloseHandle(h); - if (unit->size) { - unit->file_object = g_file_new_for_path(unit->filename); - unit->stream = g_file_read(unit->file_object, NULL, NULL); - } - if (!unit->stream) { - SPICE_DEBUG("%s: can't open stream on %s", __FUNCTION__, unit->filename); - g_clear_object(&unit->file_object); - error = -1; - } - } else { + if (!h) { SPICE_DEBUG("%s: can't open file %s", __FUNCTION__, unit->filename); - error = -1; + return -1; } - return error; + + LARGE_INTEGER size = { 0 }; + if (!GetFileSizeEx(h, &size)) { + uint64_t buffer[256]; + uint32_t res; + unit->device = check_device(h); + SPICE_DEBUG("%s: CD device %srecognized on %s", + __FUNCTION__, unit->device ? "" : "NOT ", unit->filename); + res = ioctl_out(h, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, + buffer, sizeof(buffer)); + if (!res) { + DISK_GEOMETRY_EX *pg = (DISK_GEOMETRY_EX *)buffer; + unit->blockSize = pg->Geometry.BytesPerSector; + size = pg->DiskSize; + } else { + SPICE_DEBUG("%s: can't obtain size of %s (error %u)", + __FUNCTION__, unit->filename, res); + } + } + unit->size = size.QuadPart; + CloseHandle(h); + if (unit->size) { + GFile *file_object = g_file_new_for_path(unit->filename); + unit->stream = g_file_read(file_object, NULL, NULL); + g_clear_object(&file_object); + } + if (!unit->stream) { + SPICE_DEBUG("%s: can't open stream on %s", __FUNCTION__, unit->filename); + return -1; + } + return 0; } static int cd_device_load(SpiceCdLU *unit, gboolean load) @@ -349,7 +344,6 @@ static gboolean open_stream(SpiceCdLU *unit, const char *filename) static void close_stream(SpiceCdLU *unit) { g_clear_object(&unit->stream); - g_clear_object(&unit->file_object); } static gboolean load_lun(UsbCd *d, int unit, gboolean load) -- 2.20.1 _______________________________________________ Spice-devel mailing list Spice-devel@xxxxxxxxxxxxxxxxxxxxx https://lists.freedesktop.org/mailman/listinfo/spice-devel