+/*
+ * Check if the given directory is a firmlink.
+ * Return 1 if it is firmlink otherwise return 0.
+ */
+static inline int
+__check_for_firmlink(char *dir_path)
+{
+#if TARGET_OS_OSX && !TARGET_OS_SIMULATOR
+ apfs_firmlink_control_t afc;
+ int is_firmlink;
+ int err;
+
+ afc.cmd = FIRMLINK_GET;
+ afc.val = 0;
+
+ err = fsctl(dir_path, APFSIOC_FIRMLINK_CTL, (void *)&afc, FSOPT_NOFOLLOW);
+ if (err == 0) {
+ is_firmlink = afc.val ? 1 : 0;
+ } else {
+ /*
+ * On error, we assume it is a firmlink. This could be a false positive
+ * and we will end up incurring a lstat() cost but it will give us some
+ * chance to build the path successfully.
+ */
+ is_firmlink = 1;
+ }
+
+ return is_firmlink;
+#else
+ return 0;
+#endif
+}
+