Commit Graph

11 Commits (1a74bf5024ca7fa21ecff5ea755aac393a58377e)

Author SHA1 Message Date
Jose M. Guisado 9bf2d030c9 fdisk: declare kwlist array static 2022-12-15 17:51:17 +01:00
Jose M. Guisado 5ec9ec73c8 fdisk: add set_PyErr_from_rc
python-libfdisk raises Python exceptions when the libfdisk reports an
error when executing some function. libfdisk returns negative
errno values when reporting some error.

Adds utility function to set PyErr string based on the strerror of
a given errno code. Useful when raising Python exceptions.
2022-12-15 17:46:42 +01:00
Jose M. Guisado 975acaf549 coding style: line breaks
Only two line breaks separate copyright notice from source.

For the rest of the source file any function declaration or similar
block is separated with a line break from any other block.
Except when a python function definition is previously followed by
a docstring #define block.
2022-12-15 17:37:07 +01:00
Jose M. Guisado bc6f5cbdff partition: add partition type getset
Partition type getset enables modifying and getting the type
of a give libfdisk partition.

The type of a partition is defined using PartType, which
can only be instanced via label specific functions
get_parttype_from_string or get_parttype_from_code.

For example, to set the type of a new partitions to 'EFI System':

>>> import fdisk
>>> pa = fdisk.Partition()
>>> pa.type
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> cxt.create_disklabel('gpt')
>>> efitype = cxt.label.get_parttype_from_string("c12a7328-f81f-11d2-ba4b-00a0c93ec93b")
>>> pa.type = efitype

Following the previous example, getting its current partition type:

>>> pa.type
<libfdisk.PartType object at 0x7f2f0a9a12d0, name=EFI System>
2022-12-15 17:37:07 +01:00
Jose M. Guisado 7271bc99db partition: add *_follow_default optional params
Add optional parameters inside init function of partition. Optional
parameters refer to:

- partno_follow_default
- start_follow_default
- end_follow_default

These options can be used in order to enable or disable default partno,
start and end value when adding partitions.

With those optional parameters enabled by default a user is able
to add a partition into the context label without specifying any
attribute.

>>> import fdisk
>>> cxt = fdisk.Context('./disk.bin', readonly=False)
>>> cxt.create_disklabel('gpt')
>>> pa = fdisk.Partition()
>>> cxt.add_partition(pa)

This enables:

- "Filling" the rest of the disk with last partition
- No need to track start/end sector for any following partition
- No need to track next partno number for any following partition

See:

https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-partno-follow-default
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-start-follow-default
https://cdn.kernel.org/pub/linux/utils/util-linux/v2.34/libfdisk-docs/libfdisk-Partition.html#fdisk-partition-end-follow-default
2022-12-15 17:36:57 +01:00
Jose M. Guisado 8707d43111 context: add add_partition method
This method wraps fdisk_add_partition. Allows modifying in-memory
partition table of a given context.

Remember that changes need to be written to disk using the
relevant fdisk_write_disklabel function wrapper.
2022-12-15 12:27:35 +01:00
Jose M. Guisado 46ad17eaa7 partition: add partno setter
Allows changing in-memory partno field of a Partition instance:

  >>> import fdisk
  >>> pa = fdisk.Partition()
  >>> pa.partno = 3
  >>> pa
  <libfdisk.Partition object at 0x7f4603a38f30, partno=3>

If partno is unset repr shows 'None':

  >>> import fdisk
  >>> pa = fdisk.Partition()
  >>> pa
  <libfdisk.Partition object at 0x7f86c4338f30, partno=None>
2022-12-15 12:27:22 +01:00
Jose M. Guisado bd2703c54e partition: return None if partno is unset
Undefined values in libfdisk should map to None type in Python.

Py_BuildValue("%d", -1); is also incorrectly formatted and raises an
error when executed.
2022-12-15 12:16:52 +01:00
Jose M. Guisado Gomez ba67cc7a7b Use c99 struct initialization
Declutters PyTypeObject struct initialization when declaring new types.
2022-04-28 15:47:55 +02:00
Jose M. Guisado Gomez ef613790e9 Add COPYING and license headers
LGPL2.1 or later.
2022-04-07 17:03:34 +02:00
Jose M. Guisado Gomez ca92f15e2a Initial commit
Add sources, setup.py and .gitignore

Build/Install:

	python setup.py build
	python setup.py install
2022-04-06 12:56:11 +02:00