[parted-devel] [PATCH] fix for 'parted --list' option -- does not generate an exception in non superuser mode

Rakesh Pandit rakesh.pandit at gmail.com
Mon May 5 02:12:36 UTC 2008


2008/4/9 Rakesh Pandit <rakesh.pandit at gmail.com>:
[..]
>  'parted' alone gives :
>
>  WARNING: You are not superuser.  Watch out for permissions.
>  Error: No device found
>  Retry/Cancel?
>
>  So, essentially parted -l should also give same message if we run it
>  in non super user mode and if it does not have permission to open disk
>  as in my system.
>
[..]

I am still not too sure about behavior. What are your suggestion?

Improved patch is attached. It shifts the superuser warning to a
helper function.

-- 
Rakesh Pandit

diff --git a/parted/parted.c b/parted/parted.c
index 0f6768b..9d6a5e9 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -181,6 +181,17 @@ _timer_handler (PedTimer* timer, void* context)
         }
 }

+static void
+_superuser_warn ()
+{
+#ifdef HAVE_GETUID
+        if (getuid() != 0 && !opt_script_mode) {
+            puts (_("WARNING: You are not superuser.  Watch out for "
+                    "permissions."));
+        }
+#endif
+}
+
 static int
 _partition_warn_busy (PedPartition* part)
 {
@@ -1573,11 +1584,23 @@ _print_list ()
 {
         PedDevice *current_dev = NULL;

-        ped_device_probe_all();
+	_superuser_warn();
+retry:
+        ped_device_probe_all ();
+        current_dev = ped_device_get_next (NULL);

-        while ((current_dev = ped_device_get_next(current_dev))) {
+        if (!current_dev) {
+                if (ped_exception_throw (PED_EXCEPTION_ERROR,
+                        PED_EXCEPTION_RETRY_CANCEL,
+                        _("No device found"))
+                                == PED_EXCEPTION_RETRY)
+                        goto retry;
+	}
+
+        while (current_dev) {
                 do_print (&current_dev);
                 putchar ('\n');
+		current_dev = ped_device_get_next (current_dev);
         }

         return 1;
@@ -2429,12 +2452,7 @@ _init_commands ();
 if (!_parse_options (argc_ptr, argv_ptr))
         goto error_done_commands;

-#ifdef HAVE_GETUID
-        if (getuid() != 0 && !opt_script_mode) {
-            puts (_("WARNING: You are not superuser.  Watch out for "
-                    "permissions."));
-        }
-#endif
+_superuser_warn();

 dev = _choose_device (argc_ptr, argv_ptr);
 if (!dev)



More information about the parted-devel mailing list