fdisk.c

来自「fdisk 实现源码,可以查询Linux下系统的分区信息」· C语言 代码 · 共 2,469 行 · 第 1/5 页

C
2,469
字号
}static 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;}#endifstatic void help_on (char* topic) {         FdiskCommand*	cmd;           cmd = fdisk_command_get (fdisk_main_menu_commands, topic);         if (!cmd) return;          fdisk_command_print_help (cmd); }static int_disk_reread(PedDisk **disk) {		PedDevice *dev = (*disk)->dev;	ped_disk_destroy(*disk);	*disk = ped_disk_new(dev);	uiquery.need_commit = 0;}static intdo_check (PedDisk** disk){	return perform_check(*disk,NULL);}static intdo_cp (PedDisk** disk){	return perform_cp(*disk,NULL,UI_WARN_COMMIT);}/* FIXME: Doesn't work correctly without getopt.h */void fdisk_print_options_help (){         int		i; #ifdef HAVE_GETOPT_H 	for (i=0; options_help [i][0] && options[i].val; i++) {#else #warning Printing help without getopt.h is broken. Fix it.	for (i=0; options_help [i][0]; i++) {#endif 		printf ("  -%c, --%-23.23s %s\n",#ifdef HAVE_GETOPT_H  			options [i].val,#else			options[i][0][0],#endif  			options_help [i][0],  			_(options_help [i][1]));  	} }voidfdisk_print_commands_help (FdiskCommand* cmds[]){	 int		i;  	if (cmds) {		for (i=0; cmds [i]; i++)			fdisk_command_print_summary (cmds [i]);	} else {		if (in_menu == 0) {			for (i=0; fdisk_main_menu_commands [i]; i++)				fdisk_command_print_summary (fdisk_main_menu_commands [i]);		}		else if (in_menu == 1) {			for (i=0; fdisk_ex_menu_commands [i]; i++)				fdisk_command_print_summary (fdisk_ex_menu_commands [i]);    		}		else if (in_menu == 2) {			for (i=0; fdisk_bsd_menu_commands [i]; i++)				fdisk_command_print_summary (fdisk_bsd_menu_commands [i]);    		}	}}static intdo_help(PedDisk** disk){	printf (_("Command action\n"));	if (in_menu == 0)		fdisk_print_commands_help(fdisk_main_menu_commands);	else if (in_menu == 1)		fdisk_print_commands_help(fdisk_ex_menu_commands);	else if (in_menu == 2)	        fdisk_print_commands_help(fdisk_bsd_menu_commands);	return 1;}static intdo_msdos_mklabel (PedDisk** disk){	PedDevice               *dev = (*disk)->dev; /* Save the address of dev, 							because we are going to 							destroy disk. */ 	const PedDiskType*	type = ped_disk_type_get ("msdos");	return perform_mklabel(dev,disk,type);}static intdo_sun_mklabel (PedDisk** disk){	PedDevice               *dev = (*disk)->dev; /* Save the address of dev, 							because we are going to 							destroy disk. */ 	const PedDiskType*	type = ped_disk_type_get ("sun");		return perform_mklabel(dev,disk,type);}static intdo_dvh_mklabel (PedDisk** disk){	PedDevice               *dev = (*disk)->dev; /* Save the address of dev, 							because we are going to 							destroy disk. */ 	const PedDiskType*	type = ped_disk_type_get ("dvh");	return perform_mklabel(dev,disk,type);}static intdo_mklabel (PedDisk** disk){	PedDevice               *dev = (*disk)->dev; /* Save the address of dev, 							because we are going to 							destroy disk. */ 	const PedDiskType*	type = ped_disk_probe (dev);	return perform_mklabel(dev,disk,type);}static intdo_mkfs (PedDisk** disk){	return perform_mkfs(*disk,NULL,NULL,UI_WARN_COMMIT);}static intdo_mkpart (PedDisk **disk){	PartPos			 pos;	PedPartition* 		 part;	PedPartitionType	 part_type = 0;	const PedFileSystemType	*fs_type = ped_file_system_type_get ("ext2");	char*			 peek_word;	UIOpts			 opts = UI_CUSTOM_VALUES;			if (!get_part_type (_("Partition type"), *disk,					       &part_type))		return 0;	peek_word = fdisk_command_line_peek_word ();	if (part_type == PED_PARTITION_EXTENDED	    || (peek_word && isdigit (peek_word[0]))) {		fs_type = NULL;	}	/* gfdisk should ask for filesystem, lfdisk should not */	else if (!fdisk_compatibility_mode) {		if (!get_fs_type (_("File system type"),					       &fs_type, 0))			return 0;	}	if (peek_word)		ped_free (peek_word);	pos.start.sector = 0LL;	pos.end.sector = 0LL;	pos.start.range = NULL;	pos.end.range = NULL;	if (!fdisk_compatibility_mode && fs_type->ops->create &&	    part_type != PED_PARTITION_EXTENDED &&	    command_line_prompt_boolean_question(		_("Do you want to create the filesystem on the partition?"))) 	{		return perform_mkpartfs (*disk, &pos, part_type, fs_type,		                         NULL, opts | UI_WARN_COMMIT);	}	else	{		return perform_mkpart (*disk, &pos, part_type, fs_type,		                       NULL, opts);	}	}#if 0static intdo_mkpartfs (PedDisk** disk){	PartPos pos;		pos.start.sector = 0LL;	pos.end.sector = 0LL;	pos.start.range = NULL;	pos.end.range = NULL;	return perform_mkpartfs (*disk, &pos, 0, NULL, NULL, 	                         UI_CUSTOM_VALUES | UI_WARN_COMMIT | 	                         UI_SPECIFY_PART_TYPE);}#endifstatic intdo_move (PedDisk** disk){	PartPos pos;	/* TODO: Make default position more friendly */	pos.start.sector = 0LL;	pos.end.sector = 0LL;	pos.start.range = NULL;	pos.end.range = NULL;	if(!perform_move (*disk, NULL, &pos, UI_CUSTOM_VALUES|UI_WARN_COMMIT)) {		ped_exception_throw(PED_EXCEPTION_ERROR,		                    PED_EXCEPTION_CANCEL,		         _("Partition move failed"));		if (uiquery.need_commit)				_disk_reread(disk);		return 0;	}	return 1;}static intdo_name (PedDisk** disk){	return perform_name(*disk, NULL, NULL);}/* TODO: REMOVE */#if 0static voidpartition_print_flags (PedPartition* part){	PedPartitionFlag	flag;	int			first_flag;	first_flag = 1;	for (flag = ped_partition_flag_next (0); flag;	     flag = ped_partition_flag_next (flag)) {		if (ped_partition_get_flag (part, flag)) {			if (first_flag)				first_flag = 0;			else				printf (", ");			printf (_(ped_partition_flag_get_name (flag)));		}	}}#endif/* Prints a sector out, first in compact form, and then with a percentage. * Eg: 32Gb (40%) */static voidprint_sector_compact_and_percent (PedSector sector, PedDevice* dev){	char* compact;	char* percent;	if (ped_unit_get_default() == PED_UNIT_PERCENT)		compact = ped_unit_format (dev, sector);	else		compact = ped_unit_format_custom (dev, sector,						  PED_UNIT_COMPACT);	percent = ped_unit_format_custom (dev, sector, PED_UNIT_PERCENT);	printf ("%s (%s)\n", compact, percent);	ped_free (compact);	ped_free (percent);}static intpartition_print (PedPartition* part){	PedFileSystem*	fs;	PedConstraint*	resize_constraint;	fs = ped_file_system_open (&part->geom);	if (!fs)		return 1;        printf ("\n");     	printf (_("Minor: %d\n"), part->num);	printf (_("Flags: ")); partition_print_flags (part); printf("\n");	printf (_("File System: %s\n"), fs->type->name);	printf (_("Size:         "));	print_sector_compact_and_percent (part->geom.length, part->geom.dev);	resize_constraint = ped_file_system_get_resize_constraint (fs);	if (resize_constraint) {		printf (_("Minimum size: "));		print_sector_compact_and_percent (resize_constraint->min_size,			part->geom.dev);		printf (_("Maximum size: "));		print_sector_compact_and_percent (resize_constraint->max_size,			part->geom.dev);		ped_constraint_destroy (resize_constraint);	}        printf ("\n");	ped_file_system_close (fs);	return 1;}static intdo_quit (PedDisk** disk){	_done ((*disk)->dev);	ped_disk_destroy (*disk);        exit (0);}/* FIXME: This is ugly. Won't always work. And it is untested. */static intdo_edit_bsd_disklabel (PedDisk** disk){	if (uiquery.need_commit) {	        ped_exception_throw (				     PED_EXCEPTION_ERROR,				     PED_EXCEPTION_CANCEL,				     _("Editing a BSD label, before writing the msdos "			               "partition table is not supported in GNU fdisk."));		return 0;	}		PedPartition *part;	int sect_size = (*disk)->dev->sector_size;	/* We enable external access to the device */	if (!ped_device_begin_external_access((*disk)->dev))		return 0;	/* We check if there is a BSD disklabel */	for (part = ped_disk_next_partition(*disk,NULL); part;	     part = ped_disk_next_partition(*disk,part))	{		if (part->type)			continue;		if (is_part_type_bsd(part)) {			/* Warn if there is a filesystem on the partition */			if (ped_file_system_probe(&(part->geom))) {				if (ped_exception_throw(PED_EXCEPTION_WARNING,				                        PED_EXCEPTION_YES_NO,				     _("There is a BSD partition on the disk "				       "but it seems to contain a filesystem. "				       "This is going to destroy it. "			               "Are you sure you want to continue?")) 					== PED_EXCEPTION_NO) {					ped_device_end_external_access((*disk)->dev);					return 0;				}			}			/* If there is no already created BSD label created,			   and there is a filesystem on the partition, OR			   we are in gfdisk, we warn the user about it */			else if (!is_bsd_partition((*disk)->dev->path, 				part->geom.start * sect_size, sect_size) && 				(!fdisk_compatibility_mode)) {				if (ped_exception_throw(PED_EXCEPTION_WARNING,				                        PED_EXCEPTION_YES_NO,				     _("There is a BSD partition on the disk, "				       "but there is no BSD disklabel on it. "				       "Do you want to create one?")) 					== PED_EXCEPTION_NO) {					ped_device_end_external_access((*disk)->dev);					return 0;				}			}			break;		}	}	/* FIXME: And here comes our biggest problem... */	if (part) {		PedDevice *label_dev;		PedDisk *label_disk;		char label_path[1024];		get_partition_device(label_path, sizeof(label_path), part, 0);		label_dev = ped_device_get(label_path);		if (!label_dev || ! ped_device_open(label_dev)) {			printf(_("There was an error opening the *BSD "			         "partition on %s.\n"), (*disk)->dev->path);			ped_device_end_external_access((*disk)->dev);			return 0;		}		printf("Reading disklabel of %s at sector %d.\n",			(*disk)->dev->path, part->geom.start);		ped_exception_fetch_all ();		label_disk = ped_disk_new(label_dev);		/* FIXME: This might ruin a partition on the disk */		if (!label_disk) ped_exception_catch();		ped_exception_leave_all();		if (!label_disk || strcmp(label_disk->type->name, "bsd")) {			label_disk = ped_disk_new_fresh (label_dev,					ped_disk_type_get ("bsd"));		}		in_menu = 2;		logical_offset = count_logical_partition(*disk);		fdisk_interactive_menu(&label_disk, fdisk_bsd_menu_commands, 2);		ped_disk_destroy(label_disk);		ped_device_destroy(label_dev);		logical_offset = 0;		in_menu = 0;	} else {		printf(_("There is no *BSD partition on %s.\n"),			(*disk)->dev->path);		ped_device_end_external_access((*disk)->dev);		return 0;	}	ped_device_end_external_access((*disk)->dev);	uiquery.need_commit = 0;	return 1;}static intdo_print (PedDisk **disk){	char			buf[256];	PedPartition*		part;	int			has_extended;	int                     sect_size;	char* 			cyl_size;	PedCHSGeometry* 	chs;	PedSector               heads;	PedSector               sectors;	PedSector               cylinders;	PedSector		start;	PedSector		end;	PedSector		cyl_start;	PedSector		cyl_end;	PedSector               unit = ped_unit_get_default ();	PedSector		blocks;				PedSector		sects_nbytes;	chs          = &((*disk)->dev)->bios_geom;	heads        = chs->heads;	sectors      = chs->sectors;	cylinders    = chs->cylinders;	sect_size    = (*disk)->dev->sector_size;	sects_nbytes = heads * sectors * sect_size;	cyl_size  = ped_unit_format_custom ((*disk)->dev,				heads * sectors,				PED_UNIT_BYTE);	cyl_size[strlen (cyl_size) - 1] = '\0';	PedSector total_drive_size = heads * sectors * cylinders * sect_size;	printf (_("\nDisk %s: %lld %s, %lld bytes\n"), (*disk)->dev->path, 		 total_drive_size >= 1000000000 ? 		 total_drive_size  / 1000000000 : 		 total_drive_size  / 1000000, 		(total_drive_size >= 1000000000) ? "GB" : "MB",		heads * sectors * cylinders * sect_size);	if (unit == PED_UNIT_SECTOR)		printf (_("%lld heads, %lld sectors/track, %lld cylinders, total %lld sectors\n"		  	"Units = sectors of %d * %d = %d bytes\n"),			heads, sectors, cylinders, (heads * sectors * cylinders), 1, 			sect_size, sect_size);	else		printf (_("%lld heads, %lld sectors/track, %lld cylinders\n"

⌨️ 快捷键说明

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