fsmonitor: deal with synthetic firmlinks on macOS

Starting with macOS 10.15 (Catalina), Apple introduced a new feature
called 'firmlinks' in order to separate the boot volume into two
volumes, one read-only and one writable but still present them to the
user as a single volume. Along with this change, Apple removed the
ability to create symlinks in the root directory and replaced them with
'synthetic firmlinks'. See 'man synthetic.conf'

When FSEevents reports the path of changed files, if the path involves
a synthetic firmlink, the path is reported from the point of the
synthetic firmlink and not the real path. For example:

Real path:
/System/Volumes/Data/network/working/directory/foo.txt

Synthetic firmlink:
/network -> /System/Volumes/Data/network

FSEvents path:
/network/working/directory/foo.txt

This causes the FSEvents path to not match against the worktree
directory.

There are several ways in which synthetic firmlinks can be created:
they can be defined in /etc/synthetic.conf, the automounter can create
them, and there may be other means. Simply reading /etc/synthetic.conf
is insufficient. No matter what process creates synthetic firmlinks,
they all get created in the root directory.

Therefore, in order to deal with synthetic firmlinks, the root directory
is scanned and the first possible synthetic firmink that, when resolved,
is a prefix of the worktree is used to map FSEvents paths to worktree
paths.

Signed-off-by: Eric DeCosta <edecosta@mathworks.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Eric DeCosta
2022-10-04 17:32:29 +00:00
committed by Junio C Hamano
parent 8f44976882
commit 12fd27df79
6 changed files with 167 additions and 3 deletions

View File

@ -1,5 +1,8 @@
#include "fsmonitor.h"
#include "fsmonitor-path-utils.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/mount.h>
@ -41,3 +44,92 @@ int fsmonitor__is_fs_remote(const char *path)
return fs.is_remote;
}
/*
* Scan the root directory for synthetic firmlinks that when resolved
* are a prefix of the path, stopping at the first one found.
*
* Some information about firmlinks and synthetic firmlinks:
* https://eclecticlight.co/2020/01/23/catalina-boot-volumes/
*
* macOS no longer allows symlinks in the root directory; any link found
* there is therefore a synthetic firmlink.
*
* If this function gets called often, will want to cache all the firmlink
* information, but for now there is only one caller of this function.
*
* If there is more than one alias for the path, that is another
* matter altogether.
*/
int fsmonitor__get_alias(const char *path, struct alias_info *info)
{
DIR *dir;
int retval = -1;
const char *const root = "/";
struct stat st;
struct dirent *de;
struct strbuf alias;
struct strbuf points_to = STRBUF_INIT;
dir = opendir(root);
if (!dir)
return error_errno(_("opendir('%s') failed"), root);
strbuf_init(&alias, 256);
while ((de = readdir(dir)) != NULL) {
strbuf_reset(&alias);
strbuf_addf(&alias, "%s%s", root, de->d_name);
if (lstat(alias.buf, &st) < 0) {
error_errno(_("lstat('%s') failed"), alias.buf);
goto done;
}
if (!S_ISLNK(st.st_mode))
continue;
if (strbuf_readlink(&points_to, alias.buf, st.st_size) < 0) {
error_errno(_("strbuf_readlink('%s') failed"), alias.buf);
goto done;
}
if (!strncmp(points_to.buf, path, points_to.len) &&
(path[points_to.len] == '/')) {
strbuf_addbuf(&info->alias, &alias);
strbuf_addbuf(&info->points_to, &points_to);
trace_printf_key(&trace_fsmonitor,
"Found alias for '%s' : '%s' -> '%s'",
path, info->alias.buf, info->points_to.buf);
retval = 0;
goto done;
}
}
retval = 0; /* no alias */
done:
strbuf_release(&alias);
strbuf_release(&points_to);
if (closedir(dir) < 0)
return error_errno(_("closedir('%s') failed"), root);
return retval;
}
char *fsmonitor__resolve_alias(const char *path,
const struct alias_info *info)
{
if (!info->alias.len)
return NULL;
if ((!strncmp(info->alias.buf, path, info->alias.len))
&& path[info->alias.len] == '/') {
struct strbuf tmp = STRBUF_INIT;
const char *remainder = path + info->alias.len;
strbuf_addbuf(&tmp, &info->points_to);
strbuf_add(&tmp, remainder, strlen(remainder));
return strbuf_detach(&tmp, NULL);
}
return NULL;
}