| 1 | |
| 2 | |
| 3 | @@ -324,6 +324,9 @@ |
| 4 | }; |
| 5 | |
| 6 | struct sshfs { |
| 7 | +#ifdef __APPLE__ |
| 8 | + char *cage_openwith; |
| 9 | +#endif |
| 10 | char *directport; |
| 11 | char *ssh_command; |
| 12 | char *sftp_server; |
| 13 | @@ -497,6 +500,9 @@ |
| 14 | #define SSHFS_OPT(t, p, v) { t, offsetof(struct sshfs, p), v } |
| 15 | |
| 16 | static struct fuse_opt sshfs_opts[] = { |
| 17 | +#ifdef __APPLE__ |
| 18 | + SSHFS_OPT("cage_openwith=%s", cage_openwith, 0), |
| 19 | +#endif |
| 20 | SSHFS_OPT("directport=%s", directport, 0), |
| 21 | SSHFS_OPT("ssh_command=%s", ssh_command, 0), |
| 22 | SSHFS_OPT("sftp_server=%s", sftp_server, 0), |
| 23 | @@ -3829,7 +3835,76 @@ |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | +#ifdef __APPLE__ |
| 28 | +/* Present guest-local LaunchServices metadata without creating host xattrs |
| 29 | + * or AppleDouble files in a shared project. The GUI helper publishes this |
| 30 | + * opaque value with the supported NSWorkspace per-file association API. */ |
| 31 | +static int cage_getxattr(const char *path, const char *name, char *value, |
| 32 | + size_t size) |
| 33 | +{ |
| 34 | + if (!sshfs.cage_openwith || strcmp(name, "com.apple.LaunchServices.OpenWith")) |
| 35 | + return -ENOTSUP; |
| 36 | + |
| 37 | + struct stat target; |
| 38 | + int status = sshfs_getattr(path, &target, NULL); |
| 39 | + if (status != 0) |
| 40 | + return status; |
| 41 | + if (!S_ISREG(target.st_mode)) |
| 42 | + return -ENOATTR; |
| 43 | + |
| 44 | + int fd = open(sshfs.cage_openwith, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); |
| 45 | + if (fd < 0) |
| 46 | + return errno == ENOENT ? -ENOATTR : -errno; |
| 47 | + struct stat st; |
| 48 | + int result; |
| 49 | + if (fstat(fd, &st) != 0) { |
| 50 | + result = -errno; |
| 51 | + } else if (!S_ISREG(st.st_mode) || st.st_size <= 0 || st.st_size > 65536) { |
| 52 | + result = -EIO; |
| 53 | + } else if (!size) { |
| 54 | + result = (int)st.st_size; |
| 55 | + } else if (size < (size_t)st.st_size) { |
| 56 | + result = -ERANGE; |
| 57 | + } else { |
| 58 | + size_t copied = 0; |
| 59 | + result = (int)st.st_size; |
| 60 | + while (copied < (size_t)st.st_size) { |
| 61 | + ssize_t count = read(fd, value + copied, (size_t)st.st_size - copied); |
| 62 | + if (count < 0 && errno == EINTR) |
| 63 | + continue; |
| 64 | + if (count <= 0) { |
| 65 | + result = count < 0 ? -errno : -EIO; |
| 66 | + break; |
| 67 | + } |
| 68 | + copied += (size_t)count; |
| 69 | + } |
| 70 | + } |
| 71 | + close(fd); |
| 72 | + return result; |
| 73 | +} |
| 74 | + |
| 75 | +static int cage_listxattr(const char *path, char *list, size_t size) |
| 76 | +{ |
| 77 | + const char name[] = "com.apple.LaunchServices.OpenWith"; |
| 78 | + int available = cage_getxattr(path, name, NULL, 0); |
| 79 | + if (available == -ENOATTR) |
| 80 | + return 0; |
| 81 | + if (available < 0) |
| 82 | + return available; |
| 83 | + if (!size) |
| 84 | + return sizeof(name); |
| 85 | + if (size < sizeof(name)) |
| 86 | + return -ERANGE; |
| 87 | + memcpy(list, name, sizeof(name)); |
| 88 | + return sizeof(name); |
| 89 | +} |
| 90 | +#endif |
| 91 | + |
| 92 | static struct fuse_operations sshfs_oper = { |
| 93 | +#ifdef __APPLE__ |
| 94 | + .getxattr = cage_getxattr, |
| 95 | + .listxattr = cage_listxattr, |
| 96 | +#endif |
| 97 | .init = sshfs_init, |
| 98 | .getattr = sshfs_getattr, |
| 99 | .access = sshfs_access, |
| 100 | @@ -3862,6 +3937,9 @@ |
| 101 | { |
| 102 | printf( |
| 103 | "usage: %s [user@]host:[dir] mountpoint [options]\n" |
| 104 | +#ifdef __APPLE__ |
| 105 | +" -o cage_openwith=FILE read guest-local file-opening metadata\n" |
| 106 | +#endif |
| 107 | "\n" |
| 108 | " -h --help print help\n" |
| 109 | " -V --version print version\n" |