Skip to content

Instantly share code, notes, and snippets.

@plobsing
Last active August 7, 2024 05:45
Show Gist options
  • Save plobsing/dc4456e1b461563a3a9e0e93673834b9 to your computer and use it in GitHub Desktop.
Save plobsing/dc4456e1b461563a3a9e0e93673834b9 to your computer and use it in GitHub Desktop.
log2phys - lookup physical locations of file extents on Darwin
#!/usr/bin/perl
use v5.10;
use strict;
# https://github.com/apple-oss-distributions/xnu/blob/94d3b452840153a99b38a3a9659680b2a006908e/bsd/sys/fcntl.h#L326C9-L326C23
use constant F_LOG2PHYS_EXT => 65;
foreach (@ARGV) {
say "==== $_ ====";
open my $fh, "<", $_ or die "open: $!";
my @stat = stat $fh or die "fstat: $!";
printf "st_dev: % 20d st_ino: % 20d\n", $stat[0], $stat[1];
my $sz = $stat[7];
my $pos = 0;
while ($pos < $sz) {
# from fcntl(2):
#
# struct log2phys {
# u_int32_t l2p_flags; /* unused so far */
# off_t l2p_contigbytes; /* IN: number of bytes to be queried;
# OUT: number of contiguous bytes allocated at this position */
# off_t l2p_devoffset; /* IN: bytes into file;
# OUT: bytes into device */
# };
my $log2phys = pack("LQQ", 0, $sz - $pos, $pos);
fcntl $fh, F_LOG2PHYS_EXT, $log2phys or die "fcntl: $!";
my ($l2p_flags, $l2p_contigbytes, $l2p_devoffset) = unpack "LQQ", $log2phys;
printf "logaddr: % 20d flags: %08x len: % 20d physaddr: % 20d\n", $pos, $l2p_flags, $l2p_contigbytes, $l2p_devoffset;
$pos += $l2p_contigbytes;
}
say "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment