armybox 0.3.0

A memory-safe #[no_std] BusyBox/Toybox clone in Rust - 299 Unix utilities in ~500KB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! blkid - locate/print block device attributes
//!
//! Print block device attributes like UUID, TYPE, and LABEL by reading
//! filesystem superblocks.

extern crate alloc;

use alloc::vec::Vec;
use crate::io;
use crate::applets::get_arg;

/// Filesystem detection result
struct FsInfo {
    fs_type: &'static [u8],
    uuid: Option<[u8; 16]>,
    label: Option<Vec<u8>>,
}

/// blkid - locate/print block device attributes
///
/// # Synopsis
/// ```text
/// blkid [-o format] [-s tag] [device...]
/// ```
///
/// # Description
/// Print block device attributes by reading filesystem superblocks.
/// Detects UUID, TYPE (filesystem type), and LABEL.
///
/// # Options
/// - `-o format`: Output format (full, value, device, export)
/// - `-s tag`: Show only specified tag (UUID, TYPE, LABEL)
/// - `-c /dev/null`: Don't use cache (for compatibility, ignored)
///
/// # Exit Status
/// - 0: Success
/// - 2: No devices found or specified device not found
pub fn blkid(argc: i32, argv: *const *const u8) -> i32 {
    let mut output_format = OutputFormat::Full;
    let mut show_tag: Option<&[u8]> = None;
    let mut devices: Vec<&[u8]> = Vec::new();

    // Parse arguments
    let mut i = 1i32;
    while i < argc {
        let Some(arg) = (unsafe { get_arg(argv, i) }) else {
            i += 1;
            continue;
        };
        if arg.starts_with(b"-") {
            if arg == b"-o" {
                i += 1;
                if let Some(fmt) = unsafe { get_arg(argv, i) } {
                    output_format = match fmt {
                        b"full" => OutputFormat::Full,
                        b"value" => OutputFormat::Value,
                        b"device" => OutputFormat::Device,
                        b"export" => OutputFormat::Export,
                        _ => {
                            io::write_all(2, b"blkid: unknown output format\n");
                            return 1;
                        }
                    };
                }
            } else if arg == b"-s" {
                i += 1;
                if let Some(tag) = unsafe { get_arg(argv, i) } {
                    show_tag = Some(tag);
                }
            } else if arg == b"-c" {
                // Skip cache file argument (compatibility)
                i += 1;
            } else if arg == b"-h" || arg == b"--help" {
                io::write_all(1, b"Usage: blkid [-o format] [-s tag] [device...]\n");
                io::write_all(1, b"  -o format  Output format: full, value, device, export\n");
                io::write_all(1, b"  -s tag     Show only specified tag (UUID, TYPE, LABEL)\n");
                return 0;
            }
        } else {
            devices.push(arg);
        }
        i += 1;
    }

    // If no devices specified, scan all block devices
    if devices.is_empty() {
        return scan_all_devices(output_format, show_tag);
    }

    // Process specified devices
    let mut found_any = false;
    for device in devices {
        if let Some(info) = probe_device(device) {
            print_device_info(device, &info, output_format, show_tag);
            found_any = true;
        }
    }

    if found_any { 0 } else { 2 }
}

#[derive(Clone, Copy)]
enum OutputFormat {
    Full,    // /dev/sda1: UUID="..." TYPE="..."
    Value,   // Just the value
    Device,  // Just device name
    Export,  // DEVNAME=/dev/sda1\nUUID=...\n
}

/// Scan all block devices
fn scan_all_devices(format: OutputFormat, show_tag: Option<&[u8]>) -> i32 {
    let mut found_any = false;

    // Read /sys/block to find block devices
    let dir_fd = unsafe { libc::open(b"/sys/block\0".as_ptr() as *const i8, libc::O_RDONLY | libc::O_DIRECTORY) };
    if dir_fd < 0 {
        return 2;
    }

    let mut buf = [0u8; 4096];
    loop {
        let n = unsafe { libc::syscall(libc::SYS_getdents64, dir_fd, buf.as_mut_ptr(), buf.len()) };
        if n <= 0 {
            break;
        }

        let mut offset = 0usize;
        while offset < n as usize {
            let d_reclen = u16::from_ne_bytes([buf[offset + 16], buf[offset + 17]]) as usize;
            let d_type = buf[offset + 18];

            // Find name (null-terminated after d_type)
            let name_start = offset + 19;
            let mut name_end = name_start;
            while name_end < offset + d_reclen && buf[name_end] != 0 {
                name_end += 1;
            }
            let name = &buf[name_start..name_end];

            // Skip . and .. and non-directories
            if name != b"." && name != b".." && (d_type == 4 || d_type == 10) {
                // Check this device and its partitions
                scan_block_device(name, format, show_tag, &mut found_any);
            }

            offset += d_reclen;
        }
    }

    unsafe { libc::close(dir_fd) };

    if found_any { 0 } else { 2 }
}

/// Scan a block device and its partitions
fn scan_block_device(name: &[u8], format: OutputFormat, show_tag: Option<&[u8]>, found_any: &mut bool) {
    // Skip loop, ram, and dm devices for cleaner output (like real blkid)
    if name.starts_with(b"loop") || name.starts_with(b"ram") {
        return;
    }

    // Build device path /dev/NAME
    let mut dev_path = Vec::with_capacity(name.len() + 6);
    dev_path.extend_from_slice(b"/dev/");
    dev_path.extend_from_slice(name);

    // Try the main device
    if let Some(info) = probe_device(&dev_path) {
        print_device_info(&dev_path, &info, format, show_tag);
        *found_any = true;
    }

    // Look for partitions in /sys/block/NAME/
    let mut sys_path = Vec::with_capacity(name.len() + 16);
    sys_path.extend_from_slice(b"/sys/block/");
    sys_path.extend_from_slice(name);
    sys_path.push(0);

    let dir_fd = unsafe { libc::open(sys_path.as_ptr() as *const i8, libc::O_RDONLY | libc::O_DIRECTORY) };
    if dir_fd < 0 {
        return;
    }

    let mut buf = [0u8; 4096];
    loop {
        let n = unsafe { libc::syscall(libc::SYS_getdents64, dir_fd, buf.as_mut_ptr(), buf.len()) };
        if n <= 0 {
            break;
        }

        let mut offset = 0usize;
        while offset < n as usize {
            let d_reclen = u16::from_ne_bytes([buf[offset + 16], buf[offset + 17]]) as usize;

            let name_start = offset + 19;
            let mut name_end = name_start;
            while name_end < offset + d_reclen && buf[name_end] != 0 {
                name_end += 1;
            }
            let part_name = &buf[name_start..name_end];

            // Partitions are named like sda1, nvme0n1p1 - they start with the device name
            if part_name.starts_with(name) && part_name.len() > name.len() {
                let mut part_dev_path = Vec::with_capacity(part_name.len() + 6);
                part_dev_path.extend_from_slice(b"/dev/");
                part_dev_path.extend_from_slice(part_name);

                if let Some(info) = probe_device(&part_dev_path) {
                    print_device_info(&part_dev_path, &info, format, show_tag);
                    *found_any = true;
                }
            }

            offset += d_reclen;
        }
    }

    unsafe { libc::close(dir_fd) };
}

/// Probe a device and detect filesystem
fn probe_device(path: &[u8]) -> Option<FsInfo> {
    // Need null-terminated path
    let mut path_z = Vec::with_capacity(path.len() + 1);
    path_z.extend_from_slice(path);
    path_z.push(0);

    let fd = unsafe { libc::open(path_z.as_ptr() as *const i8, libc::O_RDONLY) };
    if fd < 0 {
        return None;
    }

    // Read first 128KB to catch all filesystem superblocks
    let mut buf = [0u8; 131072];
    let n = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) };
    unsafe { libc::close(fd) };

    if n < 512 {
        return None;
    }
    let buf = &buf[..n as usize];

    // Try each filesystem detector
    if let Some(info) = detect_ext234(buf) {
        return Some(info);
    }
    if let Some(info) = detect_xfs(buf) {
        return Some(info);
    }
    if let Some(info) = detect_btrfs(buf) {
        return Some(info);
    }
    if let Some(info) = detect_swap(buf) {
        return Some(info);
    }
    if let Some(info) = detect_ntfs(buf) {
        return Some(info);
    }
    if let Some(info) = detect_vfat(buf) {
        return Some(info);
    }
    if let Some(info) = detect_iso9660(buf) {
        return Some(info);
    }
    if let Some(info) = detect_squashfs(buf) {
        return Some(info);
    }
    if let Some(info) = detect_f2fs(buf) {
        return Some(info);
    }

    None
}

/// Detect ext2/3/4 filesystem
fn detect_ext234(buf: &[u8]) -> Option<FsInfo> {
    // Superblock at offset 1024, magic at offset 0x38 (56) within superblock = 0x438
    if buf.len() < 0x480 {
        return None;
    }

    let magic = u16::from_le_bytes([buf[0x438], buf[0x439]]);
    if magic != 0xEF53 {
        return None;
    }

    // Determine ext2/3/4 by feature flags
    let compat_features = u32::from_le_bytes([buf[0x45C], buf[0x45D], buf[0x45E], buf[0x45F]]);
    let incompat_features = u32::from_le_bytes([buf[0x460], buf[0x461], buf[0x462], buf[0x463]]);
    let _ro_compat_features = u32::from_le_bytes([buf[0x464], buf[0x465], buf[0x466], buf[0x467]]);

    // ext4 features
    let has_extents = (incompat_features & 0x40) != 0;
    let has_64bit = (incompat_features & 0x80) != 0;
    let has_flex_bg = (incompat_features & 0x200) != 0;

    // ext3 feature: has_journal
    let has_journal = (compat_features & 0x4) != 0;

    let fs_type: &'static [u8] = if has_extents || has_64bit || has_flex_bg {
        b"ext4"
    } else if has_journal {
        b"ext3"
    } else {
        b"ext2"
    };

    // UUID at offset 0x468 (16 bytes)
    let mut uuid = [0u8; 16];
    uuid.copy_from_slice(&buf[0x468..0x478]);

    // Label at offset 0x478 (16 bytes, null-terminated)
    let label = extract_label(&buf[0x478..0x488]);

    Some(FsInfo {
        fs_type,
        uuid: if uuid != [0u8; 16] { Some(uuid) } else { None },
        label,
    })
}

/// Detect XFS filesystem
fn detect_xfs(buf: &[u8]) -> Option<FsInfo> {
    if buf.len() < 0x100 {
        return None;
    }

    // Magic "XFSB" at offset 0
    if &buf[0..4] != b"XFSB" {
        return None;
    }

    // UUID at offset 0x20 (32)
    let mut uuid = [0u8; 16];
    uuid.copy_from_slice(&buf[0x20..0x30]);

    // Label at offset 0x6C (108), 12 bytes
    let label = extract_label(&buf[0x6C..0x78]);

    Some(FsInfo {
        fs_type: b"xfs",
        uuid: if uuid != [0u8; 16] { Some(uuid) } else { None },
        label,
    })
}

/// Detect Btrfs filesystem
fn detect_btrfs(buf: &[u8]) -> Option<FsInfo> {
    // Btrfs superblock at 0x10000 (64KB), magic at offset 0x40 = "_BHRfS_M"
    if buf.len() < 0x10100 {
        return None;
    }

    let sb_offset = 0x10000;
    if &buf[sb_offset + 0x40..sb_offset + 0x48] != b"_BHRfS_M" {
        return None;
    }

    // UUID at offset 0x20 in superblock
    let mut uuid = [0u8; 16];
    uuid.copy_from_slice(&buf[sb_offset + 0x20..sb_offset + 0x30]);

    // Label at offset 0x12B in superblock (256 bytes)
    let label = extract_label(&buf[sb_offset + 0x12B..sb_offset + 0x22B.min(buf.len() - sb_offset)]);

    Some(FsInfo {
        fs_type: b"btrfs",
        uuid: if uuid != [0u8; 16] { Some(uuid) } else { None },
        label,
    })
}

/// Detect Linux swap
fn detect_swap(buf: &[u8]) -> Option<FsInfo> {
    // Swap magic at end of first page (typically 4096, but could be other page sizes)
    // Magic is "SWAPSPACE2" at offset pagesize - 10

    for &pagesize in &[4096usize, 8192, 16384, 65536] {
        if buf.len() >= pagesize {
            let magic_offset = pagesize - 10;
            if &buf[magic_offset..magic_offset + 10] == b"SWAPSPACE2"
               || &buf[magic_offset..magic_offset + 10] == b"SWAP-SPACE" {
                // UUID at offset 0x40C in swap header (swap_header_v1_2)
                let mut uuid = [0u8; 16];
                if buf.len() >= 0x41C {
                    uuid.copy_from_slice(&buf[0x40C..0x41C]);
                }

                // Label at offset 0x41C (16 bytes)
                let label = if buf.len() >= 0x42C {
                    extract_label(&buf[0x41C..0x42C])
                } else {
                    None
                };

                return Some(FsInfo {
                    fs_type: b"swap",
                    uuid: if uuid != [0u8; 16] { Some(uuid) } else { None },
                    label,
                });
            }
        }
    }

    None
}

/// Detect NTFS filesystem
fn detect_ntfs(buf: &[u8]) -> Option<FsInfo> {
    if buf.len() < 512 {
        return None;
    }

    // "NTFS    " at offset 3
    if &buf[3..11] != b"NTFS    " {
        return None;
    }

    // NTFS volume serial number at offset 0x48 (8 bytes)
    // This is used as a pseudo-UUID
    let mut serial = [0u8; 8];
    serial.copy_from_slice(&buf[0x48..0x50]);

    // NTFS doesn't have a label in the boot sector - it's in the $Volume file
    // We can't easily read that without parsing NTFS structures

    Some(FsInfo {
        fs_type: b"ntfs",
        uuid: None, // NTFS uses serial number, not UUID
        label: None,
    })
}

/// Detect FAT12/16/32 and exFAT
fn detect_vfat(buf: &[u8]) -> Option<FsInfo> {
    if buf.len() < 512 {
        return None;
    }

    // Check boot sector signature
    if buf[510] != 0x55 || buf[511] != 0xAA {
        return None;
    }

    // Check for exFAT first (has "EXFAT   " at offset 3)
    if &buf[3..11] == b"EXFAT   " {
        // exFAT volume serial at offset 100-103
        let label = extract_label(&buf[0x47..0x52]);
        return Some(FsInfo {
            fs_type: b"exfat",
            uuid: None,
            label,
        });
    }

    // FAT32 has "FAT32   " at offset 0x52
    if &buf[0x52..0x5A] == b"FAT32   " {
        // Volume serial at 0x43 (4 bytes)
        let serial = &buf[0x43..0x47];
        // Label at 0x47 (11 bytes)
        let label = extract_label(&buf[0x47..0x52]);

        // Create pseudo-UUID from serial
        let mut uuid = [0u8; 16];
        uuid[0..4].copy_from_slice(serial);

        return Some(FsInfo {
            fs_type: b"vfat",
            uuid: Some(uuid),
            label,
        });
    }

    // FAT12/16 has "FAT     " or "FAT12   " or "FAT16   " at offset 0x36
    if buf[0x36..0x3B].starts_with(b"FAT") {
        // Volume serial at 0x27 (4 bytes)
        let serial = &buf[0x27..0x2B];
        // Label at 0x2B (11 bytes)
        let label = extract_label(&buf[0x2B..0x36]);

        let mut uuid = [0u8; 16];
        uuid[0..4].copy_from_slice(serial);

        return Some(FsInfo {
            fs_type: b"vfat",
            uuid: Some(uuid),
            label,
        });
    }

    None
}

/// Detect ISO9660 (CD/DVD filesystem)
fn detect_iso9660(buf: &[u8]) -> Option<FsInfo> {
    // Primary volume descriptor at sector 16 (offset 0x8000)
    // Magic "CD001" at offset 1 in the descriptor
    if buf.len() < 0x8800 {
        return None;
    }

    let pvd_offset = 0x8000;
    if &buf[pvd_offset + 1..pvd_offset + 6] != b"CD001" {
        return None;
    }

    // Volume identifier at offset 40 (32 bytes)
    let label = extract_label(&buf[pvd_offset + 40..pvd_offset + 72]);

    Some(FsInfo {
        fs_type: b"iso9660",
        uuid: None,
        label,
    })
}

/// Detect squashfs
fn detect_squashfs(buf: &[u8]) -> Option<FsInfo> {
    if buf.len() < 96 {
        return None;
    }

    // Magic at offset 0: "hsqs" (little-endian) or "sqsh" (big-endian)
    let magic = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]);
    if magic != 0x73717368 { // "hsqs"
        // Try big-endian
        let magic_be = u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
        if magic_be != 0x73717368 {
            return None;
        }
    }

    Some(FsInfo {
        fs_type: b"squashfs",
        uuid: None,
        label: None,
    })
}

/// Detect F2FS (Flash-Friendly File System)
fn detect_f2fs(buf: &[u8]) -> Option<FsInfo> {
    // F2FS superblock at offset 0x400 (1024)
    // Magic at offset 0 in superblock = 0xF2F52010
    if buf.len() < 0x500 {
        return None;
    }

    let sb_offset = 0x400;
    let magic = u32::from_le_bytes([
        buf[sb_offset], buf[sb_offset + 1],
        buf[sb_offset + 2], buf[sb_offset + 3]
    ]);

    if magic != 0xF2F52010 {
        return None;
    }

    // UUID at offset 0x6C in superblock (16 bytes)
    let mut uuid = [0u8; 16];
    uuid.copy_from_slice(&buf[sb_offset + 0x6C..sb_offset + 0x7C]);

    // Volume name at offset 0x7C (512 bytes, UTF-16LE)
    // For simplicity, try to extract ASCII portion
    let label = extract_utf16_label(&buf[sb_offset + 0x7C..sb_offset + 0x27C.min(buf.len() - sb_offset)]);

    Some(FsInfo {
        fs_type: b"f2fs",
        uuid: if uuid != [0u8; 16] { Some(uuid) } else { None },
        label,
    })
}

/// Extract a null-terminated or space-padded label
fn extract_label(data: &[u8]) -> Option<Vec<u8>> {
    // Find actual end (trim nulls and spaces from end)
    let mut end = data.len();
    while end > 0 && (data[end - 1] == 0 || data[end - 1] == b' ') {
        end -= 1;
    }

    if end == 0 {
        return None;
    }

    Some(data[..end].to_vec())
}

/// Extract label from UTF-16LE data (like F2FS volume names)
fn extract_utf16_label(data: &[u8]) -> Option<Vec<u8>> {
    let mut result = Vec::new();
    let mut i = 0;

    while i + 1 < data.len() {
        let c = u16::from_le_bytes([data[i], data[i + 1]]);
        if c == 0 {
            break;
        }
        // Only keep ASCII characters
        if c < 128 {
            result.push(c as u8);
        }
        i += 2;
    }

    if result.is_empty() {
        None
    } else {
        Some(result)
    }
}

/// Print device info in the requested format
fn print_device_info(path: &[u8], info: &FsInfo, format: OutputFormat, show_tag: Option<&[u8]>) {
    match format {
        OutputFormat::Full => {
            // /dev/sda1: UUID="xxxx" TYPE="ext4" LABEL="..."
            io::write_all(1, path);
            io::write_all(1, b":");

            if let Some(ref tag) = show_tag {
                // Show only specific tag
                if *tag == b"UUID" {
                    if let Some(uuid) = &info.uuid {
                        io::write_all(1, b" UUID=\"");
                        print_uuid(uuid);
                        io::write_all(1, b"\"");
                    }
                } else if *tag == b"TYPE" {
                    io::write_all(1, b" TYPE=\"");
                    io::write_all(1, info.fs_type);
                    io::write_all(1, b"\"");
                } else if *tag == b"LABEL" {
                    if let Some(label) = &info.label {
                        io::write_all(1, b" LABEL=\"");
                        io::write_all(1, label);
                        io::write_all(1, b"\"");
                    }
                }
            } else {
                // Show all tags
                if let Some(uuid) = &info.uuid {
                    io::write_all(1, b" UUID=\"");
                    print_uuid(uuid);
                    io::write_all(1, b"\"");
                }
                io::write_all(1, b" TYPE=\"");
                io::write_all(1, info.fs_type);
                io::write_all(1, b"\"");
                if let Some(label) = &info.label {
                    io::write_all(1, b" LABEL=\"");
                    io::write_all(1, label);
                    io::write_all(1, b"\"");
                }
            }
            io::write_all(1, b"\n");
        }
        OutputFormat::Value => {
            // Just print the value(s)
            if let Some(ref tag) = show_tag {
                if *tag == b"UUID" {
                    if let Some(uuid) = &info.uuid {
                        print_uuid(uuid);
                        io::write_all(1, b"\n");
                    }
                } else if *tag == b"TYPE" {
                    io::write_all(1, info.fs_type);
                    io::write_all(1, b"\n");
                } else if *tag == b"LABEL" {
                    if let Some(label) = &info.label {
                        io::write_all(1, label);
                        io::write_all(1, b"\n");
                    }
                }
            } else {
                // Print all values on separate lines
                if let Some(uuid) = &info.uuid {
                    print_uuid(uuid);
                    io::write_all(1, b"\n");
                }
                io::write_all(1, info.fs_type);
                io::write_all(1, b"\n");
                if let Some(label) = &info.label {
                    io::write_all(1, label);
                    io::write_all(1, b"\n");
                }
            }
        }
        OutputFormat::Device => {
            io::write_all(1, path);
            io::write_all(1, b"\n");
        }
        OutputFormat::Export => {
            // DEVNAME=/dev/sda1
            // UUID=xxxx
            // TYPE=ext4
            io::write_all(1, b"DEVNAME=");
            io::write_all(1, path);
            io::write_all(1, b"\n");
            if let Some(uuid) = &info.uuid {
                io::write_all(1, b"UUID=");
                print_uuid(uuid);
                io::write_all(1, b"\n");
            }
            io::write_all(1, b"TYPE=");
            io::write_all(1, info.fs_type);
            io::write_all(1, b"\n");
            if let Some(label) = &info.label {
                io::write_all(1, b"LABEL=");
                io::write_all(1, label);
                io::write_all(1, b"\n");
            }
            io::write_all(1, b"\n");
        }
    }
}

/// Print UUID in canonical format (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
fn print_uuid(uuid: &[u8; 16]) {
    let hex = b"0123456789abcdef";
    let mut out = [0u8; 36];

    // Format: 8-4-4-4-12
    let mut j = 0;
    for (i, &b) in uuid.iter().enumerate() {
        out[j] = hex[(b >> 4) as usize];
        out[j + 1] = hex[(b & 0xf) as usize];
        j += 2;

        if i == 3 || i == 5 || i == 7 || i == 9 {
            out[j] = b'-';
            j += 1;
        }
    }

    io::write_all(1, &out);
}

#[cfg(test)]
mod tests {
    extern crate std;
    use std::process::Command;
    use std::path::PathBuf;

    fn get_armybox_path() -> PathBuf {
        if let Ok(path) = std::env::var("ARMYBOX_PATH") {
            return PathBuf::from(path);
        }
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|_| std::env::current_dir().unwrap());
        let release = manifest_dir.join("target/release/armybox");
        if release.exists() { return release; }
        manifest_dir.join("target/debug/armybox")
    }

    #[test]
    fn test_blkid_runs() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["blkid"])
            .output()
            .unwrap();

        // Exit code 0 if found devices, 2 if no devices (both acceptable in tests)
        assert!(output.status.code() == Some(0) || output.status.code() == Some(2));
    }

    #[test]
    fn test_blkid_help() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["blkid", "-h"])
            .output()
            .unwrap();

        assert_eq!(output.status.code(), Some(0));
        assert!(output.stdout.starts_with(b"Usage:"));
    }
}