disk_info_t *create_disk(const char *device_name, disk_info_t **new_disk_info) { disk_info_t *follow_disk_info; u32 major, minor; u64 size_in_kilobytes = 0; int len; char buf[128], *vendor, *model, *ptr; partition_info_t *new_partition_info, **follow_partition_list; if(!new_disk_info) return NULL; *new_disk_info = NULL; // initial value. vendor = NULL; model = NULL; if(!device_name || !is_disk_name(device_name)) return NULL; if(!initial_disk_data(&follow_disk_info)) return NULL; len = strlen(device_name); follow_disk_info->device = (char *)malloc(len+1); if (!follow_disk_info->device){ free_disk_data(follow_disk_info); return NULL; } strcpy(follow_disk_info->device, device_name); if(!get_disk_major_minor(device_name, &major, &minor)){ free_disk_data(follow_disk_info); return NULL; } follow_disk_info->major = major; follow_disk_info->minor = minor; if(!get_disk_size(device_name, &size_in_kilobytes)){ free_disk_data(follow_disk_info); return NULL; } follow_disk_info->size_in_kilobytes = size_in_kilobytes; if(!strncmp(device_name, "sd", 2)){ if(!get_usb_root_port_by_device(device_name, buf, sizeof(buf))){ free_disk_data(follow_disk_info); return NULL; } len = strlen(buf); if(len > 0){ int port_num = get_usb_root_port_number(buf); if (port_num < 0) port_num = 0; follow_disk_info->port_root = port_num; } // start get vendor. if(!get_disk_vendor(device_name, buf, sizeof(buf))){ free_disk_data(follow_disk_info); return NULL; } len = strlen(buf); if(len > 0){ vendor = (char *)malloc(len+1); if(!vendor){ free_disk_data(follow_disk_info); return NULL; } strcpy(vendor, buf); strntrim(vendor); sanity_name(vendor); follow_disk_info->vendor = vendor; } // start get model. if(get_disk_model(device_name, buf, sizeof(buf)) == NULL){ free_disk_data(follow_disk_info); return NULL; } len = strlen(buf); if(len > 0){ model = (char *)malloc(len+1); if(!model){ free_disk_data(follow_disk_info); return NULL; } strcpy(model, buf); strntrim(model); sanity_name(model); follow_disk_info->model = model; } // get USB's tag memset(buf, 0, sizeof(buf)); len = 0; ptr = buf; if(vendor){ len += strlen(vendor); strcpy(ptr, vendor); ptr += len; } if(model){ if(len > 0){ ++len; // Add a space between vendor and model. strcpy(ptr, " "); ++ptr; } len += strlen(model); strcpy(ptr, model); ptr += len; } if(len > 0){ follow_disk_info->tag = (char *)malloc(len+1); if(!follow_disk_info->tag){ free_disk_data(follow_disk_info); return NULL; } strcpy(follow_disk_info->tag, buf); } else{ len = strlen(DEFAULT_USB_TAG); follow_disk_info->tag = (char *)malloc(len+1); if(!follow_disk_info->tag){ free_disk_data(follow_disk_info); return NULL; } strcpy(follow_disk_info->tag, DEFAULT_USB_TAG); } follow_partition_list = &(follow_disk_info->partitions); while(*follow_partition_list) follow_partition_list = &((*follow_partition_list)->next); new_partition_info = create_partition(device_name, follow_partition_list); if(new_partition_info){ new_partition_info->disk = follow_disk_info; ++(follow_disk_info->partition_number); ++(follow_disk_info->mounted_number); } } if(follow_disk_info->partition_number == 0) get_disk_partitionnumber(device_name, &(follow_disk_info->partition_number), &(follow_disk_info->mounted_number)); *new_disk_info = follow_disk_info; return *new_disk_info; }
disk_info_t *create_disk(const char *device_name, disk_info_t **new_disk_info){ disk_info_t *follow_disk_info; u32 major, minor; u64 size_in_kilobytes = 0; int len; char usb_node[32], port_path[8]; char buf[64], *port, *vendor = NULL, *model = NULL, *ptr; partition_info_t *new_partition_info, **follow_partition_list; if(new_disk_info == NULL){ usb_dbg("Bad input!!\n"); return NULL; } *new_disk_info = NULL; // initial value. if(device_name == NULL || !is_disk_name(device_name)) return NULL; if(initial_disk_data(&follow_disk_info) == NULL){ usb_dbg("No memory!!(follow_disk_info)\n"); return NULL; } len = strlen(device_name); follow_disk_info->device = (char *)malloc(len+1); if(follow_disk_info->device == NULL){ usb_dbg("No memory!!(follow_disk_info->device)\n"); free_disk_data(&follow_disk_info); return NULL; } strcpy(follow_disk_info->device, device_name); follow_disk_info->device[len] = 0; if(!get_disk_major_minor(device_name, &major, &minor)){ usb_dbg("Fail to get disk's major and minor: %s.\n", device_name); free_disk_data(&follow_disk_info); return NULL; } follow_disk_info->major = major; follow_disk_info->minor = minor; if(!get_disk_size(device_name, &size_in_kilobytes)){ usb_dbg("Fail to get disk's size_in_kilobytes: %s.\n", device_name); free_disk_data(&follow_disk_info); return NULL; } follow_disk_info->size_in_kilobytes = size_in_kilobytes; if(!strncmp(device_name, "sd", 2)){ // Get USB node. if(get_usb_node_by_device(device_name, usb_node, 32) == NULL){ usb_dbg("(%s): Fail to get usb node.\n", device_name); free_disk_data(&follow_disk_info); return NULL; } if(get_path_by_node(usb_node, port_path, 8) == NULL){ usb_dbg("(%s): Fail to get usb path.\n", usb_node); free_disk_data(&follow_disk_info); return NULL; } // Get USB port. if(get_usb_port_by_string(usb_node, buf, 64) == NULL){ usb_dbg("Fail to get usb port: %s.\n", usb_node); free_disk_data(&follow_disk_info); return NULL; } len = strlen(buf); if(len > 0){ port = (char *)malloc(8); if(port == NULL){ usb_dbg("No memory!!(port)\n"); free_disk_data(&follow_disk_info); return NULL; } memset(port, 0, 8); strncpy(port, port_path, 8); follow_disk_info->port = port; } // start get vendor. if(get_disk_vendor(device_name, buf, 64) == NULL){ usb_dbg("Fail to get disk's vendor: %s.\n", device_name); free_disk_data(&follow_disk_info); return NULL; } len = strlen(buf); if(len > 0){ vendor = (char *)malloc(len+1); if(vendor == NULL){ usb_dbg("No memory!!(vendor)\n"); free_disk_data(&follow_disk_info); return NULL; } strncpy(vendor, buf, len); vendor[len] = 0; strntrim(vendor); follow_disk_info->vendor = vendor; } // start get model. if(get_disk_model(device_name, buf, 64) == NULL){ usb_dbg("Fail to get disk's model: %s.\n", device_name); free_disk_data(&follow_disk_info); return NULL; } len = strlen(buf); if(len > 0){ model = (char *)malloc(len+1); if(model == NULL){ usb_dbg("No memory!!(model)\n"); free_disk_data(&follow_disk_info); return NULL; } strncpy(model, buf, len); model[len] = 0; strntrim(model); follow_disk_info->model = model; } // get USB's tag memset(buf, 0, 64); len = 0; ptr = buf; if(vendor != NULL){ len += strlen(vendor); strcpy(ptr, vendor); ptr += len; } if(model != NULL){ if(len > 0){ ++len; // Add a space between vendor and model. strcpy(ptr, " "); ++ptr; } len += strlen(model); strcpy(ptr, model); ptr += len; } if(len > 0){ follow_disk_info->tag = (char *)malloc(len+1); if(follow_disk_info->tag == NULL){ usb_dbg("No memory!!(follow_disk_info->tag)\n"); free_disk_data(&follow_disk_info); return NULL; } strcpy(follow_disk_info->tag, buf); follow_disk_info->tag[len] = 0; } else{ len = strlen(DEFAULT_USB_TAG); follow_disk_info->tag = (char *)malloc(len+1); if(follow_disk_info->tag == NULL){ usb_dbg("No memory!!(follow_disk_info->tag)\n"); free_disk_data(&follow_disk_info); return NULL; } strcpy(follow_disk_info->tag, DEFAULT_USB_TAG); follow_disk_info->tag[len] = 0; } follow_partition_list = &(follow_disk_info->partitions); while(*follow_partition_list != NULL) follow_partition_list = &((*follow_partition_list)->next); new_partition_info = create_partition(device_name, follow_partition_list); if(new_partition_info != NULL){ new_partition_info->disk = follow_disk_info; ++(follow_disk_info->partition_number); ++(follow_disk_info->mounted_number); if(!strcmp(new_partition_info->device, follow_disk_info->device)) new_partition_info->size_in_kilobytes = follow_disk_info->size_in_kilobytes-4; } } if(!strcmp(follow_disk_info->device, follow_disk_info->partitions->device)) get_disk_partitionnumber(device_name, &(follow_disk_info->partition_number), &(follow_disk_info->mounted_number)); *new_disk_info = follow_disk_info; return *new_disk_info; }