Ejemplo n.º 1
0
int get_partition_size(const char *partition_name, u64 *size_in_kilobytes)
{
	FILE *fp;
	char disk_name[4];
	char target_file[128], buf[16], *ptr;

	if(size_in_kilobytes == NULL)
		return 0;

	*size_in_kilobytes = 0; // initial value.

	if(!is_partition_name(partition_name, NULL))
		return 0;

	strncpy(disk_name, partition_name, 3);
	disk_name[3] = 0;

	sprintf(target_file, "%s/%s/%s/size", SYS_BLOCK, disk_name, partition_name);
	if((fp = fopen(target_file, "r")) == NULL)
		return 0;

	memset(buf, 0, sizeof(buf));
	ptr = fgets(buf, sizeof(buf), fp);
	fclose(fp);
	if(ptr == NULL)
		return 0;

	*size_in_kilobytes = ((u64)strtoll(buf, NULL, 10))/2;

	return 1;
}
Ejemplo n.º 2
0
char *get_disk_name(const char *string, char *buf, const int buf_size){
	int len;

	if(string == NULL || buf_size <= 0)
		return NULL;

	if(!is_disk_name(string) && !is_partition_name(string, NULL))
		return NULL;

	len = strlen(string);
	if(!is_disk_name(string)){
		while(isdigit(string[len-1]))
			--len;
	}

	if(len > buf_size)
		return NULL;

	memset(buf, 0, buf_size);
	strncpy(buf, string, len);

	return buf;
}
Ejemplo n.º 3
0
partition_info_t *create_partition(const char *device_name, partition_info_t **new_part_info)
{
	partition_info_t *follow_part_info;
	u32 partition_order;
	u64 size_in_kilobytes = 0, total_kilobytes = 0, used_kilobytes = 0;
	char buf1[PATH_MAX], buf2[64], buf3[PATH_MAX]; // options of mount info needs more buffer size.
	int len;

	if(new_part_info == NULL)
		return NULL;

	*new_part_info = NULL; // initial value.

	if(device_name == NULL || get_device_type_by_device(device_name) != DEVICE_TYPE_DISK)
		return NULL;

	if(!is_disk_name(device_name) && !is_partition_name(device_name, &partition_order))
		return NULL;

	if(initial_part_data(&follow_part_info) == NULL)
		return NULL;

	len = strlen(device_name);
	follow_part_info->device = (char *)malloc(len+1);
	if(!follow_part_info->device){
		free_partition_data(&follow_part_info);
		return NULL;
	}
	strncpy(follow_part_info->device, device_name, len);
	follow_part_info->device[len] = 0;

	follow_part_info->partition_order = partition_order;

	if(read_mount_data(device_name, buf1, buf2, buf3)){
		len = strlen(buf1);
		follow_part_info->mount_point = (char *)malloc(len+1);
		if(!follow_part_info->mount_point){
			free_partition_data(&follow_part_info);
			return NULL;
		}
		strncpy(follow_part_info->mount_point, buf1, len);
		follow_part_info->mount_point[len] = 0;

		len = strlen(buf2);
		follow_part_info->file_system = (char *)malloc(len+1);
		if(!follow_part_info->file_system){
			free_partition_data(&follow_part_info);
			return NULL;
		}
		strncpy(follow_part_info->file_system, buf2, len);
		follow_part_info->file_system[len] = 0;

		len = strlen(buf3);
		follow_part_info->permission = (char *)malloc(len+1);
		if(!follow_part_info->permission){
			free_partition_data(&follow_part_info);
			return NULL;
		}
		strncpy(follow_part_info->permission, buf3, len);
		follow_part_info->permission[len] = 0;

		if(get_mount_size(follow_part_info->mount_point, &total_kilobytes, &used_kilobytes)){
			follow_part_info->size_in_kilobytes = total_kilobytes;
			follow_part_info->used_kilobytes = used_kilobytes;
		}
	}
	else{
		if(is_disk_name(device_name)){	// Disk
			free_partition_data(&follow_part_info);
			return NULL;
		}
		else{
			len = strlen(PARTITION_TYPE_UNKNOWN);
			follow_part_info->file_system = (char *)malloc(len+1);
			if(!follow_part_info->file_system){
				free_partition_data(&follow_part_info);
				return NULL;
			}
			strncpy(follow_part_info->file_system, PARTITION_TYPE_UNKNOWN, len);
			follow_part_info->file_system[len] = 0;
			
			get_partition_size(device_name, &size_in_kilobytes);
			follow_part_info->size_in_kilobytes = size_in_kilobytes;
		}
	}

	*new_part_info = follow_part_info;

	return *new_part_info;
}
Ejemplo n.º 4
0
disk_info_t *read_disk_data(void)
{
	FILE *fp;
	char line[64], device_name[32];
	u32 major, minor;
	disk_info_t *disk_info_list, *parent_disk_info, **follow_disk_info_list;
	partition_info_t *new_partition_info, **follow_partition_list;
	unsigned long long device_size;

	fp = fopen(PARTITION_FILE, "r");
	if (!fp)
		return NULL;

	if (!fgets(line, sizeof(line), fp)) {
		fclose(fp);
		return NULL;
	}

	fgets(line, sizeof(line), fp);

	disk_info_list = NULL;

	while (fgets(line, sizeof(line), fp))
	{
		device_name[0] = 0;
		if (sscanf(line, "%u %u %llu %[^\n ]", &major, &minor, &device_size, device_name) != 4)
			continue;
		if(major != USB_DISK_MAJOR)
			continue;
		if(device_size < 10)
			continue;
		
		if (is_disk_name(device_name))
		{
			follow_disk_info_list = &disk_info_list;
			while(*follow_disk_info_list)
				follow_disk_info_list = &((*follow_disk_info_list)->next);
			create_disk(device_name, follow_disk_info_list);
		}
		else if (is_partition_name(device_name, NULL))
		{
			parent_disk_info = disk_info_list;
			while(1)
			{
				if (!parent_disk_info)
					goto info_exit;
				
				if (!strncmp(device_name, parent_disk_info->device, 3))
					break;
				
				parent_disk_info = parent_disk_info->next;
			}
			
			follow_partition_list = &(parent_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 = parent_disk_info;
		}
	}

info_exit:
	fclose(fp);
	return disk_info_list;
}
Ejemplo n.º 5
0
disk_info_t *read_disk_data(){
	disk_info_t *disk_info_list = NULL, *new_disk_info, **follow_disk_info_list;
	char *partition_info = read_whole_file(PARTITION_FILE);
	char *follow_info;
	char line[64], device_name[16];
	u32 major;
	disk_info_t *parent_disk_info;
	partition_info_t *new_partition_info, **follow_partition_list;
	u64 device_size;

	if(partition_info == NULL){
		usb_dbg("Failed to open \"%s\"!!\n", PARTITION_FILE);
		return disk_info_list;
	}
	follow_info = partition_info;

	memset(device_name, 0, 16);
	while(get_line_from_buffer(follow_info, line, 64) != NULL){
		follow_info += strlen(line);

		if(sscanf(line, "%u %*u %llu %[^\n ]", &major, &device_size, device_name) != 3)
			continue;
		if(major != USB_DISK_MAJOR)
			continue;
		if(device_size == 1) // extend partition.
			continue;

		if(is_disk_name(device_name)){ // Disk
			follow_disk_info_list = &disk_info_list;
			while(*follow_disk_info_list != NULL)
				follow_disk_info_list = &((*follow_disk_info_list)->next);

			new_disk_info = create_disk(device_name, follow_disk_info_list);
		}
		else if(is_partition_name(device_name, NULL)){ // Partition
			// Found a partition device.
			// Find the parent disk.
			parent_disk_info = disk_info_list;
			while(1){
				if(parent_disk_info == NULL){
					usb_dbg("Error while parsing %s: found "
									"partition '%s' but haven't seen the disk device "
									"of which it is a part.\n", PARTITION_FILE, device_name);
					free(partition_info);
					return disk_info_list;
				}

				if(!strncmp(device_name, parent_disk_info->device, 3))
					break;

				parent_disk_info = parent_disk_info->next;
			}

			follow_partition_list = &(parent_disk_info->partitions);
			while(*follow_partition_list != NULL){
				if((*follow_partition_list)->partition_order == 0){
					free_partition_data(follow_partition_list);
					parent_disk_info->partitions = NULL;
					follow_partition_list = &(parent_disk_info->partitions);
				}
				else
					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 = parent_disk_info;
		}
	}

	free(partition_info);
	return disk_info_list;
}
Ejemplo n.º 6
0
partition_info_t *create_partition(const char *device_name, partition_info_t **new_part_info){
	partition_info_t *follow_part_info;
	char label[128];
	u32 partition_order = 0;
	u64 size_in_kilobytes = 0, total_kilobytes = 0, used_kilobytes = 0;
	char buf1[PATH_MAX], buf2[64], buf3[PATH_MAX]; // options of mount info needs more buffer size.
	int len;

	if(new_part_info == NULL){
		usb_dbg("Bad input!!\n");
		return NULL;
	}

	*new_part_info = NULL; // initial value.

	if(device_name == NULL || get_device_type_by_device(device_name) != DEVICE_TYPE_DISK)
		return NULL;

	if(!is_disk_name(device_name) && !is_partition_name(device_name, &partition_order))
		return NULL;

	if(initial_part_data(&follow_part_info) == NULL){
		usb_dbg("No memory!!(follow_part_info)\n");
		return NULL;
	}

	len = strlen(device_name);
	follow_part_info->device = (char *)malloc(len+1);
	if(follow_part_info->device == NULL){
		usb_dbg("No memory!!(follow_part_info->device)\n");
		free_partition_data(&follow_part_info);
		return NULL;
	}
	strncpy(follow_part_info->device, device_name, len);
	follow_part_info->device[len] = 0;

	if(find_partition_label(device_name, label)){
		strntrim(label);
		len = strlen(label);
		follow_part_info->label = (char *)malloc(len+1);
		if(follow_part_info->label == NULL){
			usb_dbg("No memory!!(follow_part_info->label)\n");
			free_partition_data(&follow_part_info);
			return NULL;
		}
		strncpy(follow_part_info->label, label, len);
		follow_part_info->label[len] = 0;
	}

	follow_part_info->partition_order = partition_order;

	if(read_mount_data(device_name, buf1, PATH_MAX, buf2, 64, buf3, PATH_MAX)){
		len = strlen(buf1);
		follow_part_info->mount_point = (char *)malloc(len+1);
		if(follow_part_info->mount_point == NULL){
			usb_dbg("No memory!!(follow_part_info->mount_point)\n");
			free_partition_data(&follow_part_info);
			return NULL;
		}
		strncpy(follow_part_info->mount_point, buf1, len);
		follow_part_info->mount_point[len] = 0;

		len = strlen(buf2);
		follow_part_info->file_system = (char *)malloc(len+1);
		if(follow_part_info->file_system == NULL){
			usb_dbg("No memory!!(follow_part_info->file_system)\n");
			free_partition_data(&follow_part_info);
			return NULL;
		}
		strncpy(follow_part_info->file_system, buf2, len);
		follow_part_info->file_system[len] = 0;

		len = strlen(buf3);
		follow_part_info->permission = (char *)malloc(len+1);
		if(follow_part_info->permission == NULL){
			usb_dbg("No memory!!(follow_part_info->permission)\n");
			free_partition_data(&follow_part_info);
			return NULL;
		}
		strncpy(follow_part_info->permission, buf3, len);
		follow_part_info->permission[len] = 0;

		if(get_mount_size(follow_part_info->mount_point, &total_kilobytes, &used_kilobytes)){
			follow_part_info->size_in_kilobytes = total_kilobytes;
			follow_part_info->used_kilobytes = used_kilobytes;
		}
	}
	else{
		/*if(is_disk_name(device_name)){	// Disk
			free_partition_data(&follow_part_info);
			return NULL;
		}
		else{//*/
			len = strlen(PARTITION_TYPE_UNKNOWN);
			follow_part_info->file_system = (char *)malloc(len+1);
			if(follow_part_info->file_system == NULL){
				usb_dbg("No memory!!(follow_part_info->file_system)\n");
				free_partition_data(&follow_part_info);
				return NULL;
			}
			strncpy(follow_part_info->file_system, PARTITION_TYPE_UNKNOWN, len);
			follow_part_info->file_system[len] = 0;

			get_partition_size(device_name, &size_in_kilobytes);
			follow_part_info->size_in_kilobytes = size_in_kilobytes;
		//}
	}

	*new_part_info = follow_part_info;

	return *new_part_info;
}