diff --git a/tools/nativeguest/hook/fileops.c b/tools/nativeguest/hook/fileops.c index 840812fe6..a515e6a24 100644 --- a/tools/nativeguest/hook/fileops.c +++ b/tools/nativeguest/hook/fileops.c @@ -739,10 +739,8 @@ char *getcwd(char *buf, size_t size) { static char *(*orig_getcwd)(char *, size_t) = NULL; if (!orig_getcwd) orig_getcwd = (char *(*)(char *, size_t))dlsym(RTLD_NEXT, "getcwd"); char *res = orig_getcwd ? orig_getcwd(buf, size) : NULL; - if (res) { - size_t len = strlen(res); - guest_path_of_mount_source(res, len, buf ? size : len + 1, NULL); - } + if (res && !buf) return guest_path_of_allocated_mount_source(res); + if (res) guest_path_of_mount_source(res, strlen(res), size, NULL); return res; } @@ -790,7 +788,8 @@ char *realpath(const char *path, char *resolved_path) { } } } - if (res) guest_path_of_mount_source(res, strlen(res), resolved_path ? PATH_MAX : strlen(res) + 1, NULL); + if (res && !resolved_path) return guest_path_of_allocated_mount_source(res); + if (res) guest_path_of_mount_source(res, strlen(res), PATH_MAX, NULL); return res; } diff --git a/tools/nativeguest/hook/paths.c b/tools/nativeguest/hook/paths.c index 89605e1eb..74983a755 100644 --- a/tools/nativeguest/hook/paths.c +++ b/tools/nativeguest/hook/paths.c @@ -975,6 +975,20 @@ static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap, int return best->dst_len + rest; } +static char *guest_path_of_allocated_mount_source(char *res) { + size_t len = strlen(res); + char tmp[PATH_MAX]; + if (len >= sizeof(tmp)) return res; + memcpy(tmp, res, len + 1); + int converted = 0; + size_t n = guest_path_of_mount_source(tmp, len, sizeof(tmp), &converted); + if (!converted) return res; + char *grown = realloc(res, n + 1); + if (!grown) return res; + memcpy(grown, tmp, n + 1); + return grown; +} + static ssize_t postprocess_readlink(const char *linkpath, char *buf, ssize_t ret, size_t bufsiz) { if (ret <= 0) return ret; if ((size_t)ret < bufsiz) { diff --git a/tools/tests/test_native_hook_mounts.py b/tools/tests/test_native_hook_mounts.py index 63177f9ba..0774f059e 100644 --- a/tools/tests/test_native_hook_mounts.py +++ b/tools/tests/test_native_hook_mounts.py @@ -88,6 +88,18 @@ int main(int argc, char **argv) { printf("%s\t%s\n", argv[i], ok ? cwd : "-"); continue; } + if (strncmp(argv[i], "cdnull:", 7) == 0) { + char *got = chdir(argv[i] + 7) == 0 ? getcwd(NULL, 0) : NULL; + printf("%s\t%s\n", argv[i], got ? got : "-"); + free(got); + continue; + } + if (strncmp(argv[i], "realnull:", 9) == 0) { + char *got = realpath(argv[i] + 9, NULL); + printf("%s\t%s\n", argv[i], got ? got : "-"); + free(got); + continue; + } if (strncmp(argv[i], "lstat:", 6) == 0) { int ok = lstat(argv[i] + 6, &st) == 0; printf("%s\t%d\t%d\n", argv[i], ok, ok && S_ISLNK(st.st_mode)); @@ -324,6 +336,19 @@ class NativeHookMountTest(unittest.TestCase): arg = f"cd:/pfx/native/dosdevices/z:{GAME}/" self.assertEqual(self.run_probe(arg)[arg], [GAME]) + def test_allocated_working_directory_and_real_path_name_a_longer_guest_mount(self): + guest = "/root/Games/" + "g" * len(str(self.library)) + binds = f"{self.library}:{guest}" + args = [f"cd:{guest}/Some Game", f"cdnull:{guest}/Some Game", f"realnull:{guest}/Some Game/game.txt"] + result = self.run_probe(*args, binds=binds) + self.assertEqual(result[args[0]], [f"{guest}/Some Game"]) + self.assertEqual(result[args[1]], [f"{guest}/Some Game"]) + self.assertEqual(result[args[2]], [f"{guest}/Some Game/game.txt"]) + + def test_allocated_working_directory_names_a_shorter_guest_mount(self): + arg = f"cdnull:{GAME}" + self.assertEqual(self.run_probe(arg)[arg], [GAME]) + def test_rootfs_path_under_a_mount_point_reaches_the_mount(self): path = f"{self.root}{GAME}/game.txt" self.assertEqual(self.run_probe(path)[path], ["1", "library"])