Skip to content

Commit

Permalink
blkid: add -s, SEC_TYPE, f2fs LABEL, and fix vfat/ntfs UUID.
Browse files Browse the repository at this point in the history
This gets the tests passing with both toybox and util-linux 2.32.1
blkid. We use -s to get around the fact that we still don't support ntfs'
$VOLUME_NAME attribute.
  • Loading branch information
enh-google authored and landley committed Aug 12, 2019
1 parent ae39913 commit 9dc24d9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
12 changes: 8 additions & 4 deletions tests/blkid.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

function BLKID()
{
bzcat $FILES/blkid/$1.bz2 > temp.img
blkid temp.img
file=$1
shift
bzcat $FILES/blkid/$file.bz2 > temp.img
blkid "$@" temp.img
rm temp.img
}

Expand All @@ -28,8 +30,10 @@ testing "f2fs" "BLKID f2fs" \
testing "msdos" "BLKID msdos" \
'temp.img: SEC_TYPE="msdos" LABEL="mymsdos" UUID="6E1E-0851" TYPE="vfat"\n' \
"" ""
testing "ntfs" "BLKID ntfs" \
'temp.img: LABEL="myntfs" UUID="6EE1BF3808608585" TYPE="ntfs"\n' "" ""

# We use -s here because toybox blkid can't do ntfs volume labels yet.
testing "ntfs" "BLKID ntfs -s UUID -s TYPE" \
'temp.img: UUID="6EE1BF3808608585" TYPE="ntfs"\n' "" ""
testing "reiserfs" "BLKID reiser3" \
'temp.img: LABEL="myreiser" UUID="a5b99bec-45cc-41d7-986e-32f4b6fc28f2" TYPE="reiserfs"\n' \
"" ""
Expand Down
76 changes: 56 additions & 20 deletions toys/other/blkid.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
* Copyright 2013 Brad Conroy <bconroy@uis.edu>
*
* See ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.24/libblkid-docs/api-index-full.html
* TODO: -U and -L should require arguments
USE_BLKID(NEWTOY(blkid, "UL[!LU]", TOYFLAG_BIN))
USE_BLKID(NEWTOY(blkid, "ULs*[!LU]", TOYFLAG_BIN))
USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN))
config BLKID
bool "blkid"
default y
help
usage: blkid [-UL] DEV...
usage: blkid [-s TAG] [-UL] DEV...
Print type, label and UUID of filesystem on a block device or image.
-U Show UUID only (or device with that UUID)
-L Show LABEL only (or device with that LABEL)
-s TAG Only show matching tags (default all)
config FSTYPE
bool "fstype"
Expand All @@ -30,6 +32,10 @@ config FSTYPE
#define FOR_blkid
#include "toys.h"

GLOBALS(
struct arg_list *s;
)

struct fstype {
char *name;
uint64_t magic;
Expand All @@ -39,13 +45,13 @@ struct fstype {
{"swap", 0x4341505350415753LL, 8, 4086, 1036, 15, 1052},
// NTFS label actually 8/16 0x4d80 but horrible: 16 bit wide characters via
// codepage, something called a uuid that's only 8 bytes long...
{"ntfs", 0x5346544e, 4, 3, 0x48+(8<<24), 0, 0},
{"ntfs", 0x5346544e, 4, 3, 0x48, 0, 0},

{"adfs", 0xadf5, 2, 0xc00, 0,0,0},
{"bfs", 0x1badface, 4, 0, 0,0,0},
{"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819},
{"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48},
{"f2fs", 0xF2F52010, 4, 1024, 1132, 16, 1110},
{"f2fs", 0xF2F52010, 4, 1024, 1132, 512, 0x47c},
{"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904},
{"nilfs", 0x3434, 2, 1030, 1176, 80, 1192},
{"reiserfs", 0x724573496552ULL, 6, 8244, 8276, 16, 8292},
Expand All @@ -54,10 +60,22 @@ struct fstype {
{"squashfs", 0x73717368, 4, 0, 0,0,0},
{"xiafs", 0x012fd16d, 4, 572, 0,0,0},
{"xfs", 0x42534658, 4, 0, 32, 12, 108},
{"vfat", 0x3233544146ULL, 5, 82, 67+(4<<24), 11, 71}, // fat32
{"vfat", 0x31544146, 4, 54, 39+(4<<24), 11, 43} // fat1
{"vfat", 0x3233544146ULL, 5, 82, 67, 11, 71}, // fat32
{"vfat", 0x31544146, 4, 54, 39, 11, 43} // fat1
};

static void show_tag(char *key, char *value)
{
int show = 0;
struct arg_list *al;

if (TT.s) {
for (al = TT.s; al; al = al->next) if (!strcmp(key, al->arg)) show = 1;
} else show = 1;

if (show) printf(" %s=\"%s\"", key, value);
}

static void flagshow(char *s, char *name)
{
if (*toys.optargs && strcmp(s, *toys.optargs)) return;
Expand Down Expand Up @@ -112,48 +130,66 @@ static void do_blkid(int fd, char *name)
if (toybuf[1120]&64) type = "ext4";
}

// Could special case NTFS here...

// Output for fstype
if (*toys.which->name == 'f') {
puts(type);
return;
}

// output for blkid
if (!toys.optflags) printf("%s:",name);
if (!FLAG(L) && !FLAG(U)) printf("%s:",name);

len = fstypes[i].label_len;
if (!FLAG(U) && len) {
s = toybuf+fstypes[i].label_off-off;
if (!strcmp(type, "vfat")) {
show_tag("SEC_TYPE", "msdos");
while (len && s[len-1]==' ') len--;
if (strstart(&s, "NO NAME")) len=0;
}
if (len && *s) {
sprintf(buf, "%.*s", len, s);
// TODO: special case NTFS $VOLUME_NAME here...
if (len) {
if (!strcmp(type, "f2fs")) {
// Convert UTF16LE to ASCII by replacing non-ASCII with '?'.
// TODO: support non-ASCII.
for (j=0; j<len; j++) {
buf[j] = s[2*j];
if (s[2*j+1]) buf[j]='?';
if (!buf[j]) break;
}
} else sprintf(buf, "%.*s", len, s);
if (FLAG(L)) return flagshow(buf, name);
printf(" LABEL=\"%s\"", buf);
show_tag("LABEL", buf);
}
}

len = fstypes[i].uuid_off;
if (!FLAG(L) && len) {
int bits = 0x550, size = len >> 24, uoff = (len&((1<<24)-1))-off;
int uoff = len-off;

// Assemble UUID with whatever size and set of dashes this filesystem uses
if (size) bits = 4*(size == 4);
else size = 16;
for (j = 0, s = buf; j < size; j++)
s += sprintf(s, "-%02x"+!(bits & (1<<j)), toybuf[uoff+j]);
s = buf;
if (!strcmp(type, "ntfs")) {
for (j = 7; j >= 0; --j) s += sprintf(s, "%02X", toybuf[uoff+j]);
} else if (!strcmp(type, "vfat")) {
s += sprintf(s, "%02X%02X-%02X%02X", toybuf[uoff+3], toybuf[uoff+2],
toybuf[uoff+1], toybuf[uoff]);
} else {
for (j = 0; j < 16; j++)
s += sprintf(s, "-%02x"+!(0x550 & (1<<j)), toybuf[uoff+j]);
}

if (FLAG(U)) return flagshow(buf, name);
printf(" UUID=\"%s\"", buf);
show_tag("UUID", buf);
}

if (toys.optflags) return;
if ((!strcmp(type, "ext3")||!strcmp(type,"ext4")) && !(toybuf[1120]&~0x12))
show_tag("SEC_TYPE", "ext2");

if (FLAG(U) || FLAG(L)) return;

printf(" TYPE=\"%s\"\n", type);
show_tag("TYPE", type);
xputc('\n');
}

void blkid_main(void)
Expand Down

0 comments on commit 9dc24d9

Please sign in to comment.