⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions.c

📁 fdisk 实现源码,可以查询Linux下系统的分区信息
💻 C
📖 第 1 页 / 共 2 页
字号:
}int open_device() {	dev = ped_device_get(tempfile);	if (!ped_device_open(dev))		return 0;	return dev != NULL;}#define FAR 128LL/* If these are too far apart, return false */intare_near(long long a, long long b) {	a -= b;	if (a < 0LL) a = -a;	return (a < FAR); }/* I don't believe our tests require this now, but I leave this here */#if 0long long llrand() {	/* We assume that long long is always 64 bits and that int is always 32 bits,	   as per /usr/include/bits/types.h */	long long result;	result = (long long) rand();	result *= 4294967296;	result += (long long) rand();		return result;}#endifvoidnext_part_geometry(PedGeometry **geom,                   PedConstraint *constraint, int factor) {	PedSector start;	PedGeometry *temp;	if (!*geom)		 start = 0LL;	else {		start = (*geom)->end+1LL;		ped_geometry_destroy(*geom);	}	temp = ped_geometry_new(dev, start, PART_LENGTH*(long long)factor);	*geom = ped_constraint_solve_nearest(constraint,temp);	ped_geometry_destroy(temp);}int_mkpart (int how, PedSector start, PedSector end,                      PedPartitionType type, const PedFileSystemType* fs_type,                      PedPartition **newpart,                      int ch_mkpart (PedDisk*, PartPos *pos,                                     PedPartitionType, const PedFileSystemType*,                                     PedPartition **, UIOpts)){	int status;	string_gets = 0;	int strings;	PartPos pos = { { 0LL, NULL }, { 0LL, NULL } };	switch(how % 3) {		/* We create the partition by specifying start and end */		case 0:			strings = 1;				if (string[0][BUFSIZE] == 1) {				strings++;				string[0][BUFSIZE] = 2;			}			else				string[0][BUFSIZE] = 0;			uiquery.getstring = getstring;			strncpy(string[0], fs_type->name, BUFSIZE-1);						pos.start.sector = start;			pos.end.sector = end;			status = ch_mkpart(disk,&pos,type,NULL,newpart,			                 UI_DEFAULT);			fail_unless(string_gets == strings,				"User queried for %d strings instead of %d",				string_gets, strings);			break;		/* We test that giving the default string is OK */		case 1:			strncpy(string[0],DEFAULT_STRING,BUFSIZE);			uiquery.getstring = getstring;			pos.start.sector = start;			pos.end.sector = end;			status = ch_mkpart(disk,&pos,type,fs_type,newpart,			                 UI_CUSTOM_VALUES);			fail_unless(string_gets == 2,				"User queried for %d strings instead of 2",				string_gets);			break;		/* We query the start and end */		case 2:			uiquery.getstring = getstring;			string[0][BUFSIZE] = 2;			snprintf(string[0],BUFSIZE,"%llds",start);			snprintf(string[1],BUFSIZE,"%llds",end);			status = ch_mkpart(disk,&pos,type,fs_type,newpart,			                 UI_CUSTOM_VALUES);			fail_unless(string_gets == 2,				"User queried for %d strings instead of 2",				string_gets);			break;	}	return status;	}/* We use this for both resize and move, as they take the same params */int_resmov (int how, PedPartition *part, PedSector start, PedSector end,         UIOpts opts, int action (PedDisk*, PedPartition*,                                  PartPos *pos, UIOpts)){	PartPos pos = { { 0LL, NULL }, { 0LL, NULL } };	switch (how % 3) {		/* Specify start, end and partition */		case 0:			pos.start.sector = start;			pos.end.sector = end;			return action (disk, part, &pos, opts);		/* We specify start and end, ask for partition */		case 1:			uiquery.getint = NULL;			uiquery.getpart = getpart;			partition = part;			pos.start.sector = start;			pos.end.sector = end;			return action (disk, NULL, &pos, opts);		/* We ask for everything, the partition with number */		case 2:			uiquery.getpart = NULL;			uiquery.getstring = getstring;			uiquery.getint = getint;			integer = part->num;			string[0][BUFSIZE] = 2;			snprintf(string[0],BUFSIZE,"%llds",start);			snprintf(string[1],BUFSIZE,"%llds",end);			opts |= UI_CUSTOM_VALUES;			return action (disk, NULL, &pos, opts);	}}voidget_max_geometry(PedGeometry **geom, PedConstraint *constraint,                  PedPartition *part){	PedGeometry *temp = NULL;	if (*geom) ped_geometry_destroy(*geom);	PedSector start, end;	int i = 0;	/* We check if we have free space before or after the partition */	if (part->prev && part->prev->type & PED_PARTITION_FREESPACE)		start = part->prev->geom.start;	else		start = part->geom.start;	if (part->next && part->next->type & PED_PARTITION_FREESPACE)		end = part->next->geom.end;	else		end = part->geom.end;		temp = ped_geometry_new(dev, start, end-start+1LL);	*geom = ped_constraint_solve_nearest(constraint,temp);	ped_geometry_destroy(temp);}#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))/* NOTE: If you create a rather big test disk, you can make this use llrand          from functions.c instead. */voidrandomize_position(PedSector *start, PedSector *end, PedGeometry *where, int i){	/* This chooses random partition position within where */	/* If i is 0, we place it at the beginning, if i is 3, at the end */	if (i == 0) {		*start = where->start;		do {			*end = *start+((unsigned int)rand() % where->length)-1;			*end = MIN(*end,where->end);		} while (are_near(*start,*end));			}	else if (i == 3) {		*end = where->end;		do {			*start = *end-((unsigned int)rand() % where->length)+1;			*start = MAX(*start,where->start);		} while (are_near(*start,*end));	}	else {		PedSector mid = where->start +		                ((unsigned int) rand() % where->length)-1;		do {			*start = where->start+				((unsigned int) rand() % (mid - where->start));			*end = mid +				((unsigned int) rand() % (where->end - mid));			*start = MAX(*start,where->start);			*end = MIN(*end,where->end);		} while (are_near(*start,*end));	}}/* This is for our size changing checks. We check on all label types */voidchange_size (const char *ltype, UIOpts opts) {	PedConstraint *constraint = ped_device_get_constraint(dev);	PedGeometry *geom = NULL;	PedSector start, end, oldstart = -1, oldend = -1;	PedPartition *part = NULL;	PedFileSystemType *fs_type = ped_file_system_type_get("linux-swap");	int is_mac = !strcmp(ltype,"mac");	int i;	fail_unless(perform_mklabel(dev,&disk,ped_disk_type_get(ltype)),	            "We could create the %s label for resizing partitions",	            ltype);		/* NOTE: This might blow up if ltype is less than three chars */	/* FIXME: This checks works differently on LE and BE platforms */	srand(2 + opts + ((int) ltype));	/* We will resize create 4 partitions */	if (is_mac) {		part = ped_disk_get_partition(disk,1);		geom = ped_geometry_new(dev,part->geom.start,part->geom.length);		part = NULL;			}	for (i = 0; i < 4; i++) {				next_part_geometry(&geom, constraint, 1);		if (ped_disk_type_check_feature(disk->type,			PED_DISK_TYPE_PARTITION_NAME)) {			/* 1 means get second string on queue */			string[0][BUFSIZE] = 1;			strncpy(string[1],"Label",BUFSIZE);		}		else			string[0][BUFSIZE] = 0;		uiquery.getstring = getstring;		set_exception_error("Creating partition %d at %llds-%llds "			"with previous one at %llds-%llds on %s",			i, geom->start, geom->end, oldstart, oldend, ltype);		fail_unless(_mkpart (0, geom->start,  geom->end,				PED_PARTITION_NORMAL, 				fs_type, &part, (opts & UI_NO_FS_RESIZE ?				           perform_mkpart : perform_mkpartfs)),			"Failed to create partition for resize on %s. "			"Number %d, size %llds, start %llds, end %llds",			ltype, i, geom->length, geom->start, geom->end);		reset_exception_error();		oldstart = geom->start;		oldend = geom->end;	}	/* And we will resize them, 4 times each, moving them as near possible	   to end of the disk as a final result */	while (part && (!is_mac || part->num != 1)) {		get_max_geometry(&geom,constraint,part);		for (i = 0; i < 4; i++) {			randomize_position(&start,&end,geom,i);			set_exception_error("Resize %d of partition %d "			            "from %llds-%llds to %llds-%llds "			            "in region %llds-%llds on %s",		                    i, part->num, part->geom.start,			            part->geom.end, start, end, geom->start,			            geom->end, ltype);			/* Note that exact values fail on some partition 			   types, and when we are querying the user,			   exact values should be used... Or? FIXME? */			//fail_unless(_resmov (strncmp("ms",ltype,2) ? 0 : i, 			fail_unless(_resmov (i, part, start,			                     end, opts, perform_resize),			            "Could not do %d resize of partition %d "			            "from %llds-%llds to %llds-%llds "			            "in region %llds-%llds on %s",			            i, part->num, part->geom.start,			            part->geom.end, start, end, geom->start,			            geom->end, ltype);			fail_unless(are_near(part->geom.start,start) &&			           are_near(part->geom.end,end),			           "The %d resize of partition %d on %s wrong."			           "we wanted %llds-%llds, we got %llds-%llds",			           i, part->num, ltype, start, end,			           part->geom.start, part->geom.end);			reset_exception_error();		}		do {			part = part->prev;		} while (part && ((part->type & PED_PARTITION_METADATA) ||		         (part->type & PED_PARTITION_FREESPACE)));	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -