#include "partx.h"
#include "sysfs.h"
#include "loopdev.h"
-#include "at.h"
#include "closestream.h"
#include "optutils.h"
};
/* columns descriptions */
-struct colinfo infos[] = {
+static struct colinfo infos[] = {
[COL_PARTNO] = { "NR", 0.25, SCOLS_FL_RIGHT, N_("partition number") },
[COL_START] = { "START", 0.30, SCOLS_FL_RIGHT, N_("start of the partition in sectors") },
[COL_END] = { "END", 0.30, SCOLS_FL_RIGHT, N_("end of the partition in sectors") },
#define NCOLS ARRAY_SIZE(infos)
/* array with IDs of enabled columns */
-static int columns[NCOLS], ncolumns;
+static int columns[NCOLS];
+static size_t ncolumns;
static int verbose;
static int partx_flags;
static inline int get_column_id(int num)
{
assert(ARRAY_SIZE(columns) == NCOLS);
- assert(num < ncolumns);
+ assert((size_t)num < ncolumns);
assert(columns[num] < (int) NCOLS);
return columns[num];
}
sz = strlen(partition);
p = partition + sz - 1;
- if (!isdigit((unsigned int) *p))
+ if (!isdigit((unsigned char) *p))
goto err;
- while (isdigit((unsigned int) *(p - 1))) p--;
+ while (isdigit((unsigned char) *(p - 1))) p--;
errno = 0;
partno = strtol(p, &end, 10);
continue;
snprintf(path, sizeof(path), "%s/partition", d->d_name);
- fd = open_at(dirfd(dir), dirname, path, O_RDONLY);
+ fd = openat(dirfd(dir), path, O_RDONLY);
if (fd) {
int x = 0;
FILE *f = fdopen(fd, "r");
return SLICES_MAX;
}
+static int recount_range_by_pt(blkid_partlist ls, int *lower, int *upper)
+{
+ int n = 0, i, nparts = blkid_partlist_numof_partitions(ls);
+
+ for (i = 0; i < nparts; i++) {
+ blkid_partition par = blkid_partlist_get_partition(ls, i);
+ int partno = blkid_partition_get_partno(par);
+ n = max(partno, n);
+ }
+
+ if (*lower < 0)
+ *lower = n + *lower + 1;
+ if (*upper < 0)
+ *upper = n + *upper + 1;
+
+ if (*lower > *upper && *upper != 0) {
+ warnx(_("specified range <%d:%d> does not make sense"), *lower, *upper);
+ return -EINVAL;
+ }
+ if (verbose)
+ printf(_("range recount: max partno=%d, lower=%d, upper=%d\n"), n, *lower, *upper);
+ return 0;
+}
+
static void del_parts_warnx(const char *device, int first, int last)
{
if (first == last)
assert(fd >= 0);
assert(device);
+ /* recount range by information in /sys */
if (!lower)
lower = 1;
if (!upper || lower < 0 || upper < 0) {
static int add_parts(int fd, const char *device,
blkid_partlist ls, int lower, int upper)
{
- int i, nparts, rc = 0, errfirst = 0, errlast = 0;
+ int i, nparts, rc, errfirst = 0, errlast = 0;
assert(fd >= 0);
assert(device);
assert(ls);
+ rc = recount_range_by_pt(ls, &lower, &upper);
+ if (rc)
+ return rc;
+
nparts = blkid_partlist_numof_partitions(ls);
for (i = 0; i < nparts; i++) {
assert(device);
assert(ls);
+ /* recount range by information in /sys, if on disk number of
+ * partitions is greater than in /sys the use on-disk limit */
nparts = blkid_partlist_numof_partitions(ls);
if (!lower)
lower = 1;
static int list_parts(blkid_partlist ls, int lower, int upper)
{
- int i, nparts;
+ int i, nparts, rc;
assert(ls);
+ rc = recount_range_by_pt(ls, &lower, &upper);
+ if (rc)
+ return rc;
+
nparts = blkid_partlist_numof_partitions(ls);
for (i = 0; i < nparts; i++) {
line = scols_table_new_line(table, NULL);
if (!line) {
- warn(_("failed to add line to output"));
+ warn(_("failed to allocate output line"));
return -ENOMEM;
}
- for (i = 0; i < ncolumns; i++) {
+ for (i = 0; (size_t)i < ncolumns; i++) {
char *str = NULL; /* allocated string */
const char *cstr = NULL; /* foreign string */
else if (str)
rc = scols_line_refer_data(line, i, str);
if (rc) {
- warn(_("failed to add data to output table"));
+ warn(_("failed to add output data"));
break;
}
}
scols_init_debug(0);
table = scols_new_table();
if (!table) {
- warn(_("failed to initialize output table"));
+ warn(_("failed to allocate output table"));
return -1;
}
scols_table_enable_raw(table, !!(scols_flags & PARTX_RAW));
scols_table_enable_export(table, !!(scols_flags & PARTX_EXPORT));
scols_table_enable_noheadings(table, !!(scols_flags & PARTX_NOHEADINGS));
- for (i = 0; i < ncolumns; i++) {
+ for (i = 0; (size_t)i < ncolumns; i++) {
struct colinfo *col = get_column_info(i);
if (!scols_table_new_column(table, col->name, col->whint, col->flags)) {
- warnx(_("failed to initialize output column"));
+ warnx(_("failed to allocate output column"));
goto done;
}
}
+ rc = recount_range_by_pt(ls, &lower, &upper);
+ if (rc)
+ goto done;
+
for (i = 0; i < nparts; i++) {
blkid_partition par = blkid_partlist_get_partition(ls, i);
int n = blkid_partition_get_partno(par);
fputs(_(" -o, --output <list> define which output columns to use\n"), out);
fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
fputs(_(" -r, --raw use raw output format\n"), out);
- fputs(_(" -t, --type <type> specify the partition type (dos, bsd, solaris, etc.)\n"), out);
+ fputs(_(" -S, --sector-size <num> overwrite sector size\n"), out);
+ fputs(_(" -t, --type <type> specify the partition type\n"), out);
+ fputs(_(" --list-types list supported partition types and exit\n"), out);
fputs(_(" -v, --verbose verbose mode\n"), out);
fputs(USAGE_SEPARATOR, out);
char *wholedisk = NULL; /* allocated, ie: /dev/sda */
char *outarg = NULL;
dev_t disk_devno = 0, part_devno = 0;
+ unsigned int sector_size = 0;
+ enum {
+ OPT_LIST_TYPES = CHAR_MAX + 1
+ };
static const struct option long_opts[] = {
{ "bytes", no_argument, NULL, 'b' },
{ "noheadings", no_argument, NULL, 'g' },
{ "delete", no_argument, NULL, 'd' },
{ "update", no_argument, NULL, 'u' },
{ "type", required_argument, NULL, 't' },
+ { "list-types", no_argument, NULL, OPT_LIST_TYPES },
{ "nr", required_argument, NULL, 'n' },
{ "output", required_argument, NULL, 'o' },
{ "pairs", no_argument, NULL, 'P' },
+ { "sector-size",required_argument, NULL, 'S' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "verbose", no_argument, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
- static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
+ static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
{ 'P','a','d','l','r','s','u' },
{ 0 }
};
atexit(close_stdout);
while ((c = getopt_long(argc, argv,
- "abdglrsuvn:t:o:PhV", long_opts, NULL)) != -1) {
+ "abdglrsuvn:t:o:PS:hV", long_opts, NULL)) != -1) {
err_exclusive_options(c, long_opts, excl, excl_st);
case 's':
what = ACT_SHOW;
break;
+ case 'S':
+ sector_size = strtou32_or_err(optarg, _("invalid sector size argument"));
+ break;
case 't':
type = optarg;
break;
case 'v':
verbose = 1;
break;
+ case OPT_LIST_TYPES:
+ {
+ size_t idx = 0;
+ const char *name = NULL;
+
+ while (blkid_partitions_get_name(idx++, &name) == 0)
+ puts(name);
+ return EXIT_SUCCESS;
+ }
case 'h':
usage(stdout);
case 'V':
printf(UTIL_LINUX_VERSION);
return EXIT_SUCCESS;
- case '?':
default:
- usage(stderr);
+ errtryhelp(EXIT_FAILURE);
}
}
} else {
device = argv[optind];
wholedisk = xstrdup(argv[optind + 1]);
+
+ if (device && wholedisk && !startswith(device, wholedisk))
+ errx(EXIT_FAILURE, _("partition and disk name do not match"));
}
} else if (optind == argc - 1) {
/* passed only one arg (ie: /dev/sda3 or /dev/sda) */
device = argv[optind];
if (stat(device, &sb))
- err(EXIT_FAILURE, _("stat failed %s"), device);
+ err(EXIT_FAILURE, _("stat of %s failed"), device);
part_devno = sb.st_rdev;
if (!pr || blkid_probe_set_device(pr, fd, 0, 0))
warnx(_("%s: failed to initialize blkid prober"),
wholedisk);
- else
+ else {
+ if (sector_size)
+ blkid_probe_set_sectorsize(pr, sector_size);
+
ls = get_partlist(pr, wholedisk, type);
+ }
if (ls) {
- int n = blkid_partlist_numof_partitions(ls);
-
- if (lower < 0)
- lower = n + lower + 1;
- if (upper < 0)
- upper = n + upper + 1;
- if (lower > upper) {
- warnx(_("specified range <%d:%d> "
- "does not make sense"), lower, upper);
- rc = -1, what = ACT_NONE;
- }
-
switch (what) {
case ACT_SHOW:
rc = show_parts(ls, scols_flags, lower, upper);
break;
case ACT_UPD:
rc = upd_parts(fd, wholedisk, disk_devno, ls, lower, upper);
+ break;
case ACT_NONE:
break;
default: