Anonymous avatar Anonymous committed c6372b5

Make fdisk work with devices/files without partition table

Comments (0)

Files changed (5)

+10/23/2008 Christian <mail.kristian@yahoo.it>
+
+	* fdisk.c: Make `do_change_system_type' not case
+	           sensitive to list all known partition types
+	
+10/22/2008 Christian <mail.kristian@yahoo.it>
+	
+	* fdisk.c: rename `do_list_devices' to `fdisk_do_list_devices' and
+	           make it global.
+        * fdisk.c: rename `print_partition_size' to `fdisk_print_partition_size'
+	           and male it global.
+        * fdisk.c: Remove code to open disk from `fdisk' function
+	           and move it in `common.c->fdisk_interactive_mode'.
+	* common.c: Change `fdisk_interactive_mode' prototype to accept
+	            a device as first parameter.
+	* fdisk: Added code to make fdisk work with devices or files
+	         without partition table.
+	
 10/17/2008 Christian <mail.kristian@yahoo.it>
 
 	* common.c: new global `compat_mode'.
 - print error message when a disk label that Linux fdisk doesn't support
   is encountered (?)
 - See the source code for others little things
+- Adjust change partition system id, typing l or L should work.
 
 -----------------------------------------------------------------------------
 
 #define MEGABYTE_SECTORS (PED_MEGABYTE_SIZE / PED_SECTOR_SIZE_DEFAULT)
 
 
-
+#define do_list_devices fdisk_do_list_devices
 
 typedef struct {
 	time_t	last_update;
 
 }
 
-static void
-print_partition_size(PedDisk *disk)
+void
+fdisk_print_partition_size(PedDisk *disk)
 {
 	PedPartition *part = ped_disk_get_partition(disk,fdisk_partsize_part);
 	if (!part) {
 		sect_size    = disk->dev->sector_size;
 		cyl_start     = (part->geom.start / (total_cyl)) + 1;
 		cyl_end       = (part->geom.end   / (total_cyl)) + 1;
-		//ped_unit_set_default(PED_UNIT_CYLINDER);
+		/* ped_unit_set_default(PED_UNIT_CYLINDER); */
 
 		printf("%lld\n",
 			((cyl_end * total_cyl) - ((part->num == 1) ?
 
 }
 
-static void
-do_list_devices (PedDisk* disk) {
+void
+fdisk_do_list_devices (PedDisk* disk) {
         if (disk == NULL) {
                 PedDevice* dev = NULL;
 
 		input = fdisk_command_line_get_word (prompt, NULL, NULL, 1);
 		if (!input)
 			return 0;
-		if (!strcmp(input,"L")) {
+		if (!strcmp(input,"L") && !strcmp(input,"l")) {
 			free(input);
 			do_list_systypes(disk);
 		}
 	program_name++;
       else
 	program_name = argv[0];
-      compat_mode = !strcmp(program_name,"lfdisk");
+      if(strcmp(program_name,"lfdisk") == 0)
+	compat_mode = 1;
     }
   else
     compat_mode = 0;
 #endif /* GNU_EXT */
   
   dev = _init (&argc, &argv);
-  
-  if (!dev && fdisk_list_table)
-    /* List all devices. */
-    do_list_devices (NULL);
-  else if(!dev)
-    return EXIT_FAILURE;
-
-  disk = ped_disk_new(dev);
-  
-  if(!disk)
-    return EXIT_FAILURE;
-  
-  /* Show the size of the partition */
-  if(fdisk_partsize_device)
-    print_partition_size(disk);
-  
-  /* List the specified disk. */
-  if (fdisk_list_table)
-    do_list_devices(disk);
-  
-  return (fdisk_interactive_mode(&disk, fdisk_main_menu_commands) 
+
+  if(!dev)
+    {
+      /* List all devices */
+      if(fdisk_list_table)
+	do_list_devices(NULL);
+      else
+	return EXIT_FAILURE;
+    }
+
+  return (fdisk_interactive_mode(&dev, fdisk_main_menu_commands) 
 	  ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 #include "strlist.h"
 #include "ui.h"
 
+/* compatibility mode flag */
+extern int compat_mode;
+/* Non NULL when -s option specifyed on command line */
+extern const char *fdisk_partsize_device;
+extern int fdisk_partsize_part; /* ?? */
+
+/* not 0 when fdisk should print the partition table? */
+extern int fdisk_list_table;
+
 #if HAVE_LOCALE_H
 # include <locale.h>
 #endif /* HAVE_LOCALE_H */
   signal (signum, &interrupt_handler);
 
   if (in_readline) {
-    printf ("\n");
+    putchar(0x0A);
     siglongjmp (readline_state.jmp_state, 1);
   }
 }
   str_list_print_wrap (list, fdisk_screen_width (), 0, 0);
   str_list_destroy (list);
 }
+
 /*
  * Begin interactive mode
  */
+
+/* Changed */
+/* 
 int
-fdisk_interactive_mode (PedDisk** disk, FdiskCommand* cmd_list[])
+fdisk_interactive_mode (PedDisk** disk, FdiskCommand* cmd_list[]) 
+*/
+
+int
+fdisk_interactive_mode(PedDevice **dev, FdiskCommand* cmd_list[])
 {
   char*	line;
+  PedDisk *disk;
   StrList* list;
   StrList* command_names = fdisk_command_get_names (cmd_list);
-  
+    
   puts (interface_name);
   do_banner_message();
-  fdisk_print_using_dev ((*disk)->dev);
+
+  /* Christian <mail.kristian@yahoo.it>:
+   * FIRST try open device as a disk with a partition table, if this doesn't
+   * work device has not partition table so we must make it */
+ 
+   
+  if(ped_disk_probe(*dev) == NULL)
+    {
+      if(compat_mode)
+	{
+	  /* If we are in compat mode we must create a dos partition table */
+	  puts( _("Device contains neither a valid DOS partition table, "
+		  "nor Sun, SGI or OSF disklabel\n"
+		  "Building a new DOS disklabel. Changes will remain in memory only,\n"
+		  "until you decide to write them. After that, of course, the previous\n"
+		  "content won't be recoverable.\n"));
+	  if((disk = ped_disk_new_fresh(*dev, ped_disk_type_get("msdos"))) == NULL)
+	    return EXIT_FAILURE;
+	}
+      else
+	{
+	  /* In extended mode we can create a partition table with all supported modes */
+	  PedDiskType *label_type;
+	  StrList *supp = NULL;
+	  char *supported_types;
+	  char *str;
+
+	  for(label_type = ped_disk_type_get_next(NULL);
+	      label_type = ped_disk_type_get_next(label_type); )
+	    {
+	      supp = str_list_append(supp, label_type->name);
+	      supp = str_list_append(supp, " ");
+	    }
+
+	  supported_types = str_list_convert(supp);
+	  str_list_destroy(supp);
+
+	  /* List resources was freed */
+
+	  puts(_("Device contains neither a valid DOS partition table, "
+		 "nor Sun, SGI or OSF disklabel.\n"
+		 "You must create a disk label first!\n"));
+	  
+	  str = malloc(32);
+
+	get_label_type_str:
+	  
+	  printf(_("What type of disk label would you create (q to quit)?\n[ %s] "), 
+		 supported_types);
+
+	retry_without_print_message:
+	  
+	  if(scanf("%31s", str) < 1)
+	    {
+	      goto retry_without_print_message;
+	    }
+	  else if((*str == 'q' || *str == 'Q') && str[1] == 0)
+	    {
+	      return EXIT_SUCCESS;
+	    }
+	  else if((label_type = ped_disk_type_get(str)) == NULL)
+	    {
+	      fputs(_("*** Invalid label!\n"), stderr);
+	      goto get_label_type_str;	      
+	    }
+
+	  disk = ped_disk_new_fresh(*dev, label_type);
+
+	  if(!disk)
+	    return EXIT_FAILURE; /* what bad? :( */
+
+	  /* Free other resources */
+	  free(str);
+	  free(supported_types);
+	  
+	}
+    } /* if(!disk) */
+  else
+    {
+      disk = ped_disk_new(*dev);
+      if(!disk)
+	return EXIT_FAILURE;
+    }
+  
+  /***
+   ** OK, We have a disk with a partition table :) 
+   */
+  
+  /* Show the size of the partition */
+  if(fdisk_partsize_device)
+    fdisk_print_partition_size(disk);
+  
+  /* List the specified disk. */
+  if (fdisk_list_table)
+    fdisk_do_list_devices(&disk);
+  
+
+  fdisk_print_using_dev (disk->dev);
 
   while (1) 
     {
 	  free (word);
 	  if (cmd) 
 	    {
-	      if (!fdisk_command_run (cmd, disk))
+	      if (!fdisk_command_run (cmd, &disk))
 		fdisk_command_line_flush ();
 	    } 
 	  else 
 extern char *interface_name;
 extern void fdisk_usage_msg();
 extern void fdisk_help_msg ();
-extern int fdisk_interactive_mode (PedDisk** disk, FdiskCommand* cmd_list[]);
+/* extern int fdisk_interactive_mode (PedDisk** disk, FdiskCommand* cmd_list[]); */
+extern int fdisk_interactive_mode (PedDevice** device, FdiskCommand* cmd_list[]);
 extern int fdisk_interactive_menu (PedDisk** disk, FdiskCommand* cmd_list[], int menu);
 extern int fdisk_screen_width ();
 extern void fdisk_wipe_line ();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.