]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/cfdisk.c
include/pidfd-utils: remove hardcoded syscall fallback
[thirdparty/util-linux.git] / disk-utils / cfdisk.c
index d7a9a4e3dc0b052385dd3532d4a7ba1818c2139c..7b61929614cfa61e5c9ce51cc78358217a6e7060 100644 (file)
@@ -1,16 +1,18 @@
 /*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
  * cfdisk.c - Display or manipulate a disk partition table.
  *
- *     Copyright (C) 2014-2015 Karel Zak <kzak@redhat.com>
+ *     Copyright (C) 2014-2023 Karel Zak <kzak@redhat.com>
  *     Copyright (C) 1994 Kevin E. Martin (martin@cs.unc.edu)
  *
  *     The original cfdisk was inspired by the fdisk program
  *           by A. V. Le Blanc (leblanc@mcc.ac.uk.
- *
- * cfdisk is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
  */
 #include <stdlib.h>
 #include <stdio.h>
@@ -21,6 +23,7 @@
 #include <assert.h>
 #include <libsmartcols.h>
 #include <sys/ioctl.h>
+#include <rpmatch.h>
 #include <libfdisk.h>
 
 #ifdef HAVE_LIBMOUNT
@@ -124,12 +127,14 @@ enum {
        CFDISK_CL_FREESPACE,
        CFDISK_CL_INFO
 };
+#ifdef HAVE_USE_DEFAULT_COLORS
 static const int color_pairs[][2] = {
        /* color            foreground, background */
        [CFDISK_CL_WARNING]   = { COLOR_RED, -1 },
        [CFDISK_CL_FREESPACE] = { COLOR_GREEN, -1 },
        [CFDISK_CL_INFO]      = { COLOR_BLUE, -1 }
 };
+#endif
 
 struct cfdisk;
 
@@ -206,14 +211,6 @@ static struct cfdisk_menuitem main_menuitems[] = {
        { 0, NULL, NULL }
 };
 
-/* extra partinfo in name:value pairs */
-struct cfdisk_extra {
-       char *name;
-       char *data;
-
-       struct list_head exs;
-};
-
 /* line and extra partinfo list_head */
 struct cfdisk_line {
        char                    *data;          /* line data */
@@ -1331,6 +1328,25 @@ static inline int iszero(const char *str)
        return !p || *p == '\0';
 }
 
+static int has_uuid(struct fdisk_table *tb, const char *uuid)
+{
+       struct fdisk_partition *pa;
+       struct fdisk_iter *itr;
+       int rc = 0;
+
+       if (!tb || !uuid || fdisk_table_is_empty(tb))
+               return 0;
+
+       itr = fdisk_new_iter(FDISK_ITER_FORWARD);
+       while (rc == 0 && fdisk_table_next_partition(tb, itr, &pa) == 0) {
+               const char *x = fdisk_partition_get_uuid(pa);
+               if (x)
+                       rc = strcmp(x, uuid) == 0;
+       }
+       fdisk_free_iter(itr);
+       return rc;
+}
+
 static void extra_prepare_data(struct cfdisk *cf)
 {
        struct fdisk_partition *pa = get_current_partition(cf);
@@ -1350,7 +1366,14 @@ static void extra_prepare_data(struct cfdisk *cf)
 
        if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_UUID, &data) && data) {
                extra_insert_pair(l, _("Partition UUID:"), data);
-               if (!mountpoint)
+
+               /* Search for mountpoint by PARTUUID= means that we need to
+                * check fstab and convert PARTUUID to the device name. This is
+                * unnecessary and overkill for newly created partitions. Let's
+                * check if the UUID already exist in the old layout, otherwise
+                * ignore it.
+                */
+               if (!mountpoint && has_uuid(cf->original_layout, data))
                        mountpoint = get_mountpoint(cf, "PARTUUID", data);
                free(data);
        }
@@ -2054,7 +2077,7 @@ done:
        }
        free(cm);
        DBG(UI, ul_debug("get parrtype done [type=%s] ", t ?
-                               fdisk_parttype_get_name(t) : NULL));
+                               fdisk_parttype_get_name(t) : ""));
        return t;
 }
 
@@ -2228,7 +2251,8 @@ static int ui_help(void)
                "  ",
                N_("Command      Meaning"),
                N_("-------      -------"),
-               N_("  b          Toggle bootable flag of the current partition"),
+               N_("  b          Toggle bootable flag of the current partition;"),
+               N_("               implemented for DOS (MBR) and SGI labels only"),
                N_("  d          Delete the current partition"),
                N_("  h          Print this screen"),
                N_("  n          Create new partition from free space"),
@@ -2252,7 +2276,7 @@ static int ui_help(void)
                N_("Use lsblk(8) or partx(8) to see more details about the device."),
                "  ",
                "  ",
-               "Copyright (C) 2014-2017 Karel Zak <kzak@redhat.com>"
+               "Copyright (C) 2014-2023 Karel Zak <kzak@redhat.com>"
        };
 
        erase();
@@ -2426,18 +2450,17 @@ static int main_menu_action(struct cfdisk *cf, int key)
        }
        case 'r': /* resize */
        {
-               struct fdisk_partition *npa, *next;
                uint64_t size, max_size, secs;
+               struct fdisk_partition *npa;
 
                if (fdisk_partition_is_freespace(pa) || !fdisk_partition_has_start(pa))
                        return -EINVAL;
 
-               size = fdisk_partition_get_size(pa);
-
-               /* is the next freespace? */
-               next = fdisk_table_get_partition(cf->table, cf->lines_idx + 1);
-               if (next && fdisk_partition_is_freespace(next))
-                       size += fdisk_partition_get_size(next);
+               rc = fdisk_partition_get_max_size(cf->cxt,
+                                                 fdisk_partition_get_partno(pa),
+                                                 &size);
+               if (rc)
+                       return rc;
 
                size *= fdisk_get_sector_size(cf->cxt);
                max_size = size;
@@ -2495,11 +2518,15 @@ static int main_menu_action(struct cfdisk *cf, int key)
                if (rc)
                        warn = _("Failed to write disklabel.");
                else {
+                       size_t q_idx = 0;
+
                        if (cf->device_is_used)
                                fdisk_reread_changes(cf->cxt, cf->original_layout);
                        else
                                fdisk_reread_partition_table(cf->cxt);
                        info = _("The partition table has been altered.");
+                       if (menu_get_menuitem_by_key(cf, 'q', &q_idx))
+                               ui_menu_goto(cf, q_idx);
                }
                cf->nwrites++;
                break;
@@ -2559,11 +2586,17 @@ static int ui_run(struct cfdisk *cf)
        DBG(UI, ul_debug("start cols=%zu, lines=%zu", ui_cols, ui_lines));
 
        if (fdisk_get_collision(cf->cxt)) {
-               ui_warnx(_("Device already contains a %s signature; it will be removed by a write command."),
-                               fdisk_get_collision(cf->cxt));
-               fdisk_enable_wipe(cf->cxt, 1);
-               ui_hint(_("Press a key to continue."));
-               getch();
+               ui_warnx(_("Device already contains a %s signature."), fdisk_get_collision(cf->cxt));
+               if (fdisk_is_readonly(cf->cxt)) {
+                       ui_hint(_("Press a key to continue."));
+                       getch();
+               } else {
+                       char buf[64] = { 0 };
+                       rc = ui_get_string(_("Do you want to remove it? [Y]es/[N]o: "), NULL,
+                                       buf, sizeof(buf));
+                       fdisk_enable_wipe(cf->cxt,
+                                       rc > 0 && rpmatch(buf) == RPMATCH_YES ? 1 : 0);
+               }
        }
 
        if (!fdisk_has_label(cf->cxt) || cf->zero_start) {
@@ -2697,9 +2730,9 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -r, --read-only          forced open cfdisk in read-only mode\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
-       printf(USAGE_HELP_OPTIONS(26));
+       fprintf(out, USAGE_HELP_OPTIONS(26));
 
-       printf(USAGE_MAN_TAIL("cfdisk(8)"));
+       fprintf(out, USAGE_MAN_TAIL("cfdisk(8)"));
        exit(EXIT_SUCCESS);
 }