Hi,

attached is a patch to allow parted via on option to keep changes
to the partition table only in memory until explicitely requested
to write them to disk. Other partitioning programs, e.g. fdisk,
can operate like that and I sometimes prefer that mode.

Feedback is welcome.

ciao
  Arvin

-- 
Arvin Schnell, <[email protected]>
Senior Software Engineer, Research & Development
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 
(AG Nürnberg)
Maxfeldstraße 5
90409 Nürnberg
Germany
diff --git a/doc/C/parted.8 b/doc/C/parted.8
index 5304375..fda211a 100644
--- a/doc/C/parted.8
+++ b/doc/C/parted.8
@@ -48,6 +48,9 @@ Use optimum alignment as given by the disk topology information. This
 aligns to a multiple of the physical block size in a way that guarantees
 optimal performance.
 .RE
+.TP
+.B -c, --confirm-commit
+require explicit confirmation to commit changes to the device
 
 .SH COMMANDS
 .TP
@@ -88,6 +91,10 @@ Display the partition table.
 .B quit
 Exit from \fBparted\fP.
 .TP
+.B commit
+Commit changes to the device and exit from \fBparted\fP. Only useful with
+option --confirm-commit.
+.TP
 .B rescue \fIstart\fP \fIend\fP
 Rescue a lost partition that was located somewhere between \fIstart\fP and
 \fIend\fP.  If a partition is found, \fBparted\fP will ask if you want to
diff --git a/parted/parted.c b/parted/parted.c
index a9426c4..c15bc68 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -117,6 +117,7 @@ static struct option const options[] = {
         {"script",      0, NULL, 's'},
         {"version",     0, NULL, 'v'},
         {"align",       required_argument, NULL, 'a'},
+        {"confirm-commit", 0, NULL, 'c'},
         {"-pretend-input-tty", 0, NULL, PRETEND_INPUT_TTY},
         {NULL,          0, NULL, 0}
 };
@@ -128,6 +129,7 @@ static const char *const options_help [][2] = {
         {"script",      N_("never prompts for user intervention")},
         {"version",     N_("displays the version")},
         {"align=[none|cyl|min|opt]", N_("alignment for new partitions")},
+        {"confirm-commit", N_("require explicit confirmation of commit to device")},
         {NULL,          NULL}
 };
 
@@ -137,6 +139,7 @@ int     opt_machine_mode = 0;
 int     disk_is_modified = 0;
 int     is_toggle_mode = 0;
 int     alignment = ALIGNMENT_OPTIMAL;
+int     confirm_commit_mode = 0;
 
 static const char* number_msg = N_(
 "NUMBER is the partition number used by Linux.  On MS-DOS disk labels, the "
@@ -535,8 +538,9 @@ do_mklabel (PedDevice** dev, PedDisk** diskp)
         if (!disk)
                 goto error;
 
-        if (!ped_disk_commit (disk))
-                goto error_destroy_disk;
+	if (!confirm_commit_mode)
+		if (!ped_disk_commit (disk))
+			goto error_destroy_disk;
 
         if ((*dev)->type != PED_DEVICE_FILE)
                 disk_is_modified = 1;
@@ -814,8 +818,9 @@ do_mkpart (PedDevice** dev, PedDisk** diskp)
         if (ped_partition_is_flag_available (part, PED_PARTITION_LBA))
                 ped_partition_set_flag (part, PED_PARTITION_LBA, 1);
 
-        if (!ped_disk_commit (disk))
-                goto error;
+	if (!confirm_commit_mode)
+		if (!ped_disk_commit (disk))
+			goto error;
 
         /* clean up */
         if (range_start != NULL)
@@ -874,8 +879,10 @@ do_name (PedDevice** dev, PedDisk** diskp)
                 goto error_free_name;
         free (name);
 
-        if (!ped_disk_commit (*diskp))
-                goto error;
+	if (!confirm_commit_mode)
+		if (!ped_disk_commit (*diskp))
+			goto error;
+
         return 1;
 
 error_free_name:
@@ -1309,6 +1316,14 @@ _print_list ()
 }
 
 static int
+do_commit_and_quit (PedDevice** dev, PedDisk **diskp)
+{
+        ped_disk_commit(*diskp);
+        _done (*dev, *diskp);
+        exit (EXIT_SUCCESS);
+}
+
+static int
 do_quit (PedDevice** dev, PedDisk **diskp)
 {
         _done (*dev, *diskp);
@@ -1328,7 +1343,7 @@ _disk_get_part_type_for_sector (PedDisk* disk, PedSector sector)
         return PED_PARTITION_LOGICAL;
 }
 
-/* This function checks if "part" contains a file system, and returs
+/* This function checks if "part" contains a file system, and returns
  *      0 if either no file system was found, or the user declined to add it.
  *      1 if a file system was found, and the user chose to add it.
  *      -1 if the user chose to cancel the entire search.
@@ -1383,7 +1398,10 @@ _rescue_add_partition (PedPartition* part)
         }
 
         ped_partition_set_system (part, fs_type);
-        ped_disk_commit (part->disk);
+
+	if (!confirm_commit_mode)
+		ped_disk_commit (part->disk);
+
         return 1;
 }
 
@@ -1558,7 +1576,9 @@ do_resizepart (PedDevice** dev, PedDisk** diskp)
                             _("Shrinking a partition can cause data loss, " \
                               "are you sure you want to continue?")) != PED_EXCEPTION_YES)
                         goto error_destroy_constraint;
-        ped_disk_commit (disk);
+
+	if (!confirm_commit_mode)
+		ped_disk_commit (disk);
 
         if ((*dev)->type != PED_DEVICE_FILE)
                 disk_is_modified = 1;
@@ -1590,7 +1610,9 @@ do_rm (PedDevice** dev, PedDisk** diskp)
                 goto error;
 
         ped_disk_delete_partition (*diskp, part);
-        ped_disk_commit (*diskp);
+
+	if (!confirm_commit_mode)
+		ped_disk_commit (*diskp);
 
         if ((*dev)->type != PED_DEVICE_FILE)
                 disk_is_modified = 1;
@@ -1698,8 +1720,10 @@ do_disk_set (PedDevice** dev, PedDisk** diskp)
 
     if (!ped_disk_set_flag (*diskp, flag, state))
         goto error;
-    if (!ped_disk_commit (*diskp))
-        goto error;
+
+    if (!confirm_commit_mode)
+	if (!ped_disk_commit (*diskp))
+	    goto error;
 
     if ((*dev)->type != PED_DEVICE_FILE)
         disk_is_modified = 1;
@@ -1735,8 +1759,10 @@ do_set (PedDevice** dev, PedDisk **diskp)
 
         if (!ped_partition_set_flag (part, flag, state))
                 goto error;
-        if (!ped_disk_commit (*diskp))
-                goto error;
+
+	if (!confirm_commit_mode)
+		if (!ped_disk_commit (*diskp))
+			goto error;
 
         if ((*dev)->type != PED_DEVICE_FILE)
                 disk_is_modified = 1;
@@ -1988,6 +2014,14 @@ NULL),
         NULL, 1));
 
 command_register (commands, command_create (
+        str_list_create_unique ("commit", _("commit"), NULL),
+        do_commit_and_quit,
+        str_list_create (
+_("commit                                   commit and exit program"),
+NULL),
+        NULL, 1));
+
+command_register (commands, command_create (
         str_list_create_unique ("rescue", _("rescue"), NULL),
         do_rescue,
         str_list_create (
@@ -2119,7 +2153,7 @@ int     opt, help = 0, list = 0, version = 0, wrong = 0;
 
 while (1)
 {
-        opt = getopt_long (*argc_ptr, *argv_ptr, "hlmsva:",
+        opt = getopt_long (*argc_ptr, *argv_ptr, "hlmsvca:",
                            options, NULL);
         if (opt == -1)
                 break;
@@ -2134,6 +2168,7 @@ while (1)
                   alignment = XARGMATCH ("--align", optarg,
                                          align_args, align_types);
                   break;
+                case 'c': confirm_commit_mode = 1; break;
                 case PRETEND_INPUT_TTY:
                   pretend_input_tty = 1;
                   break;
@@ -2145,7 +2180,7 @@ while (1)
 
 if (wrong == 1) {
         fprintf (stderr,
-                 _("Usage: %s [-hlmsv] [-a<align>] [DEVICE [COMMAND [PARAMETERS]]...]\n"),
+                 _("Usage: %s [-hlmsvc] [-a<align>] [DEVICE [COMMAND [PARAMETERS]]...]\n"),
                  program_name);
         return 0;
 }

Reply via email to