Skip to content

Commit

Permalink
refactor: replace fs type string with enum for clarity and efficiency
Browse files Browse the repository at this point in the history
- Transition from string-based to enum-based file system type handling.
- Eliminates string allocations, reducing potential for allocation failures.
- Avoids error-prone string matching, improving performance and accuracy.

Signed-off-by: Michael Adler <[email protected]>
Signed-off-by: Jan Kiszka <[email protected]>
  • Loading branch information
michaeladler authored and jan-kiszka committed Oct 19, 2023
1 parent 8c4005b commit 633ab21
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 80 deletions.
7 changes: 3 additions & 4 deletions env/env_config_partitions.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ bool probe_config_partitions(CONFIG_PART *cfgpart, bool search_all_devices)
}
PedPartition *part = pd->part_list;
while (part) {
if (!part->fs_type || !part->fs_type->name ||
(strcmp(part->fs_type->name, "fat12") != 0 &&
strcmp(part->fs_type->name, "fat16") != 0 &&
strcmp(part->fs_type->name, "fat32") != 0)) {
if (part->fs_type != FS_TYPE_FAT12 &&
part->fs_type != FS_TYPE_FAT16 &&
part->fs_type != FS_TYPE_FAT32) {
part = ped_disk_next_partition(pd, part);
continue;
}
Expand Down
13 changes: 9 additions & 4 deletions include/ebgpart.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,18 @@ struct EFIpartitionentry {
};
#pragma pack(pop)

typedef enum {
FS_TYPE_UNKNOWN = 0,
FS_TYPE_FAT12 = 1,
FS_TYPE_FAT16 = 2,
FS_TYPE_FAT32 = 3,
FS_TYPE_EXTENDED = 4
} EbgFileSystemType;

/* Implementing a minimalistic API replacing used libparted functions */
typedef struct _PedFileSystemType {
char *name;
} PedFileSystemType;

typedef struct _PedPartition {
PedFileSystemType *fs_type;
EbgFileSystemType fs_type;
uint16_t num;
struct _PedPartition *next;
} PedPartition;
Expand Down
90 changes: 32 additions & 58 deletions tools/ebgpart.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ static char *GUID_to_str(const uint8_t *g)
return buffer;
}

static char *type_to_name(char t)
static EbgFileSystemType type_to_fstype(char t)
{
switch (t) {
case MBR_TYPE_FAT12:
return "fat12";
return FS_TYPE_FAT12;
case MBR_TYPE_FAT16A:
case MBR_TYPE_FAT16:
case MBR_TYPE_FAT16_LBA:
return "fat16";
return FS_TYPE_FAT16;
case MBR_TYPE_FAT32:
case MBR_TYPE_FAT32_LBA:
return "fat32";
return FS_TYPE_FAT32;
case MBR_TYPE_EXTENDED_LBA:
case MBR_TYPE_EXTENDED:
return "extended";
return FS_TYPE_EXTENDED;
}
return "not supported";
return FS_TYPE_UNKNOWN;
}

/**
Expand Down Expand Up @@ -127,13 +127,33 @@ static int check_GPT_FAT_entry(int fd, const struct EFIpartitionentry *e)
return determine_FAT_bits(&header);
}

static inline EbgFileSystemType fat_size_to_fs_type(int fat_size)
{
switch (fat_size) {
case 0:
VERBOSE(stderr, "Not a FAT partition\n");
return FS_TYPE_UNKNOWN;
case 12:
VERBOSE(stdout, "Partition is fat12\n");
return FS_TYPE_FAT12;
case 16:
VERBOSE(stdout, "Partition is fat16\n");
return FS_TYPE_FAT16;
case 32:
VERBOSE(stdout, "Partition is fat32\n");
return FS_TYPE_FAT32;
default:
VERBOSE(stderr, "Error: Invalid FAT size %d\n", fat_size);
return FS_TYPE_UNKNOWN;
}
}

static void read_GPT_entries(int fd, uint64_t table_LBA, uint32_t num,
PedDevice *dev)
{
off64_t offset;
struct EFIpartitionentry e;
PedPartition *tmpp;
PedFileSystemType *pfst = NULL;

offset = LB_SIZE * table_LBA;
if (lseek64(fd, offset, SEEK_SET) != offset) {
Expand All @@ -154,41 +174,22 @@ static void read_GPT_entries(int fd, uint64_t table_LBA, uint32_t num,
return;
}
VERBOSE(stdout, "%u: %s\n", i, GUID_to_str(e.type_GUID));
pfst = calloc(sizeof(PedFileSystemType), 1);
if (!pfst) {
VERBOSE(stderr, "Out of memory\n");
return;
}

tmpp = calloc(sizeof(PedPartition), 1);
if (!tmpp) {
VERBOSE(stderr, "Out of memory\n");
free(pfst);
return;
}
tmpp->num = i + 1;
tmpp->fs_type = pfst;

int result = check_GPT_FAT_entry(fd, &e);
if (result < 0) {
VERBOSE(stderr, "%u: I/O error, skipping device\n", i);
free(pfst->name);
free(pfst);
free(tmpp);
dev->part_list = NULL;
continue;
}
if (result > 0) { /* result is the FAT bit size */
VERBOSE(stdout, "GPT Partition #%u is fat%d.\n", i,
result);
if (asprintf(&pfst->name, "fat%d", result) == -1) {
VERBOSE(stderr, "Error in asprintf - possibly "
"out of memory.\n");
return;
}
} else {
VERBOSE(stderr, "%u: not a FAT partition\n", i);
}
tmpp->fs_type = fat_size_to_fs_type(result);

*list_end = tmpp;
list_end = &((*list_end)->next);
Expand All @@ -200,7 +201,6 @@ static void scanLogicalVolumes(int fd, off64_t extended_start_LBA,
PedPartition *partition, int lognum)
{
struct Masterbootrecord next_ebr;
PedFileSystemType *pfst = NULL;

off64_t offset = extended_start_LBA + ebr->parttable[i].start_LBA;
if (extended_start_LBA == 0) {
Expand Down Expand Up @@ -238,21 +238,13 @@ static void scanLogicalVolumes(int fd, off64_t extended_start_LBA,
if (!partition->next) {
goto scl_out_of_mem;
}
pfst = calloc(sizeof(PedFileSystemType), 1);
if (!pfst) {
goto scl_out_of_mem;
}
if (asprintf(&pfst->name, "%s", type_to_name(t)) == -1) {
goto scl_out_of_mem;
};
partition = partition->next;
partition->num = lognum;
partition->fs_type = pfst;
partition->fs_type = type_to_fstype(t);
}
return;
scl_out_of_mem:
VERBOSE(stderr, "Out of memory\n");
free(pfst);
free(partition->next);
}

Expand Down Expand Up @@ -321,42 +313,31 @@ static bool check_partition_table(PedDevice *dev)
efihdr.partitions, dev);
break;
}
PedFileSystemType *pfst = calloc(sizeof(PedFileSystemType), 1);
if (!pfst) {
goto cpt_out_of_mem;
}

tmp = calloc(sizeof(PedPartition), 1);
if (!tmp) {
goto cpt_out_of_mem;
}

tmp->num = i + 1;
tmp->fs_type = pfst;

*list_end = tmp;
list_end = &((*list_end)->next);

if (t == MBR_TYPE_EXTENDED || t == MBR_TYPE_EXTENDED_LBA) {
if (asprintf(&pfst->name, "%s", "extended") == -1) {
goto cpt_out_of_mem;
}
tmp->fs_type = FS_TYPE_EXTENDED;
scanLogicalVolumes(fd, 0, &mbr, i, tmp, 5);
/* Could be we still have MBR entries after
* logical volumes */
while ((*list_end)->next) {
list_end = &((*list_end)->next);
}
} else {
if (asprintf(&pfst->name, "%s", type_to_name(t)) == -1) {
goto cpt_out_of_mem;
}
tmp->fs_type = type_to_fstype(t);
}
continue;
cpt_out_of_mem:
close(fd);
VERBOSE(stderr, "Out of mem while checking partition table\n.");
free(pfst);
free(tmp);
return false;
}
Expand Down Expand Up @@ -494,15 +475,8 @@ void ped_device_probe_all(char *rootdev)
closedir(sysblockdir);
}

static void ped_partition_destroy(PedPartition *p)
static inline void ped_partition_destroy(PedPartition *p)
{
if (!p) {
return;
}
if (p->fs_type) {
free(p->fs_type->name);
free(p->fs_type);
}
free(p);
}

Expand Down
16 changes: 2 additions & 14 deletions tools/tests/fake_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <env_api.h>
#include <env_config_file.h>
#include <env_config_partitions.h>
#include <fake_devices.h>
#include "fake_devices.h"

PedDevice *fake_devices;
int num_fake_devices;
Expand Down Expand Up @@ -64,15 +64,7 @@ void add_fake_partition(int devnum)
goto allocate_fake_part_error;
}
(*pp)->num = num;
(*pp)->fs_type =
(PedFileSystemType *)calloc(1, sizeof(PedFileSystemType));
if (!(*pp)->fs_type) {
goto allocate_fake_part_error;
}
if (asprintf(&(*pp)->fs_type->name, "%s", "fat16") == -1) {
(*pp)->fs_type->name = NULL;
goto allocate_fake_part_error;
}
(*pp)->fs_type = FS_TYPE_FAT16;
return;

allocate_fake_part_error:
Expand All @@ -86,10 +78,6 @@ void remove_fake_partitions(int n)
PedPartition *next;
while(pp) {
next = pp->next;
if (pp->fs_type) {
free(pp->fs_type->name);
free(pp->fs_type);
}
free(pp);
pp = next;
}
Expand Down

0 comments on commit 633ab21

Please sign in to comment.