Compare commits

..

6 Commits
v1.1 ... master

Author SHA1 Message Date
Jose M. Guisado f20ba067ed setup.py: rename name to python-libfdisk
Rename setuptools project name to fix source distribution filename from:

	libfdisk-1.X.tar.gz

to:

	python-libfdisk-1.X.tar.gz

Source distributions are created running:

	python setup.py sdist
2023-07-10 13:19:16 +02:00
Jose M. Guisado 1a74bf5024 release: bump to version 1.2 2023-03-22 13:31:19 +01:00
Jose M. Guisado 0352d77335 context: wipe new partitions by default
Enable partition wipe to remove any old filesystem/RAID signature when
adding a new partition.

For example, when adding a new partition in the same start/end where a
previous filesystem was created.
2023-03-22 13:26:43 +01:00
Jose M. Guisado e4b2964ee0 label: check parttypes identity type
parttypes can be either:

- A code for MBR/DOS
- An UUID for GPT

User needs a label instance before using a parttype getter. If checking
for a parttype using a code, check if the current label support that
kind of parttype identification before retrieving it. Same applies for
UUID based parttypes.

If wrong identity type is used, raise an exception. Errors regarding
parttype are more informative this way.
2023-01-11 17:11:51 +01:00
Jose M. Guisado 34fd2cbe48 add 'string' field getter to parttype
'string' getter wraps fdisk_parttype_get_string to fetch the partition
type UUID in case of a GPT label device.

This is useful to confirm or check the partition type UUID of any device
with a GPT label.

>>> import fdisk
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> pa = cxt.partitions[0]
>>> pa.type.string
'C12A7328-F81F-11D2-BA4B-00A0C93EC93B'
>>> pa.type.name
'EFI System'
2023-01-11 17:11:41 +01:00
Jose M. Guisado dce0b0c1e3 context: fix assign_device
Fixes a bug where calling assign_device with readonly keyword parameter
raises exception stating it takes no keyword arguments.

Context_assign_device has positional and keyword arguments. Adds
flags METH_KEYWORDS in method declaration (see Context_methods).

Replaces format string in PyArg_ParseTupleAndKeywords from "s|p" to
"s|$p". Adds $ after |, meaning all later optional arguments are
also keyword only.
(See https://docs.python.org/3/c-api/arg.html#other-objects)

Empty names in the kwlist array correspond to positional arguments.

Replaces fname variable name with device for better readability.

Fixes 88c7374db2
("context: check self->cxt and rc in assign_device")
2023-01-10 10:15:24 +01:00
4 changed files with 37 additions and 10 deletions

View File

@ -83,9 +83,9 @@ static int Context_init(ContextObject *self, PyObject *args, PyObject *kwds)
"and switch the current label driver to reflect the probing result. "
static PyObject *Context_assign_device(ContextObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = { "readonly", NULL };
static char *kwlist[] = { "", "readonly", NULL };
int rc, readonly = 0;
char *fname;
char *device;
if (!self->cxt) {
PyErr_SetString(PyExc_TypeError, ARG_ERR);
@ -93,13 +93,13 @@ static PyObject *Context_assign_device(ContextObject *self, PyObject *args, PyOb
}
if (!PyArg_ParseTupleAndKeywords(args,
kwds, "s|p", kwlist,
&readonly)) {
kwds, "s|$p", kwlist,
&device, &readonly)) {
PyErr_SetString(PyExc_TypeError, ARG_ERR);
return NULL;
}
if ((rc = fdisk_assign_device(self->cxt, fname, readonly)) < 0) {
if ((rc = fdisk_assign_device(self->cxt, device, readonly)) < 0) {
set_PyErr_from_rc(-rc);
return NULL;
}
@ -193,12 +193,17 @@ static PyObject *Context_add_partition(ContextObject *self, PyObject *args, PyOb
PyErr_Format(PyExc_RuntimeError, "Error adding partition to context: %s", strerror(-rc));
return NULL;
}
rc = fdisk_wipe_partition(self->cxt, partno, 1);
if (rc < 0) {
PyErr_Format(PyExc_RuntimeError, "Error setting wipe for new partition: %s", strerror(-rc));
return NULL;
}
return Py_BuildValue("n", partno);
}
static PyMethodDef Context_methods[] = {
{"assign_device", (PyCFunction)Context_assign_device, METH_VARARGS, Context_assign_device_HELP},
{"assign_device", (PyCFunction)Context_assign_device, METH_VARARGS | METH_KEYWORDS, Context_assign_device_HELP},
{"partition_to_string", (PyCFunction)Context_partition_to_string, METH_VARARGS, Context_partition_to_string_HELP},
{"create_disklabel", (PyCFunction)Context_create_disklabel, METH_VARARGS, Context_create_disklabel_HELP},
{"write_disklabel", (PyCFunction)Context_write_disklabel, METH_NOARGS, Context_write_disklabel_HELP},

20
label.c
View File

@ -59,15 +59,23 @@ static int Label_init(LabelObject *self, PyObject *args, PyObject *kwds)
"Search for partition type in label-specific table."
static PyObject *Label_get_parttype_from_code(LabelObject *self, PyObject *args, PyObject *kwds)
{
struct fdisk_label *label = self->lb;
struct fdisk_parttype *ptype;
unsigned int ptype_code;
const char *name;
if (!PyArg_ParseTuple(args, "I", &ptype_code)) {
PyErr_SetString(PyExc_TypeError, ARG_ERR);
return NULL;
}
ptype = fdisk_label_get_parttype_from_code(self->lb, ptype_code);
if (!fdisk_label_has_code_parttypes(label)) {
name = fdisk_label_get_name(label);
PyErr_Format(PyExc_RuntimeError, "Current label %s has no code parttypes", name);
return NULL;
}
ptype = fdisk_label_get_parttype_from_code(label, ptype_code);
if (!ptype) {
PyErr_Format(PyExc_RuntimeError, "No match for parttype with code: %d", ptype_code);
return NULL;
@ -80,7 +88,9 @@ static PyObject *Label_get_parttype_from_code(LabelObject *self, PyObject *args,
"Search by string for partition type in label-specific table."
static PyObject *Label_get_parttype_from_string(LabelObject *self, PyObject *args, PyObject *kwds)
{
struct fdisk_label *label = self->lb;
struct fdisk_parttype *ptype = NULL;
const char *name;
char *str;
if (!PyArg_ParseTuple(args, "s", &str)) {
@ -88,7 +98,13 @@ static PyObject *Label_get_parttype_from_string(LabelObject *self, PyObject *arg
return NULL;
}
ptype = fdisk_label_get_parttype_from_string(self->lb, str);
if (fdisk_label_has_code_parttypes(label)) {
name = fdisk_label_get_name(label);
PyErr_Format(PyExc_RuntimeError, "Current label %s has no string parttypes", name);
return NULL;
}
ptype = fdisk_label_get_parttype_from_string(label, str);
if (!ptype) {
PyErr_Format(PyExc_RuntimeError, "No match for parttype with string: %s", str);
return NULL;

View File

@ -25,6 +25,11 @@ static PyMethodDef PartType_methods[] = {
{NULL}
};
static PyObject *PartType_get_string(PartTypeObject *self)
{
return PyObjectResultStr(fdisk_parttype_get_string(self->type));
}
static PyObject *PartType_get_name(PartTypeObject *self)
{
return PyObjectResultStr(fdisk_parttype_get_name(self->type));
@ -36,6 +41,7 @@ static PyObject *PartType_get_code(PartTypeObject *self)
}
static PyGetSetDef PartType_getseters[] = {
{"string", (getter)PartType_get_string, NULL, "partition type string (GUID for GPT)", NULL},
{"name", (getter)PartType_get_name, NULL, "parttype human readable name", NULL},
{"code", (getter)PartType_get_code, NULL, "parttype DOS code", NULL},
{NULL}

View File

@ -5,7 +5,7 @@ libfdisk = Extension('fdisk',
sources = ['fdisk.c', 'context.c', 'label.c',
'partition.c', 'parttype.c'])
setup (name = 'libfdisk',
version = '1.1',
setup (name = 'python-libfdisk',
version = '1.2',
description = 'Python bindings for libfdisk',
ext_modules = [libfdisk])