2 * cfdisk.c - Display or manipulate a disk partition table.
4 * Copyright (C) 2014-2015 Karel Zak <kzak@redhat.com>
5 * Copyright (C) 1994 Kevin E. Martin (martin@cs.unc.edu)
7 * The original cfdisk was inspired by the fdisk program
8 * by A. V. Le Blanc (leblanc@mcc.ac.uk.
10 * cfdisk is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
22 #include <libsmartcols.h>
23 #include <sys/ioctl.h>
27 # include <libmount.h> /* keep it optional for non-linux systems */
32 #elif defined(HAVE_SLANG_SLANG_H)
33 # include <slang/slang.h>
37 # define _XOPEN_SOURCE 500 /* for inclusion of get_wch */
40 #ifdef HAVE_SLCURSES_H
41 # include <slcurses.h>
42 #elif defined(HAVE_SLANG_SLCURSES_H)
43 # include <slang/slcurses.h>
44 #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
45 # include <ncursesw/ncurses.h>
46 #elif defined(HAVE_NCURSES_H)
48 #elif defined(HAVE_NCURSES_NCURSES_H)
49 # include <ncurses/ncurses.h>
58 #include "closestream.h"
68 static const char *default_disks
[] = {
72 #elif defined(__FreeBSD__)
82 #define ARROW_CURSOR_STRING ">> "
83 #define ARROW_CURSOR_DUMMY " "
84 #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
87 #define MENU_V_SPADDING 1 /* space around menu item string */
90 #define MENU_H_SPADDING 0 /* space around menu item string */
91 #define MENU_H_BETWEEN 2 /* space between menu items */
92 #define MENU_H_PRESTR "["
93 #define MENU_H_POSTSTR "]"
95 #define MENU_TITLE_PADDING 3
97 #define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
98 #define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
100 #define TABLE_START_LINE 4
101 #define MENU_START_LINE (ui_lines - 4) /* The menu maybe use two lines */
102 #define INFO_LINE (ui_lines - 2)
103 #define WARN_LINE INFO_LINE
104 #define HINT_LINE (ui_lines - 1)
106 #define CFDISK_ERR_ESC 5000
109 # define KEY_ESC '\033'
112 # define KEY_DELETE '\177'
126 static const int color_pairs
[][2] = {
127 /* color foreground, background */
128 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
129 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 },
130 [CFDISK_CL_INFO
] = { COLOR_BLUE
, -1 }
135 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
136 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
137 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
138 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
139 static void menu_refresh_size(struct cfdisk
*cf
);
141 static int ui_end(void);
142 static int ui_refresh(struct cfdisk
*cf
);
143 static void ui_warnx(const char *fmt
, ...);
144 static void ui_warn(const char *fmt
, ...);
145 static void ui_info(const char *fmt
, ...);
146 static void ui_draw_menu(struct cfdisk
*cf
);
147 static int ui_menu_move(struct cfdisk
*cf
, int key
);
148 static void ui_menu_resize(struct cfdisk
*cf
);
150 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uint64_t *res
,
151 uint64_t low
, uint64_t up
, int *expsize
);
153 static int ui_enabled
;
154 static volatile sig_atomic_t sig_resize
;
155 static volatile sig_atomic_t sig_die
;
157 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
158 * something portable and writable */
159 static size_t ui_lines
;
160 static size_t ui_cols
;
163 struct cfdisk_menuitem
{
164 int key
; /* keyboard shortcut */
165 const char *name
; /* item name */
166 const char *desc
; /* item description (hint) */
172 char *title
; /* optional menu title */
173 struct cfdisk_menuitem
*items
; /* array with menu items */
174 char *ignore
;/* string with keys to ignore */
175 size_t width
; /* maximal width of the menu item */
176 size_t nitems
; /* number of the active menu items */
177 size_t page_sz
;/* when menu longer than screen */
178 size_t idx
; /* the current menu item */
179 int prefkey
;/* preferred menu item */
180 struct cfdisk_menu
*prev
;
182 /* @ignore keys generator */
183 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
185 unsigned int vertical
: 1; /* enable vertical mode */
189 static struct cfdisk_menuitem main_menuitems
[] = {
190 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
191 { 'd', N_("Delete"), N_("Delete the current partition") },
192 { 'r', N_("Resize"), N_("Reduce or enlarge the current partition") },
193 { 'n', N_("New"), N_("Create new partition from free space") },
194 { 'q', N_("Quit"), N_("Quit program without writing changes") },
195 { 't', N_("Type"), N_("Change the partition type") },
196 { 'h', N_("Help"), N_("Print help screen") },
197 { 's', N_("Sort"), N_("Fix partitions order") },
198 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
199 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
203 /* extra partinfo in name:value pairs */
204 struct cfdisk_extra
{
208 struct list_head exs
;
211 /* line and extra partinfo list_head */
213 char *data
; /* line data */
214 struct libscols_table
*extra
; /* extra info ('X') */
215 WINDOW
*w
; /* window with extra info */
218 /* top level control struct */
220 struct fdisk_context
*cxt
; /* libfdisk context */
221 struct fdisk_table
*table
; /* partition table */
222 struct fdisk_table
*original_layout
; /* original on-disk PT */
224 struct cfdisk_menu
*menu
; /* the current menu */
226 int *fields
; /* output columns IDs */
227 size_t nfields
; /* number of columns IDs */
229 char *linesbuf
; /* table as string */
230 size_t linesbufsz
; /* size of the tb_buf */
232 struct cfdisk_line
*lines
; /* list of lines */
234 size_t nlines
; /* number of lines */
235 size_t lines_idx
; /* current line <0..N>, exclude header */
238 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
240 WINDOW
*act_win
; /* the window currently on the screen */
243 struct libmnt_table
*mtab
;
244 struct libmnt_table
*fstab
;
245 struct libmnt_cache
*mntcache
;
247 unsigned int wrong_order
:1, /* PT not in right order */
248 zero_start
:1, /* ignore existing partition table */
249 device_is_used
: 1, /* don't use re-read ioctl */
250 show_extra
:1; /* show extra partinfo */
255 * let's use include/debug.h stuff for cfdisk too
257 static UL_DEBUG_DEFINE_MASK(cfdisk
);
258 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
260 #define CFDISK_DEBUG_INIT (1 << 1)
261 #define CFDISK_DEBUG_UI (1 << 2)
262 #define CFDISK_DEBUG_MENU (1 << 3)
263 #define CFDISK_DEBUG_MISC (1 << 4)
264 #define CFDISK_DEBUG_TABLE (1 << 5)
265 #define CFDISK_DEBUG_ALL 0xFFFF
267 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
269 static void cfdisk_init_debug(void)
271 __UL_INIT_DEBUG_FROM_ENV(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
274 /* Initialize output columns -- we follow libfdisk fields (usually specific
277 static int cols_init(struct cfdisk
*cf
)
285 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
288 static void die_on_signal(void)
290 DBG(MISC
, ul_debug("die on signal."));
295 static void resize(void)
299 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
300 && ws
.ws_row
&& ws
.ws_col
) {
301 ui_lines
= ws
.ws_row
;
304 resizeterm(ws
.ws_row
, ws
.ws_col
);
306 clearok(stdscr
, TRUE
);
310 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
315 /* Reads partition in tree-like order from scols
317 static int partition_from_scols(struct fdisk_table
*tb
,
318 struct libscols_line
*ln
)
320 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
322 fdisk_table_add_partition(tb
, pa
);
323 fdisk_unref_partition(pa
);
325 if (scols_line_has_children(ln
)) {
326 struct libscols_line
*chln
;
327 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
331 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
332 partition_from_scols(tb
, chln
);
333 scols_free_iter(itr
);
338 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
340 struct fdisk_partition
*pa
;
341 struct fdisk_label
*lb
;
342 struct fdisk_iter
*itr
;
343 struct libscols_table
*table
= NULL
;
344 struct libscols_iter
*s_itr
= NULL
;
348 struct libscols_line
*ln
, *ln_cont
= NULL
;
350 DBG(TABLE
, ul_debug("convert to string"));
357 lb
= fdisk_get_label(cf
->cxt
, NULL
);
360 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
364 /* get container (e.g. extended partition) */
365 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
366 if (fdisk_partition_is_nested(pa
)) {
367 DBG(TABLE
, ul_debug("nested detected, using tree"));
368 tree
= SCOLS_FL_TREE
;
373 table
= scols_new_table();
376 scols_table_enable_maxout(table
, 1);
377 scols_table_enable_nowrap(table
, 1);
379 #if !defined(HAVE_LIBNCURSESW) || !defined(HAVE_WIDECHAR)
380 scols_table_enable_ascii(table
, 1);
384 for (i
= 0; i
< cf
->nfields
; i
++) {
386 const struct fdisk_field
*field
=
387 fdisk_label_get_field(lb
, cf
->fields
[i
]);
391 if (fdisk_field_is_number(field
))
392 fl
|= SCOLS_FL_RIGHT
;
393 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
394 fl
|= SCOLS_FL_TRUNC
;
395 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
398 if (!scols_table_new_column(table
,
399 _(fdisk_field_get_name(field
)),
400 fdisk_field_get_width(field
), fl
))
405 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
407 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
408 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
410 ln
= scols_table_new_line(table
, parent
);
413 for (i
= 0; i
< cf
->nfields
; i
++) {
416 if (fdisk_partition_to_string(pa
, cf
->cxt
,
417 cf
->fields
[i
], &cdata
))
419 scols_line_refer_data(ln
, i
, cdata
);
421 if (tree
&& fdisk_partition_is_container(pa
))
424 scols_line_set_userdata(ln
, (void *) pa
);
425 fdisk_ref_partition(pa
);
428 if (scols_table_is_empty(table
))
431 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
432 scols_print_table_to_string(table
, &res
);
434 /* scols_* code might reorder lines, let's reorder @tb according to the
435 * final output (it's no problem because partitions are addressed by
436 * parno stored within struct fdisk_partition) */
439 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
440 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
441 fdisk_table_remove_partition(tb
, pa
);
443 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
447 /* add all in the right order (don't forget the output is tree) */
448 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
449 if (scols_line_get_parent(ln
))
451 if (partition_from_scols(tb
, ln
))
455 scols_unref_table(table
);
456 scols_free_iter(s_itr
);
457 fdisk_free_iter(itr
);
462 static void cfdisk_free_lines(struct cfdisk
*cf
)
465 while(i
< cf
->nlines
) {
466 scols_unref_table(cf
->lines
[i
].extra
);
468 DBG(UI
, ul_debug("delete window: %p",
472 delwin(cf
->lines
[i
].w
);
473 cf
->lines
[i
].w
= NULL
;
481 * Read data about partitions from libfdisk and prepare output lines.
483 static int lines_refresh(struct cfdisk
*cf
)
491 DBG(TABLE
, ul_debug("refreshing buffer"));
494 cfdisk_free_lines(cf
);
500 fdisk_unref_table(cf
->table
);
503 /* read partitions and free spaces into cf->table */
504 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
506 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
510 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
514 cf
->linesbufsz
= strlen(cf
->linesbuf
);
515 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
517 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
519 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
520 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
522 cf
->lines
= xcalloc(cf
->nlines
, sizeof(struct cfdisk_line
));
524 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
525 cf
->lines
[i
].data
= p
;
531 cf
->lines
[i
].extra
= scols_new_table();
532 scols_table_enable_noheadings(cf
->lines
[i
].extra
, 1);
533 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_RIGHT
);
534 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_TRUNC
);
540 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
545 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
548 static int is_freespace(struct cfdisk
*cf
, size_t i
)
550 struct fdisk_partition
*pa
;
555 pa
= fdisk_table_get_partition(cf
->table
, i
);
556 return fdisk_partition_is_freespace(pa
);
559 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
560 * response back to libfdisk
562 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
564 struct cfdisk_menuitem
*d
, *cm
;
566 size_t i
= 0, nitems
;
567 const char *name
, *desc
;
572 /* create cfdisk menu according to libfdisk ask-menu, note that the
573 * last cm[] item has to be empty -- so nitems + 1 */
574 nitems
= fdisk_ask_menu_get_nitems(ask
);
575 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
577 for (i
= 0; i
< nitems
; i
++) {
578 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
585 /* make the new menu active */
598 if (ui_menu_move(cf
, key
) == 0)
605 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
607 fdisk_ask_menu_set_result(ask
, d
->key
);
624 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
625 struct fdisk_ask
*ask
,
626 void *data
__attribute__((__unused__
)))
632 switch(fdisk_ask_get_type(ask
)) {
633 case FDISK_ASKTYPE_INFO
:
634 ui_info(fdisk_ask_print_get_mesg(ask
));
636 case FDISK_ASKTYPE_WARNX
:
637 ui_warnx(fdisk_ask_print_get_mesg(ask
));
639 case FDISK_ASKTYPE_WARN
:
640 ui_warn(fdisk_ask_print_get_mesg(ask
));
642 case FDISK_ASKTYPE_MENU
:
643 ask_menu(ask
, (struct cfdisk
*) data
);
646 ui_warnx(_("internal error: unsupported dialog type %d"),
647 fdisk_ask_get_type(ask
));
653 static int ui_end(void)
658 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
659 SLsmg_gotorc(ui_lines
- 1, 0);
662 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
672 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
680 xvasprintf(&buf
, fmt
, ap
);
682 width
= mbs_safe_width(buf
);
683 if (width
> (size_t) ui_cols
) {
684 char *p
= strrchr(buf
+ ui_cols
, ' ');
688 if (line
+ 1 >= ui_lines
)
691 mvaddstr(line
, 0, buf
);
692 mvaddstr(line
+ 1, 0, p
+1);
696 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
702 static void ui_center(size_t line
, const char *fmt
, ...)
706 ui_vprint_center(line
, 0, fmt
, ap
);
710 static void ui_warnx(const char *fmt
, ...)
715 ui_vprint_center(WARN_LINE
,
716 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
719 vfprintf(stderr
, fmt
, ap
);
725 static void ui_warn(const char *fmt
, ...)
730 xasprintf(&fmt_m
, "%s: %m", fmt
);
734 ui_vprint_center(WARN_LINE
,
735 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
738 vfprintf(stderr
, fmt_m
, ap
);
745 static void ui_clean_warn(void)
751 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
757 fprintf(stderr
, "%s: ", program_invocation_short_name
);
758 vfprintf(stderr
, fmt
, ap
);
765 static void ui_info(const char *fmt
, ...)
770 ui_vprint_center(INFO_LINE
,
771 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO
) : 0,
774 vfprintf(stdout
, fmt
, ap
);
780 static void ui_clean_info(void)
786 static void ui_hint(const char *fmt
, ...)
791 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
793 vfprintf(stdout
, fmt
, ap
);
799 static void ui_clean_hint(void)
806 static void sig_handler_die(int dummy
__attribute__((__unused__
)))
811 static void sig_handler_resize(int dummy
__attribute__((__unused__
)))
816 static void menu_refresh_size(struct cfdisk
*cf
)
818 if (cf
->menu
&& cf
->menu
->nitems
)
819 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
822 static void menu_update_ignore(struct cfdisk
*cf
)
824 char ignore
[128] = { 0 };
826 struct cfdisk_menu
*m
;
827 struct cfdisk_menuitem
*d
, *org
= NULL
;
832 assert(cf
->menu
->ignore_cb
);
835 DBG(MENU
, ul_debug("update menu ignored keys"));
837 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
840 /* return if no change */
841 if ((!m
->ignore
&& !*ignore
)
842 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
847 org
= menu_get_menuitem(cf
, m
->idx
);
850 m
->ignore
= xstrdup(ignore
);
853 for (d
= m
->items
; d
->name
; d
++) {
854 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
859 DBG(MENU
, ul_debug("update menu preferred keys"));
861 /* refresh menu index to be at the same menuitem or go to the first */
862 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
864 else if (m
->prefkey
&& menu_get_menuitem_by_key(cf
, m
->prefkey
, &idx
))
869 menu_refresh_size(cf
);
872 static struct cfdisk_menu
*menu_push(
874 struct cfdisk_menuitem
*items
)
876 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
877 struct cfdisk_menuitem
*d
;
881 DBG(MENU
, ul_debug("new menu"));
886 for (d
= m
->items
; d
->name
; d
++) {
887 const char *name
= _(d
->name
);
888 size_t len
= mbs_safe_width(name
);
896 menu_refresh_size(cf
);
900 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
902 struct cfdisk_menu
*m
= NULL
;
906 DBG(MENU
, ul_debug("pop menu"));
910 free(cf
->menu
->ignore
);
911 free(cf
->menu
->title
);
918 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
923 size_t len
= mbs_safe_width(title
);
924 if (len
+ MENU_TITLE_PADDING
> m
->width
)
925 m
->width
= len
+ MENU_TITLE_PADDING
;
926 str
= xstrdup(title
);
932 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
936 DBG(UI
, ul_debug("init"));
938 /* setup SIGCHLD handler */
939 sigemptyset(&sa
.sa_mask
);
941 sa
.sa_handler
= sig_handler_die
;
942 sigaction(SIGINT
, &sa
, NULL
);
943 sigaction(SIGTERM
, &sa
, NULL
);
945 sa
.sa_handler
= sig_handler_resize
;
946 sigaction(SIGWINCH
, &sa
, NULL
);
951 #ifdef HAVE_USE_DEFAULT_COLORS
952 if (colors_wanted() && has_colors()) {
956 use_default_colors();
957 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
958 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
968 keypad(stdscr
, TRUE
);
974 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
980 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
983 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
985 struct cfdisk_menu
*m
= cf
->menu
;
988 if (!m
->page_sz
) /* small menu */
989 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
990 return (idx
% m
->page_sz
) + 1;
992 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
993 size_t items
= ui_cols
/ len
; /* items per line */
997 return MENU_START_LINE
+ ((idx
/ items
));
1001 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
1003 if (cf
->menu
->vertical
) {
1004 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
1005 if ((size_t) ui_cols
<= nc
)
1007 return (ui_cols
- nc
) / 2;
1009 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
1010 size_t items
= ui_cols
/ len
; /* items per line */
1011 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
1012 ui_cols
% len
: /* - multi-line menu */
1013 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
1016 return 0; /* hmm... no space */
1018 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
1021 return (idx
* len
) + (extra
/ 2);
1022 return ((idx
% items
) * len
) + (extra
/ 2);
1026 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
1028 struct cfdisk_menu
*m
= cf
->menu
;
1030 if (m
->page_sz
== 0 ||
1031 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
1036 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
1038 struct cfdisk_menuitem
*d
;
1041 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1042 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1051 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
1052 int key
, size_t *idx
)
1054 struct cfdisk_menuitem
*d
;
1056 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1057 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1067 static void ui_draw_menuitem(struct cfdisk
*cf
,
1068 struct cfdisk_menuitem
*d
,
1074 const size_t buf_sz
= 80 * MB_CUR_MAX
;
1075 int ln
, cl
, vert
= cf
->menu
->vertical
;
1077 if (!menuitem_on_page(cf
, idx
))
1078 return; /* no visible item */
1079 ln
= menuitem_get_line(cf
, idx
);
1080 cl
= menuitem_get_column(cf
, idx
);
1082 ptr
= buf
= xmalloc(buf_sz
);
1085 width
= cf
->menu
->width
+ MENU_V_SPADDING
;
1086 memset(ptr
, ' ', MENU_V_SPADDING
);
1087 ptr
+= MENU_V_SPADDING
;
1089 width
= MENU_H_SPADDING
+ cf
->menu
->width
+ MENU_H_SPADDING
;
1092 mbsalign(name
, ptr
, buf_sz
, &width
,
1093 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
1096 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
1100 mvaddch(ln
, cl
- 1, ACS_VLINE
);
1101 mvaddch(ln
, cl
+ MENU_V_ITEMWIDTH(cf
->menu
), ACS_VLINE
);
1104 if (cf
->menu
->idx
== idx
)
1108 mvprintw(ln
, cl
, "%s", buf
);
1110 mvprintw(ln
, cl
, "%s%s%s", MENU_H_PRESTR
, buf
, MENU_H_POSTSTR
);
1113 if (cf
->menu
->idx
== idx
) {
1116 ui_hint(_(d
->desc
));
1120 static void ui_clean_menu(struct cfdisk
*cf
)
1124 struct cfdisk_menu
*m
= cf
->menu
;
1125 size_t ln
= menuitem_get_line(cf
, 0);
1128 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1130 lastline
= menuitem_get_line(cf
, m
->nitems
);
1132 for (i
= ln
; i
<= lastline
; i
++) {
1135 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1144 static void ui_draw_menu(struct cfdisk
*cf
)
1146 struct cfdisk_menuitem
*d
;
1147 struct cfdisk_menu
*m
;
1149 size_t ln
= menuitem_get_line(cf
, 0);
1155 DBG(MENU
, ul_debug("draw start"));
1161 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1163 nlines
= menuitem_get_line(cf
, m
->nitems
);
1166 menu_update_ignore(cf
);
1168 while ((d
= menu_get_menuitem(cf
, i
)))
1169 ui_draw_menuitem(cf
, d
, i
++);
1172 size_t cl
= menuitem_get_column(cf
, 0);
1173 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1175 /* corners and horizontal lines */
1176 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1177 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1179 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1180 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1181 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1184 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1185 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1187 /* draw also lines around empty lines on last page */
1189 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1190 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1191 mvaddch(i
, cl
- 1, ACS_VLINE
);
1192 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1197 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1201 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1202 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1203 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1206 DBG(MENU
, ul_debug("draw end."));
1209 inline static int extra_insert_pair(struct cfdisk_line
*l
, const char *name
, const char *data
)
1211 struct libscols_line
*lsl
;
1217 if (!data
|| !*data
)
1220 lsl
= scols_table_new_line(l
->extra
, NULL
);
1224 rc
= scols_line_set_data(lsl
, 0, name
);
1226 rc
= scols_line_set_data(lsl
, 1, data
);
1231 #ifndef HAVE_LIBMOUNT
1232 static char *get_mountpoint( struct cfdisk
*cf
__attribute__((unused
)),
1233 const char *tagname
__attribute__((unused
)),
1234 const char *tagdata
__attribute__((unused
)))
1239 static char *get_mountpoint(struct cfdisk
*cf
, const char *tagname
, const char *tagdata
)
1241 struct libmnt_fs
*fs
= NULL
;
1242 char *target
= NULL
;
1248 DBG(UI
, ul_debug("asking for mountpoint [%s=%s]", tagname
, tagdata
));
1251 cf
->mntcache
= mnt_new_cache();
1253 /* 1st try between mounted filesystems */
1255 cf
->mtab
= mnt_new_table();
1257 mnt_table_set_cache(cf
->mtab
, cf
->mntcache
);
1258 mnt_table_parse_mtab(cf
->mtab
, NULL
);
1263 fs
= mnt_table_find_tag(cf
->mtab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1268 cf
->fstab
= mnt_new_table();
1270 mnt_table_set_cache(cf
->fstab
, cf
->mntcache
);
1271 mnt_table_parse_fstab(cf
->fstab
, NULL
);
1275 fs
= mnt_table_find_tag(cf
->fstab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1281 xasprintf(&target
, _("%s (mounted)"), mnt_fs_get_target(fs
));
1283 target
= xstrdup(mnt_fs_get_target(fs
));
1288 #endif /* HAVE_LIBMOUNT */
1290 static void extra_prepare_data(struct cfdisk
*cf
)
1292 struct fdisk_partition
*pa
= get_current_partition(cf
);
1293 struct cfdisk_line
*l
= &cf
->lines
[cf
->lines_idx
];
1295 char *mountpoint
= NULL
;
1297 DBG(UI
, ul_debug("preparing extra data"));
1299 /* string data should not equal an empty string */
1300 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_NAME
, &data
) && data
) {
1301 extra_insert_pair(l
, _("Partition name:"), data
);
1303 mountpoint
= get_mountpoint(cf
, "PARTLABEL", data
);
1307 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_UUID
, &data
) && data
) {
1308 extra_insert_pair(l
, _("Partition UUID:"), data
);
1310 mountpoint
= get_mountpoint(cf
, "PARTUUID", data
);
1314 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPE
, &data
) && data
) {
1315 char *code
= NULL
, *type
= NULL
;
1317 fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPEID
, &code
);
1318 xasprintf(&type
, "%s (%s)", data
, code
);
1320 extra_insert_pair(l
, _("Partition type:"), type
);
1326 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_ATTR
, &data
) && data
) {
1327 extra_insert_pair(l
, _("Attributes:"), data
);
1331 /* for numeric data, only show non-zero rows */
1332 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_BSIZE
, &data
) && data
) {
1334 extra_insert_pair(l
, "BSIZE:", data
);
1338 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_CPG
, &data
) && data
) {
1340 extra_insert_pair(l
, "CPG:", data
);
1344 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSIZE
, &data
) && data
) {
1346 extra_insert_pair(l
, "FSIZE:", data
);
1350 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSUUID
, &data
) && data
) {
1351 extra_insert_pair(l
, _("Filesystem UUID:"), data
);
1353 mountpoint
= get_mountpoint(cf
, "UUID", data
);
1357 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSLABEL
, &data
) && data
) {
1358 extra_insert_pair(l
, _("Filesystem LABEL:"), data
);
1360 mountpoint
= get_mountpoint(cf
, "LABEL", data
);
1363 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSTYPE
, &data
) && data
) {
1364 extra_insert_pair(l
, _("Filesystem:"), data
);
1369 extra_insert_pair(l
, _("Mountpoint:"), mountpoint
);
1374 static int ui_draw_extra(struct cfdisk
*cf
)
1378 struct cfdisk_line
*ln
= &cf
->lines
[cf
->lines_idx
];
1379 char *tbstr
= NULL
, *end
;
1380 int win_ex_start_line
, win_height
, tblen
;
1383 if (!cf
->show_extra
)
1386 DBG(UI
, ul_debug("draw extra"));
1391 wclear(cf
->act_win
);
1395 if (scols_table_is_empty(ln
->extra
)) {
1396 extra_prepare_data(cf
);
1397 if (scols_table_is_empty(ln
->extra
))
1401 ndatalines
= fdisk_table_get_nents(cf
->table
) + 1;
1403 /* nents + header + one free line */
1404 win_ex_start_line
= TABLE_START_LINE
+ ndatalines
;
1405 win_height
= MENU_START_LINE
- win_ex_start_line
;
1406 tblen
= scols_table_get_nlines(ln
->extra
);
1408 /* we can't get a single line of data under the partlist*/
1412 /* number of data lines + 2 for top/bottom lines */
1413 win_height
= win_height
< tblen
+ 2 ? win_height
: tblen
+ 2;
1415 if ((size_t) win_ex_start_line
+ win_height
+ 1 < MENU_START_LINE
)
1416 win_ex_start_line
= MENU_START_LINE
- win_height
;
1418 win_ex
= subwin(stdscr
, win_height
, ui_cols
- 2, win_ex_start_line
, 1);
1420 scols_table_reduce_termwidth(ln
->extra
, 4);
1421 scols_print_table_to_string(ln
->extra
, &tbstr
);
1424 while ((end
= strchr(end
, '\n')))
1430 while (--win_height
> 1) {
1431 mvwaddstr(win_ex
, wline
++, 1 /* window column*/, tbstr
);
1432 tbstr
+= strlen(tbstr
) + 1;
1439 DBG(UI
, ul_debug("draw window: %p", win_ex
));
1443 cf
->act_win
= ln
->w
= win_ex
;
1447 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1449 struct cfdisk_menuitem
*d
;
1452 /* stop and begin/end for vertical menus */
1453 if (cf
->menu
->vertical
) {
1456 else if (where
> (int) cf
->menu
->nitems
- 1)
1457 where
= cf
->menu
->nitems
- 1;
1459 /* continue from begin/end */
1461 where
= cf
->menu
->nitems
- 1;
1462 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1465 if ((size_t) where
== cf
->menu
->idx
)
1470 old
= cf
->menu
->idx
;
1471 cf
->menu
->idx
= where
;
1473 if (!menuitem_on_page(cf
, old
)) {
1478 d
= menu_get_menuitem(cf
, old
);
1479 ui_draw_menuitem(cf
, d
, old
);
1481 d
= menu_get_menuitem(cf
, where
);
1482 ui_draw_menuitem(cf
, d
, where
);
1486 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1488 struct cfdisk_menu
*m
;
1493 if (key
== (int) ERR
)
1494 return 0; /* ignore errors */
1498 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1504 case '\016': /* ^N */
1505 case 'j': /* Vi-like alternative */
1506 ui_menu_goto(cf
, m
->idx
+ 1);
1509 case '\020': /* ^P */
1510 case 'k': /* Vi-like alternative */
1511 ui_menu_goto(cf
, (int) m
->idx
- 1);
1515 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1520 ui_menu_goto(cf
, 0);
1524 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1529 ui_menu_goto(cf
, m
->nitems
);
1536 ui_menu_goto(cf
, m
->idx
+ 1);
1542 ui_menu_goto(cf
, (int) m
->idx
- 1);
1547 if (key
== '\014') { /* ^L refresh */
1552 DBG(MENU
, ul_debug(" no menu move key"));
1556 /* but don't call me from ui_run(), this is for pop-up menus only */
1557 static void ui_menu_resize(struct cfdisk
*cf
)
1559 DBG(MENU
, ul_debug("menu resize/refresh"));
1562 menu_refresh_size(cf
);
1567 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1569 if (cf
->page_sz
== 0 ||
1570 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1575 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1577 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1578 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1579 int cur
= cf
->lines_idx
== i
;
1583 if (!partition_on_page(cf
, i
))
1585 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1586 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1590 "draw partition %zu [page_sz=%zu, "
1591 "line=%d, idx=%zu]",
1592 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1596 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1597 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1602 if (colors_wanted() && is_freespace(cf
, i
)) {
1603 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1606 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1607 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1609 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1612 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1613 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1616 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1617 mvaddch(ln
, 0, ACS_DARROW
);
1624 static int ui_draw_table(struct cfdisk
*cf
)
1626 int cl
= ARROW_CURSOR_WIDTH
;
1627 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1628 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1630 DBG(UI
, ul_debug("draw table"));
1632 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1637 if (nparts
== 0 || (size_t) cf
->lines_idx
> nparts
- 1)
1638 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1642 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0].data
);
1645 /* print partitions */
1646 for (i
= 0; i
< nparts
; i
++)
1647 ui_draw_partition(cf
, i
);
1650 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1651 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1653 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1654 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1655 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1660 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1663 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1665 DBG(UI
, ul_debug("goto table %d", where
));
1669 else if ((size_t) where
> nparts
- 1)
1672 if ((size_t) where
== cf
->lines_idx
)
1675 old
= cf
->lines_idx
;
1676 cf
->lines_idx
= where
;
1678 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1681 ui_draw_partition(cf
, old
); /* cleanup old */
1682 ui_draw_partition(cf
, where
); /* draw new */
1692 static int ui_refresh(struct cfdisk
*cf
)
1694 struct fdisk_label
*lb
;
1696 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1702 strsz
= size_to_human_string(SIZE_DECIMAL_2DIGITS
1704 | SIZE_SUFFIX_3LETTER
, bytes
);
1706 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1713 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1715 ui_center(1, _("Size: %s, %"PRIu64
" bytes, %ju sectors"),
1716 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1717 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1718 ui_center(2, _("Label: %s, identifier: %s"),
1719 fdisk_label_get_name(lb
), id
);
1721 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1730 static ssize_t
ui_get_string(const char *prompt
,
1731 const char *hint
, char *buf
, size_t len
)
1733 int ln
= MENU_START_LINE
, cl
= 1;
1735 struct mbs_editor
*edit
;
1737 DBG(UI
, ul_debug("ui get string"));
1749 mvaddstr(ln
, cl
, prompt
);
1750 cl
+= mbs_safe_width(prompt
);
1753 edit
= mbs_new_edit(buf
, len
, ui_cols
- cl
);
1757 mbs_edit_goto(edit
, MBS_EDIT_END
);
1767 wint_t c
; /* we have fallback in widechar.h */
1771 mvaddstr(ln
, cl
, edit
->buf
);
1772 move(ln
, cl
+ edit
->cursor_cells
);
1775 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1776 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1777 if (get_wch(&c
) == ERR
) {
1779 if ((c
= getch()) == (wint_t) ERR
) {
1787 if (!isatty(STDIN_FILENO
))
1793 DBG(UI
, ul_debug("ui get string: key=%lc", c
));
1795 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1802 rc
= -CFDISK_ERR_ESC
;
1805 rc
= mbs_edit_goto(edit
, MBS_EDIT_LEFT
);
1808 rc
= mbs_edit_goto(edit
, MBS_EDIT_RIGHT
);
1811 rc
= mbs_edit_goto(edit
, MBS_EDIT_END
);
1814 rc
= mbs_edit_goto(edit
, MBS_EDIT_HOME
);
1820 rc
= mbs_edit_delete(edit
);
1825 rc
= mbs_edit_backspace(edit
);
1828 rc
= mbs_edit_insert(edit
, c
);
1838 rc
= strlen(edit
->buf
); /* success */
1844 mbs_free_edit(edit
);
1849 static int ui_get_size(struct cfdisk
*cf
, /* context */
1850 const char *prompt
, /* UI dialog string */
1851 uint64_t *res
, /* result in bytes */
1852 uint64_t low
, /* minimal size */
1853 uint64_t up
, /* maximal size */
1854 int *expsize
) /* explicitly specified size */
1859 char *dflt
= size_to_human_string(0, *res
);
1861 DBG(UI
, ul_debug("get_size (default=%"PRIu64
")", *res
));
1865 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1868 int pwr
= 0, insec
= 0;
1870 rc
= ui_get_string(prompt
,
1871 _("May be followed by M for MiB, G for GiB, "
1872 "T for TiB, or S for sectors."),
1877 ui_warnx(_("Please, specify size."));
1878 continue; /* nothing specified */
1879 } else if (rc
== -CFDISK_ERR_ESC
)
1880 break; /* cancel dialog */
1882 if (strcmp(buf
, dflt
) == 0)
1883 user
= *res
, rc
= 0; /* no change, use default */
1885 size_t len
= strlen(buf
);
1886 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1888 buf
[len
- 1] = '\0';
1890 rc
= parse_size(buf
, (uintmax_t *)&user
, &pwr
); /* parse */
1894 DBG(UI
, ul_debug("get_size user=%"PRIu64
", power=%d, in-sectors=%s",
1895 user
, pwr
, insec
? "yes" : "no"));
1897 user
*= fdisk_get_sector_size(cf
->cxt
);
1899 ui_warnx(_("Minimum size is %"PRIu64
" bytes."), low
);
1902 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1903 /* ignore when the user specified size overflow
1904 * with in range specified by suffix (e.g. MiB) */
1908 ui_warnx(_("Maximum size is %"PRIu64
" bytes."), up
);
1911 if (rc
== 0 && insec
&& expsize
)
1915 ui_warnx(_("Failed to parse size."));
1922 DBG(UI
, ul_debug("get_size (result=%"PRIu64
", rc=%zd)", *res
, rc
));
1926 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1927 struct fdisk_parttype
*cur
)
1929 struct cfdisk_menuitem
*d
, *cm
;
1930 size_t i
= 0, nitems
, idx
= 0;
1931 struct fdisk_parttype
*t
= NULL
;
1932 struct fdisk_label
*lb
;
1935 DBG(UI
, ul_debug("asking for parttype."));
1937 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1939 /* create cfdisk menu according to label types, note that the
1940 * last cm[] item has to be empty -- so nitems + 1 */
1941 nitems
= fdisk_label_get_nparttypes(lb
);
1945 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1949 codetypes
= fdisk_label_has_code_parttypes(lb
);
1951 for (i
= 0; i
< nitems
; i
++) {
1952 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1955 cm
[i
].userdata
= (void *) x
;
1957 xasprintf(&name
, "%2x %s",
1958 fdisk_parttype_get_code(x
),
1959 _(fdisk_parttype_get_name(x
)));
1961 name
= (char *) _(fdisk_parttype_get_name(x
));
1962 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1969 /* make the new menu active */
1971 cf
->menu
->vertical
= 1;
1972 cf
->menu
->idx
= idx
;
1973 menu_set_title(cf
->menu
, _("Select partition type"));
1984 if (ui_menu_move(cf
, key
) == 0)
1991 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1993 t
= (struct fdisk_parttype
*) d
->userdata
;
2007 for (i
= 0; i
< nitems
; i
++)
2008 free((char *) cm
[i
].name
);
2011 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
2012 fdisk_parttype_get_name(t
) : NULL
));
2016 static int ui_script_read(struct cfdisk
*cf
)
2018 struct fdisk_script
*sc
= NULL
;
2019 char buf
[PATH_MAX
] = { 0 };
2023 rc
= ui_get_string( _("Enter script file name: "),
2024 _("The script file will be applied to in-memory partition table."),
2031 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
2033 ui_warn(_("Cannot open %s"), buf
);
2035 ui_warnx(_("Failed to parse script file %s"), buf
);
2036 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
2037 ui_warnx(_("Failed to apply script %s"), buf
);
2042 fdisk_unref_script(sc
);
2046 static int ui_script_write(struct cfdisk
*cf
)
2048 struct fdisk_script
*sc
= NULL
;
2049 char buf
[PATH_MAX
] = { 0 };
2053 rc
= ui_get_string( _("Enter script file name: "),
2054 _("The current in-memory partition table will be dumped to the file."),
2060 sc
= fdisk_new_script(cf
->cxt
);
2062 ui_warn(_("Failed to allocate script handler"));
2066 rc
= fdisk_script_read_context(sc
, NULL
);
2068 ui_warnx(_("Failed to read disk layout into script."));
2072 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
2073 f
= fopen(buf
, "w");
2075 ui_warn(_("Cannot open %s"), buf
);
2080 rc
= fdisk_script_write_file(sc
, f
);
2082 ui_info(_("Disk layout successfully dumped."));
2085 ui_warn(_("Failed to write script %s"), buf
);
2088 fdisk_unref_script(sc
);
2092 /* prints menu with libfdisk labels and waits for users response */
2093 static int ui_create_label(struct cfdisk
*cf
)
2095 struct cfdisk_menuitem
*d
, *cm
;
2096 int rc
= 1, refresh_menu
= 1;
2097 size_t i
= 0, nitems
;
2098 struct fdisk_label
*lb
= NULL
;
2102 DBG(UI
, ul_debug("asking for new disklabe."));
2104 /* create cfdisk menu according to libfdisk labels, note that the
2105 * last cm[] item has to be empty -- so nitems + 1 */
2106 nitems
= fdisk_get_nlabels(cf
->cxt
);
2107 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
2109 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
2110 if (fdisk_label_is_disabled(lb
) ||
2111 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
2113 cm
[i
++].name
= fdisk_label_get_name(lb
);
2118 /* make the new menu active */
2120 cf
->menu
->vertical
= 1;
2121 menu_set_title(cf
->menu
, _("Select label type"));
2123 if (!cf
->zero_start
)
2124 ui_info(_("Device does not contain a recognized partition table."));
2132 ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
2143 if (ui_menu_move(cf
, key
) == 0)
2149 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2151 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
2159 rc
= ui_script_read(cf
);
2172 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
2177 static int ui_help(void)
2180 static const char *help
[] = {
2181 N_("This is cfdisk, a curses-based disk partitioning program."),
2182 N_("It lets you create, delete, and modify partitions on a block device."),
2184 N_("Command Meaning"),
2185 N_("------- -------"),
2186 N_(" b Toggle bootable flag of the current partition"),
2187 N_(" d Delete the current partition"),
2188 N_(" h Print this screen"),
2189 N_(" n Create new partition from free space"),
2190 N_(" q Quit program without writing partition table"),
2191 N_(" s Fix partitions order (only when in disarray)"),
2192 N_(" t Change the partition type"),
2193 N_(" u Dump disk layout to sfdisk compatible script file"),
2194 N_(" W Write partition table to disk (you must enter uppercase W);"),
2195 N_(" since this might destroy data on the disk, you must either"),
2196 N_(" confirm or deny the write by entering 'yes' or 'no'"),
2197 N_(" x Display/hide extra information about a partition"),
2198 N_("Up Arrow Move cursor to the previous partition"),
2199 N_("Down Arrow Move cursor to the next partition"),
2200 N_("Left Arrow Move cursor to the previous menu item"),
2201 N_("Right Arrow Move cursor to the next menu item"),
2203 N_("Note: All of the commands can be entered with either upper or lower"),
2204 N_("case letters (except for Write)."),
2206 N_("Use lsblk(8) or partx(8) to see more details about the device."),
2209 "Copyright (C) 2014-2017 Karel Zak <kzak@redhat.com>"
2213 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
2214 mvaddstr(i
, 1, _(help
[i
]));
2216 ui_info(_("Press a key to continue."));
2225 /* TODO: use @sz, now 128bytes */
2226 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
2227 size_t sz
__attribute__((__unused__
)))
2229 struct fdisk_partition
*pa
= get_current_partition(cf
);
2234 if (fdisk_partition_is_freespace(pa
)) {
2235 ignore
[i
++] = 'd'; /* delete */
2236 ignore
[i
++] = 't'; /* set type */
2237 ignore
[i
++] = 'b'; /* set bootable */
2238 ignore
[i
++] = 'r'; /* resize */
2239 cf
->menu
->prefkey
= 'n';
2241 cf
->menu
->prefkey
= 'q';
2243 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
2244 !fdisk_is_label(cf
->cxt
, SGI
))
2248 if (!cf
->wrong_order
)
2251 if (fdisk_is_readonly(cf
->cxt
))
2258 /* returns: error: < 0, success: 0, quit: 1 */
2259 static int main_menu_action(struct cfdisk
*cf
, int key
)
2262 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
2263 const char *info
= NULL
, *warn
= NULL
;
2264 struct fdisk_partition
*pa
;
2271 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2276 } else if (key
!= 'w' && key
!= 'W')
2277 key
= tolower(key
); /* case insensitive except 'W'rite */
2279 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
2281 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
2282 DBG(MENU
, ul_debug(" ignore '%c'", key
));
2286 pa
= get_current_partition(cf
);
2289 n
= fdisk_partition_get_partno(pa
);
2291 DBG(MENU
, ul_debug("menu action on %p", pa
));
2296 case 'b': /* Bootable flag */
2298 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
2299 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
2301 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
2302 warn
= _("Could not toggle the flag.");
2310 case 'd': /* Delete */
2311 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
2312 warn
= _("Could not delete partition %zu.");
2314 info
= _("Partition %zu has been deleted.");
2317 case 'h': /* Help */
2324 uint64_t start
, size
, dflt_size
, secs
, max_size
;
2325 struct fdisk_partition
*npa
; /* the new partition */
2326 int expsize
= 0; /* size specified explicitly in sectors */
2328 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2331 /* free space range */
2332 start
= fdisk_partition_get_start(pa
);
2333 size
= max_size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
2335 if (ui_get_size(cf
, _("Partition size: "), &size
,
2336 fdisk_get_sector_size(cf
->cxt
),
2337 max_size
, &expsize
) == -CFDISK_ERR_ESC
)
2340 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2342 npa
= fdisk_new_partition();
2346 if (dflt_size
== size
) /* default is to fillin all free space */
2347 fdisk_partition_end_follow_default(npa
, 1);
2349 fdisk_partition_set_size(npa
, secs
);
2352 fdisk_partition_size_explicit(pa
, 1);
2354 fdisk_partition_set_start(npa
, start
);
2355 fdisk_partition_partno_follow_default(npa
, 1);
2356 /* add to disk label -- libfdisk will ask for missing details */
2357 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
2358 fdisk_unref_partition(npa
);
2363 case 'q': /* Quit */
2365 case 't': /* Type */
2367 struct fdisk_parttype
*t
;
2369 if (fdisk_partition_is_freespace(pa
))
2371 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
2372 t
= ui_get_parttype(cf
, t
);
2375 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
2376 info
= _("Changed type of partition %zu.");
2378 info
= _("The type of partition %zu is unchanged.");
2381 case 'r': /* resize */
2383 struct fdisk_partition
*npa
, *next
;
2384 uint64_t size
, max_size
, secs
;
2386 if (fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2389 size
= fdisk_partition_get_size(pa
);
2391 /* is the next freespace? */
2392 next
= fdisk_table_get_partition(cf
->table
, cf
->lines_idx
+ 1);
2393 if (next
&& fdisk_partition_is_freespace(next
))
2394 size
+= fdisk_partition_get_size(next
);
2396 size
*= fdisk_get_sector_size(cf
->cxt
);
2399 if (ui_get_size(cf
, _("New size: "), &size
,
2400 fdisk_get_sector_size(cf
->cxt
),
2401 max_size
, NULL
) == -CFDISK_ERR_ESC
)
2403 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2404 npa
= fdisk_new_partition();
2408 fdisk_partition_set_size(npa
, secs
);
2410 rc
= fdisk_set_partition(cf
->cxt
, n
, npa
);
2411 fdisk_unref_partition(npa
);
2414 info
= _("Partition %zu resized.");
2418 case 's': /* Sort */
2419 if (cf
->wrong_order
) {
2420 fdisk_reorder_partitions(cf
->cxt
);
2424 case 'u': /* dUmp */
2425 ui_script_write(cf
);
2427 case 'W': /* Write */
2429 char buf
[64] = { 0 };
2431 if (fdisk_is_readonly(cf
->cxt
)) {
2432 warn
= _("Device is open in read-only mode.");
2437 _("Are you sure you want to write the partition "
2439 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2443 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2444 strcasecmp(buf
, _("yes")) != 0)) {
2445 info
= _("Did not write partition table to disk.");
2448 rc
= fdisk_write_disklabel(cf
->cxt
);
2450 warn
= _("Failed to write disklabel.");
2452 if (cf
->device_is_used
)
2453 fdisk_reread_changes(cf
->cxt
, cf
->original_layout
);
2455 fdisk_reread_partition_table(cf
->cxt
);
2456 info
= _("The partition table has been altered.");
2475 ui_warnx(warn
, n
+ 1);
2477 ui_info(info
, n
+ 1);
2478 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2479 ui_info(_("Note that partition table entries are not in disk order now."));
2484 static void ui_resize_refresh(struct cfdisk
*cf
)
2486 DBG(UI
, ul_debug("ui resize/refresh"));
2488 menu_refresh_size(cf
);
2494 static void toggle_show_extra(struct cfdisk
*cf
)
2496 if (cf
->show_extra
&& cf
->act_win
) {
2497 wclear(cf
->act_win
);
2500 cf
->show_extra
= cf
->show_extra
? 0 : 1;
2504 DBG(MENU
, ul_debug("extra: %s", cf
->show_extra
? "ENABLED" : "DISABLED" ));
2507 static int ui_run(struct cfdisk
*cf
)
2513 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2515 if (fdisk_get_collision(cf
->cxt
)) {
2516 ui_warnx(_("Device already contains a %s signature; it will be removed by a write command."),
2517 fdisk_get_collision(cf
->cxt
));
2518 fdisk_enable_wipe(cf
->cxt
, 1);
2519 ui_hint(_("Press a key to continue."));
2523 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2524 rc
= ui_create_label(cf
);
2526 ui_errx(EXIT_FAILURE
,
2527 _("failed to create a new disklabel"));
2533 rc
= lines_refresh(cf
);
2535 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2537 menu_push(cf
, main_menuitems
);
2538 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2540 rc
= ui_refresh(cf
);
2547 if (fdisk_is_readonly(cf
->cxt
))
2548 ui_warnx(_("Device is open in read-only mode."));
2549 else if (cf
->wrong_order
)
2550 ui_info(_("Note that partition table entries are not in disk order now."));
2560 /* Note that ncurses getch() returns ERR when interrupted
2561 * by signal, but SLang does not interrupt at all. */
2562 ui_resize_refresh(cf
);
2565 if (key
== '\014') { /* ^L refresh */
2566 ui_resize_refresh(cf
);
2569 if (ui_menu_move(cf
, key
) == 0)
2572 DBG(UI
, ul_debug("main action key >%1$c< [\\0%1$o].", key
));
2576 case '\016': /* ^N */
2577 case 'j': /* Vi-like alternative */
2578 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2581 case '\020': /* ^P */
2582 case 'k': /* Vi-like alternative */
2583 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2587 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2592 ui_table_goto(cf
, 0);
2596 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2601 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2606 rc
= main_menu_action(cf
, 0);
2609 case 'x': /* Extra */
2610 toggle_show_extra(cf
);
2613 rc
= main_menu_action(cf
, key
);
2625 DBG(UI
, ul_debug("end"));
2629 static void __attribute__((__noreturn__
)) usage(void)
2632 fputs(USAGE_HEADER
, out
);
2634 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2636 fputs(USAGE_SEPARATOR
, out
);
2637 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2639 fputs(USAGE_OPTIONS
, out
);
2640 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out
);
2642 " %s\n", USAGE_COLORS_DEFAULT
);
2643 fputs(_(" -z, --zero start with zeroed partition table\n"), out
);
2645 fputs(USAGE_SEPARATOR
, out
);
2646 printf(USAGE_HELP_OPTIONS(26));
2648 printf(USAGE_MAN_TAIL("cfdisk(8)"));
2652 int main(int argc
, char *argv
[])
2654 const char *diskpath
= NULL
;
2655 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2656 struct cfdisk _cf
= { .lines_idx
= 0 },
2659 static const struct option longopts
[] = {
2660 { "color", optional_argument
, NULL
, 'L' },
2661 { "help", no_argument
, NULL
, 'h' },
2662 { "version", no_argument
, NULL
, 'V' },
2663 { "zero", no_argument
, NULL
, 'z' },
2664 { NULL
, 0, NULL
, 0 },
2667 setlocale(LC_ALL
, "");
2668 bindtextdomain(PACKAGE
, LOCALEDIR
);
2669 textdomain(PACKAGE
);
2670 close_stdout_atexit();
2672 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2678 colormode
= UL_COLORMODE_AUTO
;
2680 colormode
= colormode_or_err(optarg
,
2681 _("unsupported color mode"));
2684 print_version(EXIT_SUCCESS
);
2689 errtryhelp(EXIT_FAILURE
);
2693 colors_init(colormode
, "cfdisk");
2695 fdisk_init_debug(0);
2696 scols_init_debug(0);
2697 cfdisk_init_debug();
2698 cf
->cxt
= fdisk_new_context();
2700 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2702 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2704 if (optind
== argc
) {
2707 for (i
= 0; i
< ARRAY_SIZE(default_disks
); i
++) {
2708 if (access(default_disks
[i
], F_OK
) == 0) {
2709 diskpath
= default_disks
[i
];
2714 diskpath
= default_disks
[0]; /* default, used for "cannot open" */
2716 diskpath
= argv
[optind
];
2718 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2720 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2722 err(EXIT_FAILURE
, _("cannot open %s"), diskpath
);
2724 if (!fdisk_is_readonly(cf
->cxt
)) {
2725 cf
->device_is_used
= fdisk_device_is_used(cf
->cxt
);
2726 fdisk_get_partitions(cf
->cxt
, &cf
->original_layout
);
2729 /* Don't use err(), warn() from this point */
2734 cfdisk_free_lines(cf
);
2737 fdisk_unref_table(cf
->table
);
2738 #ifdef HAVE_LIBMOUNT
2739 mnt_unref_table(cf
->fstab
);
2740 mnt_unref_table(cf
->mtab
);
2741 mnt_unref_cache(cf
->mntcache
);
2743 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2744 fdisk_unref_context(cf
->cxt
);
2745 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2746 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;