Skip to content

Instantly share code, notes, and snippets.

@pmj
Last active May 28, 2019 10:18
Show Gist options
  • Save pmj/b496c91f051f18ac57b5d8721fa6e219 to your computer and use it in GitHub Desktop.
Save pmj/b496c91f051f18ac57b5d8721fa6e219 to your computer and use it in GitHub Desktop.
Testing APFS mtime granularity
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <mach/clock_types.h>
#include <fcntl.h>
#include <unistd.h>
static const char s_filePath[] = "./testfile"; //"/Volumes/Macintosh HD/Users/phil/Desktop/test/testfile";
int main(int argc, const char * argv[])
{
struct stat stat_buf = {};
int fd = open(s_filePath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
{
perror("open() failed");
return 1;
}
for (unsigned j = 0; j < 100; ++j)
{
// warmup
for (unsigned i = 0; i < 1000000; ++i)
{
pwrite(fd, "\0", 1, i);
}
fstat(fd, &stat_buf);
uint64_t mtime_ns = stat_buf.st_mtimespec.tv_nsec + stat_buf.st_mtimespec.tv_sec * NSEC_PER_SEC;
uint64_t new_mtime_ns;
unsigned written = 0;
do
{
pwrite(fd, "\0", 1, written);
++written;
fstat(fd, &stat_buf);
new_mtime_ns = stat_buf.st_mtimespec.tv_nsec + stat_buf.st_mtimespec.tv_sec * NSEC_PER_SEC;
} while (new_mtime_ns == mtime_ns);
printf("%u write()s until mtime change\ndelta =%13llu\n%20llu\n%20llu\n", written, new_mtime_ns - mtime_ns, new_mtime_ns, mtime_ns);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment