Skip to content

Instantly share code, notes, and snippets.

@mmstick
Created March 2, 2018 16:51
Show Gist options
  • Save mmstick/3c4384b72708a4644690196eed0dc426 to your computer and use it in GitHub Desktop.
Save mmstick/3c4384b72708a4644690196eed0dc426 to your computer and use it in GitHub Desktop.
distinst FFI
#ifndef distinst_h
#define distinst_h
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
typedef enum {
DISTINST_FILE_SYSTEM_TYPE_NONE = 0,
DISTINST_FILE_SYSTEM_TYPE_BTRFS = 1,
DISTINST_FILE_SYSTEM_TYPE_EXFAT = 2,
DISTINST_FILE_SYSTEM_TYPE_EXT2 = 3,
DISTINST_FILE_SYSTEM_TYPE_EXT3 = 4,
DISTINST_FILE_SYSTEM_TYPE_EXT4 = 5,
DISTINST_FILE_SYSTEM_TYPE_F2FS = 6,
DISTINST_FILE_SYSTEM_TYPE_FAT16 = 7,
DISTINST_FILE_SYSTEM_TYPE_FAT32 = 8,
DISTINST_FILE_SYSTEM_TYPE_NTFS = 9,
DISTINST_FILE_SYSTEM_TYPE_SWAP = 10,
DISTINST_FILE_SYSTEM_TYPE_XFS = 11,
DISTINST_FILE_SYSTEM_TYPE_LVM = 12,
} DISTINST_FILE_SYSTEM_TYPE;
/*
* Log level
*/
typedef enum {
DISTINST_LOG_LEVEL_TRACE,
DISTINST_LOG_LEVEL_DEBUG,
DISTINST_LOG_LEVEL_INFO,
DISTINST_LOG_LEVEL_WARN,
DISTINST_LOG_LEVEL_ERROR,
} DISTINST_LOG_LEVEL;
typedef enum {
DISTINST_PARTITION_FLAG_BOOT,
DISTINST_PARTITION_FLAG_ROOT,
DISTINST_PARTITION_FLAG_SWAP,
DISTINST_PARTITION_FLAG_HIDDEN,
DISTINST_PARTITION_FLAG_RAID,
DISTINST_PARTITION_FLAG_LVM,
DISTINST_PARTITION_FLAG_LBA,
DISTINST_PARTITION_FLAG_HPSERVICE,
DISTINST_PARTITION_FLAG_PALO,
DISTINST_PARTITION_FLAG_PREP,
DISTINST_PARTITION_FLAG_MSFT_RESERVED,
DISTINST_PARTITION_FLAG_BIOS_GRUB,
DISTINST_PARTITION_FLAG_APPLE_TV_RECOVERY,
DISTINST_PARTITION_FLAG_DIAG,
DISTINST_PARTITION_FLAG_LEGACY_BOOT,
DISTINST_PARTITION_FLAG_MSFT_DATA,
DISTINST_PARTITION_FLAG_IRST,
DISTINST_PARTITION_FLAG_ESP,
} DISTINST_PARTITION_FLAG;
typedef enum {
DISTINST_PARTITION_TABLE_NONE = 0,
DISTINST_PARTITION_TABLE_GPT = 1,
DISTINST_PARTITION_TABLE_MSDOS = 2,
} DISTINST_PARTITION_TABLE;
typedef enum {
DISTINST_PARTITION_TYPE_PRIMARY = 1,
DISTINST_PARTITION_TYPE_LOGICAL = 2,
} DISTINST_PARTITION_TYPE;
typedef enum {
DISTINST_SECTOR_KIND_START,
DISTINST_SECTOR_KIND_END,
DISTINST_SECTOR_KIND_UNIT,
DISTINST_SECTOR_KIND_UNIT_FROM_END,
DISTINST_SECTOR_KIND_MEGABYTE,
DISTINST_SECTOR_KIND_MEGABYTE_FROM_END,
DISTINST_SECTOR_KIND_PERCENT,
} DISTINST_SECTOR_KIND;
/*
* Bootloader steps
*/
typedef enum {
DISTINST_STEP_INIT,
DISTINST_STEP_PARTITION,
DISTINST_STEP_EXTRACT,
DISTINST_STEP_CONFIGURE,
DISTINST_STEP_BOOTLOADER,
} DISTINST_STEP;
typedef struct {
} DistinstDisk;
typedef struct {
} DistinstPartitionBuilder;
typedef struct {
} DistinstPartition;
typedef struct {
DISTINST_SECTOR_KIND flag;
uint64_t value;
} DistinstSector;
typedef struct {
} DistinstDisks;
typedef struct {
} DistinstLvmDevice;
/*
* An installer object
*/
typedef struct {
} DistinstInstaller;
/*
* Installer error message
*/
typedef struct {
DISTINST_STEP step;
int err;
} DistinstError;
/*
* Installer status message
*/
typedef struct {
DISTINST_STEP step;
int percent;
} DistinstStatus;
/*
* Installer configuration
*/
typedef struct {
const char *hostname;
const char *keyboard;
const char *lang;
const char *remove;
const char *squashfs;
} DistinstConfig;
/*
* Installer error callback
*/
typedef void (*DistinstErrorCallback)(const DistinstError*, void*);
/*
* Installer status callback
*/
typedef void (*DistinstStatusCallback)(const DistinstStatus*, void*);
/*
* Installer log callback
*/
typedef void (*DistinstLogCallback)(DISTINST_LOG_LEVEL, const char*, void*);
typedef struct {
/*
* The PV field is not optional
*/
char *physical_volume;
/*
* The password field is optional
*/
char *password;
/*
* The keydata field is optional
*/
char *keydata;
} DistinstLvmEncryption;
typedef struct {
uint8_t tag;
uint64_t value;
} DistinstPartitionUsage;
DISTINST_PARTITION_TABLE distinst_bootloader_detect(void);
int distinst_disk_add_partition(DistinstDisk *disk, DistinstPartitionBuilder *partition);
int distinst_disk_commit(DistinstDisk *disk);
/*
* A destructor for a `DistinstDisk`
*/
void distinst_disk_destroy(DistinstDisk *disk);
int distinst_disk_format_partition(DistinstDisk *disk, int partition, DISTINST_FILE_SYSTEM_TYPE fs);
const uint8_t *distinst_disk_get_device_path(const DistinstDisk *disk, int *len);
DistinstPartition *distinst_disk_get_partition(DistinstDisk *disk, int32_t partition);
uint64_t distinst_disk_get_sector(const DistinstDisk *disk, const DistinstSector *sector);
uint64_t distinst_disk_get_sector_size(const DistinstDisk *disk);
uint64_t distinst_disk_get_sectors(const DistinstDisk *disk);
DistinstPartition **distinst_disk_list_partitions(DistinstDisk *disk, int *len);
/*
* TODO: This is to be used with vectors returned from
* `distinst_disk_list_partitions`.
*/
void distinst_disk_list_partitions_destroy(DistinstPartition *partitions, size_t len);
int distinst_disk_mklabel(DistinstDisk *disk, DISTINST_PARTITION_TABLE table);
int distinst_disk_move_partition(DistinstDisk *disk, int partition, uint64_t start);
/*
* Obtains a specific disk's information by the device path.
*
* On an error, this will return a null pointer.
*/
DistinstDisk *distinst_disk_new(const char *path);
int distinst_disk_remove_partition(DistinstDisk *disk, int partition);
int distinst_disk_resize_partition(DistinstDisk *disk, int partition, uint64_t end);
/*
* The deconstructor for a `DistinstDisks`.
*/
void distinst_disks_destroy(DistinstDisks *disks);
DistinstLvmDevice *distinst_disks_find_logical_volume(DistinstDisks *disks, const char *group);
int distinst_disks_initialize_volume_groups(DistinstDisks *disks);
DistinstDisk **distinst_disks_list(DistinstDisks *disks, int *len);
/*
* TODO: This is to be used with vectors returned from `distinst_disks_list`.
*/
void distinst_disks_list_destroy(DistinstDisk *list, size_t len);
/*
* Returns an empty disks array
*
* On error, a null pointer will be returned.
*/
DistinstDisks *distinst_disks_new(void);
/*
* Probes the disk for information about every disk in the device.
*
* On error, a null pointer will be returned.
*/
DistinstDisks *distinst_disks_probe(void);
void distinst_disks_push(DistinstDisks *disks, DistinstDisk *disk);
/*
* Destroy an installer object
*/
void distinst_installer_destroy(DistinstInstaller *installer);
/*
* Send an installer status message
*/
void distinst_installer_emit_error(DistinstInstaller *installer, const DistinstError *error);
/*
* Send an installer status message
*/
void distinst_installer_emit_status(DistinstInstaller *installer, const DistinstStatus *status);
/*
* Install using this installer
*/
int distinst_installer_install(DistinstInstaller *installer,
DistinstDisks *disks,
const DistinstConfig *config);
/*
* Create an installer object
*/
DistinstInstaller *distinst_installer_new(void);
/*
* Set the installer status callback
*/
void distinst_installer_on_error(DistinstInstaller *installer,
DistinstErrorCallback callback,
void *user_data);
/*
* Set the installer status callback
*/
void distinst_installer_on_status(DistinstInstaller *installer,
DistinstStatusCallback callback,
void *user_data);
/*
* Initialize logging
*/
int distinst_log(DistinstLogCallback callback, void *user_data);
int distinst_lvm_device_add_partition(DistinstLvmDevice *device,
DistinstPartitionBuilder *partition);
uint64_t distinst_lvm_device_get_sector(DistinstLvmDevice *device, const DistinstSector *sector);
uint64_t distinst_lvm_device_last_used_sector(DistinstLvmDevice *device);
void distinst_partition_builder_destroy(DistinstPartitionBuilder *builder);
DistinstPartitionBuilder *distinst_partition_builder_flag(DistinstPartitionBuilder *builder,
DISTINST_PARTITION_FLAG flag);
DistinstPartitionBuilder *distinst_partition_builder_logical_volume(DistinstPartitionBuilder *builder,
const char *group,
DistinstLvmEncryption *encryption);
DistinstPartitionBuilder *distinst_partition_builder_mount(DistinstPartitionBuilder *builder,
const char *target);
DistinstPartitionBuilder *distinst_partition_builder_name(DistinstPartitionBuilder *builder,
const char *name);
DistinstPartitionBuilder *distinst_partition_builder_new(uint64_t start_sector,
uint64_t end_sector,
DISTINST_FILE_SYSTEM_TYPE filesystem);
DistinstPartitionBuilder *distinst_partition_builder_partition_type(DistinstPartitionBuilder *builder,
DISTINST_PARTITION_TYPE part_type);
int distinst_partition_format_with(DistinstPartition *partition, DISTINST_FILE_SYSTEM_TYPE fs);
const uint8_t *distinst_partition_get_device_path(const DistinstPartition *partition, int *len);
uint64_t distinst_partition_get_end_sector(const DistinstPartition *partition);
DISTINST_FILE_SYSTEM_TYPE distinst_partition_get_file_system(const DistinstPartition *partition);
char *distinst_partition_get_label(const DistinstPartition *partition);
uint64_t distinst_partition_get_start_sector(const DistinstPartition *partition);
char *distinst_partition_probe_os(const DistinstPartition *partition);
DistinstPartitionUsage distinst_partition_sectors_used(const DistinstPartition *partition,
uint64_t sector_size);
void distinst_partition_set_flags(DistinstPartition *partition,
const DISTINST_PARTITION_FLAG *ptr,
size_t len);
void distinst_partition_set_mount(DistinstPartition *partition, const char *target);
DistinstSector distinst_sector_init_megabyte(uint64_t value);
DistinstSector distinst_sector_init_start(void);
const char *distinst_strfilesys(DISTINST_FILE_SYSTEM_TYPE fs);
#endif /* distinst_h */
[CCode (cprefix = "Distinst", lower_case_cprefix = "distinst_", cheader_filename = "distinst.h")]
namespace Distinst {
[CCode (cname = "DISTINST_LOG_LEVEL", has_type_id = false)]
public enum LogLevel {
TRACE,
DEBUG,
INFO,
WARN,
ERROR
}
public delegate void LogCallback (Distinst.LogLevel level, string message);
[CCode (cname = "DISTINST_STEP", has_type_id = false)]
public enum Step {
INIT,
PARTITION,
EXTRACT,
CONFIGURE,
BOOTLOADER
}
[CCode (has_type_id = false, destroy_function = "")]
public struct Config {
string hostname;
string keyboard;
string lang;
string remove;
string squashfs;
}
[CCode (cname = "DISTINST_PARTITION_TABLE", has_type_id = false)]
public enum PartitionTable {
NONE,
GPT,
MSDOS
}
public PartitionTable bootloader_detect ();
[CCode (cname = "DISTINST_PARTITION_TYPE", has_type_id = false)]
public enum PartitionType {
PRIMARY,
LOGICAL
}
[CCode (cname = "DISTINST_FILE_SYSTEM_TYPE", has_type_id = false)]
public enum FileSystemType {
NONE,
BTRFS,
EXFAT,
EXT2,
EXT3,
EXT4,
F2FS,
FAT16,
FAT32,
NTFS,
SWAP,
XFS,
LVM
}
public unowned string strfilesys(FileSystemType fs);
[CCode (cname = "DISTINST_PARTITION_FLAG", has_type_id = false)]
public enum PartitionFlag {
BOOT,
ROOT,
SWAP,
HIDDEN,
RAID,
LVM,
LBA,
HPSERVICE,
PALO,
PREP,
MSFT_RESERVED,
BIOS_GRUB,
APPLE_TV_RECOVERY,
DIAG,
LEGACY_BOOT,
MSFT_DATA,
IRST,
ESP
}
[CCode (has_type_id = false, unref_function = "")]
public class PartitionBuilder {
public PartitionBuilder (uint64 start_sector, uint64 end_sector, FileSystemType filesystem);
public PartitionBuilder name (string name);
public PartitionBuilder mount (string target);
public PartitionBuilder partition_type (PartitionType part_type);
public PartitionBuilder flag (PartitionFlag flag);
public PartitionBuilder logical_volume (string group, LvmEncryption encryption);
}
[SimpleType]
[CCode (has_type_id = false)]
public struct PartitionUsage {
public uint64 value;
public uint8 tag;
}
[CCode (has_type_id = false, unref_function = "")]
public class Partition {
public unowned uint8[] get_device_path ();
public void set_flags (PartitionFlag *flags, size_t len);
public void set_mount (string target);
public int format_with (FileSystemType fs);
public uint64 get_start_sector ();
public uint64 get_end_sector ();
public string? get_label ();
public FileSystemType get_file_system ();
public string? probe_os ();
public PartitionUsage sectors_used (uint64 sector_size);
}
[CCode (has_type_id = false)]
public enum SectorKind {
START,
END,
UNIT,
UNIT_FROM_END,
MEGABYTE,
MEGABYTE_FROM_END,
PERCENT
}
[SimpleType]
[CCode (has_type_id = false)]
public struct Sector {
SectorKind flag;
uint64 value;
public static Sector start();
public static Sector megabyte(uint64 value);
}
[CCode (has_type_id = false, destroy_function = "distinst_disk_destroy", unref_function = "")]
public class Disk {
public Disk (string path);
public unowned uint8[] get_device_path();
public Partition get_partition(int partition);
public unowned Partition[] list_partitions();
public int add_partition (PartitionBuilder partition);
public int format_partition (int partition, FileSystemType fs);
public uint64 get_sectors ();
public uint64 get_sector_size ();
public uint64 get_sector (Sector sector);
public int mklabel (PartitionTable table);
public int move_partition (int partition, uint64 start);
public int remove_partition (int partition);
public int resize_partition (int partition, uint64 end);
public int commit();
public int initialize_volume_groups ();
public unowned LvmDevice find_logical_volume (string group);
}
[CCode (has_type_id = false, destroy_function = "", unref_function = "")]
public class LvmDevice {
public uint64 last_used_sector ();
public uint64 get_sector (Sector sector);
public int add_partition (PartitionBuilder partition);
}
[CCode (has_type_id = false, destroy_function = "", unref_function = "")]
public class LvmEncryption {
string physical_volume;
string? password;
string? keydata;
}
[CCode (has_type_id = false, destroy_function = "distinst_disks_destroy", free_function = "", unref_function = "")]
public class Disks {
public static Disks probe();
public Disks ();
public unowned Disk[] list();
public void push(Disk disk);
}
[CCode (has_type_id = false)]
public struct Error {
Distinst.Step step;
int err;
}
public delegate void ErrorCallback (Distinst.Error status);
[CCode (has_type_id = false)]
public struct Status {
Distinst.Step step;
int percent;
}
public delegate void StatusCallback (Distinst.Status status);
int log (Distinst.LogCallback callback);
[Compact]
[CCode (destroy_function = "distinst_installer_destroy", free_function = "", has_type_id = false)]
public class Installer {
public Installer ();
public void emit_error (Distinst.Error error);
public void on_error (Distinst.ErrorCallback callback);
public void emit_status (Distinst.Status error);
public void on_status (Distinst.StatusCallback callback);
public int install (Distinst.Disks disks, Distinst.Config config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment