diff --git a/linux/device.c b/linux/device.c index 58f287c..e91d55e 100644 --- a/linux/device.c +++ b/linux/device.c @@ -164,6 +164,7 @@ int32_t GetDeviceType(void* device_ctx) char* fc_path; char* sas_path; int ret; + char delim; char* chrptr; char* sysfs_path_scr; FILE* file; @@ -242,23 +243,26 @@ int32_t GetDeviceType(void* device_ctx) } ret = 0; - chrptr = strchr(dev_path, ':'); + delim = '.'; + chrptr = strrchr(dev_path, delim); if(!chrptr) { - chrptr = strrchr(dev_path, '.'); - if(!chrptr) - { - free((void*)sysfs_path); - free((void*)dev_path); - free((void*)host_no); - free((void*)iscsi_path); - free((void*)scsi_path); - free((void*)spi_path); - free((void*)fc_path); - free((void*)sas_path); - return dev_type; - } + delim = ':'; + chrptr = strrchr(dev_path, delim); + } + + if(!chrptr) + { + free((void*)sysfs_path); + free((void*)dev_path); + free((void*)host_no); + free((void*)iscsi_path); + free((void*)scsi_path); + free((void*)spi_path); + free((void*)fc_path); + free((void*)sas_path); + return dev_type; } chrptr--; @@ -270,8 +274,15 @@ int32_t GetDeviceType(void* device_ctx) chrptr++; break; } + else if(chrptr[0] == delim) + { + ret = 0; + } + else + { + ret++; + } - ret++; chrptr--; } diff --git a/linux/list_devices.c b/linux/list_devices.c index 37a7da9..7d77e3c 100644 --- a/linux/list_devices.c +++ b/linux/list_devices.c @@ -49,6 +49,8 @@ DeviceInfoList* ListDevices() udev = udev_new(); has_udev = udev != 0; +#else + DeviceContext tmp_ctx; #endif dir = opendir(PATH_SYS_DEVBLOCK); @@ -58,7 +60,7 @@ DeviceInfoList* ListDevices() while(dirent) { - if(dirent->d_type != DT_REG && dirent->d_type != DT_LNK) + if((dirent->d_type != DT_DIR && dirent->d_type != DT_LNK) || dirent->d_name[0] == '.') { dirent = readdir(dir); continue; @@ -137,11 +139,10 @@ DeviceInfoList* ListDevices() #else // Use sysfs if(!has_udev && !strstr(dirent->d_name, "loop")) { - tmp_string = malloc(1024); - memset((void*)tmp_string, 0, 1024); - snprintf((char*)tmp_string, 1024, "/dev/%s", dirent->d_name); + memset((void*)tmp_ctx.device_path, 0, 4096); + snprintf((char*)tmp_ctx.device_path, 4096, "/dev/%s", dirent->d_name); - switch(GetDeviceType(tmp_string)) + switch(GetDeviceType(&tmp_ctx)) { case AARUREMOTE_DEVICE_TYPE_ATA: strncpy(list_next->this.bus, "ATA", 256); break; case AARUREMOTE_DEVICE_TYPE_ATAPI: strncpy(list_next->this.bus, "ATAPI", 256); break; @@ -149,6 +150,7 @@ DeviceInfoList* ListDevices() case AARUREMOTE_DEVICE_TYPE_SECURE_DIGITAL: strncpy(list_next->this.bus, "MMC/SD", 256); break; case AARUREMOTE_DEVICE_TYPE_NVME: strncpy(list_next->this.bus, "NVMe", 256); break; case AARUREMOTE_DEVICE_TYPE_SCSI: + tmp_string = malloc(1024); memset((void*)tmp_string, 0, 1024); snprintf((char*)tmp_string, 1024, "%s/%s/device", PATH_SYS_DEVBLOCK, dirent->d_name); line_str = malloc(1024); @@ -208,11 +210,10 @@ DeviceInfoList* ListDevices() free(line_str); } + free((void*)tmp_string); break; default: memset(&list_next->this.bus, 0, 256); break; } - - free((void*)tmp_string); } #endif