X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=compat%2Flib%2Fbylabel.c;h=5a40a70e6eb5af34dfbe3d388a99904a38c80df2;hb=b0e0d7757fe7cdc08be0269f37a7193e07fb1bb3;hp=9cdd0e5da502524644ee1fe4fba7a9bb50a4a364;hpb=8954518fe88156827aa958745ba1eb9cea4b42fa;p=dump.git diff --git a/compat/lib/bylabel.c b/compat/lib/bylabel.c index 9cdd0e5..5a40a70 100644 --- a/compat/lib/bylabel.c +++ b/compat/lib/bylabel.c @@ -11,19 +11,39 @@ * - Ported to dump/restore */ +#include +#include +#include #include #include #include #include #include #include -#include -#include +#include +#include #include "bylabel.h" +#ifndef HAVE_BLKID + #define PROC_PARTITIONS "/proc/partitions" #define DEVLABELDIR "/dev" +#define EXT2_SUPER_OFFSET 1024 +#define EXT2_SUPER_SIZE sizeof(struct ext2_super_block) +#define EXT2_SUPER_MAGIC 0xEF53 + +#define VOLNAMSZ 16 + +struct ext2_super_block { + unsigned char s_dummy1[56]; + unsigned char s_magic[2]; + unsigned char s_dummy2[46]; + unsigned char s_uuid[16]; + unsigned char s_volume_name[VOLNAMSZ]; +}; +#define ext2magic(s) ((unsigned int) s.s_magic[0] + (((unsigned int) s.s_magic[1]) << 8)) + void msg __P((const char *fmt, ...)); static struct uuidCache_s { @@ -42,13 +62,13 @@ get_label_uuid(const char *device, char **label, char *uuid) { int fd; struct ext2_super_block e2sb; - fd = open(device, O_RDONLY); + fd = OPEN(device, O_RDONLY); if (fd < 0) return 1; - if (lseek(fd, 1024, SEEK_SET) != 1024 - || read(fd, (char *) &e2sb, sizeof(e2sb)) != sizeof(e2sb) - || (e2sb.s_magic != EXT2_SUPER_MAGIC)) { + if (LSEEK(fd, EXT2_SUPER_OFFSET, SEEK_SET) != EXT2_SUPER_OFFSET || + read(fd, (char *) &e2sb, EXT2_SUPER_SIZE) != EXT2_SUPER_SIZE || + ext2magic(e2sb) != EXT2_SUPER_MAGIC) { close(fd); return 1; } @@ -57,7 +77,9 @@ get_label_uuid(const char *device, char **label, char *uuid) { /* superblock is ext2 - now what is its label? */ memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid)); - *label = strdup(e2sb.s_volume_name); + *label = malloc(VOLNAMSZ + 1); + strncpy(*label, e2sb.s_volume_name, VOLNAMSZ); + (*label)[VOLNAMSZ] = 0; return 0; } @@ -67,10 +89,10 @@ uuidcache_addentry(char *device, char *label, char *uuid) { struct uuidCache_s *last; if (!uuidCache) { - last = uuidCache = malloc(sizeof(*uuidCache)); + last = uuidCache = (struct uuidCache_s *)malloc(sizeof(*uuidCache)); } else { for (last = uuidCache; last->next; last = last->next) ; - last->next = malloc(sizeof(*uuidCache)); + last->next = (struct uuidCache_s *)malloc(sizeof(*uuidCache)); last = last->next; } last->next = NULL; @@ -220,3 +242,20 @@ get_device_name(const char * item) { return rc; } + +const char * +get_device_label(const char * spec) { + struct uuidCache_s *uc; + + uuidcache_init(); + uc = uuidCache; + + while(uc) { + if (!strcmp(spec, uc->device)) + return uc->label[0] == '\0' ? NULL : strdup(uc->label); + uc = uc->next; + } + return NULL; +} + +#endif /* !HAVE_BLKID */