int find_partition_label(const char *dev_name, char *label, int partition_order) { struct volume_id id; char dev_path[128]; char usb_port[8]; int port_num; char nvram_label[32], nvram_value[512]; if(label) *label = 0; memset(usb_port, 0, 8); if(get_usb_port_by_device(dev_name, usb_port, 8) == NULL) return 0; port_num = get_usb_port_number(usb_port); memset(nvram_label, 0, 32); sprintf(nvram_label, "usb_path%d_label%d", port_num, partition_order); memset(nvram_value, 0, 512); strncpy(nvram_value, nvram_safe_get(nvram_label), 512); if(strlen(nvram_value) > 0) { strcpy(label, nvram_value); return (label && *label != 0); } memset(dev_path, 0, 128); sprintf(dev_path, "/dev/%s", dev_name); memset(&id, 0x00, sizeof(id)); if((id.fd = open(dev_path, O_RDONLY)) < 0) return 0; volume_id_get_buffer(&id, 0, SB_BUFFER_SIZE); if(volume_id_probe_linux_swap(&id) == 0 || id.error) goto ret; if(volume_id_probe_ext(&id) == 0 || id.error) goto ret; if(volume_id_probe_vfat(&id) == 0 || id.error) goto ret; if(volume_id_probe_ntfs(&id) == 0 || id.error) goto ret; if(volume_id_probe_hfs_hfsplus(&id) == 0 || id.error) goto ret; ret: volume_id_free_buffer(&id); if(label && (*id.label != 0)) strcpy(label, id.label); else strcpy(label, " "); nvram_set(nvram_label, label); close(id.fd); return (label && *label != 0); }
char *get_path_by_node(const char *usb_node, char *buf, const int buf_size){ char usb_port[32], *hub_path; int port_num = 0, len; if(usb_node == NULL || buf == NULL || buf_size <= 0) return NULL; // Get USB port. if(get_usb_port_by_string(usb_node, usb_port, sizeof(usb_port)) == NULL) return NULL; port_num = get_usb_port_number(usb_port); if(port_num == 0) return NULL; if(strlen(usb_node) > (len = strlen(usb_port))){ hub_path = (char *)usb_node+len; snprintf(buf, buf_size, "%d%s", port_num, hub_path); } else snprintf(buf, buf_size, "%d", port_num); return buf; }
extern 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[64], *port, *vendor, *model, *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 port. if(get_usb_port_by_device(device_name, buf, 64) == NULL){ usb_dbg("Fail to get usb port: %s.\n", device_name); free_disk_data(&follow_disk_info); return NULL; } len = strlen(buf); if(len > 0){ port = (char *)malloc(2); if(port == NULL){ usb_dbg("No memory!!(port)\n"); free_disk_data(&follow_disk_info); return NULL; } memset(port, 0, 2); int port_num = get_usb_port_number(buf); if(port_num != -1) sprintf(port, "%d", port_num); else strcpy(port, "0"); 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; }