Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=5ce73a1daa501b188362f0e6b613a719bb3f4045

commit 5ce73a1daa501b188362f0e6b613a719bb3f4045
Author: James Buren <[email protected]>
Date:   Sun Sep 9 06:29:54 2012 -0500

initial draft of device_read()

diff --git a/src/local.h b/src/local.h
index 8b09ca8..0d71c2a 100644
--- a/src/local.h
+++ b/src/local.h
@@ -39,7 +39,8 @@ struct device
bool lvm;
bool dos;
bool gpt;
-  void *table;
+  void *table[128];
+  size_t size;
};

struct dospartition
@@ -93,6 +94,7 @@ extern bool size_to_string(char *s,size_t n,long long 
size,bool pad);
extern int get_text_length(const char *s);
extern bool execute(const char *command,const char *root,pid_t *cpid);
extern void *malloc0(size_t size);
+extern struct device *device_read(const char *path);
extern int get_text_screen_width(const char *s);
extern bool get_text_screen_size(const char *text,int *width,int *height);
extern bool get_button_screen_size(const char *text,int *width,int *height);
diff --git a/src/utility.c b/src/utility.c
index 828daec..01515c9 100644
--- a/src/utility.c
+++ b/src/utility.c
@@ -1,5 +1,68 @@
+#include <blkid.h>
+#include <linux/major.h>
#include "local.h"

+static inline bool is_ide_disk(const struct stat *st)
+{
+  switch(major(st->st_rdev))
+  {
+    case IDE0_MAJOR:
+    case IDE1_MAJOR:
+    case IDE2_MAJOR:
+    case IDE3_MAJOR:
+    case IDE4_MAJOR:
+    case IDE5_MAJOR:
+    case IDE6_MAJOR:
+    case IDE7_MAJOR:
+    case IDE8_MAJOR:
+    case IDE9_MAJOR:
+      return true;
+    default:
+      return false;
+  }
+}
+
+static inline bool is_scsi_disk(const struct stat *st)
+{
+  switch(major(st->st_rdev))
+  {
+    case SCSI_DISK0_MAJOR:
+    case SCSI_DISK1_MAJOR:
+    case SCSI_DISK2_MAJOR:
+    case SCSI_DISK3_MAJOR:
+    case SCSI_DISK4_MAJOR:
+    case SCSI_DISK5_MAJOR:
+    case SCSI_DISK6_MAJOR:
+    case SCSI_DISK7_MAJOR:
+    case SCSI_DISK8_MAJOR:
+    case SCSI_DISK9_MAJOR:
+    case SCSI_DISK10_MAJOR:
+    case SCSI_DISK11_MAJOR:
+    case SCSI_DISK12_MAJOR:
+    case SCSI_DISK13_MAJOR:
+    case SCSI_DISK14_MAJOR:
+    case SCSI_DISK15_MAJOR:
+      return true;
+    default:
+      return false;
+  }
+}
+
+static inline bool is_virtio_disk(const struct stat *st)
+{
+  return (major(st->st_rdev) == 253);
+}
+
+static inline bool is_raid(const struct stat *st)
+{
+  return (major(st->st_rdev) == MD_MAJOR);
+}
+
+static inline bool is_lvm(const struct stat *st)
+{
+  return (major(st->st_rdev) == 254);
+}
+
extern bool mkdir_recurse(const char *path)
{
char buf[PATH_MAX] = {0};
@@ -194,6 +257,229 @@ extern void *malloc0(size_t size)
return memset(malloc(size),0,size);
}

+extern struct device *device_read(const char *path)
+{
+  int fd = -1;
+  blkid_probe probe = 0;
+  blkid_topology topology = 0;
+  long long sectorsize = 0;
+  long long alignment = 0;
+  blkid_loff_t size = 0;
+  long long sectors = 0;
+  struct stat st = {0};
+  bool disk = false;
+  bool raid = false;
+  bool lvm = false;
+  blkid_partlist partlist = 0;
+  blkid_parttable parttable = 0;
+  const char *label = 0;
+  bool dos = false;
+  bool gpt = false;
+  blkid_partition partition = 0;
+  void *table[128] = {0};
+  size_t n = 0;
+  struct device *device = 0;
+
+  if(path == 0)
+  {
+    errno = EINVAL;
+    fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+    return 0;
+  }
+
+  fprintf(logfile,_("About to read a device from path '%s'.\n"),path);
+
+  if((fd = open(path,O_RDONLY)) == -1)
+  {
+    fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+    goto bail;
+  }
+
+  if((probe = blkid_new_probe()) == 0)
+  {
+    fprintf(logfile,_("Failed to allocate a blkid_probe.\n"));
+    goto bail;
+  }
+
+  if(blkid_probe_set_device(probe,fd,0,0) == -1)
+  {
+    fprintf(logfile,_("Failed to set a blkid_probe's device.\n"));
+    goto bail;
+  }
+
+  if((topology = blkid_probe_get_topology(probe)) == 0)
+  {
+    fprintf(logfile,_("Failed to get a blkid_probe's topology.\n"));
+    goto bail;
+  }
+
+  sectorsize = blkid_topology_get_logical_sector_size(topology);
+
+  alignment = MEBIBYTE / sectorsize;
+
+  size = blkid_probe_get_size(probe);
+
+  sectors = size / sectorsize;
+
+  if(sectorsize == 0)
+  {
+    fprintf(logfile,_("Sector size is 0 when it should not be.\n"));
+    goto bail;
+  }
+
+  if((MEBIBYTE % sectorsize) != 0)
+  {
+    fprintf(logfile,_("Sector size won't cleanly divide a MEBIBYTE.\n"));
+    goto bail;
+  }
+
+  if(size <= 0)
+  {
+    fprintf(logfile,_("The size of this device is less than or equal to 
0.\n"));
+    goto bail;
+  }
+
+  if((size % sectorsize) != 0)
+  {
+    fprintf(logfile,_("The size of this device is not cleanly divisible by the 
sector size.\n"));
+    goto bail;
+  }
+
+  if(fstat(fd,&st) == -1)
+  {
+    fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+    goto bail;
+  }
+
+  if(is_ide_disk(&st) || is_scsi_disk(&st) || is_virtio_disk(&st))
+    disk = true;
+  else if(is_raid(&st))
+    raid = true;
+  else if(is_lvm(&st))
+    lvm = true;
+  else
+  {
+    fprintf(logfile,_("Unrecognized device type.\n"));
+    goto bail;
+  }
+
+  if(disk)
+  {
+    if(
+        (partlist = blkid_probe_get_partitions(probe)) != 0    &&
+        (parttable = blkid_partlist_get_table(partlist)) != 0  &&
+        (label = blkid_parttable_get_type(parttable)) != 0     &&
+        (strcmp(label,"dos") == 0 || strcmp(label,"gpt") == 0)
+      )
+    {
+      int i = 0;
+      int j = blkid_partlist_numof_partitions(partlist);
+
+      dos = (strcmp(label,"dos") == 0);
+
+      gpt = (strcmp(label,"gpt") == 0);
+
+      if((dos && j > 60) || (gpt && j > 128))
+      {
+        fprintf(logfile,_("Partition table exceeds entry limits.\n"));
+        goto bail;
+      }
+
+      while(i < j && (partition = blkid_partlist_get_partition(partlist,i)) != 
0)
+      {
+        if(dos)
+        {
+          struct dospartition *part = malloc0(sizeof(struct dospartition));
+
+          part->number = blkid_partition_get_partno(partition);
+
+          part->start = blkid_partition_get_start(partition);
+
+          part->end = blkid_partition_get_start(partition) + 
blkid_partition_get_size(partition) - 1;
+
+          part->sectors = blkid_partition_get_size(partition);
+
+          part->active = (blkid_partition_get_flags(partition) == 0x80);
+
+          part->type = blkid_partition_get_type(partition);
+
+          table[i] = part;
+        }
+
+        if(gpt)
+        {
+          struct gptpartition *part = malloc0(sizeof(struct gptpartition));
+          const char *type = 0;
+          const char *name = 0;
+          const char *uuid = 0;
+
+          part->number = blkid_partition_get_partno(partition);
+
+          part->start = blkid_partition_get_start(partition);
+
+          part->end = blkid_partition_get_start(partition) + 
blkid_partition_get_size(partition) - 1;
+
+          part->sectors = blkid_partition_get_size(partition);
+
+          part->flags = blkid_partition_get_flags(partition);
+
+          type = blkid_partition_get_type_string(partition);
+
+          part->type = (type != 0) ? strdup(type) : 0;
+
+          name = blkid_partition_get_name(partition);
+
+          part->name = (name != 0) ? strdup(name) : 0;
+
+          uuid = blkid_partition_get_uuid(partition);
+
+          part->uuid = (uuid != 0) ? strdup(uuid) : 0;
+
+          table[i] = part;
+        }
+
+        ++i;
+      }
+
+      n = i;
+    }
+  }
+
+  device = malloc0(sizeof(struct device));
+
+  device->path = strdup(path);
+
+  device->sectorsize = sectorsize;
+
+  device->alignment = alignment;
+
+  device->sectors = sectors;
+
+  device->disk = disk;
+
+  device->raid = raid;
+
+  device->lvm = lvm;
+
+  device->dos = dos;
+
+  device->gpt = gpt;
+
+  memcpy(device->table,table,sizeof(table));
+
+  device->size = n;
+
+bail:
+
+  if(fd != -1)
+    close(fd);
+
+  if(probe != 0)
+    blkid_free_probe(probe);
+
+  return device;
+}
+
extern int get_text_screen_width(const char *s)
{
wchar_t wc = 0;
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to