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

📄 common.c

📁 fdisk 实现源码,可以查询Linux下系统的分区信息
💻 C
📖 第 1 页 / 共 5 页
字号:
	ped_geometry_destroy(new_geom);}/* This functions constructs a constraint from the following information: *      start, is_start_exact, end, is_end_exact. *       * If is_start_exact == 1, then the constraint requires start be as given in * "start".  Otherwise, the constraint does not set any requirements on the * start. */PedConstraint*constraint_from_start_end (PedDevice* dev, PedGeometry* range_start,                           PedGeometry* range_end){        return ped_constraint_new (ped_alignment_any, ped_alignment_any,                range_start, range_end, 1, dev->length);}PedConstraint*constraint_intersect_and_destroy (PedConstraint* a, PedConstraint* b){        PedConstraint* result = ped_constraint_intersect (a, b);        ped_constraint_destroy (a);        ped_constraint_destroy (b);        return result;}/* This function returns the closest possible start and end for a partition   in the region first-last */intfix_part_position(PedDevice *dev, PedSector *start, PedSector *end,                  PedSector first, PedSector last){	/* Current geometry */	PedGeometry *geom = ped_geometry_new (dev,*start,*end-*start+1);	if (!geom) return 0;	/* Possible range */	PedGeometry *range = ped_geometry_new (dev, first, last-first+1);	if (!range) {		ped_geometry_destroy(geom);		return 0;	}	PedConstraint *constraint = constraint_intersect_and_destroy(			constraint_from_start_end (dev, range, range),			ped_device_get_constraint (dev));	PedGeometry *new_geom = ped_constraint_solve_nearest(constraint, geom);	ped_geometry_destroy(geom);	ped_geometry_destroy(range);	if (new_geom) {		if (*start != new_geom->start || *end != new_geom->end) {			*start = new_geom->start;			*end = new_geom->end;					}		ped_geometry_destroy(new_geom);		return 1;	} 	return 0;}/* PedSector is signed, so we can use this to move in both directions */static intmove_sector(PedDevice *dev, SectPos *value, PedSector shift) {	/* FIXME: I don't believe that we will have problems with llint overflow                  but please note that it is possible. */	/* We will store the new values in temporary variables */	PedSector start, end, sector;	sector = value->sector + shift;	/* If there is no range, we simply move the sector */	if (!value->range) {		if (sector >= dev->length || sector < 0LL)			return 0;		value->sector = sector;		return 1;	}	start = value->range->start + shift;	end = value->range->end + shift;	/* We can't move the sector out of the device */	if (end < 0LL) return 0;	if (start >= dev->length) return 0;	sector = value->sector + shift;	/* We ensure that all values are within the device */	if (start < 0LL) {		start = 0LL;		if (sector < 0LL)			sector = 0LL;	} 	if (end >= dev->length) {		end = dev->length - 1LL;		if (sector >= dev->length)			sector = dev->length - 1LL;	}	if (!ped_geometry_init(value->range, dev, start, end - start + 1LL))		return 0;	value->sector = sector;	return 1;	}#define ESC	'\033'	/* ^[ *//* TODO: This is not quite OK at the moment... *//* We ask the user where to put the partition in a region. Used for creating and resizing partitions *//* first and last mark the margins of the region, start and end mark the current values */intquery_part_position(const char* prompt, const void* context, PartPos *pos,                    PedSector first, PedSector last, PedDevice *dev,                    PedConstraint *constraint, UIOpts *opts) {		PedSector length = pos->end.sector - pos->start.sector + 1LL;	PedSector min_len = 0, max_len = dev->length;	PedGeometry *range = NULL;	int where,i,done;	char *temp,*temp2,*def_str, possibilities[6], buf[SMALLBUF];	/* We reset the ranges */	pos->start.range = NULL;	pos->end.range = NULL;	i = 0;	/* If we have a constraint, check whether we can move the partition start */	if (constraint && (constraint->start_align->grain_size == 0 || 	                   constraint->start_range->length == 1))		where = 's';	else {	/* If there the first possible sector is not the start of the selected partition,	we need one choice in the menu to make the start fixed and one to move it back.	When it is, 'b' and 's' do the same. We ditch 'b' for more friendly resize menu,	action_new should use 's' as it doesn't matter there. 	FIXME: I wrote this fragment better, but ruined it. Now I'm too lazy to fix it */		if (first < pos->start.sector) possibilities[i++] = 'b';		possibilities[i++] = 's';		possibilities[i++] = 'e';		possibilities[i++] = 'c';		possibilities [i++] = ESC;		possibilities [i] = '\0';		where = uiquery->getpartpos(prompt, context, possibilities);	}	if (where == ESC)		return 0;	else {		if (where == 'c') {			*opts |= UI_CUSTOM_VALUES;		} else {			*opts &= ~UI_CUSTOM_VALUES;			if (where == 's')				max_len = last - pos->start.sector + 1LL;			else				max_len = last - first + 1LL;			if (constraint) {				if (constraint->max_size < max_len)					max_len = constraint->max_size;				min_len = constraint->min_size;			}			done = 0;			while (!done) {				/* Put the max and min size on the prompt */				temp = ped_unit_format(dev,min_len);				temp2 = ped_unit_format(dev,max_len);				if (temp && temp2) {					snprintf(buf,SMALLBUF,_("Size (min %s, max %s)"),						temp, temp2);				} else {					strncpy(buf,_("Size"),SMALLBUF);				}				if (temp) ped_free(temp);				if (temp2) ped_free(temp2);				def_str = ped_unit_format(dev,length);				if (def_str) temp = strdup(def_str);				if (!uiquery->getstring(buf, &temp, NULL, NULL, 1)) {					if (temp) ped_free(temp);					if (def_str) ped_free(def_str);					return 0;				}					done = 1;				/* Get the new size only if the user modified it */				if(strcmp(temp,def_str)) {					if (!ped_unit_parse(temp,dev,&length,&range)) {						done = !ped_exception_throw(							PED_EXCEPTION_ERROR,							PED_EXCEPTION_RETRY_CANCEL,							_("%s is an invalid partition size"),							temp);						if (done)							return 0;					}					else if (length > max_len) {						length = max_len;					} else if (length < min_len) {						length = min_len;					}				}				if (temp) ped_free(temp);				if (def_str) ped_free(def_str);			}			/* If we place it at the beginning, we will have range			   with the end sector and conversly if we place it at			   the end. We set the desired range and then we move			   the sector with the range so that the sector is equal			   to the desired result */			if (where == 'b') {				/* As the start is not equal to the end of				   the partition, we will add some fuzz */				/* FIXME */				pos->start.range = ped_geometry_new(dev,							first, 64LL);				pos->start.sector = first;				/* Desired: end = first+length-1LL */				pos->end.sector = length;				pos->end.range = range;				move_sector(dev,&(pos->end),first-1LL);			}			else if (where == 'e') {				/* If the end has changed, we will add some fuzz */				if (pos->end.sector != last)					pos->end.range = ped_geometry_new(dev,							last-63LL, 64LL);				pos->end.sector = last;				/* Desired: start = last-length+1LL */ 				pos->start.sector = length;				pos->start.range = range;				move_sector(dev,&(pos->start),last-2*length+1LL);			}			else if (where == 's') {				/* Desired: end = start+length-1LL */				pos->end.sector = length;				pos->end.range = range;				move_sector(dev, (&pos->end),				            pos->start.sector-1LL);			}		}	}	return 1;	}intget_device(const char* prompt, PedDevice** value){	char*		dev_name = *value ? strdup((*value)->path) : NULL;	PedDevice*	dev;	if (!uiquery->getdev) {		if (!uiquery->getstring (prompt, &dev_name, NULL, NULL, 1))			return 0;		if (!dev_name)			return 0;		dev = ped_device_get (dev_name);		free (dev_name);		if (!dev)			return 0;	}	else {		if (!uiquery->getdev(prompt, &dev))			return 0;	}	*value = dev;	return 1;}intget_disk (const char* prompt, PedDisk** value){	PedDevice*	dev = *value ? (*value)->dev : NULL;	if (!get_device(prompt, &dev))		return 0;	if (dev != (*value)->dev) {		PedDisk* new_disk = ped_disk_new (dev);		if (!new_disk)			return 0;		*value = new_disk;	}	return 1;}/* These two use a different aproach, it leads to a little lack of consistency, but * this enables the use of a shiny interface in the UI */intget_partition (const char* prompt, PedDisk* disk, PedPartition** value){	PedPartition*	part;	int		num; 	char		buf[SMALLBUF];	if (!disk) 		return 0;	if (!uiquery->getpart) {		snprintf(buf,SMALLBUF,_("%s number (1-%d)"),		         prompt,ped_disk_get_last_partition_num (disk));		num = (*value) ? (*value)->num : 0;		if (!uiquery->getint (buf, &num)) {			ped_exception_throw (PED_EXCEPTION_ERROR,					     PED_EXCEPTION_CANCEL,					    _("Expecting a partition number."));			return 0;		}		part = ped_disk_get_partition (disk, num);		if (!part) {			ped_exception_throw (PED_EXCEPTION_ERROR,					     PED_EXCEPTION_CANCEL,					     _("Partition doesn't exist."));			return 0;		}		*value = part;	} else {		if (!uiquery->getpart(prompt, &disk, &part))			return 0;		*value = part;	}	return 1;}intget_disk_type (const char* prompt, const PedDiskType** value){	char*		disk_type_name = NULL;	PedDiskType*	disk_type;	int n;	if (!uiquery->getdisktype) {		if (*value) {			n = strlen((*value)->name);			disk_type_name = (char *) calloc(n,sizeof(char));			strncpy(disk_type_name,(*value)->name, n);		}		if (!uiquery->getstring (prompt, &disk_type_name, disk_type_list, NULL, -1))			return 0;		if (!disk_type_name) {			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,				     	_("Expecting a disk label type."));			return 0;		}		disk_type = ped_disk_type_get (disk_type_name);		free (disk_type_name);		if (!disk_type)			return 0;			} else {		if (!uiquery->getdisktype(prompt, &disk_type)) return 0;	}	*value = disk_type;	PED_ASSERT (*value != NULL, return 0);	return 1;}int_get_part_type (const char* prompt, const PedDisk* disk, PedPartitionType* type){	StrList*	opts = NULL;	StrList*	locopts = NULL;	char*		type_name = NULL;	int		status;	if (_can_create_primary (disk)) {		opts = str_list_append (opts, "primary");		locopts = str_list_append (locopts, _("primary"));	}	if (_can_create_extended (disk)) {		opts = str_list_append (opts, "extended");		locopts = str_list_append (locopts, _("extended"));	}	if (_can_create_logical (disk)) {		opts = str_list_append (opts, "logical");		locopts = str_list_append (locopts, _("logical"));	}	if (!opts) {		ped_exception_throw (			PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,			_("Can't create any more partitions."));		return 0;	}	status = uiquery->getstring(prompt,&type_name,opts,locopts,-1);		str_list_destroy (opts);	str_list_destroy (locopts);	if (!status) {		if(type_name) free(type_name);		return 0;	}	if (!type_name) {		ped_exception_throw (			PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,			_("Expecting a partition type."));		return 0;	}	if (!strcmp (type_name, "primary")) {		*type = 0;	}	if (!strcmp (type_name, "extended")) {		*type = PED_PARTITION_EXTENDED;	}	if (!strcmp (type_name, "logical")) {		*type = PED_PARTITION_LOGICAL;	}	free (type_name);	return 1;}int(*get_part_type) (const char* prompt, const PedDisk* disk,                  PedPartitionType* type) = _get_part_type;/* FIXME: I don't like this mkfs thing this way. Find a better one. */intget_fs_type (const char* prompt, const PedFileSystemType **value, int mkfs){	char*			fs_type_name = NULL;	PedFileSystemType*	fs_type;	int			n;	if (!uiquery->getfstype) {		if(*value) {			n = strlen((*value)->name);

⌨️ 快捷键说明

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